Project

General

Profile

Download (11 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.Ignore;
17
import org.junit.Test;
18
import org.unitils.dbunit.annotation.DataSet;
19
import org.unitils.spring.annotation.SpringBeanByName;
20
import org.unitils.spring.annotation.SpringBeanByType;
21

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

    
44
/**
45
 * @author a.kohlbecker
46
 * @since Nov 17, 2017
47
 *
48
 */
49
public class SpecimenTypeDesignationWorkingSetServiceImplTest extends CdmVaadinIntegrationTest{
50

    
51
    @SpringBeanByName
52
    private CdmRepository cdmRepository;
53

    
54
    @SpringBeanByType
55
    private ISpecimenTypeDesignationWorkingSetService service;
56

    
57
    int registrationId = 5000;
58

    
59
    private Integer publicationId = 5000;
60

    
61
    private Integer typifiedNameId = 5000;
62

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

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

    
74

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

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

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

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

    
89
       Assert.assertNotNull(workingset.getOwner());
90
       Assert.assertEquals(Registration.class, workingset.getOwner().getClass());
91

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

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

    
102
       workingset.getSpecimenTypeDesignationDTOs().add(specimenTypeDesignationDTO);
103

    
104
       service.save(workingset);
105

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

    
108
       workingset = service.loadDtoByIds(registrationId, 0);
109

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

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

    
121
       workingset.getSpecimenTypeDesignationDTOs().add(specimenTypeDesignationDTO2);
122

    
123
       service.save(workingset);
124

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

    
128

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

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

    
137
    }
138

    
139
    @Test
140
    @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest-deleteTest.xml")
141
    @Ignore
142
    public void deleteTypeDesignationTest() {
143

    
144
        // printDataSetWithNull(System.err, includeTableNames_delete);
145

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

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

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

    
163
        service.save(workingset);
164

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

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

    
170
    }
171

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

    
176
        printDataSetWithNull(System.err, includeTableNames_delete);
177

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

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

    
188

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

    
195
        printDataSetWithNull(System.err, includeTableNames_delete);
196
    }
197

    
198
    // ---------------------- TestData -------------------------------------------
199

    
200

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

    
207
    private void createRegistration() throws FileNotFoundException {
208

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

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

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

    
225

    
226
        printDataSetWithNull(System.err, includeTableNames_create);
227

    
228
        writeDbUnitDataSetFile(includeTableNames_create);
229
    }
230

    
231

    
232

    
233

    
234

    
235

    
236

    
237
}
    (1-1/1)