Project

General

Profile

Download (13.2 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.service;
10

    
11
import java.io.FileNotFoundException;
12
import java.net.URISyntaxException;
13
import java.util.UUID;
14

    
15
import org.junit.Assert;
16
import org.junit.BeforeClass;
17
import org.junit.FixMethodOrder;
18
import org.junit.Test;
19
import org.junit.runners.MethodSorters;
20
import org.unitils.database.annotations.Transactional;
21
import org.unitils.database.util.TransactionMode;
22
import org.unitils.dbunit.annotation.DataSet;
23
import org.unitils.dbunit.annotation.ExpectedDataSet;
24
import org.unitils.spring.annotation.SpringBeanByName;
25
import org.unitils.spring.annotation.SpringBeanByType;
26

    
27
import eu.etaxonomy.cdm.CdmVaadinIntegrationTest;
28
import eu.etaxonomy.cdm.api.application.CdmRepository;
29
import eu.etaxonomy.cdm.api.util.DerivedUnitConversionException;
30
import eu.etaxonomy.cdm.common.URI;
31
import eu.etaxonomy.cdm.model.agent.Team;
32
import eu.etaxonomy.cdm.model.common.Language;
33
import eu.etaxonomy.cdm.model.common.LanguageString;
34
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
35
import eu.etaxonomy.cdm.model.name.Rank;
36
import eu.etaxonomy.cdm.model.name.Registration;
37
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
38
import eu.etaxonomy.cdm.model.name.TaxonName;
39
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
40
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
41
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
42
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
43
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
44
import eu.etaxonomy.cdm.model.reference.Reference;
45
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
46
import eu.etaxonomy.cdm.model.term.DefinedTerm;
47
import eu.etaxonomy.cdm.ref.TypedEntityReference;
48
import eu.etaxonomy.cdm.vaadin.model.registration.KindOfUnitTerms;
49
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationDTO;
50
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
51

    
52
/**
53
 * @author a.kohlbecker
54
 * @since Nov 17, 2017
55
 */
56
@Transactional(TransactionMode.DISABLED)
57
// IMPORTANT: test03_deleteTypeDesignationTest executed not as last would cause the other tests to fail due to changes in the db
58
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
59
public class SpecimenTypeDesignationWorkingSetServiceImplTest extends CdmVaadinIntegrationTest{
60

    
61
    @SpringBeanByName
62
    private CdmRepository cdmRepository;
63

    
64
    @SpringBeanByType
65
    private ISpecimenTypeDesignationWorkingSetService service;
66

    
67
    @BeforeClass
68
    public static void setupLoggers()  {
69
       // Logger.getLogger("org.dbunit").setLevel(Level.DEBUG);
70
    }
71

    
72
    UUID registrationUuid = UUID.fromString("c8bb4e70-ca85-43c3-ae81-c90a2b41a93f"); // 5000;
73

    
74
    private UUID publicationUuid = UUID.fromString("45804c65-7df9-42fd-b43a-818a8958c264"); // 5000;
75

    
76
    private UUID typifiedNameUuid = UUID.fromString("47d9263e-b32a-42af-98ea-5528f154384f"); //  5000;
77

    
78
    private UUID fieldUnitUuid = UUID.fromString("22be718a-6f21-4b74-aae3-bb7d7d659e1c"); // 5001
79

    
80
    private final String[] includeTableNames_create = new String[]{"TAXONNAME", "REFERENCE", "AGENTBASE", "HOMOTYPICALGROUP", "REGISTRATION",
81
            "HIBERNATE_SEQUENCES"};
82

    
83
    @SuppressWarnings("unused")
84
    private final String[] includeTableNames_delete = new String[]{"TAXONNAME", "REFERENCE", "AGENTBASE", "HOMOTYPICALGROUP", "REGISTRATION",
85
            "DERIVATIONEVENT", "GATHERINGEVENT", "LANGUAGESTRING", "SPECIMENOROBSERVATIONBASE", "TYPEDESIGNATIONBASE",
86
            "REGISTRATION_TYPEDESIGNATIONBASE", "TAXONNAME_TYPEDESIGNATIONBASE", "SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT",
87
            "MEDIA", "MEDIA_REPRESENTATION", "MEDIAREPRESENTATION", "MEDIAREPRESENTATIONPART",
88
            "ORIGINALSOURCEBASE",
89
            "AUDITEVENT",
90
            "HIBERNATE_SEQUENCES"
91
            };
92

    
93
    @Test
94
    @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest.xml")
95
    public void test01_createAndEditTest() throws DerivedUnitConversionException, URISyntaxException, FileNotFoundException {
96

    
97
//        printDataSet(System.err, new String[]{"USERACCOUNT", "GROUPS", "USERACCOUNT_GRANTEDAUTHORITYIMPL", "USERACCOUNT_PERMISSIONGROUP"
98
//                , "PERMISSIONGROUP", "PERMISSIONGROUP_GRANTEDAUTHORITYIMPL", "GRANTEDAUTHORITYIMPL"});
99

    
100
//        printDataSet(System.err, debugTables);
101
//        writeDbUnitDataSetFile(new String[]{"AUDITEVENT", "DEFINEDTERMBASE", "DEFINEDTERMBASE_AUD", "DEFINEDTERMBASE_REPRESENTATION", "DEFINEDTERMBASE_REPRESENTATION_AUD",
102
//                "REPRESENTATION", "REPRESENTATION_AUD", "HIBERNATE_SEQUENCES"},
103
//                "RegistrationTerms");
104

    
105
       SpecimenTypeDesignationWorkingSetDTO<Registration> workingset = service.create(registrationUuid, typifiedNameUuid);
106

    
107
       Assert.assertNotNull(workingset.getOwner());
108
       Assert.assertEquals(Registration.class, workingset.getOwner().getClass());
109

    
110
       workingset.getFieldUnit().setFieldNotes("FieldNotes");
111
       // int baseEntityID = workingset.getFieldUnit().getId();
112
       workingset.getFieldUnit().setFieldNumber("FieldNumber");
113
       workingset.getFieldUnit().getGatheringEvent().setLocality(LanguageString.NewInstance("Somewhere", Language.ENGLISH()));
114

    
115
       SpecimenTypeDesignationDTO specimenTypeDesignationDTO = new SpecimenTypeDesignationDTO();
116
       specimenTypeDesignationDTO.setKindOfUnit((DefinedTerm)cdmRepository.getTermService().load(KindOfUnitTerms.PUBLISHED_IMAGE().getUuid()));
117
       specimenTypeDesignationDTO.setMediaUri(new URI("http://foo.bar.com/image1"));
118
       specimenTypeDesignationDTO.setTypeStatus(SpecimenTypeDesignationStatus.HOLOTYPE());
119
       specimenTypeDesignationDTO.setAccessionNumber("TEST_1");
120

    
121
       workingset.getSpecimenTypeDesignationDTOs().add(specimenTypeDesignationDTO);
122

    
123
       service.save(workingset);
124

    
125
       SpecimenOrObservationBase<?> baseEntity = cdmRepository.getOccurrenceService().load(workingset.getFieldUnit().getUuid());
126

    
127
//     printDataSet(System.err, new String[]{"TYPEDESIGNATIONBASE", "SPECIMENOROBSERVATIONBASE"});
128

    
129
       TypedEntityReference<FieldUnit> baseEntityRef = new TypedEntityReference<>(FieldUnit.class, baseEntity.getUuid(), baseEntity.getTitleCache());
130

    
131
       workingset = service.load(registrationUuid, baseEntityRef);
132

    
133
       Assert.assertNotNull(specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen());
134
       Assert.assertEquals(MediaSpecimen.class, specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen().getClass());
135
       Assert.assertNotNull(specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen().getOriginals().iterator().next());
136
       Assert.assertEquals(FieldUnit.class, specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen().getOriginals().iterator().next().getClass());
137
       Assert.assertEquals("FieldNumber", ((FieldUnit)specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen().getOriginals().iterator().next()).getFieldNumber());
138

    
139
       SpecimenTypeDesignationDTO specimenTypeDesignationDTO2 = new SpecimenTypeDesignationDTO();
140
       specimenTypeDesignationDTO2.setKindOfUnit((DefinedTerm)cdmRepository.getTermService().load(KindOfUnitTerms.SPECIMEN().getUuid()));
141
       specimenTypeDesignationDTO2.setTypeStatus(SpecimenTypeDesignationStatus.HOLOTYPE());
142
       specimenTypeDesignationDTO2.setAccessionNumber("TEST_2");
143

    
144
       workingset.getSpecimenTypeDesignationDTOs().add(specimenTypeDesignationDTO2);
145

    
146
       service.save(workingset);
147

    
148
       workingset = service.load(registrationUuid, baseEntityRef);
149
       Assert.assertTrue(workingset.getSpecimenTypeDesignationDTOs().size() == 2);
150

    
151
       //FIXME this fails: Assert.assertEquals("There must only be one FieldUnit and one MediaSpecimen", 2, cdmRepository.getOccurrenceService().count(DerivedUnit.class));
152

    
153
       // write test data for delete test
154
       /*
155
       // printDataSet(System.err, includeTableNames_delete);
156
        writeDbUnitDataSetFile(includeTableNames_delete, "deleteTest");
157
        */
158
       /* The following audit table fix needs also to be added to the test data:
159
           <!-- Test data is being used by more than one test - need to reset a couple of *_AUD tables -->
160
          <AUDITEVENT />
161
          <TAXONNAME_AUD />
162
          <DERIVATIONEVENT_AUD />
163
          <GATHERINGEVENT_AUD />
164
          <LANGUAGESTRING_AUD />
165
          <TYPEDESIGNATIONBASE_AUD />
166
          <SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT_AUD />
167
          <REGISTRATION_AUD />
168
          <SPECIMENOROBSERVATIONBASE_AUD />
169
          <TAXONNAME_TYPEDESIGNATIONBASE_AUD />
170
        */
171

    
172
    }
173

    
174
    @Test
175
    @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest-deleteTest.xml")
176
    @ExpectedDataSet("SpecimenTypeDesignationWorkingSetServiceImplTest.deleteTypeDesignationTest-result.xml")
177
    public void test03_deleteTypeDesignationTest() {
178

    
179
        // FieldUnit" ID="5001
180
        TypedEntityReference<FieldUnit> baseEntityRef = new TypedEntityReference<>(FieldUnit.class, fieldUnitUuid, "Somewhere, FieldNumber.");
181
        SpecimenTypeDesignationWorkingSetDTO<Registration> workingset = service.load(registrationUuid, baseEntityRef);
182
        Assert.assertTrue(workingset.getSpecimenTypeDesignationDTOs().size() == 2);
183

    
184
        SpecimenTypeDesignationDTO deleteDTO = null;
185
        for(SpecimenTypeDesignationDTO stdDTO : workingset.getSpecimenTypeDesignationDTOs()){
186
            if(stdDTO.getAccessionNumber().equals("TEST_1")){
187
                deleteDTO = stdDTO;
188
                break;
189
            }
190
        }
191
        workingset.getSpecimenTypeDesignationDTOs().remove(deleteDTO);
192

    
193
        service.save(workingset);
194

    
195
        // printDataSet(System.err, includeTableNames_delete);
196

    
197
        workingset = service.load(registrationUuid, baseEntityRef);
198
        Registration reg = workingset.getOwner();
199
        Assert.assertEquals(1, workingset.getSpecimenTypeDesignationDTOs().size());
200
        reg = workingset.getOwner();
201
        Assert.assertEquals(1, reg.getTypeDesignations().size());
202
    }
203

    
204
    @Test
205
    @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest-deleteTest.xml")
206
    public void test02_deleteWorkingset() {
207

    
208
//        printDataSet(System.err, includeTableNames_delete);
209

    
210
        TypedEntityReference<FieldUnit> baseEntityRef = new TypedEntityReference<>(FieldUnit.class, fieldUnitUuid, null);
211

    
212
        SpecimenTypeDesignationWorkingSetDTO<Registration> workingset = service.load(registrationUuid, baseEntityRef);
213
        Assert.assertNotNull(workingset.getOwner());
214
        Assert.assertEquals(2, workingset.getSpecimenTypeDesignationDTOs().size());
215
        service.delete(workingset, true);
216

    
217
//        UUID gatheringEventUUID = UUID.fromString("23d40440-38bb-46c1-af11-6e25dcfa0145");
218
//        UUID fieldUnitUUID = UUID.fromString("22be718a-6f21-4b74-aae3-bb7d7d659e1c");
219
//        UUID mediaSpecimenUUID = UUID.fromString("10eceb2c-9b51-458e-8dcd-2cb92cc558a9");
220
//        UUID specimenUUID = UUID.fromString("2e384f8e-fbb0-44eb-9d5f-1b7235493932");
221
//        UUID typeDesignation1UUID = UUID.fromString("a1896ae2-4396-4243-988e-3d74058b44ab");
222
//        UUID typeDesignation2UUID = UUID.fromString("a1896ae2-4396-4243-988e-3d74058b44ab");
223

    
224
        Assert.assertEquals("All TypeDesignations should have been deleted", 0, cdmRepository.getNameService().getAllTypeDesignations(10, 0).size());
225
        Assert.assertEquals("All derived units should have been deleted", 0, cdmRepository.getOccurrenceService().count(DerivedUnit.class));
226
        Assert.assertEquals("FieldUnit should have been deleted", 0, cdmRepository.getOccurrenceService().count(FieldUnit.class));
227
        Assert.assertEquals("Gathering event should have been deleted by orphan remove", 0, cdmRepository.getEventBaseService().count(GatheringEvent.class));
228
        // FIXME Assert.assertEquals("Media should have been deleted ", 0, cdmRepository.getMediaService().count(null));
229

    
230
        // printDataSet(System.err, includeTableNames_delete);
231
    }
232

    
233
    // ---------------------- TestData -------------------------------------------
234

    
235

    
236
    @Override
237
    // @Test
238
    public void createTestDataSet() throws FileNotFoundException {
239
        createRegistration();
240
    }
241

    
242
    private void createRegistration() throws FileNotFoundException {
243

    
244
        Team team = Team.NewTitledInstance("Novis, Braidwood & Kilroy", "Novis, Braidwood & Kilroy");
245
        Reference nomRef = ReferenceFactory.newArticle();
246
        nomRef.setUuid(publicationUuid);
247
        nomRef = cdmRepository.getReferenceService().save(nomRef);
248

    
249
        nomRef.setAuthorship(team);
250
        nomRef.setTitle("P.M. Novis, J. Braidwood & C. Kilroy, Small diatoms (Bacillariophyta) in cultures from the Styx River, New Zealand, including descriptions of three new species in Phytotaxa 64");
251
        TaxonName name = TaxonName.NewInstance(NomenclaturalCode.ICNAFP, Rank.SPECIES(), "Planothidium", null,  "victori", null, null, nomRef, "11-45", null);
252
        name.setUuid(typifiedNameUuid);
253
        name = cdmRepository.getNameService().save(name);
254

    
255
        Registration reg = Registration.NewInstance();
256
        reg.setName(name);
257
        reg.setUuid(registrationUuid);
258
        reg = cdmRepository.getRegistrationService().save(reg);
259

    
260
        //printDataSet(System.err, includeTableNames_create);
261

    
262
        writeDbUnitDataSetFile(includeTableNames_create);
263
    }
264
}
    (1-1/1)