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.URI;
13
import java.net.URISyntaxException;
14
import java.util.UUID;
15

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

    
28
import eu.etaxonomy.cdm.CdmVaadinIntegrationTest;
29
import eu.etaxonomy.cdm.api.application.CdmRepository;
30
import eu.etaxonomy.cdm.api.utility.DerivedUnitConversionException;
31
import eu.etaxonomy.cdm.model.agent.Team;
32
import eu.etaxonomy.cdm.model.common.DefinedTerm;
33
import eu.etaxonomy.cdm.model.common.Language;
34
import eu.etaxonomy.cdm.model.common.LanguageString;
35
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
36
import eu.etaxonomy.cdm.model.name.Rank;
37
import eu.etaxonomy.cdm.model.name.Registration;
38
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
39
import eu.etaxonomy.cdm.model.name.TaxonName;
40
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
41
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
42
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
43
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
44
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
45
import eu.etaxonomy.cdm.model.reference.Reference;
46
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
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
 */
57
@Transactional(TransactionMode.DISABLED)
58
// IMPORTANT: test03_deleteTypeDesignationTest executed not as last would cause the other tests to fail due to changes in the db
59
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
60
public class SpecimenTypeDesignationWorkingSetServiceImplTest extends CdmVaadinIntegrationTest{
61

    
62
    @SpringBeanByName
63
    private CdmRepository cdmRepository;
64

    
65
    @SpringBeanByType
66
    private ISpecimenTypeDesignationWorkingSetService service;
67

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

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

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

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

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

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

    
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
            "AUDITEVENT",
89
            "HIBERNATE_SEQUENCES"
90
            };
91

    
92

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

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

    
100
       //printDataSetWithNull(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, publicationUuid, 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

    
128
       printDataSetWithNull(System.err, new String[]{"TYPEDESIGNATIONBASE", "SPECIMENOROBSERVATIONBASE"});
129

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

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

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

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

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

    
147
       service.save(workingset);
148

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

    
152

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

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

    
174
    }
175

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

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

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

    
195
        service.save(workingset);
196

    
197
        // printDataSetWithNull(System.err, includeTableNames_delete);
198

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

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

    
210
//        printDataSetWithNull(System.err, includeTableNames_delete);
211

    
212
        TypedEntityReference<FieldUnit> baseEntityRef = new TypedEntityReference<FieldUnit>(FieldUnit.class, fieldUnitUuid, null);
213

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

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

    
226

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

    
233
        // printDataSetWithNull(System.err, includeTableNames_delete);
234
    }
235

    
236
    // ---------------------- TestData -------------------------------------------
237

    
238

    
239
    @Override
240
    // @Test
241
    public void createTestDataSet() throws FileNotFoundException {
242
        createRegistration();
243
    }
244

    
245
    private void createRegistration() throws FileNotFoundException {
246

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

    
252
        nomRef.setAuthorship(team);
253
        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");
254
        TaxonName name = TaxonName.NewInstance(NomenclaturalCode.ICNAFP, Rank.SPECIES(), "Planothidium", null,  "victori", null, null, nomRef, "11-45", null);
255
        name.setUuid(typifiedNameUuid);
256
        name = cdmRepository.getNameService().save(name);
257

    
258
        Registration reg = Registration.NewInstance();
259
        reg.setName(name);
260
        reg.setUuid(registrationUuid);
261
        reg = cdmRepository.getRegistrationService().save(reg);
262

    
263

    
264
        //printDataSetWithNull(System.err, includeTableNames_create);
265

    
266
        writeDbUnitDataSetFile(includeTableNames_create);
267
    }
268

    
269

    
270

    
271

    
272

    
273

    
274

    
275
}
    (1-1/1)