Project

General

Profile

Download (11.1 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.Test;
17
import org.unitils.database.annotations.Transactional;
18
import org.unitils.database.util.TransactionMode;
19
import org.unitils.dbunit.annotation.DataSet;
20
import org.unitils.spring.annotation.SpringBeanByName;
21
import org.unitils.spring.annotation.SpringBeanByType;
22

    
23
import eu.etaxonomy.cdm.CdmVaadinIntegrationTest;
24
import eu.etaxonomy.cdm.api.application.CdmRepository;
25
import eu.etaxonomy.cdm.api.utility.DerivedUnitConversionException;
26
import eu.etaxonomy.cdm.model.agent.Team;
27
import eu.etaxonomy.cdm.model.common.DefinedTerm;
28
import eu.etaxonomy.cdm.model.common.Language;
29
import eu.etaxonomy.cdm.model.common.LanguageString;
30
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
31
import eu.etaxonomy.cdm.model.name.Rank;
32
import eu.etaxonomy.cdm.model.name.Registration;
33
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
34
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
35
import eu.etaxonomy.cdm.model.name.TaxonName;
36
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
37
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
38
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
39
import eu.etaxonomy.cdm.model.reference.Reference;
40
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
41
import eu.etaxonomy.cdm.vaadin.model.registration.KindOfUnitTerms;
42
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationDTO;
43
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
44

    
45
/**
46
 * @author a.kohlbecker
47
 * @since Nov 17, 2017
48
 *
49
 */
50
@Transactional(TransactionMode.ROLLBACK)
51
public class SpecimenTypeDesignationWorkingSetServiceImplTest extends CdmVaadinIntegrationTest{
52

    
53
    @SpringBeanByName
54
    private CdmRepository cdmRepository;
55

    
56
    @SpringBeanByType
57
    private ISpecimenTypeDesignationWorkingSetService service;
58

    
59
    int registrationId = 5000;
60

    
61
    private Integer publicationId = 5000;
62

    
63
    private Integer typifiedNameId = 5000;
64

    
65
    String[] includeTableNames_create = new String[]{"TAXONNAME", "REFERENCE", "AGENTBASE", "HOMOTYPICALGROUP", "REGISTRATION",
66
            "HIBERNATE_SEQUENCES"};
67

    
68
    String[] includeTableNames_delete = new String[]{"TAXONNAME", "REFERENCE", "AGENTBASE", "HOMOTYPICALGROUP", "REGISTRATION",
69
            "DERIVATIONEVENT", "GATHERINGEVENT", "LANGUAGESTRING", "SPECIMENOROBSERVATIONBASE", "TYPEDESIGNATIONBASE",
70
            "REGISTRATION_TYPEDESIGNATIONBASE", "TAXONNAME_TYPEDESIGNATIONBASE", "SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT",
71
            "MEDIA", "MEDIA_REPRESENTATION", "MEDIAREPRESENTATION", "MEDIAREPRESENTATIONPART",
72
            "AUDITEVENT",
73
            "HIBERNATE_SEQUENCES"
74
            };
75

    
76

    
77
    @Test
78
    @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest.xml")
79
    public void createAndEditTest() throws DerivedUnitConversionException, URISyntaxException, FileNotFoundException {
80

    
81
//        printDataSetWithNull(System.err, new String[]{"USERACCOUNT", "GROUPS", "USERACCOUNT_GRANTEDAUTHORITYIMPL", "USERACCOUNT_PERMISSIONGROUP"
82
//                , "PERMISSIONGROUP", "PERMISSIONGROUP_GRANTEDAUTHORITYIMPL", "GRANTEDAUTHORITYIMPL"});
83

    
84
       //printDataSetWithNull(System.err, debugTables);
85
//        writeDbUnitDataSetFile(new String[]{"AUDITEVENT", "DEFINEDTERMBASE", "DEFINEDTERMBASE_AUD", "DEFINEDTERMBASE_REPRESENTATION", "DEFINEDTERMBASE_REPRESENTATION_AUD",
86
//                "REPRESENTATION", "REPRESENTATION_AUD", "HIBERNATE_SEQUENCES"},
87
//                "RegistrationTerms");
88

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

    
91
       Assert.assertNotNull(workingset.getOwner());
92
       Assert.assertEquals(Registration.class, workingset.getOwner().getClass());
93

    
94
       workingset.getFieldUnit().setFieldNotes("FieldNotes");
95
       workingset.getFieldUnit().setFieldNumber("FieldNumber");
96
       workingset.getFieldUnit().getGatheringEvent().setLocality(LanguageString.NewInstance("Somewhere", Language.ENGLISH()));
97

    
98
       SpecimenTypeDesignationDTO specimenTypeDesignationDTO = new SpecimenTypeDesignationDTO();
99
       specimenTypeDesignationDTO.setKindOfUnit((DefinedTerm)cdmRepository.getTermService().load(KindOfUnitTerms.PUBLISHED_IMAGE().getUuid()));
100
       specimenTypeDesignationDTO.setMediaUri(new URI("http://foo.bar.com/image1"));
101
       specimenTypeDesignationDTO.setTypeStatus(SpecimenTypeDesignationStatus.HOLOTYPE());
102
       specimenTypeDesignationDTO.setAccessionNumber("TEST_1");
103

    
104
       workingset.getSpecimenTypeDesignationDTOs().add(specimenTypeDesignationDTO);
105

    
106
       service.save(workingset);
107

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

    
110
       workingset = service.loadDtoByIds(registrationId, 0);
111

    
112
       Assert.assertNotNull(specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen());
113
       Assert.assertEquals(MediaSpecimen.class, specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen().getClass());
114
       Assert.assertNotNull(specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen().getOriginals().iterator().next());
115
       Assert.assertEquals(FieldUnit.class, specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen().getOriginals().iterator().next().getClass());
116
       Assert.assertEquals("FieldNumber", ((FieldUnit)specimenTypeDesignationDTO.asSpecimenTypeDesignation().getTypeSpecimen().getOriginals().iterator().next()).getFieldNumber());
117

    
118
       SpecimenTypeDesignationDTO specimenTypeDesignationDTO2 = new SpecimenTypeDesignationDTO();
119
       specimenTypeDesignationDTO2.setKindOfUnit((DefinedTerm)cdmRepository.getTermService().load(KindOfUnitTerms.SPECIMEN().getUuid()));
120
       specimenTypeDesignationDTO2.setTypeStatus(SpecimenTypeDesignationStatus.HOLOTYPE());
121
       specimenTypeDesignationDTO2.setAccessionNumber("TEST_2");
122

    
123
       workingset.getSpecimenTypeDesignationDTOs().add(specimenTypeDesignationDTO2);
124

    
125
       service.save(workingset);
126

    
127
       workingset = service.loadDtoByIds(registrationId, 0);
128
       Assert.assertTrue(workingset.getSpecimenTypeDesignationDTOs().size() == 2);
129

    
130

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

    
133
       // write test data for delete test
134
       /*
135
       // printDataSetWithNull(System.err, includeTableNames_delete);
136
        writeDbUnitDataSetFile(includeTableNames_delete, "deleteTest");
137
        */
138

    
139
    }
140

    
141
    @Test
142
    @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest-deleteTest.xml")
143
    //@Ignore
144
    public void deleteTypeDesignationTest() {
145

    
146
        // printDataSetWithNull(System.err, includeTableNames_delete);
147

    
148
        SpecimenTypeDesignationWorkingSetDTO<Registration> workingset = service.loadDtoByIds(registrationId, 0);
149
        Assert.assertTrue(workingset.getSpecimenTypeDesignationDTOs().size() == 2);
150

    
151
        SpecimenTypeDesignationDTO deleteDTO = null;
152
        for(SpecimenTypeDesignationDTO stdDTO : workingset.getSpecimenTypeDesignationDTOs()){
153
            if(stdDTO.getAccessionNumber().equals("TEST_1")){
154
                deleteDTO = stdDTO;
155
                break;
156
            }
157
        }
158
        workingset.getSpecimenTypeDesignationDTOs().remove(deleteDTO);
159

    
160
        // TODO once https://dev.e-taxonomy.eu/redmine/issues/7077 is fixed dissociating from the Registration could be removed here
161
        Registration reg = workingset.getOwner();
162
        SpecimenTypeDesignation std = deleteDTO.asSpecimenTypeDesignation();
163
        reg.getTypeDesignations().remove(std);
164

    
165
        service.save(workingset);
166

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

    
169
        workingset = service.loadDtoByIds(registrationId, 0);
170
        Assert.assertEquals(1, workingset.getSpecimenTypeDesignationDTOs().size());
171

    
172
    }
173

    
174
    @Test
175
    @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest-deleteTest.xml")
176
    public void deleteWorkingsetTest() {
177

    
178
        printDataSetWithNull(System.err, includeTableNames_delete);
179

    
180
        SpecimenTypeDesignationWorkingSetDTO<Registration> workingset = service.loadDtoByIds(registrationId, 0);
181
        service.delete(workingset, true);
182

    
183
//        UUID gatheringEventUUID = UUID.fromString("23d40440-38bb-46c1-af11-6e25dcfa0145");
184
//        UUID fieldUnitUUID = UUID.fromString("22be718a-6f21-4b74-aae3-bb7d7d659e1c");
185
//        UUID mediaSpecimenUUID = UUID.fromString("10eceb2c-9b51-458e-8dcd-2cb92cc558a9");
186
//        UUID specimenUUID = UUID.fromString("2e384f8e-fbb0-44eb-9d5f-1b7235493932");
187
//        UUID typeDesignation1UUID = UUID.fromString("a1896ae2-4396-4243-988e-3d74058b44ab");
188
//        UUID typeDesignation2UUID = UUID.fromString("a1896ae2-4396-4243-988e-3d74058b44ab");
189

    
190

    
191
        Assert.assertEquals("All TypeDesignations should have been deleted", 0, cdmRepository.getNameService().getAllTypeDesignations(10, 0).size());
192
        Assert.assertEquals("All derived units should have been deleted", 0, cdmRepository.getOccurrenceService().count(DerivedUnit.class));
193
        Assert.assertEquals("FieldUnit should have been deleted", 0, cdmRepository.getOccurrenceService().count(FieldUnit.class));
194
        // FIXME Assert.assertEquals("Gathering event should have been deleted by orphan remove", 0, cdmRepository.getEventBaseService().count(GatheringEvent.class));
195
        // FIXME Assert.assertEquals("Media should have been deleted ", 0, cdmRepository.getMediaService().count(null));
196

    
197
        printDataSetWithNull(System.err, includeTableNames_delete);
198
    }
199

    
200
    // ---------------------- TestData -------------------------------------------
201

    
202

    
203
    @Override
204
    // @Test
205
    public void createTestDataSet() throws FileNotFoundException {
206
        createRegistration();
207
    }
208

    
209
    private void createRegistration() throws FileNotFoundException {
210

    
211
        Team team = Team.NewTitledInstance("Novis, Braidwood & Kilroy", "Novis, Braidwood & Kilroy");
212
        Reference nomRef = ReferenceFactory.newArticle();
213
        nomRef = cdmRepository.getReferenceService().save(nomRef);
214
        publicationId = nomRef.getId();
215

    
216
        nomRef.setAuthorship(team);
217
        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");
218
        TaxonName name = TaxonName.NewInstance(NomenclaturalCode.ICNAFP, Rank.SPECIES(), "Planothidium", null,  "victori", null, null, nomRef, "11-45", null);
219
        name = cdmRepository.getNameService().save(name);
220
        typifiedNameId = name.getId();
221

    
222
        Registration reg = Registration.NewInstance();
223
        reg.setName(name);
224
        reg = cdmRepository.getRegistrationService().save(reg);
225
        registrationId = reg.getId();
226

    
227

    
228
        printDataSetWithNull(System.err, includeTableNames_create);
229

    
230
        writeDbUnitDataSetFile(includeTableNames_create);
231
    }
232

    
233

    
234

    
235

    
236

    
237

    
238

    
239
}
    (1-1/1)