Project

General

Profile

Download (24.1 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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.model.occurrence;
10

    
11
import static org.junit.Assert.assertEquals;
12
import static org.junit.Assert.assertFalse;
13
import static org.junit.Assert.assertNotNull;
14
import static org.junit.Assert.assertNotSame;
15
import static org.junit.Assert.assertNull;
16
import static org.junit.Assert.assertSame;
17
import static org.junit.Assert.assertTrue;
18

    
19
import java.util.Comparator;
20
import java.util.List;
21
import java.util.UUID;
22
import java.util.stream.Collectors;
23

    
24
import org.apache.logging.log4j.LogManager;
25
import org.apache.logging.log4j.Logger;
26
import org.joda.time.DateTime;
27
import org.junit.Assert;
28
import org.junit.Before;
29
import org.junit.Test;
30
import org.springframework.beans.BeanUtils;
31

    
32
import com.ibm.lsid.MalformedLSIDException;
33

    
34
import eu.etaxonomy.cdm.model.agent.Person;
35
import eu.etaxonomy.cdm.model.common.Annotation;
36
import eu.etaxonomy.cdm.model.common.Extension;
37
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
38
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
39
import eu.etaxonomy.cdm.model.common.LSID;
40
import eu.etaxonomy.cdm.model.common.Language;
41
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
42
import eu.etaxonomy.cdm.model.common.Marker;
43
import eu.etaxonomy.cdm.model.common.MarkerType;
44
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
45
import eu.etaxonomy.cdm.model.media.Rights;
46
import eu.etaxonomy.cdm.model.molecular.DnaSample;
47
import eu.etaxonomy.cdm.model.name.Rank;
48
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
49
import eu.etaxonomy.cdm.model.name.TaxonName;
50
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
51
import eu.etaxonomy.cdm.model.permission.User;
52
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
53
import eu.etaxonomy.cdm.model.term.DefinedTerm;
54
import eu.etaxonomy.cdm.test.unit.EntityTestBase;
55

    
56
/**
57
 * @author a.mueller
58
 * @since 28.10.2008
59
 */
60
public class SpecimenTest extends EntityTestBase {
61

    
62
    private static final Logger logger = LogManager.getLogger();
63

    
64
    /**
65
     * @author a.kohlbecker
66
     * @since Jan 18, 2021
67
     */
68
    private final class ByTtitleCacheComparator implements Comparator<IdentifiableEntity> {
69
        @Override
70
        public int compare(IdentifiableEntity o1, IdentifiableEntity o2) {
71
            return o1.getTitleCache().compareTo(o2.getTitleCache());
72
        }
73
    }
74

    
75
	private DerivedUnit specimen;
76

    
77
	@Before
78
	public void setUp() throws Exception {
79
		specimen = DerivedUnit.NewPreservedSpecimenInstance();
80
	}
81

    
82
	@Test
83
	public void testNewInstance() {
84
		DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
85
		assertNotNull(specimen);
86
		assertTrue(specimen.getRecordBasis().equals(SpecimenOrObservationType.PreservedSpecimen));
87
	}
88

    
89
	/**
90
	 * Test method for {@link eu.etaxonomy.cdm.model.occurrence.Specimen#getPreservation()} and
91
	 * {@link eu.etaxonomy.cdm.model.occurrence.Specimen#setPreservation(eu.etaxonomy.cdm.model.occurrence.PreservationMethod)}.
92
	 */
93
	@Test
94
	public void testGetSetPreservation() {
95
		PreservationMethod preservation = PreservationMethod.NewInstance();
96
		specimen.setPreservation(preservation);
97
		assertSame(preservation, specimen.getPreservation());
98
		specimen.setPreservation(null);
99
	}
100

    
101
	@Test
102
	public void testBidirectionalSpecimenDescription(){
103
		Assert.assertNotNull("Specimen should exist", specimen);
104

    
105
		SpecimenDescription desc = SpecimenDescription.NewInstance(specimen);
106
		Assert.assertNotNull("Description should exist.", desc);
107
		Assert.assertSame("Descriptions specimen should be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
108
		Assert.assertTrue("Specimen should contain description", specimen.getDescriptions().contains(desc));
109

    
110
		SpecimenDescription desc2 = SpecimenDescription.NewInstance();
111
		Assert.assertNotNull("Description should exist.", desc2);
112
		specimen.addDescription(desc2);
113
		Assert.assertSame("Description2 specimen should be set correctly", desc2.getDescribedSpecimenOrObservation(),specimen);
114
		Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
115
		Assert.assertTrue("Specimen should contain description2", specimen.getDescriptions().contains(desc2));
116
		Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
117

    
118
		SpecimenDescription desc3 = SpecimenDescription.NewInstance();
119
		Assert.assertNotNull("Description should exist.", desc3);
120
		desc3.setDescribedSpecimenOrObservation(specimen);
121
		Assert.assertSame("Description3 specimen should be set correctly", desc3.getDescribedSpecimenOrObservation(),specimen);
122
		Assert.assertSame("Descriptions2 specimen should still be set correctly", desc2.getDescribedSpecimenOrObservation(),specimen);
123
		Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
124
		Assert.assertTrue("Specimen should contain description3", specimen.getDescriptions().contains(desc3));
125
		Assert.assertTrue("Specimen should still contain description2", specimen.getDescriptions().contains(desc2));
126
		Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
127

    
128
		//change specimen of a given description
129
		DerivedUnit specimen2 = DerivedUnit.NewPreservedSpecimenInstance();
130
		Assert.assertNotNull("Specimen should exist.", specimen2);
131
		desc3.setDescribedSpecimenOrObservation(specimen2);
132
		Assert.assertSame("Description3 new specimen should be set correctly", desc3.getDescribedSpecimenOrObservation(),specimen2);
133
		Assert.assertSame("Descriptions2 specimen should still be set correctly", desc2.getDescribedSpecimenOrObservation(),specimen);
134
		Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
135
		Assert.assertTrue("Specimen2 should contain description3", specimen2.getDescriptions().contains(desc3));
136
		Assert.assertEquals("Specimen2 should contain exactly 1 description", 1, specimen2.getDescriptions().size());
137
		Assert.assertFalse("Specimen should no longer contain description3", specimen.getDescriptions().contains(desc3));
138
		Assert.assertTrue("Specimen should still contain description2", specimen.getDescriptions().contains(desc2));
139
		Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
140

    
141
		//remove description which is not contained
142
		specimen.removeDescription(desc3);
143
		Assert.assertSame("Nothing should have changed", desc3.getDescribedSpecimenOrObservation(),specimen2);
144
		Assert.assertSame("Nothing should have changed", desc2.getDescribedSpecimenOrObservation(),specimen);
145
		Assert.assertSame("Nothing should have changed", desc.getDescribedSpecimenOrObservation(),specimen);
146
		Assert.assertTrue("Nothing should have changed", specimen2.getDescriptions().contains(desc3));
147
		Assert.assertEquals("Nothing should have changed", 1, specimen2.getDescriptions().size());
148
		Assert.assertFalse("Nothing should have changed", specimen.getDescriptions().contains(desc3));
149
		Assert.assertTrue("Nothing should have changed", specimen.getDescriptions().contains(desc2));
150
		Assert.assertTrue("Nothing should have changed", specimen.getDescriptions().contains(desc));
151

    
152
		//remove description
153
		specimen.removeDescription(desc2);
154
		Assert.assertNull("Descriptions2 specimen should not exist anymore", desc2.getDescribedSpecimenOrObservation());
155
		Assert.assertSame("Description3 specimen should still be set correctly", desc3.getDescribedSpecimenOrObservation(),specimen2);
156
		Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
157
		Assert.assertTrue("Specimen2 should still contain description3", specimen2.getDescriptions().contains(desc3));
158
		Assert.assertEquals("Specimen2 should still contain exactly 1 description", 1, specimen2.getDescriptions().size());
159
		Assert.assertFalse("Specimen should not contain description2 anymore", specimen.getDescriptions().contains(desc2));
160
		Assert.assertFalse("Specimen should still no longer contain description3", specimen.getDescriptions().contains(desc3));
161
		Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
162

    
163
		//remove description by setting null specimen
164
		desc3.setDescribedSpecimenOrObservation(null);
165
		Assert.assertNull("Description3 specimen should not exist anymore", desc3.getDescribedSpecimenOrObservation());
166
		Assert.assertNull("Descriptions2 specimen should still not exist anymore", desc2.getDescribedSpecimenOrObservation());
167
		Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
168
		Assert.assertFalse("Specimen2 should not contain description3 anymore", specimen2.getDescriptions().contains(desc3));
169
		Assert.assertEquals("Specimen2 should contain no description now", 0, specimen2.getDescriptions().size());
170
		Assert.assertFalse("Specimen should still no longer contain description2", specimen.getDescriptions().contains(desc2));
171
		Assert.assertFalse("Specimen should still no longer contain description3", specimen.getDescriptions().contains(desc3));
172
		Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
173
	}
174

    
175
	@Test
176
	public void testBidirectionalTypeDesignation(){
177
		SpecimenTypeDesignation desig1 = SpecimenTypeDesignation.NewInstance();
178
		SpecimenTypeDesignation desig2 = SpecimenTypeDesignation.NewInstance();
179
		DerivedUnit specimen2 = DerivedUnit.NewPreservedSpecimenInstance();
180

    
181
		specimen.addSpecimenTypeDesignation(desig1);
182
		Assert.assertEquals("Specimen1 should be the designations specimen", specimen, desig1.getTypeSpecimen());
183
		Assert.assertEquals("specimen1 should have exactly 1 designation", 1, specimen.getSpecimenTypeDesignations().size());
184
		Assert.assertEquals("specimen1's designation should be desig1", desig1, specimen.getSpecimenTypeDesignations().iterator().next());
185

    
186
		specimen.addSpecimenTypeDesignation(desig2);
187
		Assert.assertEquals("Specimen1 should be the desig2's specimen", specimen, desig2.getTypeSpecimen());
188
		Assert.assertEquals("specimen1 should have exactly 2 designation", 2, specimen.getSpecimenTypeDesignations().size());
189

    
190
		specimen2.addSpecimenTypeDesignation(desig2);
191
		Assert.assertEquals("Specimen2 should have replaced specimen1 as desig2's specimen", specimen2, desig2.getTypeSpecimen());
192
		Assert.assertEquals("Specimen2 should have exactly 1 designation", 1, specimen2.getSpecimenTypeDesignations().size());
193
		Assert.assertEquals("Specimen1's designation should be desig2", desig2, specimen2.getSpecimenTypeDesignations().iterator().next());
194
		Assert.assertEquals("specimen1 should have exactly 1 designation", 1, specimen.getSpecimenTypeDesignations().size());
195

    
196
		specimen2.removeSpecimenTypeDesignation(desig2);
197
		Assert.assertEquals("Desig2 should not have a specimen anymore", null, desig2.getTypeSpecimen());
198
		Assert.assertEquals("Specimen2 should have no designation", 0, specimen2.getSpecimenTypeDesignations().size());
199
		Assert.assertEquals("specimen1 should have exactly 1 designation", 1, specimen.getSpecimenTypeDesignations().size());
200

    
201
		specimen.addSpecimenTypeDesignation(desig2);
202
		Assert.assertEquals("Specimen1 should be the desig2's specimen", specimen, desig2.getTypeSpecimen());
203
		Assert.assertEquals("specimen1 should have exactly 2 designation", 2, specimen.getSpecimenTypeDesignations().size());
204

    
205
		desig1.setTypeSpecimen(null);
206
		Assert.assertEquals("Desig1 should not have a specimen anymore", null, desig1.getTypeSpecimen());
207
		Assert.assertEquals("Specimen1 should have 1 designation", 1, specimen.getSpecimenTypeDesignations().size());
208
		Assert.assertEquals("Specimen1's designation should be desig2", desig2, specimen.getSpecimenTypeDesignations().iterator().next());
209

    
210
		desig1.setTypeSpecimen(specimen);
211
		Assert.assertEquals("Desig1 should have specimen1 as specimen again", specimen, desig1.getTypeSpecimen());
212
		Assert.assertEquals("Specimen1 should have 2 designation", 2, specimen.getSpecimenTypeDesignations().size());
213
	}
214

    
215
	@Test
216
	public void testClone() {
217
		logger.debug("Start testClone");
218

    
219
		//Null test is not full implemented, but an error is thrown if null throws
220
		//null pointer exception somewhere
221
		@SuppressWarnings("unused")
222
        DerivedUnit specimenNullClone = specimen.clone();
223

    
224
		String accessionNumber = "accNumber";
225
		String catalogNumber = "catNumber";
226
		Collection collection = Collection.NewInstance();
227
		collection.setCode("code");
228
		DateTime created = new DateTime();
229
		Person createdByPerson = Person.NewTitledInstance("creator");
230
		User createdBy = User.NewInstance("username", "pwd");
231
		createdBy.setPerson(createdByPerson);
232
		DerivationEvent derivedFrom = DerivationEvent.NewInstance(null);
233
		int id = 22;
234
		String individualCount = "25";
235
		DefinedTerm lifeStage = DefinedTerm.NewStageInstance(null, null, null);
236
		LSID lsid = null;
237
		try {
238
			lsid = new LSID("urn:lsid:example.com:foo:1");
239
		} catch (MalformedLSIDException e) {
240
			e.printStackTrace();
241
			Assert.fail();
242
		}
243
//		DerivedUnit nextVersion = DerivedUnit.NewPreservedSpecimenInstance();
244
//		DerivedUnit previousVersion = DerivedUnit.NewPreservedSpecimenInstance();
245
		PreservationMethod preservation = PreservationMethod.NewInstance();
246
		boolean protectedTitleCache = true;
247
		DefinedTerm sex = DefinedTerm.SEX_FEMALE();
248
		TaxonName storedUnder = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
249
		String titleCache = "title";
250
		DateTime updated = DateTime.now();
251
		Person updatedByPerson = Person.NewTitledInstance("updatedPerson");
252
	    User updatedBy = User.NewInstance("updated", "pwd2");
253
	    updatedBy.setPerson(updatedByPerson);
254
	    UUID uuidSpecimen = UUID.randomUUID();
255

    
256
		Annotation annotation = Annotation.NewDefaultLanguageInstance("annotation");
257
		String definition = "definition";
258
		//TODO
259
		DerivationEvent derivationEvent = DerivationEvent.NewInstance(null);
260
		SpecimenDescription description = SpecimenDescription.NewInstance();
261
		DeterminationEvent determination = DeterminationEvent.NewInstance();
262
		Extension extension = Extension.NewInstance();
263
		extension.setValue("extension");
264
		Marker marker = Marker.NewInstance(MarkerType.COMPLETE(), false);
265
		Rights right = Rights.NewInstance("right", Language.DEFAULT());
266
//		Media media = Media.NewInstance();
267
		IdentifiableSource source = IdentifiableSource.NewDataImportInstance("12", "idNamespace");
268

    
269
		specimen.setAccessionNumber(accessionNumber);
270
		specimen.setCatalogNumber(catalogNumber);
271
		specimen.setCollection(collection);
272
		specimen.setCreated(created);
273
		specimen.setCreatedBy(createdBy);
274
		specimen.setDerivedFrom(derivedFrom);
275
		specimen.setId(id);
276
		specimen.setIndividualCount(individualCount);
277
		specimen.setLifeStage(lifeStage);
278
		specimen.setLsid(lsid);
279
		specimen.setPreservation(preservation);
280
		specimen.setProtectedTitleCache(protectedTitleCache);
281
		specimen.setSex(sex);
282
		specimen.setStoredUnder(storedUnder);
283
		specimen.setTitleCache(titleCache, protectedTitleCache);
284
		specimen.setUpdated(updated);
285
		specimen.setUpdatedBy(updatedBy);
286
		specimen.setUuid(uuidSpecimen);
287

    
288
		specimen.addAnnotation(annotation);
289
		specimen.putDefinition(Language.DEFAULT(), definition);
290
		specimen.addDerivationEvent(derivationEvent);
291
		specimen.addDescription(description);
292
		specimen.addDetermination(determination);
293
		specimen.addExtension(extension);
294
		specimen.addMarker(marker);
295
//		specimen.addMedia(media);    #3597
296
		specimen.addRights(right);
297
		specimen.addSource(source);
298
		DefinedTerm destroyed = DefinedTerm.getTermByUuid(DefinedTerm.uuidDestroyed);
299
		specimen.addStatus(OccurrenceStatus.NewInstance(destroyed));
300

    
301
		try {
302
			Thread.sleep(200);
303
		} catch (InterruptedException e) {
304
			//ignore
305
		}
306

    
307
		//clone
308
		DerivedUnit specimenClone = specimen.clone();
309

    
310
		//test
311
		assertFalse(id == specimenClone.getId());
312
		assertFalse(created.equals(specimenClone.getCreated()));
313
		assertFalse(createdBy.equals(specimenClone.getCreatedBy()));
314
		assertFalse(updated.equals(specimenClone.getUpdated()));
315
		assertFalse(updatedBy.equals(specimenClone.getUpdatedBy()));
316
		assertNull(specimenClone.getUpdatedBy());
317
		assertNull(specimenClone.getCreatedBy());
318
		assertFalse(uuidSpecimen.equals(specimenClone.getUuid()));
319

    
320
		assertEquals(accessionNumber, specimenClone.getAccessionNumber());
321
		assertEquals(catalogNumber, specimenClone.getCatalogNumber());
322
		assertEquals(collection, specimenClone.getCollection());
323
		assertEquals(derivedFrom, specimenClone.getDerivedFrom());
324
		assertEquals(lifeStage, specimenClone.getLifeStage());
325
		assertEquals(lsid, specimenClone.getLsid());
326
		assertEquals(preservation, specimenClone.getPreservation());
327
		assertEquals(protectedTitleCache, specimenClone.isProtectedTitleCache());
328
		assertEquals(storedUnder, specimenClone.getStoredUnder());
329
		assertEquals(sex, specimenClone.getSex());
330
		assertEquals(titleCache, specimenClone.getTitleCache());
331

    
332
		Annotation clonedAnnotation = specimenClone.getAnnotations().iterator().next();
333
		assertFalse(annotation.equals(clonedAnnotation));
334
		assertEquals(annotation.getText(), ((LanguageStringBase)specimenClone.getAnnotations().iterator().next()).getText() );
335
		assertNotSame(annotation, specimenClone.getAnnotations().iterator().next() );
336

    
337
		assertEquals(definition, specimenClone.getDefinition().get(Language.DEFAULT()).getText());
338
//TODO
339
//		assertNotSame(definition, specimenClone.getDefinition().getText(Language.DEFAULT()));
340

    
341
		assertEquals(derivationEvent, specimenClone.getDerivationEvents().iterator().next());
342
		assertSame(derivationEvent, specimenClone.getDerivationEvents().iterator().next());
343

    
344
		// FIXME check if descriptions and determinations were correctly cloned
345
//		assertEquals(description, specimenClone.getDescriptions().iterator().next());
346
//		// TODO ?
347
//		assertSame(description, specimenClone.getDescriptions().iterator().next());
348
//
349
//		assertEquals(determination, specimenClone.getDeterminations().iterator().next());
350
//		// TODO ?
351
//		assertSame(determination, specimenClone.getDeterminations().iterator().next());
352

    
353
		assertFalse(extension.equals(specimenClone.getExtensions().iterator().next()));
354
		assertEquals(extension.getValue(), specimenClone.getExtensions().iterator().next().getValue());
355
		assertNotSame(extension, specimenClone.getExtensions().iterator().next());
356
		assertEquals(1, specimen.getExtensions().size());
357

    
358
		assertFalse(marker.equals(specimenClone.getMarkers().iterator().next()));
359
		assertEquals(marker.getFlag(), specimenClone.getMarkers().iterator().next().getFlag());
360
		assertNotSame(marker, specimenClone.getMarkers().iterator().next());
361
		assertEquals(1, specimenClone.getMarkers().size());
362

    
363
//		assertEquals(media, specimenClone.getMedia().iterator().next());  #3597
364
//		assertEquals(right, specimenClone.getRights().iterator().next()); #5762
365
		assertTrue("Rights must contain 1 rights object", specimenClone.getRights().size() == 1);
366
  	    assertTrue("Rights must not be cloned", specimenClone.getRights().iterator().next().equals(right));
367

    
368
		assertFalse(source.equals(specimenClone.getSources().iterator().next()));
369
		assertEquals(source.getId(), ((OriginalSourceBase)specimenClone.getSources().iterator().next()).getId());
370
		assertNotSame(source, specimenClone.getSources().iterator().next());
371
		assertEquals(1, specimenClone.getSources().size());
372
		//test status
373
		assertEquals(1, specimenClone.getStatus().size());
374
		assertEquals("unit should be adapted to new unit", specimenClone, specimenClone.getStatus().iterator().next().getUnit());
375
	    assertEquals("old status should still link to old specimen", specimen, specimen.getStatus().iterator().next().getUnit());
376
	    assertSame("Type should be same", specimen.getStatus().iterator().next().getType(), specimenClone.getStatus().iterator().next().getType());
377
	    assertNotNull("... and not null", specimen.getStatus().iterator().next().getType());
378
	}
379

    
380
    @Test
381
    public void beanTests(){
382
        //#5307 Test that BeanUtils does not fail
383
        BeanUtils.getPropertyDescriptors(DerivedUnit.class);
384
        BeanUtils.getPropertyDescriptors(SpecimenOrObservationBase.class);
385
        BeanUtils.getPropertyDescriptors(FieldUnit.class);
386
        BeanUtils.getPropertyDescriptors(MediaSpecimen.class);
387
        BeanUtils.getPropertyDescriptors(DnaSample.class);
388
    }
389

    
390
    @Test
391
    public void testCollectRootUnits() {
392

    
393
        java.util.Collection<SpecimenOrObservationBase> rootSOBs;
394

    
395
        DerivedUnit theSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
396
        theSpecimen.setTitleCache("*SP*", true);
397

    
398
        rootSOBs = theSpecimen.collectRootUnits(null);
399
        assertEquals(1, rootSOBs.size());
400
        assertEquals(theSpecimen, rootSOBs.iterator().next());
401

    
402
        rootSOBs = theSpecimen.collectRootUnits(SpecimenOrObservationBase.class);
403
        assertEquals(1, rootSOBs.size());
404
        assertEquals(theSpecimen, rootSOBs.iterator().next());
405

    
406
        java.util.Collection<DerivedUnit> rootDUs = theSpecimen.collectRootUnits(DerivedUnit.class);
407
        assertEquals(1, rootDUs.size());
408
        assertEquals(theSpecimen, rootDUs.iterator().next());
409

    
410
        java.util.Collection<FieldUnit> rootFUs = theSpecimen.collectRootUnits(FieldUnit.class);
411
        assertTrue(rootFUs.isEmpty());
412

    
413
        // -- one FieldUnit as root ----------------------------------------
414

    
415
        FieldUnit fieldUnit1 = FieldUnit.NewInstance();
416
        fieldUnit1.setTitleCache("FU1", true);
417
        DerivationEvent.NewSimpleInstance(fieldUnit1, theSpecimen, DerivationEventType.GATHERING_IN_SITU());
418

    
419
        rootSOBs = theSpecimen.collectRootUnits(null);
420
        assertEquals(1, rootSOBs.size());
421
        assertEquals(fieldUnit1, rootSOBs.iterator().next());
422

    
423
        rootSOBs = theSpecimen.collectRootUnits(SpecimenOrObservationBase.class);
424
        assertEquals(1, rootSOBs.size());
425
        assertEquals(fieldUnit1, rootSOBs.iterator().next());
426

    
427
        rootDUs = theSpecimen.collectRootUnits(DerivedUnit.class);
428
        assertTrue(rootDUs.isEmpty());
429

    
430
        rootFUs = theSpecimen.collectRootUnits(FieldUnit.class);
431
        assertEquals(1, rootFUs.size());
432
        assertEquals(fieldUnit1, rootFUs.iterator().next());
433

    
434
        // -- multiple roots ------------------------------------------
435

    
436
        DerivedUnit specimen1 = DerivedUnit.NewPreservedSpecimenInstance();
437
        specimen1.setTitleCache("SP1", true);
438
        theSpecimen.getDerivedFrom().addOriginal(specimen1);
439

    
440
        rootSOBs = theSpecimen.collectRootUnits(null);
441
        assertEquals(2, rootSOBs.size());
442

    
443
        rootSOBs = theSpecimen.collectRootUnits(SpecimenOrObservationBase.class);
444
        assertEquals(2, rootSOBs.size());
445
        List<SpecimenOrObservationBase> rootSOBsSorted = rootSOBs.stream().sorted(new ByTtitleCacheComparator()).collect(Collectors.toList());
446
        assertEquals(fieldUnit1, rootSOBsSorted.get(0));
447
        assertEquals(specimen1, rootSOBsSorted.get(1));
448

    
449
        FieldUnit fieldUnit2 = FieldUnit.NewInstance();
450
        fieldUnit2.setTitleCache("FU2", true);
451
        DerivationEvent.NewSimpleInstance(fieldUnit2, specimen1, DerivationEventType.GATHERING_IN_SITU());
452

    
453
        rootSOBs = theSpecimen.collectRootUnits(SpecimenOrObservationBase.class);
454
        assertEquals(2, rootSOBs.size());
455
        rootSOBsSorted = rootSOBs.stream().sorted(new ByTtitleCacheComparator()).collect(Collectors.toList());
456
        assertEquals(fieldUnit1, rootSOBsSorted.get(0));
457
        assertEquals(fieldUnit2, rootSOBsSorted.get(1));
458

    
459
        // -- multiple roots & cycle detection ------------------------------------------
460

    
461
        DerivedUnit specimen2 = DerivedUnit.NewPreservedSpecimenInstance();
462
        specimen2.setTitleCache("SP2", true);
463

    
464
        DerivedUnit specimen3 = DerivedUnit.NewPreservedSpecimenInstance();
465
        specimen3.setTitleCache("SP3", true);
466

    
467
        theSpecimen.getDerivedFrom().addOriginal(specimen2);
468
        DerivationEvent.NewSimpleInstance(specimen3, specimen2, DerivationEventType.GATHERING_IN_SITU());
469
        DerivationEvent.NewSimpleInstance(theSpecimen, specimen3, DerivationEventType.GATHERING_IN_SITU());
470

    
471
        rootSOBs = theSpecimen.collectRootUnits(SpecimenOrObservationBase.class);
472
        assertEquals(3, rootSOBs.size());
473
        rootSOBsSorted = rootSOBs.stream().sorted(new ByTtitleCacheComparator()).collect(Collectors.toList());
474
        assertEquals(fieldUnit1, rootSOBsSorted.get(0));
475
        assertEquals(fieldUnit2, rootSOBsSorted.get(1));
476
        assertEquals(specimen3, rootSOBsSorted.get(2));
477
    }
478
}
    (1-1/1)