1 package de.dlr.shepard.context.collection.services;
2
3 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4 import static org.junit.jupiter.api.Assertions.assertEquals;
5 import static org.junit.jupiter.api.Assertions.assertThrows;
6 import static org.mockito.ArgumentMatchers.any;
7 import static org.mockito.Mockito.verify;
8 import static org.mockito.Mockito.when;
9
10 import de.dlr.shepard.auth.permission.io.PermissionsIO;
11 import de.dlr.shepard.auth.permission.model.Permissions;
12 import de.dlr.shepard.auth.permission.services.PermissionsService;
13 import de.dlr.shepard.auth.security.AuthenticationContext;
14 import de.dlr.shepard.auth.users.entities.User;
15 import de.dlr.shepard.auth.users.services.UserService;
16 import de.dlr.shepard.common.exceptions.InvalidAuthException;
17 import de.dlr.shepard.common.exceptions.InvalidPathException;
18 import de.dlr.shepard.common.util.AccessType;
19 import de.dlr.shepard.common.util.DateHelper;
20 import de.dlr.shepard.common.util.PermissionType;
21 import de.dlr.shepard.common.util.QueryParamHelper;
22 import de.dlr.shepard.context.collection.daos.CollectionDAO;
23 import de.dlr.shepard.context.collection.entities.Collection;
24 import de.dlr.shepard.context.collection.io.CollectionIO;
25 import de.dlr.shepard.context.references.basicreference.daos.BasicReferenceDAO;
26 import de.dlr.shepard.context.version.daos.VersionDAO;
27 import de.dlr.shepard.context.version.entities.Version;
28 import de.dlr.shepard.data.file.entities.FileContainer;
29 import de.dlr.shepard.data.file.services.FileContainerService;
30 import io.quarkus.test.InjectMock;
31 import io.quarkus.test.component.QuarkusComponentTest;
32 import jakarta.inject.Inject;
33 import java.util.Date;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Optional;
37 import java.util.UUID;
38 import org.junit.jupiter.api.Test;
39
40 @QuarkusComponentTest
41 public class CollectionServiceTest {
42
43 @InjectMock
44 CollectionDAO dao;
45
46 @InjectMock
47 VersionDAO versionDAO;
48
49 @InjectMock
50 BasicReferenceDAO referenceDAO;
51
52 @InjectMock
53 UserService userService;
54
55 @InjectMock
56 AuthenticationContext authenticationContext;
57
58 @InjectMock
59 PermissionsService permissionsService;
60
61 @InjectMock
62 DateHelper dateHelper;
63
64 @InjectMock
65 FileContainerService fileContainerService;
66
67 @Inject
68 CollectionService service;
69
70 @Test
71 public void getCollectionsByShepardIdTest() {
72 String username = "manni";
73 Collection collectionNotDeleted = new Collection(5L);
74 collectionNotDeleted.setShepardId(55L);
75 Collection collectionDeleted = new Collection(6L);
76 collectionDeleted.setShepardId(65L);
77 collectionDeleted.setDeleted(true);
78
79 when(authenticationContext.getCurrentUserName()).thenReturn("manni");
80 when(dao.findAllCollectionsByShepardId(null, username)).thenReturn(List.of(collectionNotDeleted));
81 List<Collection> returned = service.getAllCollections(null);
82 assertEquals(List.of(collectionNotDeleted), returned);
83 }
84
85 @Test
86 public void getCollectionsByShepardIdTest_withName() {
87 String username = "kurac";
88 Collection collectionNotDeleted = new Collection(5L);
89 collectionNotDeleted.setShepardId(55L);
90 Collection collectionDeleted = new Collection(6L);
91 collectionDeleted.setShepardId(65L);
92 collectionDeleted.setDeleted(true);
93
94 QueryParamHelper params = new QueryParamHelper().withName("test");
95 when(authenticationContext.getCurrentUserName()).thenReturn("kurac");
96 when(dao.findAllCollectionsByShepardId(params, username)).thenReturn(List.of(collectionNotDeleted));
97 List<Collection> returned = service.getAllCollections(params);
98 assertEquals(List.of(collectionNotDeleted), returned);
99 }
100
101 @Test
102 public void createCollectionTest() {
103 User user = new User("bob");
104 Date date = new Date(23);
105 Version nullVersion = new Version(new UUID(1L, 2L));
106 CollectionIO input = new CollectionIO() {
107 {
108 setAttributes(Map.of("a", "b", "c", "d"));
109 setDescription("Desc");
110 setName("Name");
111 }
112 };
113 Collection toCreate = new Collection() {
114 {
115 setAttributes(Map.of("a", "b", "c", "d"));
116 setDescription("Desc");
117 setName("Name");
118 setCreatedAt(date);
119 setCreatedBy(user);
120 }
121 };
122 Collection created = new Collection() {
123 {
124 setAttributes(Map.of("a", "b", "c", "d"));
125 setDescription("Desc");
126 setName("Name");
127 setCreatedAt(date);
128 setCreatedBy(user);
129 setId(1L);
130 }
131 };
132 Collection createdWithShepardId = new Collection() {
133 {
134 setAttributes(Map.of("a", "b", "c", "d"));
135 setDescription("Desc");
136 setName("Name");
137 setCreatedAt(date);
138 setCreatedBy(user);
139 setId(created.getId());
140 setShepardId(created.getId());
141 }
142 };
143 createdWithShepardId.setVersion(nullVersion);
144 when(userService.getCurrentUser()).thenReturn(user);
145 when(dateHelper.getDate()).thenReturn(date);
146 when(dao.createOrUpdate(toCreate)).thenReturn(created);
147 when(dao.createOrUpdate(createdWithShepardId)).thenReturn(createdWithShepardId);
148 when(versionDAO.createOrUpdate(any())).thenReturn(nullVersion);
149 Collection actual = service.createCollection(input);
150 assertEquals(createdWithShepardId, actual);
151 verify(permissionsService).createPermissions(created, user, PermissionType.Private);
152 }
153
154 @Test
155 public void updateCollectionByShepardIdTest() {
156 User user = new User("bob");
157 Date date = new Date(23);
158 User updateUser = new User("claus");
159 Date updateDate = new Date(43);
160
161 CollectionIO input = new CollectionIO() {
162 {
163 setId(1L);
164 setAttributes(Map.of("1", "2", "c", "d"));
165 setDescription("newDesc");
166 setName("newName");
167 }
168 };
169 Collection old = new Collection() {
170 {
171 setAttributes(Map.of("a", "b", "c", "d"));
172 setDescription("Desc");
173 setName("Name");
174 setCreatedAt(date);
175 setCreatedBy(user);
176 setId(15L);
177 setShepardId(input.getId());
178 }
179 };
180 var updated = new Collection() {
181 {
182 setAttributes(Map.of("1", "2", "c", "d"));
183 setDescription("newDesc");
184 setName("newName");
185 setCreatedAt(date);
186 setCreatedBy(user);
187 setUpdatedAt(updateDate);
188 setUpdatedBy(updateUser);
189 setId(old.getId());
190 setShepardId(old.getShepardId());
191 }
192 };
193
194 when(dao.findByShepardId(old.getShepardId(), false)).thenReturn(old);
195 when(userService.getCurrentUser()).thenReturn(updateUser);
196 when(dateHelper.getDate()).thenReturn(updateDate);
197 when(dao.createOrUpdate(updated)).thenReturn(updated);
198 when(authenticationContext.getCurrentUserName()).thenReturn(updateUser.getUsername());
199
200 when(
201 permissionsService.isAccessTypeAllowedForUser(old.getShepardId(), AccessType.Read, updateUser.getUsername())
202 ).thenReturn(true);
203 when(
204 permissionsService.isAccessTypeAllowedForUser(old.getShepardId(), AccessType.Write, updateUser.getUsername())
205 ).thenReturn(true);
206
207 var actual = service.updateCollectionByShepardId(old.getShepardId(), input);
208 assertEquals(updated, actual);
209 }
210
211 @Test
212 public void updateCollectionByShepardIdTest_noUpdatePermissions() {
213 User user = new User("bob");
214 Date date = new Date(23);
215 User updateUser = new User("claus");
216 Date updateDate = new Date(43);
217
218 CollectionIO input = new CollectionIO() {
219 {
220 setId(1L);
221 setAttributes(Map.of("1", "2", "c", "d"));
222 setDescription("newDesc");
223 setName("newName");
224 }
225 };
226 Collection old = new Collection() {
227 {
228 setAttributes(Map.of("a", "b", "c", "d"));
229 setDescription("Desc");
230 setName("Name");
231 setCreatedAt(date);
232 setCreatedBy(user);
233 setId(15L);
234 setShepardId(input.getId());
235 }
236 };
237 var updated = new Collection() {
238 {
239 setAttributes(Map.of("1", "2", "c", "d"));
240 setDescription("newDesc");
241 setName("newName");
242 setCreatedAt(date);
243 setCreatedBy(user);
244 setUpdatedAt(updateDate);
245 setUpdatedBy(updateUser);
246 setId(old.getId());
247 setShepardId(old.getShepardId());
248 }
249 };
250
251 when(dao.findByShepardId(old.getShepardId(), false)).thenReturn(old);
252 when(userService.getCurrentUser()).thenReturn(updateUser);
253 when(dateHelper.getDate()).thenReturn(updateDate);
254 when(dao.createOrUpdate(updated)).thenReturn(updated);
255 when(authenticationContext.getCurrentUserName()).thenReturn(updateUser.getUsername());
256
257 when(
258 permissionsService.isAccessTypeAllowedForUser(old.getShepardId(), AccessType.Read, updateUser.getUsername())
259 ).thenReturn(true);
260 when(
261 permissionsService.isAccessTypeAllowedForUser(old.getShepardId(), AccessType.Write, updateUser.getUsername())
262 ).thenReturn(false);
263
264 assertThrows(InvalidAuthException.class, () -> service.updateCollectionByShepardId(old.getShepardId(), input));
265 }
266
267 @Test
268 public void updateCollectionByShepardId_setDefaultFileContainer() {
269 User user = new User("bob");
270 Date date = new Date(23);
271 User updateUser = new User("claus");
272 Date updateDate = new Date(43);
273 FileContainer fileContainer = new FileContainer(151L);
274
275 CollectionIO input = new CollectionIO() {
276 {
277 setId(1L);
278 setAttributes(Map.of("1", "2", "c", "d"));
279 setDescription("Desc");
280 setName("Name");
281 setDefaultFileContainerId(151L);
282 }
283 };
284 Collection old = new Collection() {
285 {
286 setAttributes(Map.of("1", "2", "c", "d"));
287 setDescription("Desc");
288 setName("Name");
289 setCreatedAt(date);
290 setCreatedBy(user);
291 setId(15L);
292 setShepardId(input.getId());
293 setFileContainer(null);
294 }
295 };
296 var updated = new Collection() {
297 {
298 setAttributes(Map.of("1", "2", "c", "d"));
299 setDescription("Desc");
300 setName("Name");
301 setCreatedAt(date);
302 setCreatedBy(user);
303 setUpdatedAt(updateDate);
304 setUpdatedBy(updateUser);
305 setId(old.getId());
306 setShepardId(old.getShepardId());
307 setFileContainer(fileContainer);
308 }
309 };
310
311 when(dao.findByShepardId(old.getShepardId(), false)).thenReturn(old);
312 when(userService.getCurrentUser()).thenReturn(updateUser);
313 when(dateHelper.getDate()).thenReturn(updateDate);
314 when(dao.createOrUpdate(updated)).thenReturn(updated);
315 when(authenticationContext.getCurrentUserName()).thenReturn(updateUser.getUsername());
316 when(fileContainerService.getContainer(fileContainer.getId())).thenReturn(fileContainer);
317
318 when(
319 permissionsService.isAccessTypeAllowedForUser(old.getShepardId(), AccessType.Read, updateUser.getUsername())
320 ).thenReturn(true);
321 when(
322 permissionsService.isAccessTypeAllowedForUser(old.getShepardId(), AccessType.Write, updateUser.getUsername())
323 ).thenReturn(true);
324
325 var actual = service.updateCollectionByShepardId(old.getShepardId(), input);
326 assertEquals(updated, actual);
327 }
328
329 @Test
330 public void deleteCollectionByShepardIdTest() {
331 User user = new User("bob");
332 Date date = new Date(23);
333
334 Collection collection = new Collection(1L);
335 collection.setShepardId(15L);
336
337 when(userService.getCurrentUser()).thenReturn(user);
338 when(dateHelper.getDate()).thenReturn(date);
339 when(dao.deleteCollectionByShepardId(collection.getShepardId(), user, date)).thenReturn(true);
340 when(dao.findByShepardId(collection.getShepardId(), true)).thenReturn(collection);
341 when(authenticationContext.getCurrentUserName()).thenReturn("bob");
342
343 when(permissionsService.isAccessTypeAllowedForUser(collection.getShepardId(), AccessType.Read, "bob")).thenReturn(
344 true
345 );
346 when(permissionsService.isAccessTypeAllowedForUser(collection.getShepardId(), AccessType.Write, "bob")).thenReturn(
347 true
348 );
349
350 assertDoesNotThrow(() -> service.deleteCollection(collection.getShepardId()));
351 }
352
353 @Test
354 public void deleteCollectionByShepardIdTestNoUpdatePermissions() {
355 User user = new User("timbo");
356 Date date = new Date(23);
357
358 Collection collection = new Collection(1L);
359 collection.setShepardId(15L);
360
361 when(userService.getCurrentUser()).thenReturn(user);
362 when(dateHelper.getDate()).thenReturn(date);
363 when(dao.deleteCollectionByShepardId(collection.getShepardId(), user, date)).thenReturn(true);
364 when(dao.findByShepardId(collection.getShepardId(), true)).thenReturn(collection);
365 when(authenticationContext.getCurrentUserName()).thenReturn("timbo");
366
367 when(permissionsService.isAccessTypeAllowedForUser(collection.getShepardId(), AccessType.Read, "timbo")).thenReturn(
368 true
369 );
370 when(
371 permissionsService.isAccessTypeAllowedForUser(collection.getShepardId(), AccessType.Write, "timbo")
372 ).thenReturn(false);
373
374 assertThrows(InvalidAuthException.class, () -> service.deleteCollection(collection.getShepardId()));
375 }
376
377 @Test
378 public void getCollectionByShepardIdNoVersion() {
379 Collection ret = new Collection(1L);
380 long shepardId = 2L;
381 when(dao.findByShepardId(shepardId, false)).thenReturn(ret);
382 when(authenticationContext.getCurrentUserName()).thenReturn("bob");
383 when(permissionsService.isAccessTypeAllowedForUser(shepardId, AccessType.Read, "bob")).thenReturn(true);
384 var result = service.getCollectionWithDataObjectsAndIncomingReferences(shepardId);
385 assertEquals(ret, result);
386 assertEquals(null, result.getFileContainer());
387 }
388
389 @Test
390 public void getCollectionByShepardIdNoVersionNotFound() {
391 long shepardId = 2L;
392 when(dao.findByShepardId(shepardId, false)).thenReturn(null);
393
394 assertThrows(InvalidPathException.class, () -> {
395 service.getCollectionWithDataObjectsAndIncomingReferences(shepardId, null);
396 });
397 }
398
399 @Test
400 public void getCollectionByShepardIdNoVersionDeleted() {
401 Collection ret = new Collection(1L);
402 ret.setDeleted(true);
403 long shepardId = 2L;
404 when(dao.findByShepardId(shepardId)).thenReturn(ret);
405
406 assertThrows(InvalidPathException.class, () -> {
407 service.getCollectionWithDataObjectsAndIncomingReferences(shepardId, null);
408 });
409 }
410
411 public void getCollectionByShepardIdNoVersionNoReadPermissions() {
412 Collection ret = new Collection(1L);
413 long shepardId = 2L;
414 when(dao.findByShepardId(shepardId, false)).thenReturn(ret);
415 when(authenticationContext.getCurrentUserName()).thenReturn("eric");
416 when(permissionsService.isAccessTypeAllowedForUser(shepardId, AccessType.Read, "eric")).thenReturn(false);
417 assertThrows(InvalidAuthException.class, () -> service.getCollectionWithDataObjectsAndIncomingReferences(shepardId)
418 );
419 }
420
421 @Test
422 public void getCollectionByShepardId() {
423 Collection ret = new Collection(1L);
424 UUID versionUID = new UUID(1L, 2L);
425 long shepardId = 2L;
426 when(dao.findByShepardId(shepardId, versionUID, false)).thenReturn(ret);
427 when(authenticationContext.getCurrentUserName()).thenReturn("bob");
428 when(permissionsService.isAccessTypeAllowedForUser(shepardId, AccessType.Read, "bob")).thenReturn(true);
429 var result = service.getCollectionWithDataObjectsAndIncomingReferences(shepardId, versionUID);
430 assertEquals(ret, result);
431 }
432
433 @Test
434 public void getCollectionByShepardIdNotFound() {
435 UUID versionUID = new UUID(1L, 2L);
436 long shepardId = 2L;
437 when(dao.findByShepardId(shepardId, versionUID)).thenReturn(null);
438
439 assertThrows(InvalidPathException.class, () -> {
440 service.getCollectionWithDataObjectsAndIncomingReferences(shepardId, versionUID);
441 });
442 }
443
444 @Test
445 public void getCollectionByShepardIdDeleted() {
446 Collection ret = new Collection(1L);
447 ret.setDeleted(true);
448 UUID versionUID = new UUID(1L, 2L);
449 long shepardId = 2L;
450 when(dao.findByShepardId(shepardId, versionUID)).thenReturn(ret);
451 assertThrows(InvalidPathException.class, () -> {
452 service.getCollectionWithDataObjectsAndIncomingReferences(shepardId, versionUID);
453 });
454 }
455
456 @Test
457 public void getCollectionPermissions() {
458 Collection col = new Collection(1L);
459 Permissions ret = new Permissions(col, new User("bob"), PermissionType.Private);
460 col.setPermissions(ret);
461 long shepardId = 2L;
462
463 when(dao.findByShepardId(shepardId, true)).thenReturn(col);
464 when(authenticationContext.getCurrentUserName()).thenReturn("bob");
465 when(permissionsService.isAccessTypeAllowedForUser(shepardId, AccessType.Read, "bob")).thenReturn(true);
466 when(permissionsService.isAccessTypeAllowedForUser(shepardId, AccessType.Manage, "bob")).thenReturn(true);
467 when(permissionsService.getPermissionsOfEntity(shepardId)).thenReturn(ret);
468
469 var result = service.getCollectionPermissions(shepardId);
470 assertEquals(ret, result);
471 }
472
473 @Test
474 public void getCollectionPermissionsNoManagePermissions() {
475 Collection col = new Collection(1L);
476 Permissions ret = new Permissions(col, new User("bob"), PermissionType.Private);
477 col.setPermissions(ret);
478 long shepardId = 2L;
479 when(dao.findByShepardId(shepardId, true)).thenReturn(col);
480 when(authenticationContext.getCurrentUserName()).thenReturn("bob");
481 when(permissionsService.isAccessTypeAllowedForUser(shepardId, AccessType.Read, "bob")).thenReturn(true);
482 when(permissionsService.isAccessTypeAllowedForUser(shepardId, AccessType.Manage, "bob")).thenReturn(false);
483 when(permissionsService.getPermissionsOfEntityOptional(shepardId)).thenReturn(Optional.of(ret));
484 assertThrows(InvalidAuthException.class, () -> service.getCollectionPermissions(shepardId));
485 }
486
487 @Test
488 public void updateCollectionPermissions() {
489 Collection col = new Collection(1L);
490 long shepardId = 2L;
491 col.setShepardId(shepardId);
492 Permissions newPermissions = new Permissions(col, new User("bob"), PermissionType.Public);
493 PermissionsIO permissionsIO = new PermissionsIO(newPermissions);
494 when(dao.findByShepardId(shepardId, true)).thenReturn(col);
495 when(authenticationContext.getCurrentUserName()).thenReturn("bob");
496 when(permissionsService.isAccessTypeAllowedForUser(shepardId, AccessType.Read, "bob")).thenReturn(true);
497 when(permissionsService.isAccessTypeAllowedForUser(shepardId, AccessType.Manage, "bob")).thenReturn(true);
498 when(permissionsService.updatePermissionsByNeo4jId(permissionsIO, shepardId)).thenReturn(newPermissions);
499 var result = service.updateCollectionPermissions(permissionsIO, shepardId);
500 assertEquals(newPermissions, result);
501 }
502
503 @Test
504 public void updateCollectionPermissionsNoManagePermissions() {
505 Collection col = new Collection(1L);
506 long shepardId = 2L;
507 col.setShepardId(shepardId);
508 Permissions newPermissions = new Permissions(col, new User("bob"), PermissionType.Public);
509 PermissionsIO permissionsIO = new PermissionsIO(newPermissions);
510 when(dao.findByShepardId(shepardId, true)).thenReturn(col);
511 when(authenticationContext.getCurrentUserName()).thenReturn("bob");
512 when(permissionsService.isAccessTypeAllowedForUser(shepardId, AccessType.Read, "bob")).thenReturn(true);
513 when(permissionsService.isAccessTypeAllowedForUser(shepardId, AccessType.Manage, "bob")).thenReturn(false);
514 when(permissionsService.updatePermissionsByNeo4jId(permissionsIO, shepardId)).thenReturn(newPermissions);
515 assertThrows(InvalidAuthException.class, () -> service.updateCollectionPermissions(permissionsIO, shepardId));
516 }
517 }