<commit_before>package ch.elexis.data;

import static org.junit.Assert.*;

import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.exceptions.PersistenceException;
import ch.elexis.core.model.IXid;
import ch.rgw.tools.JdbcLink;
import ch.rgw.tools.JdbcLink.Stm;
import ch.rgw.tools.JdbcLinkException;

public class Test_PersistentObject extends AbstractPersistentObjectTest {
	
	private JdbcLink link;
	
	@Before
	public void setUp() throws IOException{
		link = initDB();
		DBConnection dbc = new DBConnection();
		dbc.setJdbcLink(link);
	}
	
	@After
	public void tearDown(){
		try {
			if (link == null || !link.isAlive())
				return;
			link.exec("DROP ALL OBJECTS");
			link.disconnect();
		} catch (JdbcLinkException je) {
			// just tell what happend and resume
			// excpetion is allowed for tests which get rid of the connection on their own
			// for example testConnect(), ...
			je.printStackTrace();
		}
	}
	
	@Test
	public void testConnect(){
		boolean ret = PersistentObject.connect(link);
		assertTrue(ret);
		PersistentObject.disconnect();
	}
	
	@Test
	public void testConnectFail(){
		// this connect methods opens its own JdbcLink by all means
		// it is looking for a demo db:
		// File demo = new File(base.getParentFile().getParent() + "/demoDB");
		
		// then for dom SWTBot related db:
		// String template = System.getProperty("SWTBot-DBTemplate");
		// File dbDir = new File(Hub.getTempDir(), "Elexis-SWTBot");
		
		// then from some user provided config
		// String connection = Hub.getCfgVariant();
		
		// then if provider is Medelexis the db wizard is opened else
		// look for db at default location
		// String d = PreferenceInitializer.getDefaultDBPath();
		
		// this is nice for runtime but makes testing really hard :)
		// we need to mock JdbcLink.createH2Link to stop creation of database
		// PowerMockito.mockStatic(JdbcLink.class);
		// PowerMockito.when(JdbcLink.createH2Link(Matchers.anyString())).thenReturn(
		// new JdbcLink("", "", ""));
		// connect and simulate db creation failure with JdbcLink mock
		try {
			PersistentObject.connect(CoreHub.localCfg);
			// TODO: does not work at the moment in Elexis 3.0
			// fail("Expected Exception not thrown!");
		} catch (PersistenceException pe) {
			
		}
	}
	
	@Test
	public void testGet(){
		PersistentObjectImpl impl = new PersistentObjectImpl();
		String ret = impl.get("TestGet");
		assertNotNull(ret);
		assertEquals("test", ret);
	}
	
	@Test
	public void testState(){
		PersistentObjectImpl impl = new PersistentObjectImpl();
		impl.tablename = "abc";
		int ret = impl.state();
		assertEquals(PersistentObject.INEXISTENT, ret);
	}
	
	@Test
	public void testStoreToString(){
		PersistentObjectImpl impl = new PersistentObjectImpl();
		String ret = impl.storeToString();
		assertNotNull(ret);
		assertTrue(ret.startsWith("ch.elexis.data.Test_PersistentObject"));
	}
	
	@Test
	public void testGetXid(){
		PersistentObjectImpl impl = new PersistentObjectImpl();
		IXid ret = impl.getXid();
		assertNotNull(ret);
	}
	
	@Test
	public void testAddXid(){
		PersistentObjectImpl impl = new PersistentObjectImpl();
		Xid.localRegisterXIDDomain("test", "test", 1);
		boolean ret = impl.addXid("test", "addXid", false);
		assertTrue(ret);
		IXid id = impl.getXid();
		assertNotNull(id);
	}
	
	@Test(expected = PersistenceException.class)
	public void testGetFail(){
		// mock a status manager for ignoring the error status
		// StatusManager statusMock = PowerMockito.mock(StatusManager.class);
		// PowerMockito.mockStatic(StatusManager.class);
		// PowerMockito.when(StatusManager.getManager()).thenReturn(statusMock);
		
		PersistentObjectImpl impl = new PersistentObjectImpl();
		String ret = impl.get("");
		assertNotNull(ret);
		assertEquals(PersistentObject.MAPPING_ERROR_MARKER + "**", ret);
		
		// if we pass ID we should get to code that reaches into the db
		// we have no table specified so a JdbcLinkException is expected
		String id = impl.get("ID");
		fail("Expected Exception not thrown! Value is " + id);
	}
	
	@Test
	public void testTableExists(){
		assertTrue(PersistentObject.tableExists("CONFIG"));
		assertTrue(PersistentObject.tableExists("KONTAKT"));
		// SQL can be case sensitive !!
		// assertEquals(false, PersistentObject.tableExists("kontakt"));
		assertEquals(false, PersistentObject.tableExists("THIS_TABLE_SHOULD_NOT_EXISTS"));
	}
	
	@Test
	public void testUpdateUser() throws IOException{
		assertTrue(PersistentObject.tableExists("USER_"));
		assertTrue(User.load("Administrator").exists());
		assertTrue(PersistentObject.tableExists("ROLE"));
		assertTrue(Kontakt.load("Y9f697146027a577d028").exists());
//		assertTrue(User.load("cm").exists());
	}
	
	@Ignore
	public void testCaseSensitiveIdLoad(){
		//#5514
		Anwender anw = new Anwender("Username", "Uservorname", "16.1.1973", "w");<commit_msg>[5545] Fix Test_PersistentObject remove user tests<commit_after>package ch.elexis.data;

import static org.junit.Assert.*;

import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.exceptions.PersistenceException;
import ch.elexis.core.model.IXid;
import ch.rgw.tools.JdbcLink;
import ch.rgw.tools.JdbcLink.Stm;
import ch.rgw.tools.JdbcLinkException;

public class Test_PersistentObject extends AbstractPersistentObjectTest {
	
	private JdbcLink link;
	
	@Before
	public void setUp() throws IOException{
		link = initDB();
		DBConnection dbc = new DBConnection();
		dbc.setJdbcLink(link);
		executeDBScript(dbc, "/rsc/UserContactsToMigrate.sql");
	}
	
	@After
	public void tearDown(){
		try {
			if (link == null || !link.isAlive())
				return;
			link.exec("DROP ALL OBJECTS");
			link.disconnect();
		} catch (JdbcLinkException je) {
			// just tell what happend and resume
			// excpetion is allowed for tests which get rid of the connection on their own
			// for example testConnect(), ...
			je.printStackTrace();
		}
	}
	
	@Test
	public void testConnect(){
		boolean ret = PersistentObject.connect(link);
		assertTrue(ret);
		PersistentObject.disconnect();
	}
	
	@Test
	public void testConnectFail(){
		// this connect methods opens its own JdbcLink by all means
		// it is looking for a demo db:
		// File demo = new File(base.getParentFile().getParent() + "/demoDB");
		
		// then for dom SWTBot related db:
		// String template = System.getProperty("SWTBot-DBTemplate");
		// File dbDir = new File(Hub.getTempDir(), "Elexis-SWTBot");
		
		// then from some user provided config
		// String connection = Hub.getCfgVariant();
		
		// then if provider is Medelexis the db wizard is opened else
		// look for db at default location
		// String d = PreferenceInitializer.getDefaultDBPath();
		
		// this is nice for runtime but makes testing really hard :)
		// we need to mock JdbcLink.createH2Link to stop creation of database
		// PowerMockito.mockStatic(JdbcLink.class);
		// PowerMockito.when(JdbcLink.createH2Link(Matchers.anyString())).thenReturn(
		// new JdbcLink("", "", ""));
		// connect and simulate db creation failure with JdbcLink mock
		try {
			PersistentObject.connect(CoreHub.localCfg);
			// TODO: does not work at the moment in Elexis 3.0
			// fail("Expected Exception not thrown!");
		} catch (PersistenceException pe) {
			
		}
	}
	
	@Test
	public void testGet(){
		PersistentObjectImpl impl = new PersistentObjectImpl();
		String ret = impl.get("TestGet");
		assertNotNull(ret);
		assertEquals("test", ret);
	}
	
	@Test
	public void testState(){
		PersistentObjectImpl impl = new PersistentObjectImpl();
		impl.tablename = "abc";
		int ret = impl.state();
		assertEquals(PersistentObject.INEXISTENT, ret);
	}
	
	@Test
	public void testStoreToString(){
		PersistentObjectImpl impl = new PersistentObjectImpl();
		String ret = impl.storeToString();
		assertNotNull(ret);
		assertTrue(ret.startsWith("ch.elexis.data.Test_PersistentObject"));
	}
	
	@Test
	public void testGetXid(){
		PersistentObjectImpl impl = new PersistentObjectImpl();
		IXid ret = impl.getXid();
		assertNotNull(ret);
	}
	
	@Test
	public void testAddXid(){
		PersistentObjectImpl impl = new PersistentObjectImpl();
		Xid.localRegisterXIDDomain("test", "test", 1);
		boolean ret = impl.addXid("test", "addXid", false);
		assertTrue(ret);
		IXid id = impl.getXid();
		assertNotNull(id);
	}
	
	@Test(expected = PersistenceException.class)
	public void testGetFail(){
		// mock a status manager for ignoring the error status
		// StatusManager statusMock = PowerMockito.mock(StatusManager.class);
		// PowerMockito.mockStatic(StatusManager.class);
		// PowerMockito.when(StatusManager.getManager()).thenReturn(statusMock);
		
		PersistentObjectImpl impl = new PersistentObjectImpl();
		String ret = impl.get("");
		assertNotNull(ret);
		assertEquals(PersistentObject.MAPPING_ERROR_MARKER + "**", ret);
		
		// if we pass ID we should get to code that reaches into the db
		// we have no table specified so a JdbcLinkException is expected
		String id = impl.get("ID");
		fail("Expected Exception not thrown! Value is " + id);
	}
	
	@Test
	public void testTableExists(){
		assertTrue(PersistentObject.tableExists("CONFIG"));
		assertTrue(PersistentObject.tableExists("KONTAKT"));
		// SQL can be case sensitive !!
		// assertEquals(false, PersistentObject.tableExists("kontakt"));
		assertEquals(false, PersistentObject.tableExists("THIS_TABLE_SHOULD_NOT_EXISTS"));
	}
	
	@Ignore
	public void testCaseSensitiveIdLoad(){
		//#5514
		Anwender anw = new Anwender("Username", "Uservorname", "16.1.1973", "w");
{'fraction_non_alphanumeric': 0.12260259847391215, 'fraction_numerical': 0.005361930294906166, 'mean_word_length': 4.294213973799127, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2278062', 'n_tokens_mistral': 3275, 'n_tokens_neox': 2982, 'n_words': 785}