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