Project

General

Profile

Download (12.5 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.SpecimenTypeDesignation;
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.reference.Reference;
45
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
46
import eu.etaxonomy.cdm.vaadin.model.registration.KindOfUnitTerms;
47
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationDTO;
48
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
49

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

    
60
    @SpringBeanByName
61
    private CdmRepository cdmRepository;
62

    
63
    @SpringBeanByType
64
    private ISpecimenTypeDesignationWorkingSetService service;
65

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

    
71
    int registrationId = 5000;
72

    
73
    private Integer publicationId = 5000;
74

    
75
    private Integer typifiedNameId = 5000;
76

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

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

    
88

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

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

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

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

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

    
106
       workingset.getFieldUnit().setFieldNotes("FieldNotes");
107
       workingset.getFieldUnit().setFieldNumber("FieldNumber");
108
       workingset.getFieldUnit().getGatheringEvent().setLocality(LanguageString.NewInstance("Somewhere", Language.ENGLISH()));
109

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

    
116
       workingset.getSpecimenTypeDesignationDTOs().add(specimenTypeDesignationDTO);
117

    
118
       service.save(workingset);
119

    
120
       //printDataSetWithNull(System.err, new String[]{"TYPEDESIGNATIONBASE", "SPECIMENOROBSERVATIONBASE"});
121

    
122
       workingset = service.loadDtoByIds(registrationId, 0);
123

    
124
       Assert.assertNotNull(specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen());
125
       Assert.assertEquals(MediaSpecimen.class, specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen().getClass());
126
       Assert.assertNotNull(specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen().getOriginals().iterator().next());
127
       Assert.assertEquals(FieldUnit.class, specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen().getOriginals().iterator().next().getClass());
128
       Assert.assertEquals("FieldNumber", ((FieldUnit)specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen().getOriginals().iterator().next()).getFieldNumber());
129

    
130
       SpecimenTypeDesignationDTO specimenTypeDesignationDTO2 = new SpecimenTypeDesignationDTO();
131
       specimenTypeDesignationDTO2.setKindOfUnit((DefinedTerm)cdmRepository.getTermService().load(KindOfUnitTerms.SPECIMEN().getUuid()));
132
       specimenTypeDesignationDTO2.setTypeStatus(SpecimenTypeDesignationStatus.HOLOTYPE());
133
       specimenTypeDesignationDTO2.setAccessionNumber("TEST_2");
134

    
135
       workingset.getSpecimenTypeDesignationDTOs().add(specimenTypeDesignationDTO2);
136

    
137
       service.save(workingset);
138

    
139
       workingset = service.loadDtoByIds(registrationId, 0);
140
       Assert.assertTrue(workingset.getSpecimenTypeDesignationDTOs().size() == 2);
141

    
142

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

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

    
162
    }
163

    
164
    @Test
165
    @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest-deleteTest.xml")
166
    @ExpectedDataSet("SpecimenTypeDesignationWorkingSetServiceImplTest.deleteTypeDesignationTest-result.xml")
167
    public void test03_deleteTypeDesignationTest() {
168

    
169
        SpecimenTypeDesignationWorkingSetDTO<Registration> workingset = service.loadDtoByIds(registrationId, 0);
170
        Assert.assertTrue(workingset.getSpecimenTypeDesignationDTOs().size() == 2);
171

    
172
        SpecimenTypeDesignationDTO deleteDTO = null;
173
        for(SpecimenTypeDesignationDTO stdDTO : workingset.getSpecimenTypeDesignationDTOs()){
174
            if(stdDTO.getAccessionNumber().equals("TEST_1")){
175
                deleteDTO = stdDTO;
176
                break;
177
            }
178
        }
179
        workingset.getSpecimenTypeDesignationDTOs().remove(deleteDTO);
180

    
181
        // TODO once https://dev.e-taxonomy.eu/redmine/issues/7077 is fixed dissociating from the Registration could be removed here
182
        Registration reg = workingset.getOwner();
183
        SpecimenTypeDesignation std = deleteDTO.asSpecimenTypeDesignation();
184
        reg.getTypeDesignations().remove(std);
185

    
186
        service.save(workingset);
187

    
188
        //printDataSetWithNull(System.err, includeTableNames_delete);
189

    
190
        workingset = service.loadDtoByIds(registrationId, 0);
191
        Assert.assertEquals(1, workingset.getSpecimenTypeDesignationDTOs().size());
192
        reg = workingset.getOwner();
193
        Assert.assertEquals(1, reg.getTypeDesignations().size());
194
    }
195

    
196
    @Test
197
    @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest-deleteTest.xml")
198
    public void test02_deleteWorkingset() {
199

    
200
        printDataSetWithNull(System.err, includeTableNames_delete);
201

    
202
        try {
203
        SpecimenTypeDesignationWorkingSetDTO<Registration> workingset = service.loadDtoByIds(registrationId, 0);
204
        Assert.assertNotNull(workingset.getOwner());
205
        Assert.assertEquals(2, workingset.getSpecimenTypeDesignationDTOs().size());
206
        service.delete(workingset, true);
207

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

    
215

    
216
        Assert.assertEquals("All TypeDesignations should have been deleted", 0, cdmRepository.getNameService().getAllTypeDesignations(10, 0).size());
217
        Assert.assertEquals("All derived units should have been deleted", 0, cdmRepository.getOccurrenceService().count(DerivedUnit.class));
218
        Assert.assertEquals("FieldUnit should have been deleted", 0, cdmRepository.getOccurrenceService().count(FieldUnit.class));
219
        Assert.assertEquals("Gathering event should have been deleted by orphan remove", 0, cdmRepository.getEventBaseService().count(GatheringEvent.class));
220
        // FIXME  Assert.assertEquals("Media should have been deleted ", 0, cdmRepository.getMediaService().count(null));
221
        } catch (Exception e){
222
            e.printStackTrace(System.err);
223
        }
224
        // printDataSetWithNull(System.err, includeTableNames_delete);
225
    }
226

    
227
    // ---------------------- TestData -------------------------------------------
228

    
229

    
230
    @Override
231
    // @Test
232
    public void createTestDataSet() throws FileNotFoundException {
233
        createRegistration();
234
    }
235

    
236
    private void createRegistration() throws FileNotFoundException {
237

    
238
        Team team = Team.NewTitledInstance("Novis, Braidwood & Kilroy", "Novis, Braidwood & Kilroy");
239
        Reference nomRef = ReferenceFactory.newArticle();
240
        nomRef = cdmRepository.getReferenceService().save(nomRef);
241
        publicationId = nomRef.getId();
242

    
243
        nomRef.setAuthorship(team);
244
        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");
245
        TaxonName name = TaxonName.NewInstance(NomenclaturalCode.ICNAFP, Rank.SPECIES(), "Planothidium", null,  "victori", null, null, nomRef, "11-45", null);
246
        name = cdmRepository.getNameService().save(name);
247
        typifiedNameId = name.getId();
248

    
249
        Registration reg = Registration.NewInstance();
250
        reg.setName(name);
251
        reg = cdmRepository.getRegistrationService().save(reg);
252
        registrationId = reg.getId();
253

    
254

    
255
        //printDataSetWithNull(System.err, includeTableNames_create);
256

    
257
        writeDbUnitDataSetFile(includeTableNames_create);
258
    }
259

    
260

    
261

    
262

    
263

    
264

    
265

    
266
}
    (1-1/1)