1
|
/**
|
2
|
* Copyright (C) 2017 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.api.utility;
|
10
|
|
11
|
import static org.junit.Assert.assertEquals;
|
12
|
|
13
|
import java.io.FileNotFoundException;
|
14
|
import java.util.Arrays;
|
15
|
import java.util.UUID;
|
16
|
|
17
|
import org.hibernate.SessionFactory;
|
18
|
import org.junit.Test;
|
19
|
import org.unitils.dbunit.annotation.DataSet;
|
20
|
import org.unitils.spring.annotation.SpringBeanByType;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
23
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
24
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
25
|
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
|
26
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
27
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
28
|
|
29
|
/**
|
30
|
*
|
31
|
* @author a.kohlbecker
|
32
|
* @since Jun 23, 2017
|
33
|
*
|
34
|
*/
|
35
|
@DataSet // the dataset cleans up the DerivedUnits created in the tests
|
36
|
public class DerivedUnitConverterIntegrationTest extends CdmTransactionalIntegrationTest {
|
37
|
|
38
|
@SpringBeanByType
|
39
|
private IOccurrenceService service;
|
40
|
|
41
|
@SpringBeanByType
|
42
|
SessionFactory sessionFactory;
|
43
|
|
44
|
@Test
|
45
|
public void toMediaSpecimen_issue7114() throws DerivedUnitConversionException {
|
46
|
|
47
|
// NOTE:
|
48
|
// normally we would run this test as CdmIntegrationTest, but due to bug #7138
|
49
|
// this is not possible, so we use CdmTransactionalIntegrationTest as super class
|
50
|
// and stop the transaction at the beginning of the test
|
51
|
commit();
|
52
|
|
53
|
assertEquals(0, service.list(null, null, null, null, null).size());
|
54
|
|
55
|
DerivedUnit du = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
|
56
|
du.setTitleCache("test derived unit", true);
|
57
|
du = (DerivedUnit) service.save(du); // intermediate save is essential for this test
|
58
|
DerivedUnitConverter<MediaSpecimen> duc = DerivedUnitConverterFactory.createDerivedUnitConverter(du, MediaSpecimen.class);
|
59
|
duc.convertTo(MediaSpecimen.class, SpecimenOrObservationType.StillImage);
|
60
|
|
61
|
assertEquals(1, service.list(null, null, null, null, null).size());
|
62
|
assertEquals(1, service.list(MediaSpecimen.class, null, null, null, null).size());
|
63
|
}
|
64
|
|
65
|
@Test
|
66
|
public void toDerivedUnit_issue7114() throws DerivedUnitConversionException {
|
67
|
|
68
|
// NOTE:
|
69
|
// normally we would run this test as CdmIntegrationTest, but due to bug #7138
|
70
|
// this is not possible, so we use CdmTransactionalIntegrationTest as super class
|
71
|
// and stop the transaction at the beginning of the test
|
72
|
commit();
|
73
|
|
74
|
assertEquals(0, service.list(null, null, null, null, null).size());
|
75
|
|
76
|
MediaSpecimen du = MediaSpecimen.NewInstance(SpecimenOrObservationType.StillImage);
|
77
|
du.setTitleCache("test media specimen", true);
|
78
|
DerivedUnitConverter<DerivedUnit> duc = DerivedUnitConverterFactory.createDerivedUnitConverter(du, DerivedUnit.class);
|
79
|
du = (MediaSpecimen) service.save(du); // intermediate save is essential for this test
|
80
|
duc.convertTo(DerivedUnit.class, SpecimenOrObservationType.PreservedSpecimen);
|
81
|
|
82
|
assertEquals(1, service.list(null, null, null, null, null).size());
|
83
|
assertEquals(1, service.list(DerivedUnit.class, null, null, null, null).size());
|
84
|
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* Test with DerivedUnit which is used in a couple of associations to prevent from
|
89
|
* org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade ...
|
90
|
*/
|
91
|
@DataSet("DerivedUnitConverterIntegrationTest.cascadeDelete.xml")
|
92
|
@Test
|
93
|
public void cascadeDelete() throws DerivedUnitConversionException{
|
94
|
|
95
|
// NOTE:
|
96
|
// normally we would run this test as CdmIntegrationTest, but due to bug #7138
|
97
|
// this is not possible, so we use CdmTransactionalIntegrationTest as super class
|
98
|
// and stop the transaction at the beginning of the test
|
99
|
commit();
|
100
|
|
101
|
UUID uuid = UUID.fromString("10eceb2c-9b51-458e-8dcd-2cb92cc558a9");
|
102
|
MediaSpecimen du = (MediaSpecimen) service.load(uuid, Arrays.asList(new String[]{"*",
|
103
|
"derivedFrom.*",
|
104
|
"derivedFrom.originals.*",
|
105
|
"derivedFrom.originals.derivationEvents",
|
106
|
"specimenTypeDesignations.typifiedNames.typeDesignations",
|
107
|
//
|
108
|
"derivedFrom.originals.gatheringEvent.$",
|
109
|
"derivedFrom.originals.gatheringEvent.country",
|
110
|
"derivedFrom.originals.gatheringEvent.collectingAreas",
|
111
|
"derivedFrom.originals.gatheringEvent.actor.teamMembers",
|
112
|
"derivedFrom.originals.derivationEvents.derivatives" }));
|
113
|
DerivedUnitConverter<DerivedUnit> duc = DerivedUnitConverterFactory.createDerivedUnitConverter(du, DerivedUnit.class);
|
114
|
DerivedUnit target = duc.convertTo(DerivedUnit.class, SpecimenOrObservationType.HumanObservation);
|
115
|
|
116
|
//service.save(target); // save is performed in convertTo()
|
117
|
|
118
|
assertEquals(2, service.list(null, null, null, null, null).size());
|
119
|
assertEquals(1, service.list(DerivedUnit.class, null, null, null, null).size());
|
120
|
assertEquals(1, service.list(FieldUnit.class, null, null, null, null).size());
|
121
|
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* {@inheritDoc}
|
126
|
*/
|
127
|
@Override
|
128
|
public void createTestDataSet() throws FileNotFoundException {
|
129
|
// using empty database
|
130
|
|
131
|
}
|
132
|
|
133
|
}
|