Project

General

Profile

Download (12.9 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

    
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.utility.DerivedUnitConversionException;
30
import eu.etaxonomy.cdm.model.agent.Team;
31
import eu.etaxonomy.cdm.model.common.DefinedTerm;
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.vaadin.model.TypedEntityReference;
47
import eu.etaxonomy.cdm.vaadin.model.registration.KindOfUnitTerms;
48
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationDTO;
49
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
50

    
51
/**
52
 * @author a.kohlbecker
53
 * @since Nov 17, 2017
54
 *
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
    int registrationId = 5000;
73

    
74
    private Integer publicationId = 5000;
75

    
76
    private Integer typifiedNameId = 5000;
77

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

    
81
    private final String[] includeTableNames_delete = new String[]{"TAXONNAME", "REFERENCE", "AGENTBASE", "HOMOTYPICALGROUP", "REGISTRATION",
82
            "DERIVATIONEVENT", "GATHERINGEVENT", "LANGUAGESTRING", "SPECIMENOROBSERVATIONBASE", "TYPEDESIGNATIONBASE",
83
            "REGISTRATION_TYPEDESIGNATIONBASE", "TAXONNAME_TYPEDESIGNATIONBASE", "SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT",
84
            "MEDIA", "MEDIA_REPRESENTATION", "MEDIAREPRESENTATION", "MEDIAREPRESENTATIONPART",
85
            "AUDITEVENT",
86
            "HIBERNATE_SEQUENCES"
87
            };
88

    
89

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

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

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

    
102
       SpecimenTypeDesignationWorkingSetDTO<Registration> workingset = service.create(registrationId, publicationId, typifiedNameId);
103

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

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

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

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

    
120
       service.save(workingset);
121

    
122
       SpecimenOrObservationBase baseEntity = cdmRepository.getOccurrenceService().load(workingset.getFieldUnit().getUuid());
123

    
124

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

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

    
129
       workingset = service.loadDtoByIds(registrationId, 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.loadDtoByIds(registrationId, baseEntityRef);
147
       Assert.assertTrue(workingset.getSpecimenTypeDesignationDTOs().size() == 2);
148

    
149

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

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

    
169
    }
170

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

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

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

    
190
        service.save(workingset);
191

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

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

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

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

    
207
        TypedEntityReference<FieldUnit> baseEntityRef = new TypedEntityReference<FieldUnit>(FieldUnit.class, 5001, null);
208

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

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

    
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 = cdmRepository.getReferenceService().save(nomRef);
245
        publicationId = nomRef.getId();
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 = cdmRepository.getNameService().save(name);
251
        typifiedNameId = name.getId();
252

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

    
258

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

    
261
        writeDbUnitDataSetFile(includeTableNames_create);
262
    }
263

    
264

    
265

    
266

    
267

    
268

    
269

    
270
}
    (1-1/1)