Merge branch 'release/5.45.0'
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / api / service / CommonServiceImplTest.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.api.service;
10
11 import static org.junit.Assert.assertEquals;
12
13 import java.io.FileNotFoundException;
14 import java.util.Set;
15 import java.util.UUID;
16
17 import org.apache.logging.log4j.LogManager;
18 import org.apache.logging.log4j.Logger;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.unitils.dbunit.annotation.DataSet;
22 import org.unitils.spring.annotation.SpringBeanByType;
23
24 import eu.etaxonomy.cdm.model.agent.Person;
25 import eu.etaxonomy.cdm.model.common.Annotation;
26 import eu.etaxonomy.cdm.model.common.CdmBase;
27 import eu.etaxonomy.cdm.model.common.Language;
28 import eu.etaxonomy.cdm.model.name.IBotanicalName;
29 import eu.etaxonomy.cdm.model.name.Rank;
30 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
31 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
32 import eu.etaxonomy.cdm.model.reference.Reference;
33 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
34 import eu.etaxonomy.cdm.model.reference.ReferenceType;
35 import eu.etaxonomy.cdm.model.taxon.Taxon;
36 import eu.etaxonomy.cdm.persistence.dto.ReferencingObjectDto;
37 import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
38
39 /**
40 * @author a.mueller
41 */
42 public class CommonServiceImplTest extends CdmIntegrationTest {
43
44 private static final Logger logger = LogManager.getLogger();
45
46 @SpringBeanByType
47 private ICommonService service;
48
49 @SpringBeanByType
50 private ITaxonService taxonService;
51
52 @SpringBeanByType
53 private IReferenceService referenceService;
54
55 @SpringBeanByType
56 private IOccurrenceService occurrenceService;
57
58 /****************** TESTS *****************************/
59
60 @Test
61 public final void testSetDao() {
62 Assert.assertNotNull(service);
63 }
64
65 @Test
66 @DataSet
67 public final void testGetReferencingObjectsDto() {
68
69 IBotanicalName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
70 name.setTitleCache("A name", true);
71 Reference ref1 = ReferenceFactory.newArticle();
72 Taxon taxon = Taxon.NewInstance(name, ref1);
73 taxon.addImportSource("id1", null, ref1, null);
74 Person author = Person.NewInstance();
75 author.setTitleCache("Author", true);
76 ref1.addAnnotation(Annotation.NewInstance("A1", Language.DEFAULT()));
77 ref1.setAuthorship(author);
78 name.setBasionymAuthorship(author);
79
80 name.setNomenclaturalReference(ref1);
81
82 taxonService.save(taxon);
83
84 Set<ReferencingObjectDto> referencedObjects = service.getReferencingObjectDtos(ref1);
85 String debug = "############## RESULT ###################\n";
86 for (ReferencingObjectDto obj: referencedObjects){
87 debug += "Object: " + obj.getClass().getSimpleName() + " - " + obj + "\n";
88 }
89 assertEquals(3, referencedObjects.size()); //AM: was expected=3 first, as annotations are not reported I reduced to 2 (this is not related to not having a commit before, I tested it)
90 //should not throw an exception
91 referencedObjects = service.initializeReferencingObjectDtos(referencedObjects, true, true, true, null);
92 debug += "############## END ###################\n";
93
94 referencedObjects = service.getReferencingObjectDtos(author);
95 debug += "############## RESULT ###################\n";
96 for (ReferencingObjectDto obj: referencedObjects){
97 debug += "Object: " + obj.getClass().getSimpleName() + " - " + obj + "\n";
98 }
99 assertEquals(2, referencedObjects.size());
100 referencedObjects = service.initializeReferencingObjectDtos(referencedObjects, true, true, true, null);
101 debug += "############## END ###################\n";
102 logger.info(debug);
103 }
104
105 @Test
106 @DataSet
107 public final void testGetReferencingObjects() {
108
109 IBotanicalName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
110 name.setTitleCache("A name", true);
111 Reference ref1 = ReferenceFactory.newArticle();
112 Taxon taxon = Taxon.NewInstance(name, ref1);
113 Person author = Person.NewInstance();
114 author.setTitleCache("Author", true);
115 ref1.addAnnotation(Annotation.NewInstance("A1", Language.DEFAULT()));
116 ref1.setAuthorship(author);
117 name.setBasionymAuthorship(author);
118
119 name.setNomenclaturalReference(ref1);
120
121 taxonService.save(taxon);
122
123 Set<CdmBase> referencedObjects = service.getReferencingObjects(ref1);
124 System.out.println("############## RESULT ###################\n");
125 for (CdmBase obj: referencedObjects){
126 System.out.println("Object: " + obj.getClass().getSimpleName() + " - " + obj + "\n");
127 }
128 assertEquals(2, referencedObjects.size()); //AM: was expected=3 first, as annotations are not reported I reduced to 2 (this is not related to not having a commit before, I tested it)
129 System.out.println("############## END ###################\n");
130
131 referencedObjects = service.getReferencingObjects(author);
132 System.out.println("############## RESULT ###################\n");
133 for (CdmBase obj: referencedObjects){
134 System.out.println("Object: " + obj.getClass().getSimpleName() + " - " + obj + "\n");
135 }
136 assertEquals(2, referencedObjects.size());
137 System.out.println("############## END ###################\n");
138 }
139
140 @Test
141 @DataSet
142 public final void testGetReferencingObjects2() {
143 // SpecimenDescription desc1 = SpecimenDescription.NewInstance();
144 // desc1.setTitleCache("desc1");
145 // SpecimenDescription desc2 = SpecimenDescription.NewInstance();
146 // desc2.setTitleCache("desc2");
147 //
148 // SpecimenOrObservationBase spec1 = Specimen.NewInstance();
149 //
150 // desc1.addDescribedSpecimenOrObservation(spec1);
151 // //Taxon taxon = Taxon.NewInstance(taxonName, sec)
152 // spec1.addDescription(desc2);
153 //
154 // occurrenceService.save(spec1);
155
156 UUID uuidSpec = UUID.fromString("41539e9c-3764-4f14-9712-2d07d00c8e4c");
157 SpecimenOrObservationBase<?> spec1 = occurrenceService.find(uuidSpec);
158
159 Set<CdmBase> referencedObjects = service.getReferencingObjects(spec1);
160 // System.out.println("############## RESULT ###################");
161 // for (CdmBase obj: referencedObjects){
162 // System.out.println("Object: " + obj.getClass().getSimpleName() + " - " + obj);
163 // }
164 assertEquals(2, referencedObjects.size());
165 System.out.println("############## ENDE ###################");
166 }
167
168 @Test
169 @DataSet
170 public final void testLoadCacheStrategyForReference(){
171 Reference ref = referenceService.load(UUID.fromString("613980ac-9bd5-43b9-a374-d71e1794688f"));
172 ref.setType(ReferenceType.Article);
173 referenceService.update(ref);
174 referenceService.updateCaches();
175 }
176
177 @Override
178 public void createTestDataSet() throws FileNotFoundException {}
179 }