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.logging.log4j.LogManager;import org.apache.logging.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.OccurrenceStatus;
|
39
|
import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
|
40
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
41
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
42
|
import eu.etaxonomy.cdm.model.term.DefinedTerm;
|
43
|
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
|
44
|
import eu.etaxonomy.cdm.test.TermTestBase;
|
45
|
|
46
|
/**
|
47
|
* Note: this class is mostly a copy from the orignal class DerivedUnitFacadeCacheStrategyTest
|
48
|
* in cdmlib-service. (#9678)
|
49
|
*
|
50
|
* @author a.mueller
|
51
|
* @since 22.06.2021
|
52
|
*/
|
53
|
public class DerivedUnitDefaultCacheStrategyTest extends TermTestBase {
|
54
|
|
55
|
@SuppressWarnings("unused")
|
56
|
private static final Logger logger = LogManager.getLogger(DerivedUnitDefaultCacheStrategyTest.class);
|
57
|
|
58
|
private DerivedUnit specimen;
|
59
|
private DerivationEvent derivationEvent;
|
60
|
private FieldUnit fieldUnit;
|
61
|
private GatheringEvent gatheringEvent;
|
62
|
private Integer absoluteElevation = 40;
|
63
|
private Integer absoluteElevationError = 2;
|
64
|
private Team collector = Team.NewInstance();
|
65
|
private String collectingMethod = "Collection Method";
|
66
|
private Double distanceToGround = 22.0;
|
67
|
private Double distanceToSurface = 50.0;
|
68
|
private ReferenceSystem referenceSystem = ReferenceSystem.WGS84();
|
69
|
private Point exactLocation = Point.NewInstance(12.3, 10.567, referenceSystem, 22);
|
70
|
private String gatheringEventDescription = "A nice gathering description";
|
71
|
private TimePeriod gatheringPeriod = TimePeriodParser.parseString("03.05.2005");
|
72
|
private String ecology = "sand dunes";
|
73
|
private String plantDescription = "flowers blue";
|
74
|
|
75
|
private String fieldNumber = "5678";
|
76
|
private String fieldNotes = "such a beautiful specimen";
|
77
|
private Person primaryCollector;
|
78
|
|
79
|
private String individualCount = "1";
|
80
|
private DefinedTerm lifeStage = DefinedTerm.NewStageInstance("A wonderful stage", "stage", "st");
|
81
|
private DefinedTerm sex = DefinedTerm.NewSexInstance("FemaleMale", "FM", "FM");
|
82
|
private LanguageString locality = LanguageString.NewInstance("Berlin-Dahlem, E side of Englerallee", Language.DEFAULT());
|
83
|
private NamedArea country = Country.GERMANY();
|
84
|
|
85
|
private String exsiccatum = "Greuter, Pl. Dahlem. 456";
|
86
|
private String accessionNumber = "8909756";
|
87
|
private String catalogNumber = "UU879873590";
|
88
|
private String barcode = "B12345678";
|
89
|
private TaxonName taxonName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS(), "Abies", null, null, null, null, null, null, null);
|
90
|
private String collectorsNumber = "234589913A34";
|
91
|
private Collection collection = Collection.NewInstance();
|
92
|
|
93
|
private PreservationMethod preservationMethod = PreservationMethod.NewInstance(null, "my prservation");
|
94
|
|
95
|
private DerivedUnit collectionSpecimen;
|
96
|
private GatheringEvent existingGatheringEvent;
|
97
|
private DerivationEvent firstDerivationEvent;
|
98
|
private FieldUnit firstFieldObject;
|
99
|
private Media media1 = Media.NewInstance();
|
100
|
|
101
|
//****************************** SET UP *****************************************/
|
102
|
|
103
|
@Before
|
104
|
public void setUp() throws Exception {
|
105
|
specimen = DerivedUnit.NewPreservedSpecimenInstance();
|
106
|
|
107
|
derivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
|
108
|
specimen.setDerivedFrom(derivationEvent);
|
109
|
fieldUnit = FieldUnit.NewInstance();
|
110
|
fieldUnit.addDerivationEvent(derivationEvent);
|
111
|
gatheringEvent = GatheringEvent.NewInstance();
|
112
|
fieldUnit.setGatheringEvent(gatheringEvent);
|
113
|
gatheringEvent.setAbsoluteElevation(absoluteElevation);
|
114
|
// gatheringEvent.setAbsoluteElevationError(absoluteElevationError);
|
115
|
gatheringEvent.setActor(collector);
|
116
|
gatheringEvent.setCollectingMethod(collectingMethod);
|
117
|
gatheringEvent.setDistanceToGround(distanceToGround);
|
118
|
gatheringEvent.setDistanceToWaterSurface(distanceToSurface);
|
119
|
gatheringEvent.setExactLocation(exactLocation);
|
120
|
gatheringEvent.setDescription(gatheringEventDescription);
|
121
|
|
122
|
gatheringEvent.setTimeperiod(gatheringPeriod);
|
123
|
gatheringEvent.setLocality(locality);
|
124
|
gatheringEvent.setCountry(country);
|
125
|
|
126
|
fieldUnit.setFieldNumber(fieldNumber);
|
127
|
fieldUnit.setFieldNotes(fieldNotes);
|
128
|
fieldUnit.setIndividualCount(individualCount);
|
129
|
fieldUnit.setSex(sex);
|
130
|
fieldUnit.setLifeStage(lifeStage);
|
131
|
primaryCollector = Person.NewTitledInstance("Kilian");
|
132
|
collector.addTeamMember(primaryCollector);
|
133
|
Person secondCollector = Person.NewInstance();
|
134
|
secondCollector.setGivenName("Andreas");
|
135
|
secondCollector.setFamilyName("Muller");
|
136
|
collector.addTeamMember(secondCollector);
|
137
|
Person thirdCollector = Person.NewTitledInstance("Kohlbecker");
|
138
|
collector.addTeamMember(thirdCollector);
|
139
|
fieldUnit.setPrimaryCollector(primaryCollector);
|
140
|
|
141
|
specimen.setAccessionNumber(accessionNumber);
|
142
|
specimen.setCatalogNumber(catalogNumber);
|
143
|
specimen.setBarcode(barcode);
|
144
|
specimen.setStoredUnder(taxonName);
|
145
|
specimen.setCollection(collection);
|
146
|
specimen.setPreservation(preservationMethod);
|
147
|
specimen.setExsiccatum(exsiccatum);
|
148
|
|
149
|
//existing specimen with 2 derivation events in line
|
150
|
collectionSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
|
151
|
DerivedUnit middleSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
|
152
|
firstFieldObject = FieldUnit.NewInstance();
|
153
|
|
154
|
//TODO maybe we should define concrete event types here
|
155
|
DerivationEvent lastDerivationEvent = DerivationEvent.NewInstance(null);
|
156
|
DerivationEvent middleDerivationEvent = DerivationEvent.NewInstance(null);
|
157
|
firstDerivationEvent = DerivationEvent.NewInstance(null);
|
158
|
|
159
|
collectionSpecimen.setDerivedFrom(lastDerivationEvent);
|
160
|
|
161
|
lastDerivationEvent.addOriginal(middleSpecimen);
|
162
|
middleSpecimen.setDerivedFrom(firstDerivationEvent);
|
163
|
firstDerivationEvent.addOriginal(firstFieldObject);
|
164
|
existingGatheringEvent = GatheringEvent.NewInstance();
|
165
|
firstFieldObject.setGatheringEvent(existingGatheringEvent);
|
166
|
}
|
167
|
|
168
|
@Test
|
169
|
public void testGetTitleCache() {
|
170
|
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";
|
171
|
addEcology(fieldUnit, ecology);
|
172
|
addPlantDescription(fieldUnit, plantDescription);
|
173
|
collection.setCode("B");
|
174
|
Assert.assertEquals(correctCache, specimen.getTitleCache());
|
175
|
|
176
|
//collection without code but with name
|
177
|
collection.setCode(null);
|
178
|
collection.setName("Herbarium Berolinense");
|
179
|
Assert.assertEquals(correctCache.replace("B: 8909756", "Herbarium Berolinense: 8909756"), specimen.getTitleCache());
|
180
|
|
181
|
//test status
|
182
|
collection.setCode("B");
|
183
|
Reference statusSource = ReferenceFactory.newBook(); //TODO not yet handled in cache strategy as we do not have tagged text here
|
184
|
statusSource.setTitle("Status test");
|
185
|
specimen.addStatus(OccurrenceStatus.NewInstance(DefinedTerm.getTermByUuid(DefinedTerm.uuidDestroyed), statusSource, "335"));
|
186
|
Assert.assertEquals(correctCache.replace("8909756", "8909756, destroyed"), specimen.getTitleCache());
|
187
|
|
188
|
//test 2 status
|
189
|
specimen.addStatus(OccurrenceStatus.NewInstance(DefinedTerm.getTermByUuid(DefinedTerm.uuidLost), statusSource, "335"));
|
190
|
Assert.assertEquals(correctCache.replace("8909756", "8909756, destroyed, lost"), specimen.getTitleCache());
|
191
|
|
192
|
}
|
193
|
|
194
|
@Test
|
195
|
public void testGetTitleCacheWithEtAl() {
|
196
|
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";
|
197
|
collector.setHasMoreMembers(true);
|
198
|
addEcology(fieldUnit, ecology);
|
199
|
addPlantDescription(fieldUnit, plantDescription);
|
200
|
collection.setCode("B");
|
201
|
Assert.assertEquals(correctCache, specimen.getTitleCache());
|
202
|
}
|
203
|
|
204
|
//#6381
|
205
|
@Test
|
206
|
public void testGetTitleCacheAccessionBarcodeCatalogNumber() {
|
207
|
addPlantDescription(fieldUnit, plantDescription);
|
208
|
collection.setCode("B");
|
209
|
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";
|
210
|
Assert.assertEquals(correctCache, specimen.getTitleCache());
|
211
|
|
212
|
specimen.setAccessionNumber(null);
|
213
|
correctCache = correctCache.replace("B: 8909756", "B: B12345678");
|
214
|
Assert.assertEquals(correctCache, specimen.getTitleCache());
|
215
|
|
216
|
specimen.setBarcode(null);
|
217
|
correctCache = correctCache.replace("B: B12345678", "B: UU879873590");
|
218
|
Assert.assertEquals(correctCache, specimen.getTitleCache());
|
219
|
|
220
|
//no deduplication of collection code in accession number according to #6865
|
221
|
specimen.setAccessionNumber("B 12345");
|
222
|
correctCache = correctCache.replace("B: UU879873590", "B: B 12345");
|
223
|
Assert.assertEquals(correctCache, specimen.getTitleCache());
|
224
|
specimen.setAccessionNumber("B12345");
|
225
|
correctCache = correctCache.replace("B 12345", "B12345");
|
226
|
Assert.assertEquals(correctCache, specimen.getTitleCache());
|
227
|
|
228
|
//but deduplication should take place if explicitly set
|
229
|
specimen.setAccessionNumber("B 12345");
|
230
|
specimen.setCacheStrategy(DerivedUnitDefaultCacheStrategy.NewInstance(false, false, true));
|
231
|
correctCache = correctCache.replace("B: B12345", "B: 12345");
|
232
|
Assert.assertEquals(correctCache, specimen.getTitleCache());
|
233
|
|
234
|
//with trailing full stop #9849
|
235
|
specimen.setCacheStrategy(DerivedUnitDefaultCacheStrategy.NewInstance(false, true, true));
|
236
|
correctCache = correctCache + ".";
|
237
|
Assert.assertEquals(correctCache, specimen.getTitleCache());
|
238
|
|
239
|
//skip field unit
|
240
|
specimen.setCacheStrategy(DerivedUnitDefaultCacheStrategy.NewInstance(true, false, false));
|
241
|
correctCache = "Greuter, Pl. Dahlem. 456 (B: B 12345)";
|
242
|
Assert.assertEquals(correctCache, specimen.getTitleCache());
|
243
|
|
244
|
}
|
245
|
|
246
|
@Test
|
247
|
public void testDerivedUnitWithEmptyFieldUnit() {
|
248
|
specimen = DerivedUnit.NewPreservedSpecimenInstance();
|
249
|
derivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
|
250
|
specimen.setDerivedFrom(derivationEvent);
|
251
|
fieldUnit = FieldUnit.NewInstance();
|
252
|
fieldUnit.addDerivationEvent(derivationEvent);
|
253
|
gatheringEvent = GatheringEvent.NewInstance();
|
254
|
fieldUnit.setGatheringEvent(gatheringEvent);
|
255
|
Assert.assertEquals("DerivedUnit#0<"+specimen.getUuid()+">", specimen.getTitleCache());
|
256
|
|
257
|
specimen.setBarcode("B996633");
|
258
|
Assert.assertEquals("(B996633)", specimen.getTitleCache()); //maybe brackets will be removed in future
|
259
|
|
260
|
}
|
261
|
|
262
|
private void addEcology(FieldUnit fieldUnit, String ecology) {
|
263
|
SpecimenDescription description = SpecimenDescription.NewInstance(fieldUnit);
|
264
|
TextData textData = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
|
265
|
description.addElement(textData);
|
266
|
}
|
267
|
|
268
|
private void addPlantDescription(FieldUnit fieldUnit, String plantDescription) {
|
269
|
SpecimenDescription description = SpecimenDescription.NewInstance(fieldUnit);
|
270
|
TextData textData = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
|
271
|
description.addElement(textData);
|
272
|
}
|
273
|
}
|