.classpath : added test resources in classpath
authorCherian Mathew <c.mathew@bgbm.org>
Tue, 6 May 2014 14:45:58 +0000 (14:45 +0000)
committerCherian Mathew <c.mathew@bgbm.org>
Tue, 6 May 2014 14:45:58 +0000 (14:45 +0000)
CdmPersistentRemoteSourceTest : added new tests for remote source connection
CdmRemoteSourceBase : added code to connect to the target database (before initialising the full application context) to retrieve metadata
CdmRemoteSource, CdmPersistentRemoteSource : added required methods
remotingApplicationContext : optimisations

eu.etaxonomy.taxeditor.cdmlib/.classpath
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/cdm/api/application/CdmApplicationRemoteController.java
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/cdm/remote/CdmPersistentRemoteSource.java
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/cdm/remote/CdmRemoteSource.java
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/cdm/remote/CdmRemoteSourceBase.java
eu.etaxonomy.taxeditor.cdmlib/src/main/resources/eu/etaxonomy/cdm/remotingApplicationContext.xml
eu.etaxonomy.taxeditor.cdmlib/src/test/java/eu/etaxonomy/taxeditor/remoting/CdmPersistentRemoteSourceTest.java

index 12b318a2af8e49b6736281ea8140097b43c595e4..0ad6b0dd8ef98cfccd79e3505fb01ebafe651c6e 100644 (file)
@@ -5,6 +5,7 @@
        <classpathentry kind="src" path="src/main/java"/>
        <classpathentry kind="src" path="src/test/java"/>
        <classpathentry kind="src" path="src/main/resources"/>
+       <classpathentry kind="src" path="src/test/resources"/>
        <classpathentry exported="true" kind="lib" path="lib/activation-1.1.1.jar"/>
        <classpathentry exported="true" kind="lib" path="lib/antlr-2.7.7.jar"/>
        <classpathentry exported="true" kind="lib" path="lib/aopalliance-1.0.jar"/>
index c25b01ca6f5430a264b69a4eed2887f90fdb43e4..5473ac141981dafc244e0b157a25e9ab7282a0b5 100644 (file)
@@ -148,6 +148,7 @@ public class CdmApplicationRemoteController  extends CdmApplicationController {
         progressMonitor.worked(1);
 
         XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
+        //xmlReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
         progressMonitor.subTask("Registering resources.");
         xmlReader.loadBeanDefinitions(applicationContextResource);
         progressMonitor.worked(1);
index 487430b713852af3ea0fa292c69a82a1cbf2bcff..a6cd14cd2e1d2d16e6787d30e5f3d2617752dd2b 100644 (file)
@@ -14,6 +14,7 @@ import static eu.etaxonomy.cdm.common.XmlHelp.insertXmlBean;
 import static eu.etaxonomy.cdm.common.XmlHelp.insertXmlValueProperty;
 import static eu.etaxonomy.cdm.common.XmlHelp.saveToXml;
 
+import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.List;
 import java.util.Properties;
@@ -26,6 +27,7 @@ import eu.etaxonomy.cdm.common.CdmUtils;
 import eu.etaxonomy.cdm.common.XmlHelp;
 import eu.etaxonomy.cdm.config.CdmPersistentSourceUtils;
 import eu.etaxonomy.cdm.config.CdmPersistentXMLSource;
+import eu.etaxonomy.cdm.config.CdmSourceException;
 import eu.etaxonomy.cdm.config.ICdmPersistentSource;
 import eu.etaxonomy.cdm.config.CdmPersistentXMLSource.CdmSourceProperties;
 import eu.etaxonomy.cdm.database.CdmDataSource;
@@ -79,7 +81,7 @@ public class CdmPersistentRemoteSource extends CdmRemoteSourceBase implements IC
         * @throws CdmRemoteSourceException 
         */
        private CdmPersistentRemoteSource(String remoteSource) throws CdmRemoteSourceException { 
-               setName(remoteSource);          
+               
                CdmPersistentXMLSource cdmPersistentXMLSource = CdmPersistentXMLSource.NewInstance(remoteSource, REMOTESOURCE_BEAN_POSTFIX);
                if(cdmPersistentXMLSource.getElement() != null) {
                        beanName = cdmPersistentXMLSource.getBeanName();
@@ -90,6 +92,7 @@ public class CdmPersistentRemoteSource extends CdmRemoteSourceBase implements IC
                } else {
                        throw new CdmRemoteSourceException("Remote source '" + remoteSource + "' does not exist in settings file");
                }               
+               initDatabaseConnection();
        }
        
        private static void checkProperties(Properties cdmSourceProperties) throws CdmRemoteSourceException {
@@ -207,5 +210,40 @@ public class CdmPersistentRemoteSource extends CdmRemoteSourceBase implements IC
                Element bean = CdmPersistentSourceUtils.getCdmSourceBeanXml(strRemoteSourceName, REMOTESOURCE_BEAN_POSTFIX);
                return (bean != null);
        }
+       
+       /**
+        * Returns a list of all datasources stored in the datasource config file
+        * @return all existing data sources
+        * @throws CdmRemoteSourceException 
+        */
+       @SuppressWarnings("unchecked")
+       static public List<CdmPersistentRemoteSource> getAllRemoteSources() throws CdmRemoteSourceException{
+               List<CdmPersistentRemoteSource> remoteSources = new ArrayList<CdmPersistentRemoteSource>();
+               
+               Element root = getBeansRoot(CdmPersistentSourceUtils.getCdmSourceInputStream());
+               if (root == null){
+                       return null;
+               }else{
+               List<Element> lsChildren  = root.getChildren("bean", root.getNamespace());
+               
+               for (Element elBean : lsChildren){
+                       String strId = elBean.getAttributeValue("id");
+                       if (strId != null && strId.endsWith(REMOTESOURCE_BEAN_POSTFIX)){
+                               strId = strId.replace(REMOTESOURCE_BEAN_POSTFIX, "");
+                               remoteSources.add(new CdmPersistentRemoteSource(strId));
+                       }
+               }
+               }
+               return remoteSources;
+       }
+       
+       @Override
+       public String toString(){
+               if (getName() != null){
+                       return getName();
+               }else{
+                       return null;
+               }
+       }
 
 }
index 197d40bb1846919cbfed1746f5c00989c21aa8d3..9dbb34b3abf2d0cad71d8a22736c473603d8e9e2 100644 (file)
@@ -16,6 +16,19 @@ import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
  */
 public class CdmRemoteSource extends CdmRemoteSourceBase {
        
+       /**
+        * Creates a new instance of CdmRemoteSource
+        * 
+        * @param name
+        * @param server
+        * @param port
+        * @param contextPath
+        * @param nomenclaturalCode
+        * @return
+        */
+       public static CdmRemoteSource NewInstance() {
+               return new CdmRemoteSource(DEFAULT_NAME, DEFAULT_SERVER, DEFAULT_PORT, DEFAULT_CONTEXT_PATH, DEFAULT_NOMENCLATURAL_CODE);
+       }
        
        /**
         * Creates a new instance of CdmRemoteSource
index 031377cb064e0360c1b49db7cdeaa64635692280..d24baa6ee8717126a7696dcb04f4c10a7f1ad271 100644 (file)
@@ -8,11 +8,15 @@
 */
 package eu.etaxonomy.cdm.remote;
 
+import java.util.Map;
+
 import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
 
 import eu.etaxonomy.cdm.api.service.IDatabaseService;
 import eu.etaxonomy.cdm.config.CdmSource;
 import eu.etaxonomy.cdm.config.CdmSourceException;
+import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
+import eu.etaxonomy.cdm.model.metadata.CdmMetaData.MetaDataPropertyName;
 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
 
 /**
@@ -23,6 +27,11 @@ import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
  */
 public class CdmRemoteSourceBase extends CdmSource implements ICdmRemoteSource {
 
+       protected static final String DEFAULT_NAME = "default";
+       protected static final String DEFAULT_SERVER = "127.0.0.1";
+       protected static final int DEFAULT_PORT = 8080;
+       protected static final String DEFAULT_CONTEXT_PATH = "";
+       protected static final NomenclaturalCode DEFAULT_NOMENCLATURAL_CODE = NomenclaturalCode.ICNAFP;
        private String contextPath;
        private String baseUrl;
        
@@ -33,11 +42,11 @@ public class CdmRemoteSourceBase extends CdmSource implements ICdmRemoteSource {
         * 
         */
        protected CdmRemoteSourceBase() {
-               setName("default");
-               setServer("127.0.0.1");
-               setPort(8080);
-               setContextPath("");
-               setNomenclaturalCode(NomenclaturalCode.ICNAFP);
+               setName(DEFAULT_NAME);
+               setServer(DEFAULT_SERVER);
+               setPort(DEFAULT_PORT);
+               setContextPath(DEFAULT_CONTEXT_PATH);
+               setNomenclaturalCode(DEFAULT_NOMENCLATURAL_CODE);
        }
        
        /**
@@ -54,20 +63,25 @@ public class CdmRemoteSourceBase extends CdmSource implements ICdmRemoteSource {
                setServer(server);
                setPort(port);
                setContextPath(contextPath);
-               setNomenclaturalCode(nomenclaturalCode);
-               if(contextPath == null || contextPath.equals("")) {
-                       setBaseUrl("http://" + server + ":" + String.valueOf(port));
+               setNomenclaturalCode(nomenclaturalCode);                        
+               initDatabaseConnection();
+       }
+       
+       protected void initDatabaseConnection() {
+               if(getContextPath() == null || getContextPath().equals("")) {
+                       setBaseUrl("http://" + getServer() + ":" + String.valueOf(getPort()));
                } else {
-                       setBaseUrl("http://" + server + ":" + String.valueOf(port) + "/" + contextPath);
-               }
+                       setBaseUrl("http://" + getServer()  + ":" + String.valueOf(getPort()) + "/" + getContextPath());
+               }       
                // the database service needs to be initialised (before the spring
                // application context initialsation) since it is required to 
                // to make queries related to the source database
            HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
            proxy.setServiceInterface(IDatabaseService.class);
-           proxy.setServiceUrl(getBaseUrl() + "/remoting/database.service");
+           proxy.setServiceUrl(baseUrl + "/remoting/database.service");
            proxy.afterPropertiesSet();
            databaseService = (IDatabaseService) proxy.getObject();
+
        }
        
        /* (non-Javadoc)
@@ -150,4 +164,11 @@ public class CdmRemoteSourceBase extends CdmSource implements ICdmRemoteSource {
                return "Conncting to Remote CDM Server " + getName();
        }
 
+               
+       @Override
+       public Map<MetaDataPropertyName, String> getMetaDataMap() throws CdmSourceException {
+               return databaseService.getCdmMetadataMap();
+       }
+
+
 }
index 825c80acd487e13ccdecc357c9f3487f5d091ab1..2f564e9947535d3296bf76ec6fa3e5d0f49e9a57 100644 (file)
        \r
     <!--  <bean id="remoteTermInitializer" class="eu.etaxonomy.cdm.remote.service.RemoteTermInitializer"/>-->\r
         \r
-       <context:annotation-config/>\r
+<!--   <context:annotation-config/> -->\r
        \r
-       <context:component-scan base-package="eu/etaxonomy/cdm/api/application">\r
-               <context:exclude-filter type="regex" expression="eu\.etaxonomy\.cdm\.api\.application\.CdmApplicationDefaultConfiguration" /> \r
-       </context:component-scan>\r
+<!--   <context:component-scan base-package="eu/etaxonomy/cdm/api/application"> -->\r
+<!--                   <context:exclude-filter type="regex" expression="eu\.etaxonomy\.cdm\.api\.application\.CdmApplicationDefaultConfiguration" />  -->\r
+<!--           </context:component-scan> -->\r
+    \r
+    <bean id="cdmApplicationRemoteConfiguration" class="eu.etaxonomy.cdm.api.application.CdmApplicationRemoteConfiguration"/>\r
     \r
        <import resource="classpath:/eu/etaxonomy/cdm/remoting_services_security.xml"/> \r
 \r
index 40032ef767bf9f0dd782fb147b0cc4aa753293a5..c601d3ad40196558e4b5da19f7f531d79d47f094 100644 (file)
@@ -9,6 +9,8 @@
 package eu.etaxonomy.taxeditor.remoting;
 
 
+import java.util.Map;
+
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 import org.junit.Assert;
@@ -20,6 +22,8 @@ import static org.hamcrest.CoreMatchers.*;
 
 import eu.etaxonomy.cdm.config.CdmPersistentSourceUtils;
 import eu.etaxonomy.cdm.config.CdmPersistentXMLSource.CdmSourceProperties;
+import eu.etaxonomy.cdm.config.CdmSourceException;
+import eu.etaxonomy.cdm.model.metadata.CdmMetaData.MetaDataPropertyName;
 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
 import eu.etaxonomy.cdm.remote.CdmPersistentRemoteSource;
 import eu.etaxonomy.cdm.remote.CdmRemoteSource;
@@ -72,9 +76,67 @@ public class CdmPersistentRemoteSourceTest extends UnitilsJUnit4 {
                        
                } catch (CdmRemoteSourceException e) {
                        Assert.fail("Initialising an existing remote source should not throw an exception");
-               }
+               }                       
+       }
+       
+
+       
+       
+       @Test
+       public void whenConnectingToAnActiveServerThenConnectSuccessfully() {
+               // check if active server throws the right exception
+               try {
+                       remotePersistentSource = CdmPersistentRemoteSource.NewInstance("default");
+
+                       String dbSchemaVersion = "";
+                       try {
+                               dbSchemaVersion = remotePersistentSource.getDbSchemaVersion();
+                       } catch (CdmSourceException e) {
+                               Assert.fail("getDbSchemaVersion() on active cdm server should not have thrown CdmSourceException");
+                       }
+                       logger.info("dbSchemaVersion is " + dbSchemaVersion);
+
+
+                       boolean isDbEmpty = false;
+                       try {
+                               isDbEmpty = remotePersistentSource.isDbEmpty();
+                       } catch (CdmSourceException e) {
+                               Assert.fail("isDbEmpty() on active cdm server should not have thrown CdmSourceException");
+                       }
+                       Assert.assertFalse(isDbEmpty);
+
 
-                               
+                       boolean check = false;
+                       try {
+                               check = remotePersistentSource.checkConnection();
+                       } catch (CdmSourceException e) {
+                               Assert.fail("checkConnection() on active cdm server should not have thrown CdmSourceException");
+                       }
+                       Assert.assertTrue(check);
+               } catch (CdmRemoteSourceException e1) {
+                       // TODO Auto-generated catch block
+                       e1.printStackTrace();
+               }
+               
+       }
+       
+       @Test
+       public void whenRequestingMetadatFromRemoteSourceThenMetaDataIsRetrieved() {
+               try {
+                       remotePersistentSource = CdmPersistentRemoteSource.NewInstance("default");
+                       Map<MetaDataPropertyName, String> cdmMetaDataMap = remotePersistentSource.getMetaDataMap();
+                       for(MetaDataPropertyName mdpn : cdmMetaDataMap.keySet()) {
+                               String value = cdmMetaDataMap.get(mdpn);
+                               logger.info("MetaData Property " + mdpn.name() + " has value " + value);
+                               Assert.assertNotNull("Meta Data Value for property for " + mdpn.name() + " should not be null", value);                         
+                       }
+                       
+               } catch (CdmRemoteSourceException e) {
+                       Assert.fail("Initialising an existing remote source should not throw an exception");
+               } catch (CdmSourceException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
        }
        
        @Test
@@ -166,5 +228,7 @@ public class CdmPersistentRemoteSourceTest extends UnitilsJUnit4 {
                        logger.info(e.getMessage());
                }       
        }
+       
+
 
 }