Project

General

Profile

Download (13.3 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.occurrence.SpecimenOrObservationBase;
45
import eu.etaxonomy.cdm.model.reference.Reference;
46
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
47
import eu.etaxonomy.cdm.vaadin.model.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
    int registrationId = 5000;
74

    
75
    private Integer publicationId = 5000;
76

    
77
    private Integer typifiedNameId = 5000;
78

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

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

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

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

    
130
       workingset = service.loadDtoByIds(registrationId, baseEntityRef);
131

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

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

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

    
145
       service.save(workingset);
146

    
147
       workingset = service.loadDtoByIds(registrationId, baseEntityRef);
148
       Assert.assertTrue(workingset.getSpecimenTypeDesignationDTOs().size() == 2);
149

    
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
       // printDataSetWithNull(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
          <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, 5001, "Somewhere, FieldNumber.");
179
        SpecimenTypeDesignationWorkingSetDTO<Registration> workingset = service.loadDtoByIds(registrationId, 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
        // TODO once https://dev.e-taxonomy.eu/redmine/issues/7077 is fixed dissociating from the Registration could be removed here
192
        Registration reg = workingset.getOwner();
193
        SpecimenTypeDesignation std = deleteDTO.asSpecimenTypeDesignation();
194
        reg.getTypeDesignations().remove(std);
195

    
196
        service.save(workingset);
197

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

    
200
        workingset = service.loadDtoByIds(registrationId, baseEntityRef);
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, 5001, null);
213

    
214
        try {
215

    
216
        SpecimenTypeDesignationWorkingSetDTO<Registration> workingset = service.loadDtoByIds(registrationId, baseEntityRef);
217
        Assert.assertNotNull(workingset.getOwner());
218
        Assert.assertEquals(2, workingset.getSpecimenTypeDesignationDTOs().size());
219
        service.delete(workingset, true);
220

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

    
228

    
229
        Assert.assertEquals("All TypeDesignations should have been deleted", 0, cdmRepository.getNameService().getAllTypeDesignations(10, 0).size());
230
        Assert.assertEquals("All derived units should have been deleted", 0, cdmRepository.getOccurrenceService().count(DerivedUnit.class));
231
        Assert.assertEquals("FieldUnit should have been deleted", 0, cdmRepository.getOccurrenceService().count(FieldUnit.class));
232
        Assert.assertEquals("Gathering event should have been deleted by orphan remove", 0, cdmRepository.getEventBaseService().count(GatheringEvent.class));
233
        // FIXME  Assert.assertEquals("Media should have been deleted ", 0, cdmRepository.getMediaService().count(null));
234
        } catch (Exception e){
235
            e.printStackTrace(System.err);
236
        }
237
        // printDataSetWithNull(System.err, includeTableNames_delete);
238
    }
239

    
240
    // ---------------------- TestData -------------------------------------------
241

    
242

    
243
    @Override
244
    // @Test
245
    public void createTestDataSet() throws FileNotFoundException {
246
        createRegistration();
247
    }
248

    
249
    private void createRegistration() throws FileNotFoundException {
250

    
251
        Team team = Team.NewTitledInstance("Novis, Braidwood & Kilroy", "Novis, Braidwood & Kilroy");
252
        Reference nomRef = ReferenceFactory.newArticle();
253
        nomRef = cdmRepository.getReferenceService().save(nomRef);
254
        publicationId = nomRef.getId();
255

    
256
        nomRef.setAuthorship(team);
257
        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");
258
        TaxonName name = TaxonName.NewInstance(NomenclaturalCode.ICNAFP, Rank.SPECIES(), "Planothidium", null,  "victori", null, null, nomRef, "11-45", null);
259
        name = cdmRepository.getNameService().save(name);
260
        typifiedNameId = name.getId();
261

    
262
        Registration reg = Registration.NewInstance();
263
        reg.setName(name);
264
        reg = cdmRepository.getRegistrationService().save(reg);
265
        registrationId = reg.getId();
266

    
267

    
268
        //printDataSetWithNull(System.err, includeTableNames_create);
269

    
270
        writeDbUnitDataSetFile(includeTableNames_create);
271
    }
272

    
273

    
274

    
275

    
276

    
277

    
278

    
279
}
    (1-1/1)