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.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
    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
    private final String[] includeTableNames_delete = new String[]{"TAXONNAME", "REFERENCE", "AGENTBASE", "HOMOTYPICALGROUP", "REGISTRATION",
84
            "DERIVATIONEVENT", "GATHERINGEVENT", "LANGUAGESTRING", "SPECIMENOROBSERVATIONBASE", "TYPEDESIGNATIONBASE",
85
            "REGISTRATION_TYPEDESIGNATIONBASE", "TAXONNAME_TYPEDESIGNATIONBASE", "SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT",
86
            "MEDIA", "MEDIA_REPRESENTATION", "MEDIAREPRESENTATION", "MEDIAREPRESENTATIONPART",
87
            "AUDITEVENT",
88
            "HIBERNATE_SEQUENCES"
89
            };
90

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

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

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

    
103
       SpecimenTypeDesignationWorkingSetDTO<Registration> workingset = service.create(registrationUuid, publicationUuid, typifiedNameUuid);
104

    
105
       Assert.assertNotNull(workingset.getOwner());
106
       Assert.assertEquals(Registration.class, workingset.getOwner().getClass());
107

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

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

    
119
       workingset.getSpecimenTypeDesignationDTOs().add(specimenTypeDesignationDTO);
120

    
121
       service.save(workingset);
122

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

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

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

    
129
       workingset = service.load(registrationUuid, baseEntityRef);
130

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

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

    
142
       workingset.getSpecimenTypeDesignationDTOs().add(specimenTypeDesignationDTO2);
143

    
144
       service.save(workingset);
145

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

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

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

    
170
    }
171

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

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

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

    
191
        service.save(workingset);
192

    
193
        // printDataSetWithNull(System.err, includeTableNames_delete);
194

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

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

    
206
//        printDataSetWithNull(System.err, includeTableNames_delete);
207

    
208
        TypedEntityReference<FieldUnit> baseEntityRef = new TypedEntityReference<FieldUnit>(FieldUnit.class, fieldUnitUuid, null);
209

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

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

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

    
228
        // printDataSetWithNull(System.err, includeTableNames_delete);
229
    }
230

    
231
    // ---------------------- TestData -------------------------------------------
232

    
233

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

    
240
    private void createRegistration() throws FileNotFoundException {
241

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

    
247
        nomRef.setAuthorship(team);
248
        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");
249
        TaxonName name = TaxonName.NewInstance(NomenclaturalCode.ICNAFP, Rank.SPECIES(), "Planothidium", null,  "victori", null, null, nomRef, "11-45", null);
250
        name.setUuid(typifiedNameUuid);
251
        name = cdmRepository.getNameService().save(name);
252

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

    
258
        //printDataSetWithNull(System.err, includeTableNames_create);
259

    
260
        writeDbUnitDataSetFile(includeTableNames_create);
261
    }
262
}
    (1-1/1)