ref #10089 remove TypedEntityReference from TypeDesignationWorkingSet in vaadin
[cdm-vaadin.git] / src / test / java / eu / etaxonomy / cdm / service / SpecimenTypeDesignationWorkingSetServiceImplTest.java
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.URISyntaxException;
13 import java.util.UUID;
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.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;
51
52 /**
53 * @author a.kohlbecker
54 * @since Nov 17, 2017
55 */
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{
60
61 @SpringBeanByName
62 private CdmRepository cdmRepository;
63
64 @SpringBeanByType
65 private ISpecimenTypeDesignationWorkingSetService service;
66
67 @SpringBeanByType
68 private IOccurrenceService occurrenceService;
69
70 @BeforeClass
71 public static void setupLoggers() {
72 // Logger.getLogger("org.dbunit").setLevel(Level.DEBUG);
73 }
74
75 UUID registrationUuid = UUID.fromString("c8bb4e70-ca85-43c3-ae81-c90a2b41a93f"); // 5000;
76
77 private UUID publicationUuid = UUID.fromString("45804c65-7df9-42fd-b43a-818a8958c264"); // 5000;
78
79 private UUID typifiedNameUuid = UUID.fromString("47d9263e-b32a-42af-98ea-5528f154384f"); // 5000;
80
81 private UUID fieldUnitUuid = UUID.fromString("22be718a-6f21-4b74-aae3-bb7d7d659e1c"); // 5001
82
83 private final String[] includeTableNames_create = new String[]{"TAXONNAME", "REFERENCE", "AGENTBASE", "HOMOTYPICALGROUP", "REGISTRATION",
84 "HIBERNATE_SEQUENCES"};
85
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",
91 "ORIGINALSOURCEBASE",
92 "AUDITEVENT",
93 "HIBERNATE_SEQUENCES"
94 };
95
96 @Test
97 @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest.xml")
98 public void test01_createAndEditTest() throws DerivedUnitConversionException, URISyntaxException, FileNotFoundException {
99
100 // printDataSet(System.err, new String[]{"USERACCOUNT", "GROUPS", "USERACCOUNT_GRANTEDAUTHORITYIMPL", "USERACCOUNT_PERMISSIONGROUP"
101 // , "PERMISSIONGROUP", "PERMISSIONGROUP_GRANTEDAUTHORITYIMPL", "GRANTEDAUTHORITYIMPL"});
102
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");
107
108 SpecimenTypeDesignationWorkingSetDTO<Registration> workingset = service.create(registrationUuid, typifiedNameUuid);
109
110 Assert.assertNotNull(workingset.getOwner());
111 Assert.assertEquals(Registration.class, workingset.getOwner().getClass());
112
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()));
117
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");
123
124 workingset.getSpecimenTypeDesignationDTOs().add(specimenTypeDesignationDTO);
125
126 service.save(workingset);
127
128 SpecimenOrObservationBase<?> baseEntity = cdmRepository.getOccurrenceService().load(workingset.getFieldUnit().getUuid());
129
130 // printDataSet(System.err, new String[]{"TYPEDESIGNATIONBASE", "SPECIMENOROBSERVATIONBASE"});
131
132 workingset = service.load(registrationUuid, baseEntity);
133
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());
139
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");
144
145 workingset.getSpecimenTypeDesignationDTOs().add(specimenTypeDesignationDTO2);
146
147 service.save(workingset);
148
149 workingset = service.load(registrationUuid, baseEntity);
150 Assert.assertTrue(workingset.getSpecimenTypeDesignationDTOs().size() == 2);
151
152 //FIXME this fails: Assert.assertEquals("There must only be one FieldUnit and one MediaSpecimen", 2, cdmRepository.getOccurrenceService().count(DerivedUnit.class));
153
154 // write test data for delete test
155 /*
156 // printDataSet(System.err, includeTableNames_delete);
157 writeDbUnitDataSetFile(includeTableNames_delete, "deleteTest");
158 */
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 -->
161 <AUDITEVENT />
162 <TAXONNAME_AUD />
163 <DERIVATIONEVENT_AUD />
164 <GATHERINGEVENT_AUD />
165 <LANGUAGESTRING_AUD />
166 <TYPEDESIGNATIONBASE_AUD />
167 <SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT_AUD />
168 <REGISTRATION_AUD />
169 <SPECIMENOROBSERVATIONBASE_AUD />
170 <TAXONNAME_TYPEDESIGNATIONBASE_AUD />
171 */
172
173 }
174
175 @Test
176 @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest-deleteTest.xml")
177 @ExpectedDataSet("SpecimenTypeDesignationWorkingSetServiceImplTest.deleteTypeDesignationTest-result.xml")
178 public void test03_deleteTypeDesignationTest() {
179
180 FieldUnit baseEntity = (FieldUnit)occurrenceService.load(fieldUnitUuid);
181
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);
186
187 SpecimenTypeDesignationDTO deleteDTO = null;
188 for(SpecimenTypeDesignationDTO stdDTO : workingset.getSpecimenTypeDesignationDTOs()){
189 if(stdDTO.getAccessionNumber().equals("TEST_1")){
190 deleteDTO = stdDTO;
191 break;
192 }
193 }
194 workingset.getSpecimenTypeDesignationDTOs().remove(deleteDTO);
195
196 service.save(workingset);
197
198 // printDataSet(System.err, includeTableNames_delete);
199
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());
205 }
206
207 @Test
208 @DataSet("SpecimenTypeDesignationWorkingSetServiceImplTest-deleteTest.xml")
209 public void test02_deleteWorkingset() {
210
211 // printDataSet(System.err, includeTableNames_delete);
212
213 FieldUnit baseEntity = (FieldUnit)occurrenceService.load(fieldUnitUuid);
214
215 // TypedEntityReference<FieldUnit> baseEntityRef = new TypedEntityReference<>(FieldUnit.class, fieldUnitUuid, null);
216
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);
221
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");
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
235 // printDataSet(System.err, includeTableNames_delete);
236 }
237
238 // ---------------------- TestData -------------------------------------------
239
240
241 @Override
242 // @Test
243 public void createTestDataSet() throws FileNotFoundException {
244 createRegistration();
245 }
246
247 private void createRegistration() throws FileNotFoundException {
248
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);
253
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);
259
260 Registration reg = Registration.NewInstance();
261 reg.setName(name);
262 reg.setUuid(registrationUuid);
263 reg = cdmRepository.getRegistrationService().save(reg);
264
265 //printDataSet(System.err, includeTableNames_create);
266
267 writeDbUnitDataSetFile(includeTableNames_create);
268 }
269 }