import java.io.IOException;
import java.lang.reflect.Field;
+import java.util.Map;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.core.context.SecurityContextImpl;
import org.unitils.UnitilsJUnit4;
import org.unitils.database.DatabaseUnitils;
import org.unitils.database.annotations.Transactional;
import org.unitils.spring.annotation.SpringApplicationContext;
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
-import eu.etaxonomy.cdm.api.cache.CdmClientCacheException;
-import eu.etaxonomy.cdm.api.cache.CdmTransientEntityCacher;
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
+import eu.etaxonomy.taxeditor.remoting.cache.CdmClientCacheException;
+import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
import eu.etaxonomy.taxeditor.remoting.source.CdmPersistentRemoteSource;
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
+import eu.etaxonomy.taxeditor.session.CdmEntitySession;
+import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
-import eu.etaxonomy.taxeditor.session.ISessionEventListener;
/**
*
*/
@Transactional(TransactionMode.DISABLED)
-@SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
+@SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
public class BaseRemotingTest extends UnitilsJUnit4 {
//private static final Logger logger = Logger.getLogger(BaseRemotingTest.class);
private static ICdmRemoteSource cdmRemoteSource;
private static CdmPersistentRemoteSource remotePersistentSource;
- public static boolean useManagedServer = true;
+ public static boolean useManagedServer = false;
public static final Resource CDMLIB_DISK_STORE_RESOURCE =
new ClassPathResource("/eu/etaxonomy/cache");
-
+
protected static ICdmEntitySessionManager cdmEntitySessionManager;
-
+
+
@BeforeClass
public static void initializeBaseRemotingTest() {
//NOTE: Run this the cdmTest H2 DB whenever it needs to be
// recreated e.g. after a model change
- //DatabaseUnitils.disableConstraints();
+ DatabaseUnitils.disableConstraints();
try {
System.setProperty("ehcache.disk.store.dir", CDMLIB_DISK_STORE_RESOURCE.getFile().getAbsolutePath());
} catch (IOException e) {
throw new CdmClientCacheException(e);
}
-
+
useManagedServer = (System.getProperty("use.managed.server") == null) ? useManagedServer : Boolean.valueOf(System.getProperty("use.managed.server"));
if(useManagedServer) {
try {
CDMServer.getInstance().getHost(),
CDMServer.getInstance().getPort(),
CDMServer.getInstance().getContextPath(),
- NomenclaturalCode.ICNAFP);
+ NomenclaturalCode.ICNAFP,
+ "admin",
+ "00000");
}
+
+
}
-
+
@Test
public void disableConstraints() {
// To be run on the src/test/resources/h2/cdmTest h2 db after
// updating the unitils.properties 'database.url' property
DatabaseUnitils.disableConstraints();
}
- public static void initializeController(String sourceName, String host, int port, String contextPath, NomenclaturalCode ncode) {
+
+ public static void initializeController(String sourceName, String host, int port, String contextPath, NomenclaturalCode ncode, String username, String password) {
cdmRemoteSource = CdmRemoteSource.NewInstance(sourceName, host, port, contextPath, ncode);
remoteApplicationController =
CdmApplicationRemoteController.NewInstance(cdmRemoteSource,
- false,
null,
null);
Assert.fail("Default Remote Persistent Source failed to load. Reason : " + e.getMessage());
}
cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
+
+ SecurityContextHolder.clearContext();
+ SecurityContextImpl sc = new SecurityContextImpl();
+ Authentication token = new UsernamePasswordAuthenticationToken(username,password);
+ Authentication authentication = getRemoteApplicationController().getAuthenticationManager().authenticate(token);
+ authentication = new UsernamePasswordAuthenticationToken(username,password, authentication.getAuthorities());
+ sc.setAuthentication(authentication);
+
+ SecurityContextHolder.setContext(sc);
}
protected static CdmPersistentRemoteSource getCdmPersistentRemoteSource() {
return remotePersistentSource;
}
-
+
protected static ICdmEntitySessionManager getCdmEntitySessionManager() {
return cdmEntitySessionManager;
}
-
- protected static CdmTransientEntityCacher getCacher(ISessionEventListener sessionOwner) {
- return (CdmTransientEntityCacher) getFieldValueViaReflection(cdmEntitySessionManager.getSession(sessionOwner), "cdmTransientEntityCacher");
+
+
+ protected static CdmEntitySession getSession(ICdmEntitySessionEnabled sessionOwner) {
+ Map<ICdmEntitySessionEnabled, CdmEntitySession> ownerSessionMap =
+ (Map<ICdmEntitySessionEnabled, CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "ownerSessionMap");
+ return ownerSessionMap.get(sessionOwner);
+ }
+
+ protected static CdmTransientEntityCacher getActiveSession() {
+ return (CdmTransientEntityCacher) getFieldValueViaReflection(cdmEntitySessionManager, "activeSession");
+ }
+
+ protected static CdmTransientEntityCacher getCacher(ICdmEntitySessionEnabled sessionOwner) {
+ return (CdmTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher");
}
@AfterClass