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.anyLong;
7 import static org.mockito.Mockito.atLeast;
8 import static org.mockito.Mockito.spy;
9 import static org.mockito.Mockito.verify;
10 import static org.mockito.Mockito.when;
11
12 import de.dlr.shepard.auth.permission.services.PermissionsService;
13 import de.dlr.shepard.auth.users.daos.UserDAO;
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.InvalidBodyException;
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.QueryParamHelper;
21 import de.dlr.shepard.context.collection.daos.DataObjectDAO;
22 import de.dlr.shepard.context.collection.entities.Collection;
23 import de.dlr.shepard.context.collection.entities.DataObject;
24 import de.dlr.shepard.context.collection.io.DataObjectIO;
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 io.quarkus.test.InjectMock;
29 import io.quarkus.test.component.QuarkusComponentTest;
30 import jakarta.inject.Inject;
31 import java.util.Date;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.UUID;
35 import org.junit.jupiter.api.Test;
36 import org.mockito.Mockito;
37
38 @QuarkusComponentTest
39 public class DataObjectServiceTest {
40
41 @InjectMock
42 DataObjectDAO dao;
43
44 @InjectMock
45 CollectionService collectionService;
46
47 @InjectMock
48 BasicReferenceDAO referenceDAO;
49
50 @InjectMock
51 UserDAO userDAO;
52
53 @InjectMock
54 VersionDAO versionDAO;
55
56 @InjectMock
57 DateHelper dateHelper;
58
59 @InjectMock
60 UserService userService;
61
62 @InjectMock
63 PermissionsService permissionsService;
64
65 @Inject
66 DataObjectService service;
67
68 private final User defaultUser = new User("Bob");
69
70 @Test
71 public void getDataObjectTest() {
72 Collection collection = new Collection(555L);
73 collection.setShepardId(5555L);
74 DataObject dataObject = new DataObject(5L);
75 dataObject.setShepardId(55L);
76 dataObject.setCollection(collection);
77 when(dao.findByShepardId(dataObject.getShepardId())).thenReturn(dataObject);
78 when(userService.getCurrentUser()).thenReturn(defaultUser);
79 when(
80 permissionsService.isAccessTypeAllowedForUser(
81 collection.getShepardId(),
82 AccessType.Read,
83 defaultUser.getUsername()
84 )
85 ).thenReturn(true);
86 DataObject returned = service.getDataObject(dataObject.getShepardId());
87 assertEquals(dataObject, returned);
88 }
89
90 @Test
91 public void getDataObjectWithVersionUIDTest() {
92 Collection collection = new Collection(555L);
93 collection.setShepardId(5555L);
94 DataObject dataObject = new DataObject(5L);
95 UUID versionUID = new UUID(0L, 1L);
96 dataObject.setShepardId(55L);
97 dataObject.setCollection(collection);
98 when(dao.findByShepardId(dataObject.getShepardId(), versionUID)).thenReturn(dataObject);
99 when(userService.getCurrentUser()).thenReturn(defaultUser);
100 when(
101 permissionsService.isAccessTypeAllowedForUser(
102 collection.getShepardId(),
103 AccessType.Read,
104 defaultUser.getUsername()
105 )
106 ).thenReturn(true);
107 DataObject returned = service.getDataObject(dataObject.getShepardId(), versionUID);
108 assertEquals(dataObject, returned);
109 }
110
111 @Test
112 public void getDataObjectTest_deleted() {
113 DataObject dataObject = new DataObject(5L);
114 dataObject.setDeleted(true);
115 dataObject.setShepardId(55L);
116 when(dao.findByShepardId(dataObject.getShepardId())).thenReturn(dataObject);
117 assertThrows(InvalidPathException.class, () -> {
118 service.getDataObject(dataObject.getShepardId());
119 });
120 }
121
122 @Test
123 public void getDataObjectTest_isNull() {
124 Long shepardId = 65L;
125 when(dao.findByShepardId(shepardId)).thenReturn(null);
126 assertThrows(InvalidPathException.class, () -> {
127 service.getDataObject(shepardId);
128 });
129 }
130
131 @Test
132 public void getDataObjectTest_deletedParent() {
133 Collection collection = new Collection(555L);
134 collection.setShepardId(5555L);
135 DataObject parent = new DataObject(1L);
136 parent.setShepardId(15L);
137 parent.setDeleted(true);
138 DataObject dataObject = new DataObject(2L);
139 dataObject.setShepardId(25L);
140 dataObject.setParent(parent);
141 dataObject.setCollection(collection);
142 DataObject dataObjectCut = new DataObject(2L);
143 dataObjectCut.setShepardId(25L);
144 dataObjectCut.setCollection(collection);
145
146 when(dao.findByShepardId(dataObject.getShepardId())).thenReturn(dataObject);
147 when(userService.getCurrentUser()).thenReturn(defaultUser);
148 when(
149 permissionsService.isAccessTypeAllowedForUser(
150 collection.getShepardId(),
151 AccessType.Read,
152 defaultUser.getUsername()
153 )
154 ).thenReturn(true);
155 DataObject returned = service.getDataObject(dataObject.getShepardId());
156 assertEquals(dataObjectCut, returned);
157 }
158
159 @Test
160 public void getDataObjectTest_withParent() {
161 Collection collection = new Collection(555L);
162 collection.setShepardId(5555L);
163 DataObject parent = new DataObject(1L);
164 parent.setShepardId(15L);
165 DataObject dataObject = new DataObject(2L);
166 dataObject.setShepardId(25L);
167 dataObject.setParent(parent);
168 dataObject.setCollection(collection);
169
170 when(dao.findByShepardId(dataObject.getShepardId())).thenReturn(dataObject);
171 when(userService.getCurrentUser()).thenReturn(defaultUser);
172 when(
173 permissionsService.isAccessTypeAllowedForUser(
174 collection.getShepardId(),
175 AccessType.Read,
176 defaultUser.getUsername()
177 )
178 ).thenReturn(true);
179 DataObject returned = service.getDataObject(dataObject.getShepardId());
180 assertEquals(dataObject, returned);
181 }
182
183 @Test
184 public void getDataObjectTest_notInCollection() {
185 Collection collection = new Collection(555L);
186 collection.setShepardId(5555L);
187 Collection otherCollection = new Collection(15L);
188 otherCollection.setShepardId(10L);
189 DataObject dataObject = new DataObject(2L);
190 dataObject.setShepardId(25L);
191 dataObject.setCollection(otherCollection);
192
193 when(dao.findByShepardId(dataObject.getShepardId())).thenReturn(dataObject);
194 when(userService.getCurrentUser()).thenReturn(defaultUser);
195 when(
196 permissionsService.isAccessTypeAllowedForUser(
197 collection.getShepardId(),
198 AccessType.Read,
199 defaultUser.getUsername()
200 )
201 ).thenReturn(true);
202 assertThrows(InvalidPathException.class, () ->
203 service.getDataObject(collection.getShepardId(), dataObject.getShepardId())
204 );
205 }
206
207 @Test
208 public void getDataObjectsByShepardIdsTest() {
209 DataObject dataObjectNotDeleted = new DataObject(5L);
210 dataObjectNotDeleted.setShepardId(55L);
211
212 QueryParamHelper params = new QueryParamHelper().withName("Name");
213 Long collectionShepardId = 1L;
214 when(dao.findByCollectionByShepardIds(collectionShepardId, params, null)).thenReturn(List.of(dataObjectNotDeleted));
215 List<DataObject> returned = service.getAllDataObjectsByShepardIds(collectionShepardId, params, null);
216 assertEquals(List.of(dataObjectNotDeleted), returned);
217 }
218
219 @Test
220 public void createDataObjectByShepardIdWithoutPredecessorsTest() {
221 Date date = new Date(23);
222 Version version = new Version(new UUID(1L, 2L));
223 Collection collection = new Collection(2L);
224 collection.setShepardId(25L);
225 collection.setVersion(version);
226 DataObject parent = new DataObject(3L);
227 parent.setShepardId(35L);
228 parent.setCollection(collection);
229 DataObjectIO input = new DataObjectIO() {
230 {
231 setAttributes(Map.of("a", "b", "c", "d"));
232 setDescription("Desc");
233 setName("Name");
234 setParentId(parent.getShepardId());
235 }
236 };
237 DataObject toCreate = new DataObject() {
238 {
239 setAttributes(Map.of("a", "b", "c", "d"));
240 setDescription("Desc");
241 setName("Name");
242 setCreatedAt(date);
243 setCreatedBy(defaultUser);
244 setCollection(collection);
245 setParent(parent);
246 }
247 };
248 DataObject created = new DataObject() {
249 {
250 setAttributes(Map.of("a", "b", "c", "d"));
251 setDescription("Desc");
252 setName("Name");
253 setCreatedAt(date);
254 setCreatedBy(defaultUser);
255 setCollection(collection);
256 setParent(parent);
257 setId(1L);
258 }
259 };
260 DataObject createdWithShepardId = new DataObject() {
261 {
262 setAttributes(Map.of("a", "b", "c", "d"));
263 setDescription("Desc");
264 setName("Name");
265 setCreatedAt(date);
266 setCreatedBy(defaultUser);
267 setCollection(collection);
268 setParent(parent);
269 setId(created.getId());
270 setShepardId(created.getId());
271 }
272 };
273 when(dao.findByShepardId(parent.getShepardId())).thenReturn(parent);
274 when(dateHelper.getDate()).thenReturn(date);
275 when(collectionService.getCollection(collection.getShepardId())).thenReturn(collection);
276 when(dao.createOrUpdate(toCreate)).thenReturn(created);
277 when(dao.createOrUpdate(createdWithShepardId)).thenReturn(createdWithShepardId);
278 when(userService.getCurrentUser()).thenReturn(defaultUser);
279
280 DataObject actual = service.createDataObject(collection.getShepardId(), input);
281 assertEquals(createdWithShepardId, actual);
282 }
283
284 @Test
285 public void createDataObjectByShepardIdTest() {
286 Date date = new Date(23);
287 Version version = new Version(new UUID(1L, 2L));
288 Collection collection = new Collection(2L);
289 collection.setShepardId(25L);
290 collection.setVersion(version);
291 DataObject parent = new DataObject(3L);
292 parent.setShepardId(35L);
293 parent.setCollection(collection);
294 DataObject predecessor = new DataObject(4L);
295 predecessor.setShepardId(45L);
296 predecessor.setCollection(collection);
297 DataObjectIO input = new DataObjectIO() {
298 {
299 setAttributes(Map.of("a", "b", "c", "d"));
300 setDescription("Desc");
301 setName("Name");
302 setParentId(parent.getShepardId());
303 setPredecessorIds(new long[] { predecessor.getShepardId() });
304 }
305 };
306 DataObject toCreate = new DataObject() {
307 {
308 setAttributes(Map.of("a", "b", "c", "d"));
309 setDescription("Desc");
310 setName("Name");
311 setCreatedAt(date);
312 setCreatedBy(defaultUser);
313 setCollection(collection);
314 setParent(parent);
315 setPredecessors(List.of(predecessor));
316 }
317 };
318 DataObject created = new DataObject() {
319 {
320 setAttributes(Map.of("a", "b", "c", "d"));
321 setDescription("Desc");
322 setName("Name");
323 setCreatedAt(date);
324 setCreatedBy(defaultUser);
325 setCollection(collection);
326 setParent(parent);
327 setPredecessors(List.of(predecessor));
328 setId(1L);
329 }
330 };
331 DataObject createdWithShepardId = new DataObject() {
332 {
333 setAttributes(Map.of("a", "b", "c", "d"));
334 setDescription("Desc");
335 setName("Name");
336 setCreatedAt(date);
337 setCreatedBy(defaultUser);
338 setCollection(collection);
339 setParent(parent);
340 setPredecessors(List.of(predecessor));
341 setId(created.getId());
342 setShepardId(created.getId());
343 }
344 };
345 when(dao.findByShepardId(parent.getShepardId())).thenReturn(parent);
346 when(dao.findByShepardId(predecessor.getShepardId())).thenReturn(predecessor);
347 when(dateHelper.getDate()).thenReturn(date);
348 when(collectionService.getCollection(collection.getShepardId())).thenReturn(collection);
349 when(dao.createOrUpdate(toCreate)).thenReturn(created);
350 when(dao.createOrUpdate(createdWithShepardId)).thenReturn(createdWithShepardId);
351 when(userService.getCurrentUser()).thenReturn(defaultUser);
352
353 DataObject actual = service.createDataObject(collection.getShepardId(), input);
354 assertEquals(createdWithShepardId, actual);
355 }
356
357 @Test
358 public void createDataObjectByShepardIdTest_wrongParent() {
359 User user = new User("bob");
360 Date date = new Date(23);
361 Collection collection = new Collection(2L);
362 collection.setShepardId(25L);
363 DataObjectIO input = new DataObjectIO() {
364 {
365 setAttributes(Map.of("a", "b", "c", "d"));
366 setDescription("Desc");
367 setName("Name");
368 setParentId(3L);
369 }
370 };
371 when(dao.findByShepardId(input.getParentId())).thenReturn(null);
372 when(dateHelper.getDate()).thenReturn(date);
373 when(userDAO.find(user.getUsername())).thenReturn(user);
374 when(collectionService.getCollection(collection.getShepardId())).thenReturn(collection);
375 assertThrows(InvalidBodyException.class, () -> service.createDataObject(collection.getShepardId(), input));
376 }
377
378 @Test
379 public void createDataObjectByShepardIdTest_wrongPredecessor() {
380 User user = new User("bob");
381 Date date = new Date(23);
382 Collection collection = new Collection(2L);
383 collection.setShepardId(25L);
384 DataObjectIO input = new DataObjectIO() {
385 {
386 setAttributes(Map.of("a", "b", "c", "d"));
387 setDescription("Desc");
388 setName("Name");
389 setPredecessorIds(new long[] { 3L });
390 }
391 };
392 when(dao.findByShepardId(input.getPredecessorIds()[0])).thenReturn(null);
393 when(dateHelper.getDate()).thenReturn(date);
394 when(userDAO.find(user.getUsername())).thenReturn(user);
395 when(collectionService.getCollection(collection.getShepardId())).thenReturn(collection);
396 assertThrows(InvalidBodyException.class, () -> service.createDataObject(collection.getShepardId(), input));
397 }
398
399 @Test
400 public void createDataObjectByShepardIdTest_deletedParent() {
401 User user = new User("bob");
402 Date date = new Date(23);
403 Collection collection = new Collection(2L);
404 collection.setShepardId(25L);
405 DataObject parent = new DataObject(3L);
406 parent.setShepardId(35L);
407 parent.setDeleted(true);
408 DataObjectIO input = new DataObjectIO() {
409 {
410 setAttributes(Map.of("a", "b", "c", "d"));
411 setDescription("Desc");
412 setName("Name");
413 setParentId(parent.getShepardId());
414 }
415 };
416 when(dao.findByShepardId(parent.getShepardId())).thenReturn(parent);
417 when(dateHelper.getDate()).thenReturn(date);
418 when(userDAO.find(user.getUsername())).thenReturn(user);
419 when(collectionService.getCollection(collection.getShepardId())).thenReturn(collection);
420 assertThrows(InvalidBodyException.class, () -> service.createDataObject(collection.getShepardId(), input));
421 }
422
423 @Test
424 public void createDataObjectByShepardIdTest_deletedPredecessor() {
425 User user = new User("bob");
426 Date date = new Date(23);
427 Collection collection = new Collection(2L);
428 collection.setShepardId(25L);
429 DataObject predecessor = new DataObject(3L);
430 predecessor.setShepardId(35L);
431 predecessor.setDeleted(true);
432 DataObjectIO input = new DataObjectIO() {
433 {
434 setAttributes(Map.of("a", "b", "c", "d"));
435 setDescription("Desc");
436 setName("Name");
437 setPredecessorIds(new long[] { predecessor.getShepardId() });
438 }
439 };
440 when(dao.findByShepardId(input.getPredecessorIds()[0])).thenReturn(predecessor);
441 when(dateHelper.getDate()).thenReturn(date);
442 when(userDAO.find(user.getUsername())).thenReturn(user);
443 when(collectionService.getCollection(collection.getShepardId())).thenReturn(collection);
444 assertThrows(InvalidBodyException.class, () -> service.createDataObject(collection.getShepardId(), input));
445 }
446
447 @Test
448 public void createDataObjectByShepardIdTest_ParentWrongCollection() {
449 User user = new User("bob");
450 Date date = new Date(23);
451 Collection collection = new Collection(2L);
452 collection.setShepardId(25L);
453 Collection wrong = new Collection(200L);
454 wrong.setShepardId(2005L);
455 DataObject parent = new DataObject(3L);
456 parent.setShepardId(35L);
457 parent.setCollection(wrong);
458 DataObjectIO input = new DataObjectIO() {
459 {
460 setAttributes(Map.of("a", "b", "c", "d"));
461 setDescription("Desc");
462 setName("Name");
463 setParentId(parent.getShepardId());
464 }
465 };
466 when(dao.findByShepardId(parent.getShepardId())).thenReturn(parent);
467 when(dateHelper.getDate()).thenReturn(date);
468 when(userDAO.find(user.getUsername())).thenReturn(user);
469 when(collectionService.getCollection(collection.getShepardId())).thenReturn(collection);
470 assertThrows(InvalidBodyException.class, () -> service.createDataObject(collection.getShepardId(), input));
471 }
472
473 @Test
474 public void createDataObjectByShepardIdTest_PredecessorWrongCollection() {
475 User user = new User("bob");
476 Date date = new Date(23);
477 Collection collection = new Collection(2L);
478 collection.setShepardId(25L);
479 Collection wrong = new Collection(200L);
480 wrong.setShepardId(2005L);
481 DataObject predecessor = new DataObject(4L);
482 predecessor.setShepardId(45L);
483 predecessor.setCollection(wrong);
484 DataObjectIO input = new DataObjectIO() {
485 {
486 setAttributes(Map.of("a", "b", "c", "d"));
487 setDescription("Desc");
488 setName("Name");
489 setPredecessorIds(new long[] { predecessor.getShepardId() });
490 }
491 };
492 when(dao.findByShepardId(predecessor.getShepardId())).thenReturn(predecessor);
493 when(dateHelper.getDate()).thenReturn(date);
494 when(userDAO.find(user.getUsername())).thenReturn(user);
495 when(collectionService.getCollection(collection.getShepardId())).thenReturn(collection);
496 assertThrows(InvalidBodyException.class, () -> service.createDataObject(collection.getShepardId(), input));
497 }
498
499 @Test
500 public void updateDataObjectByShepardIdTest() {
501 Collection collection = new Collection(100L);
502 collection.setShepardId(1005L);
503 Date date = new Date(23);
504 User updateUser = new User("claus");
505 Date updateDate = new Date(43);
506
507 DataObject parent = new DataObject(3L);
508 parent.setShepardId(35L);
509 parent.setCollection(collection);
510
511 DataObject aPredecessor = new DataObject(4L);
512 aPredecessor.setShepardId(45L);
513 aPredecessor.setCollection(collection);
514
515 List<DataObject> predecessors = List.of(aPredecessor);
516
517 DataObjectIO input = new DataObjectIO() {
518 {
519 setId(1L);
520 setAttributes(Map.of("1", "2", "c", "d"));
521 setDescription("newDesc");
522 setName("newName");
523 setParentId(parent.getShepardId());
524 setPredecessorIds(predecessors.stream().mapToLong(DataObject::getShepardId).toArray());
525 }
526 };
527 DataObject old = new DataObject() {
528 {
529 setAttributes(Map.of("a", "b", "c", "d"));
530 setDescription("Desc");
531 setName("Name");
532 setCreatedAt(date);
533 setCreatedBy(defaultUser);
534 setId(1L);
535 setShepardId(1L);
536 setCollection(collection);
537 setPredecessors(predecessors);
538 }
539 };
540 DataObject updated = new DataObject() {
541 {
542 setAttributes(Map.of("1", "2", "c", "d"));
543 setDescription("newDesc");
544 setName("newName");
545 setCreatedAt(date);
546 setCreatedBy(defaultUser);
547 setUpdatedAt(updateDate);
548 setUpdatedBy(updateUser);
549 setParent(parent);
550 setPredecessors(predecessors);
551 setId(old.getId());
552 setShepardId(old.getShepardId());
553 setCollection(collection);
554 }
555 };
556 when(dao.findByShepardId(old.getShepardId())).thenReturn(old);
557 when(dao.findByShepardId(parent.getShepardId())).thenReturn(parent);
558 when(dao.createOrUpdate(old)).thenReturn(updated);
559 when(dao.findByNeo4jId(aPredecessor.getId())).thenReturn(aPredecessor);
560
561 when(dateHelper.getDate()).thenReturn(updateDate);
562 when(collectionService.getCollection(collection.getShepardId())).thenReturn(collection);
563 when(userService.getCurrentUser()).thenReturn(defaultUser);
564 when(
565 permissionsService.isAccessTypeAllowedForUser(old.getShepardId(), AccessType.Write, updateUser.getUsername())
566 ).thenReturn(true);
567 when(
568 permissionsService.isAccessTypeAllowedForUser(old.getShepardId(), AccessType.Read, updateUser.getUsername())
569 ).thenReturn(true);
570
571 predecessors.forEach(predecessor -> when(dao.createOrUpdate(predecessor)).thenReturn(predecessor));
572 predecessors.forEach(predecessor -> when(dao.findByShepardId(predecessor.getShepardId())).thenReturn(predecessor));
573
574 DataObject actual = service.updateDataObject(collection.getShepardId(), old.getShepardId(), input);
575
576 predecessors.forEach(predecessor -> verify(dao, atLeast(1)).findByShepardId(predecessor.getShepardId()));
577 predecessors.forEach(predecessor ->
578 verify(dao).deleteHasSuccessorRelation(predecessor.getShepardId(), old.getShepardId())
579 );
580 assertEquals(updated, actual);
581 }
582
583 @Test
584 public void updateDataObjectByShepardIdTest_UpdateParent() {
585 Collection collection = new Collection(100L);
586 collection.setShepardId(1005L);
587 Date date = new Date(23);
588 User updateUser = new User("claus");
589 Date updateDate = new Date(43);
590 DataObject parent = new DataObject(3L);
591 parent.setShepardId(35L);
592 parent.setCollection(collection);
593 DataObject oldParent = new DataObject(3L);
594 oldParent.setShepardId(35L);
595 oldParent.setCollection(collection);
596 @SuppressWarnings("unchecked")
597 List<DataObject> children = Mockito.mock(List.class);
598 oldParent.setChildren(children);
599
600 DataObjectIO input = new DataObjectIO() {
601 {
602 setId(1L);
603 setAttributes(Map.of("1", "2", "c", "d"));
604 setDescription("newDesc");
605 setName("newName");
606 setParentId(parent.getShepardId());
607 }
608 };
609 DataObject old = new DataObject() {
610 {
611 setAttributes(Map.of("a", "b", "c", "d"));
612 setDescription("Desc");
613 setName("Name");
614 setCreatedAt(date);
615 setCreatedBy(defaultUser);
616 setId(1L);
617 setShepardId(1L);
618 setCollection(collection);
619 setParent(oldParent);
620 }
621 };
622
623 DataObject updated = new DataObject() {
624 {
625 setAttributes(Map.of("1", "2", "c", "d"));
626 setDescription("newDesc");
627 setName("newName");
628 setCreatedAt(date);
629 setCreatedBy(defaultUser);
630 setUpdatedAt(updateDate);
631 setUpdatedBy(updateUser);
632 setParent(parent);
633 setId(old.getId());
634 setShepardId(old.getShepardId());
635 setCollection(collection);
636 }
637 };
638
639 DataObject oldParentSpy = spy(oldParent);
640 when(oldParentSpy.getChildren()).thenReturn(children);
641 when(dao.findByShepardId(old.getShepardId())).thenReturn(old);
642 when(dao.findByShepardId(parent.getShepardId())).thenReturn(parent);
643 when(dao.findByShepardId(oldParent.getShepardId())).thenReturn(oldParent);
644 when(dao.findByNeo4jId(parent.getId())).thenReturn(parent);
645 when(dao.createOrUpdate(old)).thenReturn(updated);
646
647 when(dateHelper.getDate()).thenReturn(updateDate);
648 when(collectionService.getCollection(collection.getShepardId())).thenReturn(collection);
649 when(userService.getCurrentUser()).thenReturn(defaultUser);
650 when(
651 permissionsService.isAccessTypeAllowedForUser(old.getShepardId(), AccessType.Write, updateUser.getUsername())
652 ).thenReturn(true);
653 when(
654 permissionsService.isAccessTypeAllowedForUser(old.getShepardId(), AccessType.Read, updateUser.getUsername())
655 ).thenReturn(true);
656
657 var actual = service.updateDataObject(collection.getShepardId(), old.getShepardId(), input);
658 verify(dao).deleteHasChildRelation(anyLong(), anyLong());
659
660 assertEquals(updated, actual);
661 }
662
663 @Test
664 public void updateDataObjectByShepardIdTest_SelfReferences() {
665 Collection collection = new Collection(100L);
666 collection.setShepardId(1005L);
667 Date date = new Date(23);
668
669 DataObjectIO input = new DataObjectIO() {
670 {
671 setId(1L);
672 setPredecessorIds(new long[] { 1L });
673 }
674 };
675 DataObject old = new DataObject() {
676 {
677 setId(input.getId());
678 setShepardId(input.getId());
679 setCollection(collection);
680 }
681 };
682
683 when(dao.findByShepardId(old.getShepardId())).thenReturn(old);
684 when(dateHelper.getDate()).thenReturn(date);
685 when(collectionService.getCollection(collection.getShepardId())).thenReturn(collection);
686 when(userService.getCurrentUser()).thenReturn(defaultUser);
687 when(
688 permissionsService.isAccessTypeAllowedForUser(old.getShepardId(), AccessType.Write, defaultUser.getUsername())
689 ).thenReturn(true);
690 when(
691 permissionsService.isAccessTypeAllowedForUser(old.getShepardId(), AccessType.Read, defaultUser.getUsername())
692 ).thenReturn(true);
693
694 assertThrows(InvalidBodyException.class, () ->
695 service.updateDataObject(collection.getShepardId(), old.getShepardId(), input)
696 );
697 }
698
699 @Test
700 public void deleteDataObjectByShepardIdTest() {
701 Date date = new Date(23);
702
703 Collection collection = new Collection();
704 collection.setShepardId(1005L);
705
706 DataObject dataObject = new DataObject(1L);
707 dataObject.setShepardId(15L);
708 dataObject.setCollection(collection);
709
710 when(dateHelper.getDate()).thenReturn(date);
711 when(dao.findByShepardId(dataObject.getShepardId())).thenReturn(dataObject);
712 when(dao.deleteDataObjectByShepardId(dataObject.getShepardId(), defaultUser, date)).thenReturn(true);
713 when(collectionService.getCollection(collection.getShepardId())).thenReturn(collection);
714 when(userService.getCurrentUser()).thenReturn(defaultUser);
715 when(
716 permissionsService.isAccessTypeAllowedForUser(
717 dataObject.getShepardId(),
718 AccessType.Write,
719 defaultUser.getUsername()
720 )
721 ).thenReturn(true);
722 when(
723 permissionsService.isAccessTypeAllowedForUser(
724 dataObject.getShepardId(),
725 AccessType.Read,
726 defaultUser.getUsername()
727 )
728 ).thenReturn(true);
729
730 assertDoesNotThrow(() -> service.deleteDataObject(1005L, dataObject.getShepardId()));
731 }
732 }