ref #9359 upgrade TaxEditor to log4j2
[taxeditor.git] / eu.etaxonomy.taxeditor.test / src / test / java / eu / etaxonomy / taxeditor / httpinvoker / BaseRemotingTest.java
index 694d80e57e2f7830ea1834dd4dbaa11fde34ac65..f29b47d0750cc8f1c47a860cd0a5b1518c945c9b 100644 (file)
@@ -8,29 +8,14 @@
  */
 package eu.etaxonomy.taxeditor.httpinvoker;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.InputStream;
 import java.lang.reflect.Field;
-import java.net.URL;
 import java.util.Map;
-import java.util.Properties;
 
-import net.sf.ehcache.CacheManager;
-
-import org.apache.log4j.Logger;
-import org.eclipse.core.runtime.FileLocator;
-import org.eclipse.core.runtime.Platform;
-import org.junit.Assert;
+import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
 import org.junit.BeforeClass;
-import org.osgi.framework.Bundle;
-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.database.util.TransactionMode;
@@ -40,16 +25,16 @@ import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
 import eu.etaxonomy.cdm.api.application.CdmDataChangeService;
 import eu.etaxonomy.cdm.api.cache.CdmServiceCacher;
-import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
-import eu.etaxonomy.taxeditor.remoting.cache.CdmRemoteCacheManager;
-import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
+import eu.etaxonomy.cdm.cache.CdmRemoteCacheManager;
+import eu.etaxonomy.cdm.model.permission.User;
+import eu.etaxonomy.taxeditor.remoting.cache.ConversationalTransientEntityCacher;
 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 net.sf.ehcache.CacheManager;
 
 
 /**
@@ -61,83 +46,33 @@ import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
  */
 @Transactional(TransactionMode.DISABLED)
 @SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
-public abstract class BaseRemotingTest extends UnitilsJUnit4 {
-    private static final Logger logger = Logger.getLogger(BaseRemotingTest.class);
-
-
-    public static final Resource SERVER_PROPERTIES_FILE =
-            new ClassPathResource("server.properties");
+public abstract class BaseRemotingTest extends ThreadedTest {
+    @SuppressWarnings("unused")
+       private static final Logger logger = LogManager.getLogger(BaseRemotingTest.class);
 
     private static ICdmRemoteSource cdmRemoteSource;
     private static CdmPersistentRemoteSource remotePersistentSource;
-
-    public static boolean useManagedServer = true;
-
-
     protected static ICdmEntitySessionManager cdmEntitySessionManager;
 
-    private final static String DEFAULT_USER = "admin";
-    private final static String DEFAULT_PASSWORD = "00000";
-
-    private static String userHomeKey = "user.home";
-
-    private static String user = DEFAULT_USER;
-    private static String password = DEFAULT_PASSWORD;
-
-    protected static CDMServer cdmServer;
-
     @BeforeClass
     public static void initializeBaseRemotingTest() {
 
         DatabaseUnitils.disableConstraints();
 
         try {
-            String userHomeDirPath;
-            Bundle bundle = Platform.getBundle("eu.etaxonomy.taxeditor.test");
-
-            URL userHomeDirURL = bundle.getEntry("src/test/resources");
-            File userHomeDir = new File(FileLocator.resolve(userHomeDirURL).toURI());
-            userHomeDirPath = userHomeDir.getAbsolutePath();
-
-            URL serverPropertiesURL = bundle.getEntry("src/test/resources/server.properties");
-            File serverPropertiesFile = new File(FileLocator.resolve(serverPropertiesURL).toURI());
-            InputStream inputStream = new FileInputStream(serverPropertiesFile);
-
-            Properties prop = new Properties();
-            if (inputStream != null) {
-                prop.load(inputStream);
-                inputStream.close();
-            }
-
-            logger.info("Setting user.home to " + userHomeDirPath);
-            System.setProperty(userHomeKey, userHomeDirPath);
-
-
-            cdmServer = new CDMServer("cdmTest", serverPropertiesURL);
-
-
-            if(prop.getProperty("user") != null) {
-                user = prop.getProperty("user");
-            }
-
-            if(prop.getProperty("password") != null) {
-                password = prop.getProperty("password");
-            }
-
-            initializeController(cdmServer.getName(),
-                    cdmServer.getHost(),
-                    cdmServer.getPort(),
-                    cdmServer.getContextPath(),
-                    NomenclaturalCode.ICNAFP,
+            initializeController(sourceName,
+                    host,
+                    httpPort,
+                    contextPath,
                     user,
                     password);
         } catch (Exception e) {
+            System.err.println("user: " + user + " password: " + password);
             e.printStackTrace();
            // Assert.fail("Server failed to start. Reason : " + e.getMessage());
         }
 
-        logger.info("Emptying all caches (except model cache) ");
-        //emptyAllCachesExceptModelCache();
+
     }
 
     public static void emptyAllCachesExceptModelCache() {
@@ -151,42 +86,63 @@ public abstract class BaseRemotingTest extends UnitilsJUnit4 {
     }
 
 
-    public static void initializeController(String sourceName, String host, int port, String contextPath, NomenclaturalCode ncode, String username, String password) {
+    public static void initializeController(String sourceName, String host, int port, String contextPath, String username, String password) {
 
         if(CdmApplicationState.getCurrentAppConfig() != null) {
             return;
         }
-        cdmRemoteSource = CdmRemoteSource.NewInstance(sourceName, host, port, contextPath, ncode);
+        cdmRemoteSource = CdmRemoteSource.NewInstance(sourceName, host, port, contextPath);
         CdmApplicationRemoteController remoteApplicationController =
                 CdmApplicationRemoteController.NewInstance(cdmRemoteSource,
                         null,
                         null);
         CdmApplicationState.setCurrentAppConfig(remoteApplicationController);
         CdmApplicationState.setCdmServiceCacher(new CdmServiceCacher());
-        try {
-            remotePersistentSource = CdmPersistentRemoteSource.NewInstance(sourceName);
-        } catch (CdmRemoteSourceException e) {
-            Assert.fail("Default Remote Persistent Source failed to load. Reason : " + e.getMessage());
-        }
+
         cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
 
         CdmApplicationState.setCurrentDataChangeService(new CdmDataChangeService());
 
+        authenticate(username, password);
+
+    }
+
+    protected static void authenticate(String username, String password) {
+
         //FIXME:Remoting the authentication code should be replaced by a method call which actually
         // does the authentication in the editor code so that the 'real' authentication can be tested
         SecurityContextHolder.clearContext();
-        SecurityContextImpl sc = new SecurityContextImpl();
-        Authentication token = new UsernamePasswordAuthenticationToken(username,password);
+
+        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
         Authentication authentication = getRemoteApplicationController().getAuthenticationManager().authenticate(token);
-        authentication = new UsernamePasswordAuthenticationToken(username,password, authentication.getAuthorities());
-        sc.setAuthentication(authentication);
 
-        SecurityContextHolder.setContext(sc);
+
+        User user = (User) authentication.getPrincipal();
+        /* circumventing problem with hibernate not refreshing the transient collection authorities in this case,
+         * see http://dev.e-taxonomy.eu/trac/ticket/4053 */
+        user.initAuthorities();
+        authentication = new UsernamePasswordAuthenticationToken(user,password, authentication.getAuthorities());
+        SecurityContextHolder.getContext().setAuthentication(authentication);
         CdmApplicationState.setCurrentSecurityContext(SecurityContextHolder.getContext());
-    }
 
 
 
+
+
+//        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);
+//        CdmApplicationState.setCurrentSecurityContext(SecurityContextHolder.getContext());
+
+    }
+
+
     protected static CdmApplicationRemoteController getRemoteApplicationController() {
         return (CdmApplicationRemoteController) CdmApplicationState.getCurrentAppConfig();
     }
@@ -211,11 +167,11 @@ public abstract class BaseRemotingTest extends UnitilsJUnit4 {
     }
 
     protected static CdmEntitySession getActiveSession() {
-        return (CdmEntitySession) getFieldValueViaReflection(cdmEntitySessionManager, "activeSession");
+        return ((InheritableThreadLocal<CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "tlActiveSession")).get();
     }
 
-    protected static CdmTransientEntityCacher getCacher(ICdmEntitySessionEnabled sessionOwner) {
-        return (CdmTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher");
+    protected static ConversationalTransientEntityCacher getCacher(ICdmEntitySessionEnabled sessionOwner) {
+        return (ConversationalTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher");
     }
 
 
@@ -238,6 +194,9 @@ public abstract class BaseRemotingTest extends UnitilsJUnit4 {
     }
 
 
+    protected static void authenticateDefaultUser() {
+        authenticate(user, password);
+    }
 
 
 }