- extended test
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / api / service / OccurenceServiceTest.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.assertTrue;
14
15 import org.apache.log4j.Logger;
16 import org.junit.Test;
17 import org.unitils.spring.annotation.SpringBeanByType;
18
19 import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
20 import eu.etaxonomy.cdm.model.agent.Institution;
21 import eu.etaxonomy.cdm.model.agent.Person;
22 import eu.etaxonomy.cdm.model.common.DefinedTerm;
23 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
24 import eu.etaxonomy.cdm.model.common.Language;
25 import eu.etaxonomy.cdm.model.common.TimePeriod;
26 import eu.etaxonomy.cdm.model.location.Country;
27 import eu.etaxonomy.cdm.model.location.NamedArea;
28 import eu.etaxonomy.cdm.model.location.Point;
29 import eu.etaxonomy.cdm.model.location.ReferenceSystem;
30 import eu.etaxonomy.cdm.model.molecular.DnaSample;
31 import eu.etaxonomy.cdm.model.molecular.Sequence;
32 import eu.etaxonomy.cdm.model.name.BotanicalName;
33 import eu.etaxonomy.cdm.model.name.Rank;
34 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
35 import eu.etaxonomy.cdm.model.occurrence.Collection;
36 import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
37 import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
38 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
39 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
40 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
41 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
42 import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
43 import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
44 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
45 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
46 import eu.etaxonomy.cdm.model.reference.Reference;
47 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
48 import eu.etaxonomy.cdm.model.taxon.Taxon;
49 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
50
51 /**
52 * @author pplitzner
53 * @date 31.03.2014
54 *
55 */
56 public class OccurenceServiceTest extends CdmTransactionalIntegrationTest {
57 private static final Logger logger = Logger.getLogger(OccurenceServiceTest.class);
58
59 @SpringBeanByType
60 private IOccurrenceService occurrenceService;
61
62 @SpringBeanByType
63 private ISequenceService sequenceService;
64
65 @Test
66 public void testGetNonCascadedAssociatedElements(){
67 //Collection
68 Collection collection = Collection.NewInstance();
69 Collection subCollection = Collection.NewInstance();
70 subCollection.setSuperCollection(collection);
71
72 Institution institution = Institution.NewInstance();
73 institution.addType(DefinedTerm.NewInstitutionTypeInstance("Research and teaching", "botanical garden", "BGBM"));
74 collection.setInstitute(institution);
75
76 //Source
77 Reference<?> article = ReferenceFactory.newArticle(getReference(), Person.NewInstance(), "title", "pages", "series", "volume", TimePeriod.NewInstance(2014));
78 IdentifiableSource source = IdentifiableSource.NewPrimarySourceInstance(article, "microCitation");
79
80 //FieldUnit
81 FieldUnit fieldUnit = FieldUnit.NewInstance();
82 Person primaryCollector = Person.NewInstance();
83 primaryCollector.setLifespan(TimePeriod.NewInstance(2014));
84 fieldUnit.setPrimaryCollector(primaryCollector);
85 fieldUnit.addSource(source);
86
87 //GatheringEvent
88 GatheringEvent gatheringEvent = GatheringEvent.NewInstance();
89 fieldUnit.setGatheringEvent(gatheringEvent);
90 gatheringEvent.putLocality(Language.ENGLISH(), "locality");
91 gatheringEvent.setExactLocation(Point.NewInstance(22.4, -34.2,
92 ReferenceSystem.NewInstance("MyReferenceSystem", "label", "labelAbbrev"), 33));
93 gatheringEvent.setCountry(Country.GERMANY());
94 gatheringEvent.addCollectingArea(NamedArea.EUROPE());
95
96 //Derived Unit
97 MediaSpecimen mediaSpecimen = MediaSpecimen.NewInstance(SpecimenOrObservationType.StillImage);
98 mediaSpecimen.setCollection(collection);
99 BotanicalName storedUnder = BotanicalName.NewInstance(Rank.SPECIES());
100 mediaSpecimen.setStoredUnder(storedUnder);
101 PreservationMethod preservation = PreservationMethod.NewInstance(null, "My preservation");
102 preservation.setMedium(DefinedTerm.NewDnaMarkerInstance("medium", "medium", "medium"));//dummy defined term
103 mediaSpecimen.setPreservation(preservation);
104
105 //DerivationEvent
106 DerivationEvent event = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
107 event.addOriginal(fieldUnit);
108 event.addDerivative(mediaSpecimen);
109
110 //SpecOrObservationBase
111 fieldUnit.setSex(DefinedTerm.SEX_FEMALE());
112 fieldUnit.setLifeStage(DefinedTerm.NewStageInstance("Live stage", "stage", null));
113 fieldUnit.setKindOfUnit(DefinedTerm.NewKindOfUnitInstance("Kind of unit", "Kind of unit", null));
114 fieldUnit.putDefinition(Language.ENGLISH(), "definition");
115
116 //Determination
117 DeterminationEvent determinationEvent = DeterminationEvent.NewInstance(getTaxon(), mediaSpecimen);
118 determinationEvent.setModifier(DefinedTerm.NewModifierInstance("modifierDescription", "modifierLabel", "mofifierLabelAbbrev"));
119 determinationEvent.setPreferredFlag(true);
120 Reference<?> reference = getReference();
121 determinationEvent.addReference(reference);
122
123 /*NonCascaded
124 * SOOB
125 * - sex (FEMALE)
126 * - stage (Live stage)
127 * - kindOfUnit (Kind of unit)
128 * GatheringEvent
129 * - country (GERMANY)
130 * - collectingArea (EUROPE)
131 * DerivedUnit
132 * - storedUnder (botanical name)
133 * DerivedUnit-> Collection -> institiute
134 * - type (botanical garden)
135 *
136 * */
137
138 assertEquals("Incorrect number of non cascaded CDM entities", 9, occurrenceService.getNonCascadedAssociatedElements(fieldUnit).size());
139 assertEquals("Incorrect number of non cascaded CDM entities", 9, occurrenceService.getNonCascadedAssociatedElements(mediaSpecimen).size());
140
141 }
142 private Reference<?> getReference() {
143 Reference<?> result = ReferenceFactory.newGeneric();
144 result.setTitle("some generic reference");
145 return result;
146 }
147 private Taxon getTaxon() {
148 Reference<?> sec = getReference();
149 TaxonNameBase<?,?> name = BotanicalName.NewInstance(Rank.GENUS());
150 Taxon taxon = Taxon.NewInstance(name, sec);
151 return taxon;
152
153 }
154
155 @Test
156 public void testMoveDerivate(){
157 DerivedUnit specimenA = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
158 DerivedUnit specimenB = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
159 DerivedUnit dnaSample = DerivedUnit.NewInstance(SpecimenOrObservationType.DnaSample);
160
161 occurrenceService.saveOrUpdate(specimenA);
162 occurrenceService.saveOrUpdate(specimenB);
163 occurrenceService.saveOrUpdate(dnaSample);
164
165 DerivationEvent.NewSimpleInstance(specimenA, dnaSample, DerivationEventType.DNA_EXTRACTION());
166
167 occurrenceService.moveDerivate(specimenA, specimenB, dnaSample);
168 assertTrue("DerivationEvent not removed from source!", specimenA.getDerivationEvents().isEmpty());
169 assertEquals("DerivationEvent not moved to source!", 1, specimenB.getDerivationEvents().size());
170 DerivationEvent derivationEvent = specimenB.getDerivationEvents().iterator().next();
171 assertEquals("Moved DerivationEvent not of same type!", DerivationEventType.DNA_EXTRACTION(), derivationEvent.getType());
172 assertEquals("Wrong number of derivation originals!", 1, derivationEvent.getOriginals().size());
173 SpecimenOrObservationBase<?> newOriginal = derivationEvent.getOriginals().iterator().next();
174 assertEquals("Origin of moved object not correct", specimenB, newOriginal);
175 assertEquals("Wrong number of derivatives!", 1, derivationEvent.getDerivatives().size());
176 DerivedUnit derivedUnit = derivationEvent.getDerivatives().iterator().next();
177 assertEquals("Moved derivate has wrong type", SpecimenOrObservationType.DnaSample, derivedUnit.getRecordBasis());
178
179 }
180
181 @Test
182 public void testMoveSequence(){
183 DnaSample dnaSampleA = DnaSample.NewInstance();
184 DnaSample dnaSampleB = DnaSample.NewInstance();
185 String consensusSequence = "ATTCG";
186 Sequence sequence = Sequence.NewInstance(consensusSequence);
187
188 occurrenceService.saveOrUpdate(dnaSampleA);
189 occurrenceService.saveOrUpdate(dnaSampleB);
190 sequenceService.saveOrUpdate(sequence);
191
192 dnaSampleA.addSequence(sequence);
193
194 occurrenceService.moveSequence(dnaSampleA, dnaSampleB, sequence);
195 assertEquals("Number of sequences is wrong", 0, dnaSampleA.getSequences().size());
196 assertEquals("Number of sequences is wrong", 1, dnaSampleB.getSequences().size());
197 Sequence next = dnaSampleB.getSequences().iterator().next();
198 assertEquals("Sequences are not equals", sequence, next);
199 assertEquals("Sequences are not equals", consensusSequence, next.getSequenceString());
200 }
201 }