Project

General

Profile

Download (6.62 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
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
30

    
31
/**
32
 * @author a.kohlbecker
33
 * @since Jun 23, 2017
34
 */
35
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class) // 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
        SpecimenTypeDesignation std = SpecimenTypeDesignation.NewInstance();
58
        std.setTypeSpecimen(du);
59
        du = (DerivedUnit) service.save(du); // intermediate save is essential for this test
60
        DerivedUnitConverter<MediaSpecimen> converter = new DerivedUnitConverter<>(std);
61
        SpecimenTypeDesignation newDu = converter.convertTo(MediaSpecimen.class, SpecimenOrObservationType.StillImage);
62
        assertEquals(du, converter.oldDerivedUnit());
63
//        printDataSet(System.err, new String[]{"SpecimenOrObservationBase", "TypeDesignationBase"});
64
        service.saveOrUpdate(newDu.getTypeSpecimen());
65
        service.delete(du);
66
//        printDataSet(System.err, new String[]{"SpecimenOrObservationBase", "TypeDesignationBase"});
67
        assertEquals(1, service.list(null, null, null, null, null).size());
68
        assertEquals(1, service.list(MediaSpecimen.class, null, null, null, null).size());
69
    }
70

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

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

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

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

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

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

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

    
109
        UUID uuid = UUID.fromString("10eceb2c-9b51-458e-8dcd-2cb92cc558a9");
110
        MediaSpecimen du = (MediaSpecimen) service.load(uuid, Arrays.asList(new String[]{"*",
111
                "derivedFrom.*",
112
                "derivedFrom.originals.*",
113
                "derivedFrom.originals.derivationEvents",
114
                "specimenTypeDesignations.typifiedNames.typeDesignations",
115
                "specimenTypeDesignations.typifiedNames.homotypicalGroup",
116
                "specimenTypeDesignations.annotations",
117
                "specimenTypeDesignations.markers",
118
                "specimenTypeDesignations.registrations",
119
                "specimenTypeDesignations.sources",
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)