merge-update from trunk
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / api / service / OccurrenceServiceTest.java
1 // $Id$
2 /**
3 * Copyright (C) 2014 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.service;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotEquals;
15 import static org.junit.Assert.assertTrue;
16
17 import java.io.FileNotFoundException;
18 import java.util.Set;
19 import java.util.UUID;
20
21 import org.apache.log4j.Logger;
22 import org.junit.Ignore;
23 import org.junit.Test;
24 import org.unitils.dbunit.annotation.DataSet;
25 import org.unitils.spring.annotation.SpringBeanByType;
26
27 import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
28 import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
29 import eu.etaxonomy.cdm.model.agent.Institution;
30 import eu.etaxonomy.cdm.model.agent.Person;
31 import eu.etaxonomy.cdm.model.common.CdmBase;
32 import eu.etaxonomy.cdm.model.common.DefinedTerm;
33 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
34 import eu.etaxonomy.cdm.model.common.Language;
35 import eu.etaxonomy.cdm.model.common.TimePeriod;
36 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
37 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
38 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
39 import eu.etaxonomy.cdm.model.description.TaxonDescription;
40 import eu.etaxonomy.cdm.model.location.Country;
41 import eu.etaxonomy.cdm.model.location.NamedArea;
42 import eu.etaxonomy.cdm.model.location.Point;
43 import eu.etaxonomy.cdm.model.location.ReferenceSystem;
44 import eu.etaxonomy.cdm.model.molecular.Amplification;
45 import eu.etaxonomy.cdm.model.molecular.DnaSample;
46 import eu.etaxonomy.cdm.model.molecular.Sequence;
47 import eu.etaxonomy.cdm.model.molecular.SingleRead;
48 import eu.etaxonomy.cdm.model.name.BotanicalName;
49 import eu.etaxonomy.cdm.model.name.Rank;
50 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
51 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
52 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
53 import eu.etaxonomy.cdm.model.occurrence.Collection;
54 import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
55 import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
56 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
57 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
58 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
59 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
60 import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
61 import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
62 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
63 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
64 import eu.etaxonomy.cdm.model.reference.Reference;
65 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
66 import eu.etaxonomy.cdm.model.taxon.Taxon;
67 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
68 import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
69
70 /**
71 * @author pplitzner
72 * @date 31.03.2014
73 *
74 */
75 public class OccurrenceServiceTest extends CdmTransactionalIntegrationTest {
76 @SuppressWarnings("unused")
77 private static final Logger logger = Logger.getLogger(OccurrenceServiceTest.class);
78
79 @SpringBeanByType
80 private IOccurrenceService occurrenceService;
81
82 @SpringBeanByType
83 private ISequenceService sequenceService;
84
85 @SpringBeanByType
86 private ITaxonService taxonService;
87
88 @SpringBeanByType
89 private INameService nameService;
90
91 @SpringBeanByType
92 private IDescriptionService descriptionService;
93
94 @Test
95 public void testGetNonCascadedAssociatedElements(){
96 //Collection
97 Collection collection = Collection.NewInstance();
98 Collection subCollection = Collection.NewInstance();
99 subCollection.setSuperCollection(collection);
100
101 Institution institution = Institution.NewInstance();
102 institution.addType(DefinedTerm.NewInstitutionTypeInstance("Research and teaching", "botanical garden", "BGBM"));
103 collection.setInstitute(institution);
104
105 //Source
106 Reference<?> article = ReferenceFactory.newArticle(getReference(), Person.NewInstance(), "title", "pages", "series", "volume", TimePeriod.NewInstance(2014));
107 IdentifiableSource source = IdentifiableSource.NewPrimarySourceInstance(article, "microCitation");
108
109 //FieldUnit
110 FieldUnit fieldUnit = FieldUnit.NewInstance();
111 Person primaryCollector = Person.NewInstance();
112 primaryCollector.setLifespan(TimePeriod.NewInstance(2014));
113 fieldUnit.setPrimaryCollector(primaryCollector);
114 fieldUnit.addSource(source);
115
116 //GatheringEvent
117 GatheringEvent gatheringEvent = GatheringEvent.NewInstance();
118 fieldUnit.setGatheringEvent(gatheringEvent);
119 gatheringEvent.putLocality(Language.ENGLISH(), "locality");
120 gatheringEvent.setExactLocation(Point.NewInstance(22.4, -34.2,
121 ReferenceSystem.NewInstance("MyReferenceSystem", "label", "labelAbbrev"), 33));
122 gatheringEvent.setCountry(Country.GERMANY());
123 gatheringEvent.addCollectingArea(NamedArea.EUROPE());
124
125 //Derived Unit
126 MediaSpecimen mediaSpecimen = MediaSpecimen.NewInstance(SpecimenOrObservationType.StillImage);
127 mediaSpecimen.setCollection(collection);
128 BotanicalName storedUnder = BotanicalName.NewInstance(Rank.SPECIES());
129 mediaSpecimen.setStoredUnder(storedUnder);
130 PreservationMethod preservation = PreservationMethod.NewInstance(null, "My preservation");
131 preservation.setMedium(DefinedTerm.NewDnaMarkerInstance("medium", "medium", "medium"));//dummy defined term
132 mediaSpecimen.setPreservation(preservation);
133
134 //DerivationEvent
135 DerivationEvent event = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
136 event.addOriginal(fieldUnit);
137 event.addDerivative(mediaSpecimen);
138
139 //SpecOrObservationBase
140 fieldUnit.setSex(DefinedTerm.SEX_FEMALE());
141 fieldUnit.setLifeStage(DefinedTerm.NewStageInstance("Live stage", "stage", null));
142 fieldUnit.setKindOfUnit(DefinedTerm.NewKindOfUnitInstance("Kind of unit", "Kind of unit", null));
143 fieldUnit.putDefinition(Language.ENGLISH(), "definition");
144
145 //Determination
146 DeterminationEvent determinationEvent = DeterminationEvent.NewInstance(getTaxon(), mediaSpecimen);
147 determinationEvent.setModifier(DefinedTerm.NewModifierInstance("modifierDescription", "modifierLabel", "mofifierLabelAbbrev"));
148 determinationEvent.setPreferredFlag(true);
149 Reference<?> reference = getReference();
150 determinationEvent.addReference(reference);
151
152 /*NonCascaded
153 * SOOB
154 * - sex (FEMALE)
155 * - stage (Live stage)
156 * - kindOfUnit (Kind of unit)
157 * GatheringEvent
158 * - country (GERMANY)
159 * - collectingArea (EUROPE)
160 * DerivedUnit
161 * - storedUnder (botanical name)
162 * DerivedUnit-> Collection -> institiute
163 * - type (botanical garden)
164 *
165 * */
166
167 assertEquals("Incorrect number of non cascaded CDM entities", 9, occurrenceService.getNonCascadedAssociatedElements(fieldUnit).size());
168 assertEquals("Incorrect number of non cascaded CDM entities", 9, occurrenceService.getNonCascadedAssociatedElements(mediaSpecimen).size());
169
170 }
171 private Reference<?> getReference() {
172 Reference<?> result = ReferenceFactory.newGeneric();
173 result.setTitle("some generic reference");
174 return result;
175 }
176 private Taxon getTaxon() {
177 Reference<?> sec = getReference();
178 TaxonNameBase<?,?> name = BotanicalName.NewInstance(Rank.GENUS());
179 Taxon taxon = Taxon.NewInstance(name, sec);
180 return taxon;
181
182 }
183
184 @Test
185 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="OccurenceServiceTest.move.xml")
186 public void testMoveDerivate(){
187 DerivedUnit specimenA = (DerivedUnit) occurrenceService.load(UUID.fromString("35cfb0b3-588d-4eee-9db6-ac9caa44e39a"));
188 DerivedUnit specimenB = (DerivedUnit) occurrenceService.load(UUID.fromString("09496534-efd0-44c8-b1ce-01a34a8a0229"));
189 DerivedUnit dnaSample = (DnaSample) occurrenceService.load(UUID.fromString("5995f852-0e78-405c-b849-d923bd6781d9"));
190
191
192 occurrenceService.saveOrUpdate(specimenA);
193 occurrenceService.saveOrUpdate(specimenB);
194 occurrenceService.saveOrUpdate(dnaSample);
195
196 DerivationEvent originalDerivedFromEvent = DerivationEvent.NewSimpleInstance(specimenA, dnaSample, DerivationEventType.DNA_EXTRACTION());
197
198 occurrenceService.moveDerivate(specimenA, specimenB, dnaSample);
199 assertTrue("DerivationEvent not removed from source!", specimenA.getDerivationEvents().isEmpty());
200 assertEquals("DerivationEvent not moved to source!", 1, specimenB.getDerivationEvents().size());
201 DerivationEvent derivationEvent = specimenB.getDerivationEvents().iterator().next();
202 assertEquals("Moved DerivationEvent not of same type!", DerivationEventType.DNA_EXTRACTION(), derivationEvent.getType());
203 assertEquals("Wrong number of derivation originals!", 1, derivationEvent.getOriginals().size());
204 SpecimenOrObservationBase<?> newOriginal = derivationEvent.getOriginals().iterator().next();
205 assertEquals("Origin of moved object not correct", specimenB, newOriginal);
206 assertEquals("Wrong number of derivatives!", 1, derivationEvent.getDerivatives().size());
207 DerivedUnit movedDerivate = derivationEvent.getDerivatives().iterator().next();
208 assertEquals("Moved derivate has wrong type", SpecimenOrObservationType.DnaSample, movedDerivate.getRecordBasis());
209 assertNotEquals("DerivationEvent 'derivedFrom' has not been changed after moving", originalDerivedFromEvent, movedDerivate.getDerivedFrom());
210
211 }
212
213 @Test
214 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="OccurenceServiceTest.move.xml")
215 public void testMoveSequence(){
216 DnaSample dnaSampleA = (DnaSample) occurrenceService.load(UUID.fromString("5995f852-0e78-405c-b849-d923bd6781d9"));
217 DnaSample dnaSampleB = (DnaSample) occurrenceService.load(UUID.fromString("85fccc2f-c796-46b3-b2fc-6c9a4d68cfda"));
218 String consensusSequence = "ATTCG";
219 Sequence sequence = sequenceService.load(UUID.fromString("6da4f378-9861-4338-861b-7b8073763e7a"));
220
221 occurrenceService.saveOrUpdate(dnaSampleA);
222 occurrenceService.saveOrUpdate(dnaSampleB);
223 sequenceService.saveOrUpdate(sequence);
224
225 dnaSampleA.addSequence(sequence);
226
227 occurrenceService.moveSequence(dnaSampleA, dnaSampleB, sequence);
228 assertEquals("Number of sequences is wrong", 0, dnaSampleA.getSequences().size());
229 assertEquals("Number of sequences is wrong", 1, dnaSampleB.getSequences().size());
230 Sequence next = dnaSampleB.getSequences().iterator().next();
231 assertEquals("Sequences are not equals", sequence, next);
232 assertEquals("Sequences are not equals", consensusSequence, next.getSequenceString());
233 }
234
235 // @Test
236 // public void testDeleteDerivateHierarchy_FieldUnit(){
237 // String assertMessage = "Incorrect number of specimens after deletion.";
238 //
239 // FieldUnit fieldUnit = initDerivateHierarchy();
240 //
241 // //delete field unit
242 // occurrenceService.deleteDerivateHierarchy(fieldUnit);
243 // commit();
244 // assertEquals(assertMessage, 0, occurrenceService.count(SpecimenOrObservationBase.class));
245 // assertEquals(assertMessage, 0, occurrenceService.count(FieldUnit.class));
246 // assertEquals(assertMessage, 0, occurrenceService.count(DerivedUnit.class));
247 // assertEquals(assertMessage, 0, occurrenceService.count(DnaSample.class));
248 // }
249
250 // @Test
251 // public void testDeleteDerivateHierarchy_DerivedUnit(){
252 // String assertMessage = "Incorrect number of specimens after deletion.";
253 //
254 // FieldUnit fieldUnit = initDerivateHierarchy();
255 //
256 // //delete derived unit
257 // DerivedUnit derivedUnit = fieldUnit.getDerivationEvents().iterator().next().getDerivatives().iterator().next();
258 // occurrenceService.deleteDerivateHierarchy(derivedUnit);
259 // commit();
260 // assertEquals(assertMessage, 1, occurrenceService.count(SpecimenOrObservationBase.class));
261 // assertEquals(assertMessage, 1, occurrenceService.count(FieldUnit.class));
262 // assertEquals(assertMessage, 0, occurrenceService.count(DerivedUnit.class));
263 // assertEquals(assertMessage, 0, occurrenceService.count(DnaSample.class));
264 // }
265
266 @Test
267 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class) //loads OccurrenceServiceTest.xml as base DB
268 public void testDeleteIndividualAssociatedAndTypeSpecimen(){
269 FieldUnit associatedFieldUnit = (FieldUnit) occurrenceService.load(UUID.fromString("afbe6682-2bd6-4f9e-ae24-b7479e0e585f"));
270 DerivedUnit typeSpecimen = (DerivedUnit) occurrenceService.load(UUID.fromString("3699806e-8d2b-4ae6-b96e-d065525b654a"));
271 BotanicalName name = (BotanicalName) nameService.load(UUID.fromString("da487692-6b8e-4e88-bda8-4afc93ff6461"));
272 TaxonDescription taxonDescription = (TaxonDescription) descriptionService.load(UUID.fromString("6ef35ede-4906-4f9c-893a-e4186f84dfc4"));
273 //check initial state (IndividualsAssociation)
274 Set<DescriptionElementBase> elements = taxonDescription.getElements();
275 DescriptionElementBase descriptionElement = elements.iterator().next();
276 assertTrue("wrong type of description element", descriptionElement instanceof IndividualsAssociation);
277 assertEquals("associated specimen is incorrect", associatedFieldUnit, ((IndividualsAssociation)descriptionElement).getAssociatedSpecimenOrObservation());
278 //check initial state (Type Designation)
279 Set<TypeDesignationBase> typeDesignations = name.getTypeDesignations();
280 TypeDesignationBase typeDesignation = typeDesignations.iterator().next();
281 assertTrue("wrong type of type designation", typeDesignation instanceof SpecimenTypeDesignation);
282 assertEquals("type specimen is incorrect", typeSpecimen, ((SpecimenTypeDesignation)typeDesignation).getTypeSpecimen());
283
284 SpecimenDeleteConfigurator config = new SpecimenDeleteConfigurator();
285 config.setDeleteChildren(false);
286 //delete associated field unit from IndividualsAssociation
287 config.setDeleteFromIndividualsAssociation(true);
288 occurrenceService.delete(associatedFieldUnit, config);
289 assertTrue(((IndividualsAssociation)descriptionElement).getAssociatedSpecimenOrObservation()==null);
290 //delete type specimen from type designation
291 config.setDeleteFromTypeDesignation(true);
292 occurrenceService.delete(typeSpecimen, config);
293 assertTrue(((SpecimenTypeDesignation)typeDesignation).getTypeSpecimen()==null);
294
295 }
296
297 @Test
298 @DataSet(value="OccurrenceService.loadData.xml")
299 public void testLoadData() {
300 String fieldUnitUuid = "5a31df5a-2e4d-40b1-8d4e-5754736ae7ef";
301 String derivedUnitUuid = "18f70977-5d9c-400a-96c4-0cb7a2cd287e";
302
303 FieldUnit fieldUnit = (FieldUnit) occurrenceService.load(UUID.fromString(fieldUnitUuid));
304 DerivedUnit derivedUnit = (DerivedUnit) occurrenceService.load(UUID.fromString(derivedUnitUuid));
305
306 assertFalse(fieldUnit.getDerivationEvents().iterator().next().getDerivatives().isEmpty());
307 assertTrue(derivedUnit.getDerivedFrom()!=null);
308 }
309
310 @Test
311 @DataSet//(loadStrategy=CleanSweepInsertLoadStrategy.class) //loads OccurrenceServiceTest.xml as base DB
312 @Ignore
313 //FIXME re-generate test XML
314 public void testDeleteDerivateHierarchy_StepByStep(){
315 String assertMessage = "Incorrect number of specimens after deletion.";
316 DeleteResult deleteResult = null;
317 SpecimenDeleteConfigurator config = new SpecimenDeleteConfigurator();
318 config.setDeleteChildren(false);
319
320 FieldUnit fieldUnit = (FieldUnit) occurrenceService.load(UUID.fromString("afbe6682-2bd6-4f9e-ae24-b7479e0e585f"));
321 DerivedUnit derivedUnit = (DerivedUnit) occurrenceService.load(UUID.fromString("3699806e-8d2b-4ae6-b96e-d065525b654a"));
322 DnaSample dnaSample = (DnaSample) occurrenceService.load(UUID.fromString("b62d34ae-5093-43dc-b82e-fe60997feda0"));
323 Sequence consensusSequence = sequenceService.load(UUID.fromString("adec5a43-1bcb-42dc-ba68-9387a200859a"));
324
325 //check initial state
326 assertEquals(assertMessage, 4, occurrenceService.count(SpecimenOrObservationBase.class));
327 assertEquals(assertMessage, 1, occurrenceService.count(FieldUnit.class));
328 assertEquals(assertMessage, 3, occurrenceService.count(DerivedUnit.class));
329 assertEquals(assertMessage, 1, occurrenceService.count(DnaSample.class));
330 assertEquals("number of sequences incorrect", 1, dnaSample.getSequences().size());
331
332 //delete sequence
333 deleteResult = occurrenceService.deleteDerivateHierarchy(consensusSequence, config);
334 assertTrue(deleteResult.toString(), DeleteResult.DeleteStatus.OK.equals(deleteResult.getStatus()));
335 assertEquals("number of sequences incorrect", 0, dnaSample.getSequences().size());
336
337
338 //delete dna sample
339 deleteResult = occurrenceService.deleteDerivateHierarchy(dnaSample, config);
340 assertTrue(deleteResult.toString(), DeleteResult.DeleteStatus.OK.equals(deleteResult.getStatus()));
341 assertEquals(assertMessage, 3, occurrenceService.count(SpecimenOrObservationBase.class));
342 assertEquals(assertMessage, 1, occurrenceService.count(FieldUnit.class));
343 assertEquals(assertMessage, 2, occurrenceService.count(DerivedUnit.class));
344 assertEquals(assertMessage, 0, occurrenceService.count(DnaSample.class));
345
346 //delete derived unit
347 deleteResult = occurrenceService.deleteDerivateHierarchy(derivedUnit, config);
348 //deleting type specimen should fail
349 assertFalse(deleteResult.toString(), DeleteResult.DeleteStatus.OK.equals(deleteResult.getStatus()));
350 config.setDeleteFromTypeDesignation(true);
351 deleteResult = occurrenceService.deleteDerivateHierarchy(derivedUnit, config);
352 //deleting type specimen should work
353 assertTrue(deleteResult.toString(), DeleteResult.DeleteStatus.OK.equals(deleteResult.getStatus()));
354 assertEquals(assertMessage, 2, occurrenceService.count(SpecimenOrObservationBase.class));
355 assertEquals(assertMessage, 1, occurrenceService.count(FieldUnit.class));
356 assertEquals(assertMessage, 1, occurrenceService.count(DerivedUnit.class));
357 assertEquals(assertMessage, 0, occurrenceService.count(DnaSample.class));
358
359 //delete field unit
360 deleteResult = occurrenceService.deleteDerivateHierarchy(fieldUnit, config);
361 //deleting specimen with IndividualsAssociation should fail
362 assertFalse(deleteResult.toString(), DeleteResult.DeleteStatus.OK.equals(deleteResult.getStatus()));
363 config.setDeleteFromIndividualsAssociation(true);
364 deleteResult = occurrenceService.deleteDerivateHierarchy(fieldUnit, config);
365 //deleting specimen with IndividualsAssociation should work
366 assertTrue(deleteResult.toString(), DeleteResult.DeleteStatus.OK.equals(deleteResult.getStatus()));
367 assertEquals(assertMessage, 0, occurrenceService.count(SpecimenOrObservationBase.class));
368 assertEquals(assertMessage, 0, occurrenceService.count(FieldUnit.class));
369 assertEquals(assertMessage, 0, occurrenceService.count(DerivedUnit.class));
370 assertEquals(assertMessage, 0, occurrenceService.count(DnaSample.class));
371 }
372
373 // @Test
374 // public void testDeleteDerivateHierarchy_Sequence(){
375 // String assertMessage = "Incorrect number of specimens after deletion.";
376 //
377 // FieldUnit fieldUnit = initDerivateHierarchy();
378 //
379 // //delete sequence
380 // Sequence consensusSequence = ((DnaSample)fieldUnit.getDerivationEvents().iterator().next().getDerivatives().iterator().next()
381 // .getDerivationEvents().iterator().next().getDerivatives().iterator().next()).getSequences().iterator().next();
382 // occurrenceService.deleteDerivateHierarchy(consensusSequence);
383 // commit();
384 // assertEquals(assertMessage, 3, occurrenceService.count(SpecimenOrObservationBase.class));
385 // assertEquals(assertMessage, 1, occurrenceService.count(FieldUnit.class));
386 // assertEquals(assertMessage, 2, occurrenceService.count(DerivedUnit.class));
387 // assertEquals(assertMessage, 1, occurrenceService.count(DnaSample.class));
388 // }
389
390 @Test
391 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
392 public void testListAssociatedAndTypedTaxa(){
393 FieldUnit associatedFieldUnit = (FieldUnit) occurrenceService.load(UUID.fromString("afbe6682-2bd6-4f9e-ae24-b7479e0e585f"));
394 DerivedUnit typeSpecimen = (DerivedUnit) occurrenceService.load(UUID.fromString("3699806e-8d2b-4ae6-b96e-d065525b654a"));
395 Taxon taxon = (Taxon) taxonService.load(UUID.fromString("d3668127-be97-47df-bb0b-f03bc1089bc5"));
396 TaxonDescription taxonDescription = (TaxonDescription) descriptionService.load(UUID.fromString("6ef35ede-4906-4f9c-893a-e4186f84dfc4"));
397 //check for FieldUnit (IndividualsAssociation)
398 java.util.Collection<IndividualsAssociation> individualsAssociations = occurrenceService.listIndividualsAssociations(associatedFieldUnit, null, null, null,null);
399 assertEquals("Number of individuals associations is incorrect", 1, individualsAssociations.size());
400 IndividualsAssociation individualsAssociation = individualsAssociations.iterator().next();
401 assertTrue("association has wrong type", individualsAssociation.getInDescription().isInstanceOf(TaxonDescription.class));
402 //FIXME loading from XML of specimen.derivedFrom an taxonDescription.taxon fails
403 // TaxonDescription retrievedTaxonDescription = HibernateProxyHelper.deproxy(individualsAssociation.getInDescription(), TaxonDescription.class);
404 // assertEquals(taxonDescription, retrievedTaxonDescription);
405 // assertEquals("Associated taxon is incorrect", taxon, retrievedTaxonDescription.getTaxon());
406
407
408 //check for DerivedUnit (Type Designation should exist)
409 java.util.Collection<SpecimenTypeDesignation> typeDesignations = occurrenceService.listTypeDesignations(typeSpecimen, null, null, null,null);
410 assertEquals("Number of type designations is incorrect", 1, typeDesignations.size());
411 SpecimenTypeDesignation specimenTypeDesignation = typeDesignations.iterator().next();
412 Set<TaxonNameBase> typifiedNames = specimenTypeDesignation.getTypifiedNames();
413 assertEquals("number of typified names is incorrect", 1, typifiedNames.size());
414 Set taxonBases = typifiedNames.iterator().next().getTaxonBases();
415 assertEquals("number of taxa incorrect", 1, taxonBases.size());
416 Object next = taxonBases.iterator().next();
417 assertTrue(next instanceof CdmBase && ((CdmBase)next).isInstanceOf(Taxon.class));
418 assertEquals("Typed taxon is incorrect", taxon, next);
419 }
420 /* (non-Javadoc)
421 * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
422 */
423 @Override
424 protected void createTestDataSet() throws FileNotFoundException {
425 //how the XML was generated
426 FieldUnit associatedFieldUnit = FieldUnit.NewInstance();
427 //sub derivates (DerivedUnit, DnaSample)
428 DerivedUnit typeSpecimen = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
429 DerivedUnit voucherSpecimen = DerivedUnit.NewInstance(SpecimenOrObservationType.HumanObservation);
430 DnaSample dnaSample = DnaSample.NewInstance();
431 //description for voucher specimen (with InidividualsAssociation to type specimen just to make it complex ;) )
432 SpecimenDescription voucherSpecimenDescription = SpecimenDescription.NewInstance(voucherSpecimen);
433 voucherSpecimenDescription.addElement(IndividualsAssociation.NewInstance(typeSpecimen));
434
435 //derivation events
436 DerivationEvent.NewSimpleInstance(associatedFieldUnit, typeSpecimen, DerivationEventType.ACCESSIONING());
437 DerivationEvent.NewSimpleInstance(associatedFieldUnit, voucherSpecimen, DerivationEventType.ACCESSIONING());
438 DerivationEvent.NewSimpleInstance(typeSpecimen, dnaSample, DerivationEventType.DNA_EXTRACTION());
439
440 //DNA (Sequence, SingleRead, Amplification)
441 Sequence consensusSequence = Sequence.NewInstance(dnaSample, "ATTCG", 5);
442 SingleRead singleRead = SingleRead.NewInstance();
443 consensusSequence.addSingleRead(singleRead);
444 dnaSample.addSequence(consensusSequence);
445 Amplification amplification = Amplification.NewInstance(dnaSample);
446 amplification.addSingleRead(singleRead);
447 occurrenceService.save(associatedFieldUnit);
448 occurrenceService.save(typeSpecimen);
449 occurrenceService.save(dnaSample);
450
451 //create name with type specimen
452 BotanicalName name = BotanicalName.PARSED_NAME("Campanula patual sec L.");
453 SpecimenTypeDesignation typeDesignation = SpecimenTypeDesignation.NewInstance();
454 typeDesignation.setTypeSpecimen(typeSpecimen);
455
456 // create taxon with name and two taxon descriptions (one with
457 // IndividualsAssociations and a "described" voucher specimen, and an
458 // empty one)
459 Taxon taxon = Taxon.NewInstance(name, null);
460 TaxonDescription taxonDescription = TaxonDescription.NewInstance();
461 //add voucher
462 taxonDescription.setDescribedSpecimenOrObservation(voucherSpecimen);
463 taxonDescription.addElement(IndividualsAssociation.NewInstance(associatedFieldUnit));
464 taxon.addDescription(taxonDescription);
465 //add type designation to name
466 name.addTypeDesignation(typeDesignation, false);
467 //add another taxon description to taxon which is not associated with a specimen thus should not be taken into account
468 taxon.addDescription(TaxonDescription.NewInstance());
469 taxonService.saveOrUpdate(taxon);
470
471
472 commitAndStartNewTransaction(new String[]{"SpecimenOrObservationBase",
473 "SpecimenOrObservationBase_DerivationEvent",
474 "DerivationEvent",
475 "Sequence",
476 "Sequence_SingleRead",
477 "SingleRead",
478 "Amplification",
479 "Amplification_SingleRead",
480 "DescriptionElementBase",
481 "DescriptionBase",
482 "TaxonBase",
483 "TypeDesignationBase",
484 "TaxonNameBase",
485 "TaxonNameBase_TypeDesignationBase",
486 "HomotypicalGroup"});
487 }
488
489 }