Project

General

Profile

Download (11.2 KB) Statistics
| Branch: | Tag: | Revision:
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.strategy.cache.occurrence;
10

    
11
import org.apache.log4j.Logger;
12
import org.junit.Assert;
13
import org.junit.Before;
14
import org.junit.Test;
15

    
16
import eu.etaxonomy.cdm.model.agent.Person;
17
import eu.etaxonomy.cdm.model.agent.Team;
18
import eu.etaxonomy.cdm.model.common.Language;
19
import eu.etaxonomy.cdm.model.common.LanguageString;
20
import eu.etaxonomy.cdm.model.common.TimePeriod;
21
import eu.etaxonomy.cdm.model.description.Feature;
22
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
23
import eu.etaxonomy.cdm.model.description.TextData;
24
import eu.etaxonomy.cdm.model.location.Country;
25
import eu.etaxonomy.cdm.model.location.NamedArea;
26
import eu.etaxonomy.cdm.model.location.Point;
27
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
28
import eu.etaxonomy.cdm.model.media.Media;
29
import eu.etaxonomy.cdm.model.name.Rank;
30
import eu.etaxonomy.cdm.model.name.TaxonName;
31
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
32
import eu.etaxonomy.cdm.model.occurrence.Collection;
33
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
34
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
35
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
36
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
37
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
38
import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
39
import eu.etaxonomy.cdm.model.term.DefinedTerm;
40
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
41
import eu.etaxonomy.cdm.test.TermTestBase;
42

    
43
/**
44
 * Note: this class is mostly a copy from the orignal class DerivedUnitFacadeCacheStrategyTest
45
 *       in cdmlib-service. (#9678)
46
 *
47
 * @author a.mueller
48
 * @since 22.06.2021
49
 */
50
public class DerivedUnitDefaultCacheStrategyTest extends TermTestBase {
51

    
52
    @SuppressWarnings("unused")
53
	private static final Logger logger = Logger.getLogger(DerivedUnitDefaultCacheStrategyTest.class);
54

    
55
    private DerivedUnit specimen;
56
    private DerivationEvent derivationEvent;
57
    private FieldUnit fieldUnit;
58
    private GatheringEvent gatheringEvent;
59
    private Integer absoluteElevation = 40;
60
    private Integer absoluteElevationError = 2;
61
    private Team collector = Team.NewInstance();
62
    private String collectingMethod = "Collection Method";
63
    private Double distanceToGround = 22.0;
64
    private Double distanceToSurface = 50.0;
65
    private ReferenceSystem referenceSystem = ReferenceSystem.WGS84();
66
    private Point exactLocation = Point.NewInstance(12.3, 10.567, referenceSystem, 22);
67
    private String gatheringEventDescription = "A nice gathering description";
68
    private TimePeriod gatheringPeriod = TimePeriodParser.parseString("03.05.2005");
69
    private String ecology = "sand dunes";
70
    private String plantDescription = "flowers blue";
71

    
72
    private String fieldNumber = "5678";
73
    private String fieldNotes = "such a beautiful specimen";
74
    private Person primaryCollector;
75

    
76
    private String individualCount = "1";
77
    private DefinedTerm lifeStage = DefinedTerm.NewStageInstance("A wonderful stage", "stage", "st");
78
    private DefinedTerm sex = DefinedTerm.NewSexInstance("FemaleMale", "FM", "FM");
79
    private LanguageString locality = LanguageString.NewInstance("Berlin-Dahlem, E side of Englerallee", Language.DEFAULT());
80
    private NamedArea country = Country.GERMANY();
81

    
82
    private String exsiccatum = "Greuter, Pl. Dahlem. 456";
83
    private String accessionNumber = "8909756";
84
    private String catalogNumber = "UU879873590";
85
    private String barcode = "B12345678";
86
    private TaxonName taxonName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS(), "Abies", null, null, null, null, null, null, null);
87
    private String collectorsNumber = "234589913A34";
88
    private Collection collection = Collection.NewInstance();
89

    
90
    private PreservationMethod preservationMethod = PreservationMethod.NewInstance(null, "my prservation");
91

    
92
    private DerivedUnit collectionSpecimen;
93
    private GatheringEvent existingGatheringEvent;
94
    private DerivationEvent firstDerivationEvent;
95
    private FieldUnit firstFieldObject;
96
    private Media media1 = Media.NewInstance();
97

    
98
//****************************** SET UP *****************************************/
99

    
100
    @Before
101
    public void setUp() throws Exception {
102
        specimen = DerivedUnit.NewPreservedSpecimenInstance();
103

    
104
        derivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
105
        specimen.setDerivedFrom(derivationEvent);
106
        fieldUnit = FieldUnit.NewInstance();
107
        fieldUnit.addDerivationEvent(derivationEvent);
108
        gatheringEvent = GatheringEvent.NewInstance();
109
        fieldUnit.setGatheringEvent(gatheringEvent);
110
        gatheringEvent.setAbsoluteElevation(absoluteElevation);
111
//      gatheringEvent.setAbsoluteElevationError(absoluteElevationError);
112
        gatheringEvent.setActor(collector);
113
        gatheringEvent.setCollectingMethod(collectingMethod);
114
        gatheringEvent.setDistanceToGround(distanceToGround);
115
        gatheringEvent.setDistanceToWaterSurface(distanceToSurface);
116
        gatheringEvent.setExactLocation(exactLocation);
117
        gatheringEvent.setDescription(gatheringEventDescription);
118

    
119
        gatheringEvent.setTimeperiod(gatheringPeriod);
120
        gatheringEvent.setLocality(locality);
121
        gatheringEvent.setCountry(country);
122

    
123
        fieldUnit.setFieldNumber(fieldNumber);
124
        fieldUnit.setFieldNotes(fieldNotes);
125
        fieldUnit.setIndividualCount(individualCount);
126
        fieldUnit.setSex(sex);
127
        fieldUnit.setLifeStage(lifeStage);
128
        primaryCollector = Person.NewTitledInstance("Kilian");
129
        collector.addTeamMember(primaryCollector);
130
        Person secondCollector = Person.NewInstance();
131
        secondCollector.setGivenName("Andreas");
132
        secondCollector.setFamilyName("Muller");
133
        collector.addTeamMember(secondCollector);
134
        Person thirdCollector = Person.NewTitledInstance("Kohlbecker");
135
        collector.addTeamMember(thirdCollector);
136
        fieldUnit.setPrimaryCollector(primaryCollector);
137

    
138
        specimen.setAccessionNumber(accessionNumber);
139
        specimen.setCatalogNumber(catalogNumber);
140
        specimen.setBarcode(barcode);
141
        specimen.setStoredUnder(taxonName);
142
        specimen.setCollection(collection);
143
        specimen.setPreservation(preservationMethod);
144
        specimen.setExsiccatum(exsiccatum);
145

    
146
        //existing specimen with 2 derivation events in line
147
        collectionSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
148
        DerivedUnit middleSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
149
        firstFieldObject = FieldUnit.NewInstance();
150

    
151
        //TODO maybe we should define concrete event types here
152
        DerivationEvent lastDerivationEvent = DerivationEvent.NewInstance(null);
153
        DerivationEvent middleDerivationEvent = DerivationEvent.NewInstance(null);
154
        firstDerivationEvent = DerivationEvent.NewInstance(null);
155

    
156
        collectionSpecimen.setDerivedFrom(lastDerivationEvent);
157

    
158
        lastDerivationEvent.addOriginal(middleSpecimen);
159
        middleSpecimen.setDerivedFrom(firstDerivationEvent);
160
        firstDerivationEvent.addOriginal(firstFieldObject);
161
        existingGatheringEvent = GatheringEvent.NewInstance();
162
        firstFieldObject.setGatheringEvent(existingGatheringEvent);
163
    }
164

    
165
    @Test
166
    public void testGetTitleCache() {
167
        String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), sand dunes, 3 May 2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (B 8909756); flowers blue.";
168
        addEcology(fieldUnit, ecology);
169
        addPlantDescription(fieldUnit, plantDescription);
170
        collection.setCode("B");
171
        Assert.assertEquals(correctCache, specimen.getTitleCache());
172
        collection.setCode(null);
173
        collection.setName("Herbarium Berolinense");
174
        Assert.assertEquals(correctCache.replace("B 8909756", "Herbarium Berolinense 8909756"), specimen.getTitleCache());
175
    }
176

    
177
    @Test
178
    public void testGetTitleCacheWithEtAl() {
179
        String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), sand dunes, 3 May 2005, Kilian 5678, A. Muller, Kohlbecker & al.; Greuter, Pl. Dahlem. 456 (B 8909756); flowers blue.";
180
        collector.setHasMoreMembers(true);
181
        addEcology(fieldUnit, ecology);
182
        addPlantDescription(fieldUnit, plantDescription);
183
        collection.setCode("B");
184
        Assert.assertEquals(correctCache, specimen.getTitleCache());
185
    }
186

    
187
    //#6381
188
    @Test
189
    public void testGetTitleCacheAccessionBarcodeCatalogNumber() {
190
        //Note: Collection Code B might be deduplicated in future
191
        addPlantDescription(fieldUnit, plantDescription);
192
        collection.setCode("B");
193
        String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), 3 May 2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (B 8909756); flowers blue.";
194
        Assert.assertEquals(correctCache, specimen.getTitleCache());
195
        specimen.setAccessionNumber(null);
196
        correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), 3 May 2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (B B12345678); flowers blue.";
197
        Assert.assertEquals(correctCache, specimen.getTitleCache());
198
        specimen.setBarcode(null);
199
        correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), 3 May 2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (B UU879873590); flowers blue.";
200
        Assert.assertEquals(correctCache, specimen.getTitleCache());
201
    }
202

    
203
    @Test
204
    public void testDerivedUnitWithEmptyFieldUnit() {
205
        specimen = DerivedUnit.NewPreservedSpecimenInstance();
206
        derivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
207
        specimen.setDerivedFrom(derivationEvent);
208
        fieldUnit = FieldUnit.NewInstance();
209
        fieldUnit.addDerivationEvent(derivationEvent);
210
        gatheringEvent = GatheringEvent.NewInstance();
211
        fieldUnit.setGatheringEvent(gatheringEvent);
212
        Assert.assertEquals("DerivedUnit#0<"+specimen.getUuid()+">", specimen.getTitleCache());
213

    
214
        specimen.setBarcode("B996633");
215
        Assert.assertEquals("(B996633).", specimen.getTitleCache());  //maybe brackets and/or the full stop will be removed in future
216

    
217
    }
218

    
219
    private void addEcology(FieldUnit fieldUnit, String ecology) {
220
        SpecimenDescription description = SpecimenDescription.NewInstance(fieldUnit);
221
        TextData textData = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
222
        description.addElement(textData);
223
    }
224

    
225
    private void addPlantDescription(FieldUnit fieldUnit, String plantDescription) {
226
        SpecimenDescription description = SpecimenDescription.NewInstance(fieldUnit);
227
        TextData textData = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
228
        description.addElement(textData);
229
    }
230
}
(1-1/4)