Project

General

Profile

Download (6.42 KB) Statistics
| Branch: | Tag: | Revision:
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.name.SpecimenTypeDesignation;
24
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
25
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
26
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
27
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
28
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
29

    
30
/**
31
 *
32
 * @author a.kohlbecker
33
 * @since Jun 23, 2017
34
 *
35
 */
36
@DataSet // the dataset cleans up the DerivedUnits created in the tests
37
public class DerivedUnitConverterIntegrationTest extends CdmTransactionalIntegrationTest {
38

    
39
    @SpringBeanByType
40
    private IOccurrenceService service;
41

    
42
    @SpringBeanByType
43
    SessionFactory sessionFactory;
44

    
45
    @Test
46
    public void toMediaSpecimen_issue7114() throws DerivedUnitConversionException {
47

    
48
        // NOTE:
49
        // normally we would run this test as CdmIntegrationTest, but due to bug #7138
50
        // this is not possible, so we use CdmTransactionalIntegrationTest as super class
51
        // and stop the transaction at the beginning of the test
52
        commit();
53

    
54
        assertEquals(0, service.list(null, null, null, null, null).size());
55

    
56
        DerivedUnit du = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
57
        du.setTitleCache("test derived unit", true);
58
        SpecimenTypeDesignation std = SpecimenTypeDesignation.NewInstance();
59
        std.setTypeSpecimen(du);
60
        du = (DerivedUnit) service.save(du); // intermediate save is essential for this test
61
        DerivedUnitConverter<MediaSpecimen> converter = new DerivedUnitConverter<MediaSpecimen>(std);
62
        SpecimenTypeDesignation newDu = converter.convertTo(MediaSpecimen.class, SpecimenOrObservationType.StillImage);
63
        assertEquals(du, converter.oldDerivedUnit());
64
        printDataSet(System.err, new String[]{"SpecimenOrObservationBase", "TypeDesignationBase"});
65
        service.saveOrUpdate(newDu.getTypeSpecimen());
66
        service.delete(du);
67
        printDataSet(System.err, new String[]{"SpecimenOrObservationBase", "TypeDesignationBase"});
68
        assertEquals(1, service.list(null, null, null, null, null).size());
69
        assertEquals(1, service.list(MediaSpecimen.class, null, null, null, null).size());
70
    }
71

    
72
    @Test
73
    public void toDerivedUnit_issue7114() throws DerivedUnitConversionException {
74

    
75
        // NOTE:
76
        // normally we would run this test as CdmIntegrationTest, but due to bug #7138
77
        // this is not possible, so we use CdmTransactionalIntegrationTest as super class
78
        // and stop the transaction at the beginning of the test
79
        commit();
80

    
81
        assertEquals(0, service.list(null, null, null, null, null).size());
82

    
83
        MediaSpecimen du = MediaSpecimen.NewInstance(SpecimenOrObservationType.StillImage);
84
        du.setTitleCache("test media specimen", true);
85
        SpecimenTypeDesignation std = SpecimenTypeDesignation.NewInstance();
86
        std.setTypeSpecimen(du);
87
        DerivedUnitConverter<DerivedUnit> duc = new DerivedUnitConverter<DerivedUnit>(std);
88
        du = (MediaSpecimen) service.save(du); // intermediate save is essential for this test
89
        duc.convertTo(DerivedUnit.class, SpecimenOrObservationType.PreservedSpecimen);
90

    
91
        assertEquals(1, service.list(null, null, null, null, null).size());
92
        assertEquals(1, service.list(DerivedUnit.class, null, null, null, null).size());
93

    
94
    }
95

    
96
    /**
97
     * Test with DerivedUnit which is used in a couple of associations to prevent from
98
     * org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade ...
99
     */
100
    @DataSet("DerivedUnitConverterIntegrationTest.cascadeDelete.xml")
101
    @Test
102
    public void cascadeDelete() throws DerivedUnitConversionException{
103

    
104
        // NOTE:
105
        // normally we would run this test as CdmIntegrationTest, but due to bug #7138
106
        // this is not possible, so we use CdmTransactionalIntegrationTest as super class
107
        // and stop the transaction at the beginning of the test
108
        commit();
109

    
110
        UUID uuid = UUID.fromString("10eceb2c-9b51-458e-8dcd-2cb92cc558a9");
111
        MediaSpecimen du = (MediaSpecimen) service.load(uuid, Arrays.asList(new String[]{"*",
112
                "derivedFrom.*",
113
                "derivedFrom.originals.*",
114
                "derivedFrom.originals.derivationEvents",
115
                "specimenTypeDesignations.typifiedNames.typeDesignations",
116
                "specimenTypeDesignations.typifiedNames.homotypicalGroup",
117
                "specimenTypeDesignations.annotations",
118
                "specimenTypeDesignations.markers",
119
                "specimenTypeDesignations.registrations",
120
                //
121
                "derivedFrom.originals.gatheringEvent.$",
122
                "derivedFrom.originals.gatheringEvent.country",
123
                "derivedFrom.originals.gatheringEvent.collectingAreas",
124
                "derivedFrom.originals.gatheringEvent.actor.teamMembers",
125
                "derivedFrom.originals.derivationEvents.derivatives" }));
126
        SpecimenTypeDesignation specimenTypeDesignation = du.getSpecimenTypeDesignations().iterator().next();
127
        DerivedUnitConverter<DerivedUnit> duc = new DerivedUnitConverter<>(specimenTypeDesignation);
128
        SpecimenTypeDesignation newSpecimenTypeDesignation = duc.convertTo(DerivedUnit.class, SpecimenOrObservationType.HumanObservation);
129
        DerivedUnit target = newSpecimenTypeDesignation.getTypeSpecimen();
130

    
131
        //service.save(target); // save is performed in convertTo()
132

    
133
        assertEquals(2, service.list(null, null, null, null, null).size());
134
        assertEquals(1, service.list(DerivedUnit.class, null, null, null, null).size());
135
        assertEquals(1, service.list(FieldUnit.class, null, null, null, null).size());
136

    
137
    }
138

    
139
    /**
140
     * {@inheritDoc}
141
     */
142
    @Override
143
    public void createTestDataSet() throws FileNotFoundException {
144
       // using empty database
145

    
146
    }
147

    
148
}
(1-1/3)