2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.service
;
11 import java
.io
.FileNotFoundException
;
12 import java
.net
.URISyntaxException
;
13 import java
.util
.UUID
;
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
;
27 import eu
.etaxonomy
.cdm
.CdmVaadinIntegrationTest
;
28 import eu
.etaxonomy
.cdm
.api
.application
.CdmRepository
;
29 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
30 import eu
.etaxonomy
.cdm
.api
.util
.DerivedUnitConversionException
;
31 import eu
.etaxonomy
.cdm
.common
.URI
;
32 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
33 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
34 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
35 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
36 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
37 import eu
.etaxonomy
.cdm
.model
.name
.Registration
;
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
.model
.term
.DefinedTerm
;
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
;
53 * @author a.kohlbecker
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
{
62 private CdmRepository cdmRepository
;
65 private ISpecimenTypeDesignationWorkingSetService service
;
68 private IOccurrenceService occurrenceService
;
71 public static void setupLoggers() {
72 // Logger.getLogger("org.dbunit").setLevel(Level.DEBUG);
75 UUID registrationUuid
= UUID
.fromString("c8bb4e70-ca85-43c3-ae81-c90a2b41a93f"); // 5000;
77 private UUID publicationUuid
= UUID
.fromString("45804c65-7df9-42fd-b43a-818a8958c264"); // 5000;
79 private UUID typifiedNameUuid
= UUID
.fromString("47d9263e-b32a-42af-98ea-5528f154384f"); // 5000;
81 private UUID fieldUnitUuid
= UUID
.fromString("22be718a-6f21-4b74-aae3-bb7d7d659e1c"); // 5001
83 private final String
[] includeTableNames_create
= new String
[]{"TAXONNAME", "REFERENCE", "AGENTBASE", "HOMOTYPICALGROUP", "REGISTRATION",
84 "HIBERNATE_SEQUENCES"};
86 @SuppressWarnings("unused")
87 private final String
[] includeTableNames_delete
= new String
[]{"TAXONNAME", "REFERENCE", "AGENTBASE", "HOMOTYPICALGROUP", "REGISTRATION",
88 "DERIVATIONEVENT", "GATHERINGEVENT", "LANGUAGESTRING", "SPECIMENOROBSERVATIONBASE", "TYPEDESIGNATIONBASE",
89 "REGISTRATION_TYPEDESIGNATIONBASE", "TAXONNAME_TYPEDESIGNATIONBASE", "SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT",
90 "MEDIA", "MEDIA_REPRESENTATION", "MEDIAREPRESENTATION", "MEDIAREPRESENTATIONPART",
97 @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest.xml")
98 public void test01_createAndEditTest() throws DerivedUnitConversionException
, URISyntaxException
, FileNotFoundException
{
100 // printDataSet(System.err, new String[]{"USERACCOUNT", "GROUPS", "USERACCOUNT_GRANTEDAUTHORITYIMPL", "USERACCOUNT_PERMISSIONGROUP"
101 // , "PERMISSIONGROUP", "PERMISSIONGROUP_GRANTEDAUTHORITYIMPL", "GRANTEDAUTHORITYIMPL"});
103 // printDataSet(System.err, debugTables);
104 // writeDbUnitDataSetFile(new String[]{"AUDITEVENT", "DEFINEDTERMBASE", "DEFINEDTERMBASE_AUD", "DEFINEDTERMBASE_REPRESENTATION", "DEFINEDTERMBASE_REPRESENTATION_AUD",
105 // "REPRESENTATION", "REPRESENTATION_AUD", "HIBERNATE_SEQUENCES"},
106 // "RegistrationTerms");
108 SpecimenTypeDesignationWorkingSetDTO
<Registration
> workingset
= service
.create(registrationUuid
, typifiedNameUuid
);
110 Assert
.assertNotNull(workingset
.getOwner());
111 Assert
.assertEquals(Registration
.class, workingset
.getOwner().getClass());
113 workingset
.getFieldUnit().setFieldNotes("FieldNotes");
114 // int baseEntityID = workingset.getFieldUnit().getId();
115 workingset
.getFieldUnit().setFieldNumber("FieldNumber");
116 workingset
.getFieldUnit().getGatheringEvent().setLocality(LanguageString
.NewInstance("Somewhere", Language
.ENGLISH()));
118 SpecimenTypeDesignationDTO specimenTypeDesignationDTO
= new SpecimenTypeDesignationDTO();
119 specimenTypeDesignationDTO
.setKindOfUnit((DefinedTerm
)cdmRepository
.getTermService().load(KindOfUnitTerms
.PUBLISHED_IMAGE().getUuid()));
120 specimenTypeDesignationDTO
.setMediaUri(new URI("http://foo.bar.com/image1"));
121 specimenTypeDesignationDTO
.setTypeStatus(SpecimenTypeDesignationStatus
.HOLOTYPE());
122 specimenTypeDesignationDTO
.setAccessionNumber("TEST_1");
124 workingset
.getSpecimenTypeDesignationDTOs().add(specimenTypeDesignationDTO
);
126 service
.save(workingset
);
128 SpecimenOrObservationBase
<?
> baseEntity
= cdmRepository
.getOccurrenceService().load(workingset
.getFieldUnit().getUuid());
130 // printDataSet(System.err, new String[]{"TYPEDESIGNATIONBASE", "SPECIMENOROBSERVATIONBASE"});
132 workingset
= service
.load(registrationUuid
, baseEntity
);
134 Assert
.assertNotNull(specimenTypeDesignationDTO
.asSpecimenTypeDesignation().getTypeSpecimen());
135 Assert
.assertEquals(MediaSpecimen
.class, specimenTypeDesignationDTO
.asSpecimenTypeDesignation().getTypeSpecimen().getClass());
136 Assert
.assertNotNull(specimenTypeDesignationDTO
.asSpecimenTypeDesignation().getTypeSpecimen().getOriginals().iterator().next());
137 Assert
.assertEquals(FieldUnit
.class, specimenTypeDesignationDTO
.asSpecimenTypeDesignation().getTypeSpecimen().getOriginals().iterator().next().getClass());
138 Assert
.assertEquals("FieldNumber", ((FieldUnit
)specimenTypeDesignationDTO
.asSpecimenTypeDesignation().getTypeSpecimen().getOriginals().iterator().next()).getFieldNumber());
140 SpecimenTypeDesignationDTO specimenTypeDesignationDTO2
= new SpecimenTypeDesignationDTO();
141 specimenTypeDesignationDTO2
.setKindOfUnit((DefinedTerm
)cdmRepository
.getTermService().load(KindOfUnitTerms
.SPECIMEN().getUuid()));
142 specimenTypeDesignationDTO2
.setTypeStatus(SpecimenTypeDesignationStatus
.HOLOTYPE());
143 specimenTypeDesignationDTO2
.setAccessionNumber("TEST_2");
145 workingset
.getSpecimenTypeDesignationDTOs().add(specimenTypeDesignationDTO2
);
147 service
.save(workingset
);
149 workingset
= service
.load(registrationUuid
, baseEntity
);
150 Assert
.assertTrue(workingset
.getSpecimenTypeDesignationDTOs().size() == 2);
152 //FIXME this fails: Assert.assertEquals("There must only be one FieldUnit and one MediaSpecimen", 2, cdmRepository.getOccurrenceService().count(DerivedUnit.class));
154 // write test data for delete test
156 // printDataSet(System.err, includeTableNames_delete);
157 writeDbUnitDataSetFile(includeTableNames_delete, "deleteTest");
159 /* The following audit table fix needs also to be added to the test data:
160 <!-- Test data is being used by more than one test - need to reset a couple of *_AUD tables -->
163 <DERIVATIONEVENT_AUD />
164 <GATHERINGEVENT_AUD />
165 <LANGUAGESTRING_AUD />
166 <TYPEDESIGNATIONBASE_AUD />
167 <SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT_AUD />
169 <SPECIMENOROBSERVATIONBASE_AUD />
170 <TAXONNAME_TYPEDESIGNATIONBASE_AUD />
176 @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest-deleteTest.xml")
177 @ExpectedDataSet("SpecimenTypeDesignationWorkingSetServiceImplTest.deleteTypeDesignationTest-result.xml")
178 public void test03_deleteTypeDesignationTest() {
180 FieldUnit baseEntity
= (FieldUnit
)occurrenceService
.load(fieldUnitUuid
);
182 // FieldUnit" ID="5001
183 // TypedEntityReference<FieldUnit> baseEntityRef = new TypedEntityReference<>(FieldUnit.class, fieldUnitUuid, "Somewhere, FieldNumber.");
184 SpecimenTypeDesignationWorkingSetDTO
<Registration
> workingset
= service
.load(registrationUuid
, baseEntity
);
185 Assert
.assertTrue(workingset
.getSpecimenTypeDesignationDTOs().size() == 2);
187 SpecimenTypeDesignationDTO deleteDTO
= null;
188 for(SpecimenTypeDesignationDTO stdDTO
: workingset
.getSpecimenTypeDesignationDTOs()){
189 if(stdDTO
.getAccessionNumber().equals("TEST_1")){
194 workingset
.getSpecimenTypeDesignationDTOs().remove(deleteDTO
);
196 service
.save(workingset
);
198 // printDataSet(System.err, includeTableNames_delete);
200 workingset
= service
.load(registrationUuid
, baseEntity
);
201 Registration reg
= workingset
.getOwner();
202 Assert
.assertEquals(1, workingset
.getSpecimenTypeDesignationDTOs().size());
203 reg
= workingset
.getOwner();
204 Assert
.assertEquals(1, reg
.getTypeDesignations().size());
208 @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest-deleteTest.xml")
209 public void test02_deleteWorkingset() {
211 // printDataSet(System.err, includeTableNames_delete);
213 FieldUnit baseEntity
= (FieldUnit
)occurrenceService
.load(fieldUnitUuid
);
215 // TypedEntityReference<FieldUnit> baseEntityRef = new TypedEntityReference<>(FieldUnit.class, fieldUnitUuid, null);
217 SpecimenTypeDesignationWorkingSetDTO
<Registration
> workingset
= service
.load(registrationUuid
, baseEntity
);
218 Assert
.assertNotNull(workingset
.getOwner());
219 Assert
.assertEquals(2, workingset
.getSpecimenTypeDesignationDTOs().size());
220 service
.delete(workingset
, true);
222 // UUID gatheringEventUUID = UUID.fromString("23d40440-38bb-46c1-af11-6e25dcfa0145");
223 // UUID fieldUnitUUID = UUID.fromString("22be718a-6f21-4b74-aae3-bb7d7d659e1c");
224 // UUID mediaSpecimenUUID = UUID.fromString("10eceb2c-9b51-458e-8dcd-2cb92cc558a9");
225 // UUID specimenUUID = UUID.fromString("2e384f8e-fbb0-44eb-9d5f-1b7235493932");
226 // UUID typeDesignation1UUID = UUID.fromString("a1896ae2-4396-4243-988e-3d74058b44ab");
227 // UUID typeDesignation2UUID = UUID.fromString("a1896ae2-4396-4243-988e-3d74058b44ab");
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));
235 // printDataSet(System.err, includeTableNames_delete);
238 // ---------------------- TestData -------------------------------------------
243 public void createTestDataSet() throws FileNotFoundException
{
244 createRegistration();
247 private void createRegistration() throws FileNotFoundException
{
249 Team team
= Team
.NewTitledInstance("Novis, Braidwood & Kilroy", "Novis, Braidwood & Kilroy");
250 Reference nomRef
= ReferenceFactory
.newArticle();
251 nomRef
.setUuid(publicationUuid
);
252 nomRef
= cdmRepository
.getReferenceService().save(nomRef
);
254 nomRef
.setAuthorship(team
);
255 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");
256 TaxonName name
= TaxonName
.NewInstance(NomenclaturalCode
.ICNAFP
, Rank
.SPECIES(), "Planothidium", null, "victori", null, null, nomRef
, "11-45", null);
257 name
.setUuid(typifiedNameUuid
);
258 name
= cdmRepository
.getNameService().save(name
);
260 Registration reg
= Registration
.NewInstance();
262 reg
.setUuid(registrationUuid
);
263 reg
= cdmRepository
.getRegistrationService().save(reg
);
265 //printDataSet(System.err, includeTableNames_create);
267 writeDbUnitDataSetFile(includeTableNames_create
);