1
|
/**
|
2
|
* Copyright (C) 2019 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.api.service;
|
10
|
|
11
|
import static org.junit.Assert.assertEquals;
|
12
|
import static org.junit.Assert.assertNotNull;
|
13
|
import static org.junit.Assert.assertNull;
|
14
|
import static org.junit.Assert.assertTrue;
|
15
|
|
16
|
import java.io.FileNotFoundException;
|
17
|
import java.util.Arrays;
|
18
|
import java.util.Collection;
|
19
|
import java.util.HashSet;
|
20
|
import java.util.List;
|
21
|
import java.util.UUID;
|
22
|
|
23
|
import org.junit.Test;
|
24
|
import org.springframework.beans.factory.annotation.Qualifier;
|
25
|
import org.unitils.dbunit.annotation.DataSet;
|
26
|
import org.unitils.spring.annotation.SpringBeanByType;
|
27
|
|
28
|
import eu.etaxonomy.cdm.api.application.ICdmRepository;
|
29
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
30
|
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
|
31
|
import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
|
32
|
import eu.etaxonomy.cdm.model.name.Rank;
|
33
|
import eu.etaxonomy.cdm.model.name.Registration;
|
34
|
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
|
35
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
|
36
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
|
37
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
38
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
39
|
import eu.etaxonomy.cdm.model.permission.User;
|
40
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
41
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
42
|
import eu.etaxonomy.cdm.persistence.permission.Role;
|
43
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
44
|
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
|
45
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTestWithSecurity;
|
46
|
|
47
|
/**
|
48
|
* @author a.kohlbecker
|
49
|
* @since Jun 28, 2019
|
50
|
*
|
51
|
*/
|
52
|
public class RegistrationServiceTest extends CdmTransactionalIntegrationTestWithSecurity {
|
53
|
|
54
|
public static final UUID USER2_UUID = UUID.fromString("669f582c-e97f-425b-97f6-bc3b0c08f2a5");
|
55
|
|
56
|
public static final UUID USER1_UUID = UUID.fromString("68033f81-9947-4b61-b33b-3d05bd438579");
|
57
|
|
58
|
public static final UUID NDT1_UUID = UUID.fromString("be66964a-ea2b-480e-9dcf-0ee1dd7313eb");
|
59
|
|
60
|
public static final UUID STD2_UUID = UUID.fromString("8cd056fb-259a-45aa-ab4f-b34033eef2e9");
|
61
|
|
62
|
public static final UUID STD1_UUID = UUID.fromString("1c29e80a-2611-4be4-9b2f-15bbd15066bf");
|
63
|
|
64
|
public static final UUID NAME1_UUID = UUID.fromString("6ffa3217-f520-43d9-99d7-755b8c11d049");
|
65
|
|
66
|
public static final UUID NAME2_UUID = UUID.fromString("c739163e-c1fa-4ea6-b992-78fe050a6d2c");
|
67
|
|
68
|
public static final UUID NAME3_UUID = UUID.fromString("fdcaf5a8-aa6d-4f5c-86e5-6e3d93dc0c72");
|
69
|
|
70
|
@SpringBeanByType
|
71
|
@Qualifier("CdmRepository")
|
72
|
private ICdmRepository repo;
|
73
|
|
74
|
@Test
|
75
|
@DataSet
|
76
|
public void testPageWithPatternFilters(){
|
77
|
|
78
|
OrderHint orderBySpecificIdentifier = new OrderHint("specificIdentifier", SortOrder.ASCENDING);
|
79
|
OrderHint orderById = new OrderHint("id", SortOrder.ASCENDING);
|
80
|
|
81
|
repo.authenticate("user1", "00000");
|
82
|
|
83
|
Pager<Registration> pager;
|
84
|
|
85
|
pager = repo.getRegistrationService().page((UUID)null, null, null, null, null, null, null, null, null, null);
|
86
|
assertEquals(pager.getRecords().size(), pager.getCount().intValue());
|
87
|
assertEquals("with authenticated user expecting all 4 Registrations", 4l, pager.getCount().longValue());
|
88
|
|
89
|
pager = repo.getRegistrationService().page(USER1_UUID, null, null, null, null, null, null, null, null, null);
|
90
|
assertEquals(2l, pager.getCount().longValue());
|
91
|
|
92
|
pager = repo.getRegistrationService().page(USER2_UUID, null, null, null, null, null, null, null, null, null);
|
93
|
assertEquals(2l, pager.getCount().longValue());
|
94
|
|
95
|
// status filter
|
96
|
pager = repo.getRegistrationService().page((UUID)null, Arrays.asList(RegistrationStatus.PREPARATION), null, null, null, null, null, null, null, null);
|
97
|
assertEquals(1l, pager.getCount().longValue());
|
98
|
assertEquals(RegistrationStatus.PREPARATION, pager.getRecords().get(0).getStatus());
|
99
|
|
100
|
pager = repo.getRegistrationService().page((UUID)null, Arrays.asList(RegistrationStatus.PREPARATION, RegistrationStatus.PUBLISHED), null, null, null, null, null, null, null, null);
|
101
|
assertEquals(2l, pager.getCount().longValue());
|
102
|
|
103
|
// status filter with submitter
|
104
|
pager = repo.getRegistrationService().page(USER1_UUID, Arrays.asList(RegistrationStatus.PREPARATION, RegistrationStatus.PUBLISHED), null, null, null, null, null, null, null, null);
|
105
|
assertEquals(2l, pager.getCount().longValue());
|
106
|
|
107
|
pager = repo.getRegistrationService().page(USER2_UUID, Arrays.asList(RegistrationStatus.PREPARATION, RegistrationStatus.PUBLISHED), null, null, null, null, null, null, null, null);
|
108
|
assertEquals(0l, pager.getCount().longValue());
|
109
|
|
110
|
pager = repo.getRegistrationService().page(USER2_UUID, Arrays.asList(RegistrationStatus.CURATION), null, null, null, null, null, null, null, null);
|
111
|
assertEquals(1l, pager.getCount().longValue());
|
112
|
|
113
|
// identifier filter
|
114
|
pager = repo.getRegistrationService().page((UUID)null, null, "100", null, null, null, null, null, null, null);
|
115
|
assertEquals(4l, pager.getCount().longValue());
|
116
|
|
117
|
pager = repo.getRegistrationService().page((UUID)null, null, "test/1001", null, null, null, null, null, null, null);
|
118
|
assertEquals(1l, pager.getCount().longValue());
|
119
|
|
120
|
// identifier filter with submitter
|
121
|
pager = repo.getRegistrationService().page(USER1_UUID, null, "100", null, null, null, null, null, null, null);
|
122
|
assertEquals(2l, pager.getCount().longValue());
|
123
|
|
124
|
pager = repo.getRegistrationService().page(USER2_UUID, null, "1002", null, null, null, null, null, null, null);
|
125
|
assertEquals(1l, pager.getCount().longValue());
|
126
|
|
127
|
// taxon name filter
|
128
|
pager = repo.getRegistrationService().page((UUID)null, null, null, "Digitalus", null, null, null, null, null, null);
|
129
|
assertEquals(4l, pager.getCount().longValue());
|
130
|
|
131
|
pager = repo.getRegistrationService().page((UUID)null, null, null, "Dig*lus", null, null, null, null, null, null);
|
132
|
assertEquals(4l, pager.getCount().longValue());
|
133
|
|
134
|
pager = repo.getRegistrationService().page((UUID)null, null, null, "Digitalus prim", null, null, null, null, null, null);
|
135
|
assertEquals(1l, pager.getCount().longValue());
|
136
|
|
137
|
pager = repo.getRegistrationService().page((UUID)null, null, null, "Digitalus secundus", null, null, null, null, Arrays.asList(orderBySpecificIdentifier), null);
|
138
|
assertEquals(2l, pager.getCount().longValue());
|
139
|
|
140
|
assertEquals("test/1001", pager.getRecords().get(0).getIdentifier());
|
141
|
assertNotNull("expecting registration test/1001 to be with name", pager.getRecords().get(0).getName());
|
142
|
assertTrue("expecting registration test/1001 to be witout type designation", pager.getRecords().get(0).getTypeDesignations().isEmpty());
|
143
|
|
144
|
assertEquals("test/1003", pager.getRecords().get(1).getIdentifier());
|
145
|
assertNull("Expecting the registration test/1003 to be without name", pager.getRecords().get(1).getName());
|
146
|
assertEquals(STD2_UUID, pager.getRecords().get(1).getTypeDesignations().iterator().next().getUuid());
|
147
|
|
148
|
pager = repo.getRegistrationService().page((UUID)null, null, null, "Digita*", null, null, null, null, null, null);
|
149
|
assertEquals(4l, pager.getCount().longValue());
|
150
|
|
151
|
pager = repo.getRegistrationService().page((UUID)null, null, null, "*imus", null, null, null, null, null, null);
|
152
|
assertEquals(1l, pager.getCount().longValue());
|
153
|
|
154
|
// taxon name filter with user
|
155
|
pager = repo.getRegistrationService().page(USER2_UUID, null, null, "Digitalus", null, null, null, null, null, null);
|
156
|
assertEquals(2l, pager.getCount().longValue());
|
157
|
|
158
|
// taxon name filter with user and status
|
159
|
pager = repo.getRegistrationService().page(USER1_UUID, Arrays.asList(RegistrationStatus.PREPARATION), null, "Digitalus", null, null, null, null, null, null);
|
160
|
assertEquals(1l, pager.getCount().longValue());
|
161
|
|
162
|
pager = repo.getRegistrationService().page(USER1_UUID, Arrays.asList(RegistrationStatus.PREPARATION, RegistrationStatus.PUBLISHED), "1001", "Digitalus", null, null, null, null, null, null);
|
163
|
assertEquals(1l, pager.getCount().longValue());
|
164
|
|
165
|
// type designation status
|
166
|
|
167
|
// assure the terms are loaded
|
168
|
assertNotNull(SpecimenTypeDesignationStatus.HOLOTYPE());
|
169
|
assertNotNull(NameTypeDesignationStatus.TAUTONYMY());
|
170
|
|
171
|
pager = repo.getRegistrationService().page((UUID)null, null, null, null, null,
|
172
|
Arrays.asList(SpecimenTypeDesignationStatus.HOLOTYPE().getUuid()), null, null, null, null);
|
173
|
assertEquals(1l, pager.getCount().longValue());
|
174
|
assertEquals(STD1_UUID, pager.getRecords().get(0).getTypeDesignations().iterator().next().getUuid());
|
175
|
|
176
|
pager = repo.getRegistrationService().page((UUID)null, null, null, null, null,
|
177
|
Arrays.asList(NameTypeDesignationStatus.TAUTONYMY().getUuid()), null, null, Arrays.asList(orderBySpecificIdentifier), null);
|
178
|
assertEquals(1l, pager.getCount().longValue());
|
179
|
assertEquals(NDT1_UUID, pager.getRecords().get(0).getTypeDesignations().iterator().next().getUuid());
|
180
|
|
181
|
pager = repo.getRegistrationService().page((UUID)null, null, null, null, null,
|
182
|
Arrays.asList(SpecimenTypeDesignationStatus.HOLOTYPE().getUuid(), NameTypeDesignationStatus.TAUTONYMY().getUuid()), null, null, Arrays.asList(orderBySpecificIdentifier), null);
|
183
|
assertEquals(2l, pager.getCount().longValue());
|
184
|
assertEquals("test/1000", pager.getRecords().get(0).getIdentifier());
|
185
|
assertEquals(STD1_UUID, pager.getRecords().get(0).getTypeDesignations().iterator().next().getUuid());
|
186
|
assertEquals("test/1002", pager.getRecords().get(1).getIdentifier());
|
187
|
assertEquals(NDT1_UUID, pager.getRecords().get(1).getTypeDesignations().iterator().next().getUuid());
|
188
|
|
189
|
// type designation status with user
|
190
|
pager = repo.getRegistrationService().page(USER2_UUID, null, null, null, null,
|
191
|
Arrays.asList(SpecimenTypeDesignationStatus.HOLOTYPE().getUuid(), NameTypeDesignationStatus.TAUTONYMY().getUuid()), null, null, null, null);
|
192
|
assertEquals(1l, pager.getCount().longValue());
|
193
|
assertEquals(NDT1_UUID, pager.getRecords().get(0).getTypeDesignations().iterator().next().getUuid());
|
194
|
|
195
|
// type designation status with name
|
196
|
pager = repo.getRegistrationService().page((UUID)null, null, null, "Digital",
|
197
|
null,
|
198
|
Arrays.asList(SpecimenTypeDesignationStatus.HOLOTYPE().getUuid(), NameTypeDesignationStatus.TAUTONYMY().getUuid()), null, null, Arrays.asList(orderBySpecificIdentifier), null);
|
199
|
assertEquals(2l, pager.getCount().longValue());
|
200
|
assertEquals(STD1_UUID, pager.getRecords().get(0).getTypeDesignations().iterator().next().getUuid());
|
201
|
assertEquals(NDT1_UUID, pager.getRecords().get(1).getTypeDesignations().iterator().next().getUuid());
|
202
|
|
203
|
// reference
|
204
|
pager = repo.getRegistrationService().page((UUID)null, null, null, null,
|
205
|
"book1", null, null, null, Arrays.asList(orderBySpecificIdentifier), null);
|
206
|
assertEquals(2l, pager.getCount().longValue());
|
207
|
|
208
|
pager = repo.getRegistrationService().page((UUID)null, null, null, null,
|
209
|
"book*", null, null, null, Arrays.asList(orderBySpecificIdentifier), null);
|
210
|
assertEquals(4l, pager.getCount().longValue());
|
211
|
}
|
212
|
|
213
|
@Test
|
214
|
@DataSet
|
215
|
public void testPageByNameUUID(){
|
216
|
|
217
|
OrderHint orderBySpecificIdentifier = new OrderHint("specificIdentifier", SortOrder.ASCENDING);
|
218
|
OrderHint orderById = new OrderHint("id", SortOrder.ASCENDING);
|
219
|
|
220
|
repo.authenticate("user1", "00000");
|
221
|
|
222
|
Pager<Registration> pager;
|
223
|
|
224
|
List<UUID> nameUuids = Arrays.asList(NAME2_UUID);
|
225
|
|
226
|
pager = repo.getRegistrationService().page((UUID)null, null, nameUuids, null, null, Arrays.asList(orderBySpecificIdentifier), null);
|
227
|
assertEquals(2l, pager.getCount().longValue());
|
228
|
assertEquals("test/1001", pager.getRecords().get(0).getIdentifier());
|
229
|
assertEquals("test/1003", pager.getRecords().get(1).getIdentifier());
|
230
|
}
|
231
|
|
232
|
@Test
|
233
|
public void testPageByNameUUID_unautheticated(){
|
234
|
Collection<UUID> nameUuids = null;
|
235
|
Pager<Registration> pager = repo.getRegistrationService().page((UUID)null, null, nameUuids , null, null, null, null);
|
236
|
assertEquals(pager.getRecords().size(), pager.getCount().intValue());
|
237
|
assertEquals("expecting no Registration, since the user is not authenticated", 0l, pager.getCount().longValue());
|
238
|
}
|
239
|
|
240
|
|
241
|
@Test
|
242
|
public void testPageWithPatternFilters_unautheticated(){
|
243
|
Pager<Registration> pager = repo.getRegistrationService().page((UUID)null, null, null, null, null, null, null, null, null, null);
|
244
|
assertEquals(pager.getRecords().size(), pager.getCount().intValue());
|
245
|
assertEquals("expecting only the PUBLISHED Registration, since the user is not authenticated", 1l, pager.getCount().longValue());
|
246
|
}
|
247
|
|
248
|
@Override
|
249
|
// @Test
|
250
|
public void createTestDataSet() throws FileNotFoundException {
|
251
|
|
252
|
User user1 = User.NewInstance("user1", "00000");
|
253
|
User user2 = User.NewInstance("user2", "00000");
|
254
|
// granted authorities are irrelevant for the test but are required to allow creating the test data
|
255
|
user1.getAuthorities().add(Role.ROLE_ADMIN);
|
256
|
user2.getAuthorities().add(Role.ROLE_ADMIN);
|
257
|
user1.setUuid(USER1_UUID);
|
258
|
user2.setUuid(USER2_UUID);
|
259
|
repo.getUserService().save(user1);
|
260
|
repo.getUserService().save(user2);
|
261
|
|
262
|
Reference book1 = ReferenceFactory.newBook();
|
263
|
book1.setTitle("book1");
|
264
|
Reference book2 = ReferenceFactory.newBook();
|
265
|
book2.setTitle("book2");
|
266
|
repo.getReferenceService().save(book1);
|
267
|
repo.getReferenceService().save(book2);
|
268
|
|
269
|
TaxonName species1 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES(), "Digitalus", null, "primus", null, null, book1, "11", null);
|
270
|
species1.setUuid(NAME1_UUID);
|
271
|
TaxonName species2 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES(), "Digitalus", null, "secundus", null, null, book2, "22", null);
|
272
|
species2.setUuid(NAME2_UUID);
|
273
|
TaxonName genus = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS(), "Digitalus", null, null, null, null, book1, "33", null);
|
274
|
genus.setUuid(NAME2_UUID);
|
275
|
repo.getNameService().save(Arrays.asList(species1, species2, genus));
|
276
|
|
277
|
SpecimenTypeDesignation std1 = SpecimenTypeDesignation.NewInstance();
|
278
|
std1.setCitation(book1);
|
279
|
std1.setUuid(STD1_UUID);
|
280
|
std1.setTypeStatus(SpecimenTypeDesignationStatus.HOLOTYPE());
|
281
|
|
282
|
SpecimenTypeDesignation std2 = SpecimenTypeDesignation.NewInstance();
|
283
|
std2.setCitation(book2);
|
284
|
std2.setUuid(STD2_UUID);
|
285
|
std2.setTypeStatus(SpecimenTypeDesignationStatus.EPITYPE());
|
286
|
|
287
|
NameTypeDesignation ntd1 = NameTypeDesignation.NewInstance();
|
288
|
ntd1.setCitation(book1);
|
289
|
ntd1.setTypeName(species1);
|
290
|
ntd1.setUuid(NDT1_UUID);
|
291
|
ntd1.setTypeStatus(NameTypeDesignationStatus.TAUTONYMY());
|
292
|
|
293
|
genus.addTypeDesignation(ntd1, false);
|
294
|
species1.addTypeDesignation(std1, false);
|
295
|
species2.addTypeDesignation(std2, false);
|
296
|
|
297
|
repo.getNameService().saveOrUpdate(Arrays.asList(species1, species2, genus));
|
298
|
|
299
|
repo.authenticate("user1", "00000");
|
300
|
Registration reg1 = Registration.NewInstance("test/1000", "1000", species1, new HashSet(Arrays.asList(std1))); // --> book1
|
301
|
reg1.setStatus(RegistrationStatus.PUBLISHED);
|
302
|
// the authenticated user will be set as submitter in new Registrations
|
303
|
// see RegistrationServiceImpl.prepareForSave(Registration reg)
|
304
|
repo.getRegistrationService().save(reg1);
|
305
|
|
306
|
// the authenticated user will be set as submitter in new Registrations
|
307
|
// see RegistrationServiceImpl.prepareForSave(Registration reg)
|
308
|
Registration reg2 = Registration.NewInstance("test/1001", "1001", species2, null); // --> book2
|
309
|
reg2.setStatus(RegistrationStatus.PREPARATION);
|
310
|
repo.getRegistrationService().save(reg2);
|
311
|
|
312
|
repo.authenticate("user2", "00000");
|
313
|
// the authenticated user will be set as submitter in new Registrations
|
314
|
// see RegistrationServiceImpl.prepareForSave(Registration reg)
|
315
|
Registration reg3 = Registration.NewInstance("test/1002", "1002", genus, new HashSet(Arrays.asList(ntd1))); // --> book1
|
316
|
repo.getRegistrationService().save(reg3);
|
317
|
reg3.setStatus(RegistrationStatus.CURATION);
|
318
|
|
319
|
Registration reg4 = Registration.NewInstance("test/1003", "1003", null, new HashSet(Arrays.asList(std2))); // --> book2
|
320
|
repo.getRegistrationService().save(reg4);
|
321
|
reg4.setStatus(RegistrationStatus.READY);
|
322
|
|
323
|
// 2. end the transaction so that all data is actually written to the db
|
324
|
setComplete();
|
325
|
endTransaction();
|
326
|
|
327
|
// use the fileNameAppendix if you are creating a data set file which need to be named differently
|
328
|
// from the standard name. For example if a single test method needs different data then the other
|
329
|
// methods the test class you may want to set the fileNameAppendix when creating the data for this method.
|
330
|
String fileNameAppendix = null;
|
331
|
|
332
|
// 3.
|
333
|
writeDbUnitDataSetFile(new String[] {
|
334
|
"UserAccount",
|
335
|
"REFERENCE",
|
336
|
"Registration", "Registration_TypeDesignationBase",
|
337
|
"TaxonName", "TaxonName_TypeDesignationBase",
|
338
|
"TypeDesignationBase",
|
339
|
"AGENTBASE", "HOMOTYPICALGROUP",
|
340
|
"HIBERNATE_SEQUENCES" // IMPORTANT!!!
|
341
|
},
|
342
|
fileNameAppendix, true );
|
343
|
}
|
344
|
}
|