added new project for remoting
[taxeditor.git] / eu.etaxonomy.taxeditor.remoting / src / test / java / eu / etaxonomy / taxeditor / lazyloading / AbstractLazyInitializerTest.java
diff --git a/eu.etaxonomy.taxeditor.remoting/src/test/java/eu/etaxonomy/taxeditor/lazyloading/AbstractLazyInitializerTest.java b/eu.etaxonomy.taxeditor.remoting/src/test/java/eu/etaxonomy/taxeditor/lazyloading/AbstractLazyInitializerTest.java
new file mode 100644 (file)
index 0000000..727802a
--- /dev/null
@@ -0,0 +1,214 @@
+/**
+ * Copyright (C) 2014 EDIT
+ * European Distributed Institute of Taxonomy
+ * http://www.e-taxonomy.eu
+ *
+ * The contents of this file are subject to the Mozilla Public License Version 1.1
+ * See LICENSE.TXT at the top of this package for the full license terms.
+ */
+package eu.etaxonomy.taxeditor.lazyloading;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.unitils.dbunit.annotation.DataSet;
+
+import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
+import eu.etaxonomy.cdm.api.service.IClassificationService;
+import eu.etaxonomy.cdm.api.service.ITaxonService;
+import eu.etaxonomy.cdm.model.agent.Person;
+import eu.etaxonomy.cdm.model.agent.Team;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.name.BotanicalName;
+import eu.etaxonomy.cdm.model.name.NonViralName;
+import eu.etaxonomy.cdm.model.name.TaxonNameBase;
+import eu.etaxonomy.cdm.model.taxon.Classification;
+import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
+import eu.etaxonomy.cdm.model.taxon.Taxon;
+import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.taxeditor.httpinvoker.BaseRemotingTest;
+
+
+
+
+/**
+ * Class to test the {@link CdmApplicationRemoteController}
+ *
+ */
+@DataSet
+public class AbstractLazyInitializerTest extends BaseRemotingTest {
+    private static final Logger logger = Logger.getLogger(AbstractLazyInitializerTest.class);
+
+    private static IClassificationService classificationService;
+    private static ITaxonService taxonService;
+
+    private static List<TaxonNode> taxonNodes;
+
+    private final UUID taxonUuid1 = UUID.fromString("8217ef77-2ab1-4318-bd67-ccd0cdef07c4");
+    private final UUID taxonUuid2 = UUID.fromString("ef96fafa-7750-4141-b31b-1ad1daab3e76");
+
+
+    @BeforeClass
+    public static void initializeRemoteLazyLoading() {
+
+        Logger.getRootLogger().setLevel(Level.DEBUG);
+
+        taxonService = getRemoteApplicationController().getTaxonService();
+        classificationService= getRemoteApplicationController().getClassificationService();
+        List<Classification> classifications = classificationService.listClassifications(1,0,null,null);
+        Assert.assertFalse(classifications.isEmpty());
+
+        Classification classification = classifications.get(0);
+        Assert.assertNotNull(classification);
+        taxonNodes = classificationService.getAllNodes();
+        Assert.assertFalse(taxonNodes.isEmpty());
+
+    }
+
+
+
+    @Test
+    public void testCDMEntityGet() {
+        //ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
+        Iterator<TaxonNode> taxonNodeItr = taxonNodes.iterator();
+        int maxcount = 30;
+        int count = 0;
+        while(taxonNodeItr.hasNext() && count <= maxcount) {
+            TaxonNode taxonNode = taxonNodeItr.next();
+            Assert.assertNotNull(taxonNode);
+
+            Taxon taxon = taxonNode.getTaxon();
+            Assert.assertNotNull(taxon);
+
+            String taxonTitle = taxon.getTitleCache();
+            logger.info("Taxon : " + taxonTitle);
+
+            TaxonNameBase name = taxon.getName();
+            Assert.assertNotNull(name);
+
+            String nameTitle = name.getTitleCache();
+            logger.info("Taxon Name : " + nameTitle);
+
+            count++;
+        }
+    }
+
+    @Test
+    public void taxonReadTest() {
+        Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
+
+    }
+
+
+    @Test
+    public void testCDMEntitySave() {
+        Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
+        String oldTitleCache = taxon.getTitleCache();
+
+        System.out.println("Taxon title : " + oldTitleCache);
+
+        taxon.setTitleCache(oldTitleCache + ":updated", true);
+        taxonService.merge(taxon);
+
+        Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
+        System.out.println("New Taxon Title : " + taxonNew.getTitleCache());
+
+        Assert.assertNotEquals("Title caches should not be equal",oldTitleCache,taxonNew.getTitleCache());
+
+        taxonNew.setTitleCache(oldTitleCache, true);
+        taxonService.merge(taxonNew);
+
+        Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
+        System.out.println("Old Taxon Title : " + taxonOld.getTitleCache());
+
+        Assert.assertEquals("Title caches should be equal",oldTitleCache,taxonOld.getTitleCache());
+
+    }
+
+
+        @Test
+        public void testCDMEntitySaveLazyNew() {
+            Team combAuthor = Team.NewInstance();
+            combAuthor.addTeamMember(Person.NewTitledInstance("test member"));
+            BotanicalName name = BotanicalName.NewInstance(null, "Test1", null, null, null, null, null, null, null);
+            name.setCombinationAuthorTeam(combAuthor);
+            Taxon tax1 = Taxon.NewInstance(name, null);
+            UUID taxonUuid1 = taxonService.save(tax1);
+
+            Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
+
+            NonViralName nvn = CdmBase.deproxy(taxon.getName(),NonViralName.class);
+            String oldTitleCache = nvn.getTitleCache();
+            logger.info("Taxon Name Title : " + oldTitleCache);
+            nvn.setTitleCache(oldTitleCache + ":updated",false);
+            taxonService.update(taxon);
+
+            Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
+            NonViralName nvnNew = CdmBase.deproxy(taxon.getName(),NonViralName.class);
+            logger.info("New Taxon Name Title : " + nvnNew.getTitleCache());
+
+            Assert.assertNotEquals("Title caches should not be equal",oldTitleCache,nvnNew.getTitleCache());
+
+            nvnNew.setTitleCache(oldTitleCache, true);
+            taxonService.update(taxon);
+
+            Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
+            NonViralName nvnOld = CdmBase.deproxy(taxon.getName(),NonViralName.class);
+            logger.info("Old Taxon Name Title : " + nvnNew.getTitleCache());
+
+            Assert.assertEquals("Title caches should be equal",oldTitleCache,nvnOld.getTitleCache());
+        }
+
+        @Ignore
+        @Test
+        public void testCDMEntitySaveCollection() {
+            Taxon taxon = (Taxon)taxonService.find(taxonUuid1);
+
+            Set<SynonymRelationship> synRelations = taxon.getSynonymRelations();
+            Set<String> relToTitles = new HashSet<String>();
+            Iterator<SynonymRelationship> srItr = synRelations.iterator();
+            while(srItr.hasNext()) {
+                SynonymRelationship sr = srItr.next();
+                System.out.println("Synonym Title Cache : " + sr.getSynonym().getTitleCache());
+                relToTitles.add(sr.getSynonym().getTitleCache());
+                sr.getSynonym().setTitleCache(sr.getSynonym().getTitleCache() + ":updated");
+
+            }
+            taxonService.merge(taxon);
+
+            Taxon taxonNew = (Taxon)taxonService.find(taxonUuid1);
+            Set<SynonymRelationship> synRelationsNew = taxonNew.getSynonymRelations();
+
+            Iterator<SynonymRelationship> srItrNew = synRelationsNew.iterator();
+            Iterator<String> relToTitlesItr = relToTitles.iterator();
+            while(srItrNew.hasNext() && relToTitlesItr.hasNext()) {
+                SynonymRelationship srNew = srItrNew.next();
+                String relToTitle = relToTitlesItr.next();
+                System.out.println("New Synonym Title Cache: " + srNew.getSynonym().getTitleCache());
+                Assert.assertNotEquals("Synonym Title caches should not be equal", srNew.getSynonym().getTitleCache(), relToTitle);
+                srNew.getSynonym().setTitleCache(relToTitle);
+            }
+
+            Taxon taxonOld = (Taxon)taxonService.find(taxonUuid1);
+
+            Set<SynonymRelationship> synRelationsOld = taxonNew.getSynonymRelations();
+            Iterator<SynonymRelationship> srItrOld = synRelationsOld.iterator();
+            relToTitlesItr = relToTitles.iterator();
+            while(srItrOld.hasNext() && relToTitlesItr.hasNext()) {
+                SynonymRelationship srOld = srItrOld.next();
+                String relToTitle = relToTitlesItr.next();
+                System.out.println("New Synonym Title Cache: " + srOld.getSynonym().getTitleCache());
+                Assert.assertEquals("Synonym Title caches should be equal", srOld.getSynonym().getTitleCache(), relToTitle);
+
+            }
+        }
+}