ref #6630 and ref #6368 remove generics from TaxonName
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / api / facade / DerivedUnitFacadeCacheStrategyInjectionTest.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.facade;
10
11 import java.io.FileNotFoundException;
12 import java.util.UUID;
13
14 import org.apache.log4j.Logger;
15 import org.hibernate.proxy.HibernateProxy;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Ignore;
19 import org.junit.Test;
20 import org.unitils.dbunit.annotation.DataSet;
21 import org.unitils.spring.annotation.SpringBeanByType;
22
23 import eu.etaxonomy.cdm.api.service.IDescriptionService;
24 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
25 import eu.etaxonomy.cdm.api.service.ITaxonService;
26 import eu.etaxonomy.cdm.model.agent.Person;
27 import eu.etaxonomy.cdm.model.agent.Team;
28 import eu.etaxonomy.cdm.model.common.CdmBase;
29 import eu.etaxonomy.cdm.model.common.DefinedTerm;
30 import eu.etaxonomy.cdm.model.common.Language;
31 import eu.etaxonomy.cdm.model.common.LanguageString;
32 import eu.etaxonomy.cdm.model.common.TimePeriod;
33 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
34 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
35 import eu.etaxonomy.cdm.model.location.Country;
36 import eu.etaxonomy.cdm.model.location.NamedArea;
37 import eu.etaxonomy.cdm.model.location.Point;
38 import eu.etaxonomy.cdm.model.location.ReferenceSystem;
39 import eu.etaxonomy.cdm.model.media.Media;
40 import eu.etaxonomy.cdm.model.name.Rank;
41 import eu.etaxonomy.cdm.model.name.TaxonName;
42 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
43 import eu.etaxonomy.cdm.model.occurrence.Collection;
44 import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
45 import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
46 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
47 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
48 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
49 import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
50 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
51 import eu.etaxonomy.cdm.model.taxon.Taxon;
52 import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
53 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
54
55 /**
56 * NOTE: The only reason for having this test is to test if injection of the cache strategy into a
57 * standard specimen works. Once we use another default cache strategy then the derived unit facade
58 * this test can be deleted or adapted and moved to cdmlib-model.
59 *
60 * @author a.mueller
61 * @date 03.06.2010
62 *
63 */
64 public class DerivedUnitFacadeCacheStrategyInjectionTest extends CdmTransactionalIntegrationTest {
65 @SuppressWarnings("unused")
66 private static final Logger logger = Logger.getLogger(DerivedUnitFacadeCacheStrategyInjectionTest.class);
67
68 @SpringBeanByType
69 private IOccurrenceService occService;
70 @SpringBeanByType
71 private ITaxonService taxonService;
72 @SpringBeanByType
73 private IDescriptionService descService;
74
75 private static final UUID taxonUuid = UUID.fromString("10cfb372-0b1a-4d82-9707-c5ffd2b93a55");
76
77
78 private DerivedUnit specimen;
79 private DerivationEvent derivationEvent;
80 private FieldUnit fieldUnit;
81 private GatheringEvent gatheringEvent;
82 private final Integer absoluteElevation = 40;
83 private final Integer absoluteElevationError = 2;
84 private final Team collector = Team.NewInstance();
85 private final String collectingMethod = "Collection Method";
86 private final Double distanceToGround = 22.0;
87 private final Double distanceToSurface = 50.0;
88 private final ReferenceSystem referenceSystem = ReferenceSystem.WGS84();
89 private final Point exactLocation = Point.NewInstance(12.3, 10.567, referenceSystem, 22);
90 private final String gatheringEventDescription = "A nice gathering description";
91 private final TimePeriod gatheringPeriod = TimePeriodParser.parseString("03.05.2005");
92 private final String ecology = "sand dunes";
93 private final String plantDescription = "flowers blue";
94
95 private final String fieldNumber = "5678";
96 private final String fieldNotes = "such a beautiful specimen";
97 private Person primaryCollector;
98
99 private final Integer individualCount = 1;
100 private final DefinedTerm lifeStage = DefinedTerm.NewStageInstance("A wonderful stage", "stage", "st");
101 private final DefinedTerm sex = DefinedTerm.NewSexInstance("FemaleMale", "FM", "FM");
102 private final NamedArea country = Country.GERMANY();
103 private final LanguageString locality = LanguageString.NewInstance("Berlin-Dahlem, E side of Englerallee", Language.DEFAULT());
104
105 private final String exsiccatum = "Greuter, Pl. Dahlem. 456";
106 private final String accessionNumber = "8909756";
107 private final String catalogNumber = "UU879873590";
108 private final TaxonName taxonName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS(), "Abies", null, null, null, null, null, null, null);
109 private final String collectorsNumber = "234589913A34";
110 private final Collection collection = Collection.NewInstance();
111
112 private final PreservationMethod preservationMethod = PreservationMethod.NewInstance(null, "my prservation");
113
114 private DerivedUnit collectionSpecimen;
115 private GatheringEvent existingGatheringEvent;
116 private DerivationEvent firstDerivationEvent;
117 private FieldUnit firstFieldObject;
118 private final Media media1 = Media.NewInstance();
119
120
121 //****************************** SET UP *****************************************/
122
123 // /**
124 // * @throws java.lang.Exception
125 // */
126 // @BeforeClass
127 // public static void setUpBeforeClass() throws Exception {
128 // // FIXME maybe this will cause problems in other tests
129 // // INDEED !!!! it causes problems thus this is replaced by making this test a CdmIntegrationTest !!!
130 // new DefaultTermInitializer().initialize();
131 // }
132
133 /**
134 * @throws java.lang.Exception
135 */
136 @Before
137 public void setUp() throws Exception {
138 specimen = DerivedUnit.NewPreservedSpecimenInstance();
139
140 derivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
141 specimen.setDerivedFrom(derivationEvent);
142 fieldUnit = FieldUnit.NewInstance();
143 fieldUnit.addDerivationEvent(derivationEvent);
144 gatheringEvent = GatheringEvent.NewInstance();
145 fieldUnit.setGatheringEvent(gatheringEvent);
146 gatheringEvent.setAbsoluteElevation(absoluteElevation);
147 // gatheringEvent.setAbsoluteElevationError(absoluteElevationError);
148 gatheringEvent.setActor(collector);
149 gatheringEvent.setCollectingMethod(collectingMethod);
150 gatheringEvent.setDistanceToGround(distanceToGround);
151 gatheringEvent.setDistanceToWaterSurface(distanceToSurface);
152 gatheringEvent.setExactLocation(exactLocation);
153 gatheringEvent.setDescription(gatheringEventDescription);
154
155 gatheringEvent.setTimeperiod(gatheringPeriod);
156 gatheringEvent.setLocality(locality);
157 gatheringEvent.setCountry(country);
158
159 fieldUnit.setFieldNumber(fieldNumber);
160 fieldUnit.setFieldNotes(fieldNotes);
161 fieldUnit.setIndividualCount(individualCount);
162 fieldUnit.setSex(sex);
163 fieldUnit.setLifeStage(lifeStage);
164 primaryCollector = Person.NewTitledInstance("Kilian");
165 collector.addTeamMember(primaryCollector);
166 Person secondCollector = Person.NewInstance();
167 secondCollector.setFirstname("Andreas");
168 secondCollector.setLastname("Muller");
169 collector.addTeamMember(secondCollector);
170 Person thirdCollector = Person.NewTitledInstance("Kohlbecker");
171 collector.addTeamMember(thirdCollector);
172 fieldUnit.setPrimaryCollector(primaryCollector);
173
174 specimen.setAccessionNumber(accessionNumber);
175 specimen.setCatalogNumber(catalogNumber);
176 specimen.setStoredUnder(taxonName);
177 specimen.setCollection(collection);
178 specimen.setPreservation(preservationMethod);
179 specimen.setExsiccatum(exsiccatum);
180
181 //existing specimen with 2 derivation events in line
182 collectionSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
183 DerivedUnit middleSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
184 firstFieldObject = FieldUnit.NewInstance();
185
186 //TODO maybe we should define concrete event types here
187 DerivationEvent lastDerivationEvent = DerivationEvent.NewInstance(null);
188 DerivationEvent middleDerivationEvent = DerivationEvent.NewInstance(null);
189 firstDerivationEvent = DerivationEvent.NewInstance(null);
190
191 collectionSpecimen.setDerivedFrom(lastDerivationEvent);
192
193 lastDerivationEvent.addOriginal(middleSpecimen);
194 middleSpecimen.setDerivedFrom(firstDerivationEvent);
195 firstDerivationEvent.addOriginal(firstFieldObject);
196 existingGatheringEvent = GatheringEvent.NewInstance();
197 firstFieldObject.setGatheringEvent(existingGatheringEvent);
198
199 }
200 /**
201 * Test method for {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.occurrence.Specimen)}.
202 */
203 @Test
204 public void testGetSpecimenTitleCache() {
205 // String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), sand dunes, 3.5.2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (B 8909756); flowers blue.";
206 String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), 3.5.2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (B 8909756).";
207
208 // DescriptionElementBase ecologyItem = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
209 // SpecimenDescription fieldUnitDescription = SpecimenDescription.NewInstance(fieldUnit);
210 // fieldUnitDescription.addElement(ecologyItem);
211 //// specimenFacade.setEcology(ecology);
212 // DescriptionElementBase plantDescItem = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
213 // fieldUnitDescription.addElement(plantDescItem);
214 //// specimenFacade.setPlantDescription(plantDescription);
215
216 collection.setCode("B");
217 Assert.assertEquals(correctCache, specimen.getTitleCache());
218
219
220
221 }
222
223 /**
224 * Test method for {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.occurrence.Specimen)}.
225 */
226 @Test
227 public void testGetFieldUnitTitleCache() {
228 // String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), sand dunes, 3.5.2005, Kilian 5678, A. Muller & Kohlbecker; flowers blue.";
229 String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), 3.5.2005, Kilian 5678, A. Muller & Kohlbecker.";
230
231 // DescriptionElementBase ecologyItem = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
232 // SpecimenDescription fieldUnitDescription = SpecimenDescription.NewInstance(fieldUnit);
233 // fieldUnitDescription.addElement(ecologyItem);
234 // DescriptionElementBase plantDescItem = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
235 // fieldUnitDescription.addElement(plantDescItem);
236
237 collection.setCode("B"); //no effect
238 Assert.assertEquals(correctCache, fieldUnit.getTitleCache());
239 }
240
241 //Test if even a hibernate proxy (javassist) class correctly loads the DerivedUnitCacheStrategy.
242 @Test
243 @DataSet
244 public void testPersistedDerivedUnit(){
245 Taxon taxon = (Taxon)this.taxonService.find(taxonUuid);
246
247 IndividualsAssociation indivAssoc = getDescriptionElement(taxon, 5000);
248 SpecimenOrObservationBase<?> specimen = indivAssoc.getAssociatedSpecimenOrObservation();
249 Assert.assertTrue("Specimen should be proxy otherwise the test does not test what it should", specimen instanceof HibernateProxy);
250 DerivedUnit myUnit = CdmBase.deproxy(specimen, DerivedUnit.class);
251 myUnit.setTitleCache(null);
252 String expectedDerivedUnitCache = "Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10°34'1.2\"N, 12°18'E, 3.5.2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (8909756).";
253 Assert.assertEquals(expectedDerivedUnitCache, myUnit.getTitleCache());
254 }
255
256 @Test
257 @DataSet
258 public void testPersistedFieldUnit(){
259 Taxon taxon = (Taxon)this.taxonService.find(taxonUuid);
260 IndividualsAssociation indivAssoc = getDescriptionElement(taxon, 5001);
261 SpecimenOrObservationBase<?> fieldUnit = indivAssoc.getAssociatedSpecimenOrObservation();
262 Assert.assertTrue("FieldUnit should be proxy otherwise the test does not test what it should", fieldUnit instanceof HibernateProxy);
263 FieldUnit myFieldUnit = CdmBase.deproxy(fieldUnit, FieldUnit.class);
264 myFieldUnit.setTitleCache(null);
265 String expectedFieldUnitCache = "Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10°34'1.2\"N, 12°18'E, 3.5.2005, Kilian 5678, A. Muller & Kohlbecker.";
266 Assert.assertEquals(expectedFieldUnitCache, myFieldUnit.getTitleCache());
267 }
268
269
270
271 /**
272 * @param taxon
273 * @param i
274 * @return
275 */
276 private IndividualsAssociation getDescriptionElement(Taxon taxon, int id) {
277 for (DescriptionElementBase el : taxon.getDescriptions().iterator().next().getElements()){
278 if (el.getId() == id) {
279 return (IndividualsAssociation)el;
280 }
281 }
282 return null;
283 }
284
285 @Override
286 @Test
287 @Ignore
288 public void createTestDataSet() throws FileNotFoundException {
289 // specimen.setUuid(derivedUnitUuid);
290 // fieldUnit.setUuid(fieldUnitUuid);
291 // Taxon taxon = Taxon.NewInstance(null, null);
292 // taxon.setUuid(taxonUuid);
293 // TaxonDescription desc = TaxonDescription.NewInstance(taxon);
294 //
295 // IndividualsAssociation indAssoc = IndividualsAssociation.NewInstance(specimen);
296 // desc.addElement(indAssoc);
297 //
298 // this.taxonService.saveOrUpdate(taxon);
299 //
300 // setComplete();
301 // endTransaction();
302 //
303 // writeDbUnitDataSetFile(new String[]{"SpecimenOrObservationBase",
304 // "DerivationEvent", "DescriptionBase","DescriptionElementBase",
305 // "GatheringEvent","AgentBase","LanguageString","TaxonName",
306 // "TaxonBase","Collection",
307 // "MaterialOrMethodEvent","SpecimenOrObservationBase_DerivationEvent"});
308 }
309
310 }