fix findByIdentifier failing test
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / api / service / IdentifiableServiceBaseTest.java
index 1efd71ab72376b6c640494ac612e51b2c1ac43c0..9bb7b1a91769c2566f28717b5455c710d986aae6 100644 (file)
@@ -9,7 +9,8 @@
 \r
 package eu.etaxonomy.cdm.api.service;\r
 \r
-import java.io.FileOutputStream;\r
+import java.io.FileNotFoundException;\r
+import java.util.UUID;\r
 \r
 import org.apache.log4j.Logger;\r
 import org.junit.Assert;\r
@@ -20,7 +21,23 @@ import org.unitils.dbunit.annotation.DataSet;
 import org.unitils.dbunit.annotation.ExpectedDataSet;\r
 import org.unitils.spring.annotation.SpringBeanByType;\r
 \r
+import eu.etaxonomy.cdm.api.service.dto.FindByIdentifierDTO;\r
+import eu.etaxonomy.cdm.api.service.pager.Pager;\r
+import eu.etaxonomy.cdm.model.common.CdmBase;\r
+import eu.etaxonomy.cdm.model.common.DefinedTerm;\r
+import eu.etaxonomy.cdm.model.common.Identifier;\r
+import eu.etaxonomy.cdm.model.common.TermVocabulary;\r
+import eu.etaxonomy.cdm.model.common.VocabularyEnum;\r
+import eu.etaxonomy.cdm.model.name.BotanicalName;\r
+import eu.etaxonomy.cdm.model.name.Rank;\r
 import eu.etaxonomy.cdm.model.name.TaxonNameBase;\r
+import eu.etaxonomy.cdm.model.taxon.Classification;\r
+import eu.etaxonomy.cdm.model.taxon.Synonym;\r
+import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;\r
+import eu.etaxonomy.cdm.model.taxon.Taxon;\r
+import eu.etaxonomy.cdm.model.taxon.TaxonBase;\r
+import eu.etaxonomy.cdm.model.taxon.TaxonNode;\r
+import eu.etaxonomy.cdm.persistence.query.MatchMode;\r
 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;\r
 \r
 /**\r
@@ -29,12 +46,25 @@ import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
  */\r
 @Transactional(TransactionMode.DISABLED)\r
 public class IdentifiableServiceBaseTest extends CdmTransactionalIntegrationTest {\r
+       @SuppressWarnings("unused")\r
        private static final Logger logger = Logger.getLogger(IdentifiableServiceBaseTest.class);\r
        \r
        \r
        @SpringBeanByType\r
        private INameService nameService;\r
        \r
+       @SpringBeanByType\r
+       private ITermService termService;\r
+       \r
+       @SpringBeanByType\r
+       private IVocabularyService vocService;\r
+       \r
+       @SpringBeanByType\r
+       private ITaxonService taxonService;\r
+       \r
+       @SpringBeanByType\r
+       private IClassificationService classificationService;\r
+       \r
 /****************** TESTS *****************************/\r
        \r
        @Test\r
@@ -45,25 +75,174 @@ public class IdentifiableServiceBaseTest extends CdmTransactionalIntegrationTest
        \r
        @Test\r
        @DataSet\r
-       @ExpectedDataSet\r
+       @ExpectedDataSet        \r
        public final void testUpdateTitleCache() {\r
                Assert.assertEquals("There should be 5 TaxonNames in the data set", 5, nameService.count(TaxonNameBase.class));\r
                Class clazz = TaxonNameBase.class;\r
                int stepSize = 2;\r
-               nameService.updateTitleCache(clazz, stepSize, null);\r
-//             TaxonNameBase name = nameService.find(UUID.fromString("5d74500b-9fd5-4d18-b9cd-cc1c8a372fec"));\r
-               setComplete();\r
-               endTransaction();\r
-               try {\r
-                       printDataSet(new FileOutputStream("C:\\tmp\\test.xml"), new String[]{"TaxonNameBase"});\r
-               } catch(Exception e) { \r
-                       logger.warn(e);\r
-               } \r
+               nameService.updateTitleCache(clazz, stepSize, null, null);\r
+               commit();\r
+//             commitAndStartNewTransaction(new String[]{"TaxonNameBase","TaxonNameBase_AUD"});        \r
+       }\r
+       \r
+       \r
+       @Test\r
+       @DataSet(value="IdentifiableServiceBaseTest.testFindByIdentifier.xml")\r
+       public final void testListByIdentifier(){\r
+               UUID uuidIdentifierType1 = UUID.fromString("02bb62db-a229-4eeb-83e6-a9a093943d5e");\r
+               UUID uuidIdentifierType2 = UUID.fromString("ef6e960f-5289-456c-b25c-cff7f4de2f63");\r
+               \r
+               \r
+               DefinedTerm it1 = (DefinedTerm)termService.find(uuidIdentifierType1);\r
+               Assert.assertNotNull("identifier type must not be null", it1);\r
+               \r
+               boolean includeEntity = true;\r
+               Pager<FindByIdentifierDTO<Taxon>> taxonPager = taxonService.findByIdentifier(Taxon.class, "ext-1234", it1, null, includeEntity, null, null, null);\r
+               Assert.assertTrue("Result should not be empty", taxonPager.getCount() == 1);\r
+               FindByIdentifierDTO<Taxon>.CdmEntity entity = taxonPager.getRecords().get(0).getCdmEntity();\r
+               Taxon taxon = entity.getEntity();\r
+               Assert.assertEquals(UUID.fromString("888cded1-cadc-48de-8629-e32927919879"), taxon.getUuid());\r
+               Assert.assertEquals(UUID.fromString("888cded1-cadc-48de-8629-e32927919879"), entity.getCdmUuid());\r
+               Assert.assertEquals("Taxon should have 1 identifier", 1, taxon.getIdentifiers().size());\r
+               Identifier<?> identifier = taxon.getIdentifiers().get(0);\r
+               DefinedTerm type = CdmBase.deproxy(identifier.getType(), DefinedTerm.class);\r
+               Assert.assertEquals(uuidIdentifierType1, type.getUuid());\r
+               \r
+               Pager<FindByIdentifierDTO<TaxonNameBase>> names = nameService.findByIdentifier(\r
+                               TaxonNameBase.class, "ext-1234", null, null, includeEntity, null, null, null);\r
+               Assert.assertTrue("Identifier does not exist for TaxonName", names.getCount() == 0);\r
+               \r
+               taxonPager = taxonService.findByIdentifier(null, "ext-1234", null, null, includeEntity, null, null, null);\r
+               Assert.assertEquals("Result size for 'ext-1234' should be 1", 1, taxonPager.getRecords().size());\r
+               \r
+               taxonPager = taxonService.findByIdentifier(Taxon.class, null, null, null, includeEntity, null, null, null);\r
+               Assert.assertEquals("Result should not be empty", 2 , taxonPager.getRecords().size());\r
+               \r
+               //includeEntity\r
+               includeEntity = false;\r
+               taxonPager = taxonService.findByIdentifier(Taxon.class, "ext-1234", it1, null, includeEntity, null, null, null);\r
+               entity = taxonPager.getRecords().get(0).getCdmEntity();\r
+               Assert.assertNull("Taxon must not be returned with includeEntity = false", entity.getEntity());\r
+               \r
                \r
+               \r
+               //Matchmode\r
+               includeEntity = false;\r
+               MatchMode matchmode = null;\r
+               taxonPager = taxonService.findByIdentifier(Taxon.class, "123", null, matchmode, includeEntity, null, null, null);\r
+               Assert.assertTrue("Result size for '123' should be 0", taxonPager.getCount() == 0);\r
+\r
+               taxonPager = taxonService.findByIdentifier(Taxon.class, "123", null, MatchMode.EXACT, includeEntity, null, null, null);\r
+               Assert.assertTrue("Result size for '123' should be 0", taxonPager.getCount() == 0);\r
+\r
+               taxonPager = taxonService.findByIdentifier(Taxon.class, "123", null, MatchMode.ANYWHERE, includeEntity, null, null, null);\r
+               Assert.assertTrue("Result size for '123' should be 1", taxonPager.getCount() == 1);\r
+\r
+               taxonPager = taxonService.findByIdentifier(Taxon.class, "123", null, MatchMode.BEGINNING, includeEntity, null, null, null);\r
+               Assert.assertTrue("Result size for '123' should be 0", taxonPager.getCount() == 0);\r
+\r
+               taxonPager = taxonService.findByIdentifier(Taxon.class, "ext", null, MatchMode.BEGINNING, includeEntity, null, null, null);\r
+               Assert.assertTrue("Result size for 'ext' should be 1", taxonPager.getCount() == 2);\r
+\r
+               //Paging\r
+               taxonPager = taxonService.findByIdentifier(null, "ext", null, MatchMode.BEGINNING, includeEntity, null, null, null);\r
+               Assert.assertEquals("Total result size for starts with 'ext' should be 4", 4, taxonPager.getRecords().size());\r
+               taxonPager = taxonService.findByIdentifier(null, "ext", null, MatchMode.BEGINNING, includeEntity, 2, 1, null);\r
+               Assert.assertEquals("Total result size for starts with 'ext' should be 4", Integer.valueOf(4), taxonPager.getCount());\r
+               Assert.assertEquals("Result size for starts with 'ext' second page should be 2", Integer.valueOf(2), taxonPager.getPageSize());\r
+               Assert.assertEquals("The third taxon (first on second page) should be ext-syn1", "ext-syn1", taxonPager.getRecords().get(0).getIdentifier().getIdentifier());\r
+               \r
+               taxonPager = taxonService.findByIdentifier(Taxon.class, "ext", null, MatchMode.BEGINNING, includeEntity, null, null, null);\r
+               Assert.assertTrue("Result size for 'ext' should be 2", taxonPager.getCount() == 2);\r
+\r
        }\r
+       \r
+       @Test\r
+       @DataSet(value="IdentifiableServiceBaseTest.testFindByIdentifier.xml")\r
+       public final void testListByIdentifierClassification(){\r
+               //classification Filter\r
+               Classification classification = classificationService.find(5000);\r
+               TaxonNode rootNode = classification.getRootNode();\r
+               Pager<FindByIdentifierDTO<Taxon>> taxonPager = taxonService.findByIdentifier(Taxon.class, "ext-1234", null, rootNode, MatchMode.EXACT, false, null, null, null);\r
+               Assert.assertEquals("Result size for 'ext' should be 1", Integer.valueOf(1), taxonPager.getCount());\r
+               Assert.assertEquals("Result size for 'ext' should be 1", Integer.valueOf(1), (Integer)taxonPager.getRecords().size());\r
+               \r
+               Pager<FindByIdentifierDTO<Taxon>> taxPager = taxonService.findByIdentifier(Taxon.class, "ext-cache1", null, rootNode, MatchMode.EXACT, false, null, null, null);\r
+               Assert.assertEquals("Result size for 'ext' should be 0", Integer.valueOf(0), taxPager.getCount());\r
+               Assert.assertEquals("Result size for 'ext' should be 0", 0, taxPager.getRecords().size());\r
+               \r
+               rootNode = null;  //check against missing filter\r
+               taxPager = taxonService.findByIdentifier(Taxon.class, "ext-cache1", null, rootNode, MatchMode.EXACT, false, null, null, null);\r
+               Assert.assertEquals("Result size for 'ext-cache1' without filter should be 1", Integer.valueOf(1), taxPager.getCount());\r
+               Assert.assertEquals("Result size for 'ext-cache1' without filter should be 1", 1, taxPager.getRecords().size());\r
+               \r
+               //TaxonBase\r
+               rootNode = classification.getRootNode();\r
+               Pager<FindByIdentifierDTO<TaxonBase>> tbPager = taxonService.findByIdentifier(TaxonBase.class, "ext-1234", null, rootNode, MatchMode.EXACT, false, null, null, null);\r
+               Assert.assertEquals("Result size for 'ext' should be 1", Integer.valueOf(1), tbPager.getCount());\r
+               Assert.assertEquals("Result size for 'ext' should be 1", Integer.valueOf(1), (Integer)tbPager.getRecords().size());\r
 \r
+               tbPager = taxonService.findByIdentifier(TaxonBase.class, "ext-cache1", null, rootNode, MatchMode.EXACT, false, null, null, null);\r
+               Assert.assertEquals("Result size for 'ext' should be 0", Integer.valueOf(0), tbPager.getCount());\r
+               Assert.assertEquals("Result size for 'ext' should be 0", 0, tbPager.getRecords().size());\r
+               \r
+               //Synonym\r
+               Pager<FindByIdentifierDTO<Synonym>> synPager = taxonService.findByIdentifier(Synonym.class, "ext-syn", null, rootNode, MatchMode.BEGINNING, false, null, null, null);\r
+               Assert.assertEquals("1 Synonym should be linked to the according classification", Integer.valueOf(1), synPager.getCount());\r
+               Assert.assertEquals("1 Synonym should be linked to the according classification", 1, synPager.getRecords().size());\r
 \r
-       public static Logger getLogger() {\r
-               return logger;\r
        }\r
+\r
+\r
+\r
+    /* (non-Javadoc)\r
+     * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()\r
+     */\r
+//     @Test\r
+    @Override\r
+    public void createTestDataSet() throws FileNotFoundException {\r
+               TermVocabulary<DefinedTerm> voc = vocService.find(VocabularyEnum.IdentifierType.getUuid());\r
+       \r
+               DefinedTerm identifierType1 = DefinedTerm.NewIdentifierTypeInstance(null, "identifierType1", null);\r
+       voc.addTerm(identifierType1);\r
+               termService.save(identifierType1);\r
+       DefinedTerm identifierType2 = DefinedTerm.NewIdentifierTypeInstance(null, "identifierType2", null);\r
+       voc.addTerm(identifierType2);\r
+               termService.save(identifierType2);\r
+       \r
+       \r
+       BotanicalName name = BotanicalName.NewInstance(Rank.SPECIES());\r
+        Taxon tb = Taxon.NewInstance(name, null);\r
+        Identifier.NewInstance(tb, "ext-1234", identifierType1);\r
+        Identifier.NewInstance(name, "ext-name12", identifierType2);\r
+        taxonService.saveOrUpdate(tb);\r
+        \r
+        Taxon tb2 = Taxon.NewInstance(null, null);\r
+        tb2.setTitleCache("Cached taxon", true);\r
+        Identifier.NewInstance(tb2, "ext-cache1", identifierType2);\r
+        taxonService.saveOrUpdate(tb2);\r
+        \r
+        Classification classification = Classification.NewInstance("My classification");\r
+        classification.addChildTaxon(tb, null, null);\r
+        classificationService.saveOrUpdate(classification);\r
+        \r
+        tb2.addSynonymName(null, SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF());\r
+        \r
+        commitAndStartNewTransaction(null);\r
+\r
+        // this will write flat xml file to the same package in the test resources \r
+        // the test file is named after the test class like: TestClassName.xml\r
+               writeDbUnitDataSetFile(new String[] {\r
+                       "TAXONBASE", "TAXONNAMEBASE","IDENTIFIER","TAXONBASE_IDENTIFIER",\r
+                       "TAXONNAMEBASE_IDENTIFIER",\r
+                       "REFERENCE",\r
+                       "CLASSIFICATION", "TAXONNODE",\r
+                       "HOMOTYPICALGROUP",\r
+                       "TERMVOCABULARY",\r
+                       "SYNONYMRELATIONSHIP"\r
+                }, "xxxx");\r
+            \r
+    }\r
+\r
+\r
 }\r