1 package de.dlr.shepard.context.labJournal.services;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertFalse;
5 import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
6
7 import de.dlr.shepard.auth.security.AuthenticationContext;
8 import de.dlr.shepard.auth.security.JWTPrincipal;
9 import de.dlr.shepard.auth.users.entities.User;
10 import de.dlr.shepard.auth.users.services.UserService;
11 import de.dlr.shepard.common.exceptions.InvalidPathException;
12 import de.dlr.shepard.context.collection.entities.Collection;
13 import de.dlr.shepard.context.collection.entities.DataObject;
14 import de.dlr.shepard.context.collection.io.CollectionIO;
15 import de.dlr.shepard.context.collection.io.DataObjectIO;
16 import de.dlr.shepard.context.collection.services.CollectionService;
17 import de.dlr.shepard.context.collection.services.DataObjectService;
18 import de.dlr.shepard.context.labJournal.entities.LabJournalEntry;
19 import io.quarkus.test.junit.QuarkusTest;
20 import jakarta.inject.Inject;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26
27 @QuarkusTest
28 public class LabJournalTest {
29
30 @Inject
31 LabJournalEntryService labJournalEntryService;
32
33 @Inject
34 DataObjectService dataObjectService;
35
36 @Inject
37 CollectionService collectionService;
38
39 @Inject
40 UserService userService;
41
42 @Inject
43 AuthenticationContext authenticationContext;
44
45 private Collection collection;
46 private DataObject dataObject;
47
48 private final String userName = "user_name";
49 private final String collectionName = "collection_name";
50
51 @BeforeEach
52 public void setup() {
53 User user = new User(userName);
54 userService.createOrUpdateUser(user);
55 authenticationContext.setPrincipal(new JWTPrincipal(userName, "key"));
56 CollectionIO collectionIO = new CollectionIO();
57 collectionIO.setName(collectionName);
58 collection = collectionService.createCollection(collectionIO);
59 collection.setPermissions(null);
60 DataObjectIO dataObjectIO = new DataObjectIO();
61 dataObject = dataObjectService.createDataObject(collection.getId(), dataObjectIO);
62 }
63
64 @Test
65 public void createLabJournalEntry_success() {
66 LabJournalEntry created = labJournalEntryService.createLabJournalEntry(dataObject.getId(), "content 1");
67 LabJournalEntry actual = labJournalEntryService.getLabJournalEntry(created.getId());
68 assertEquals(created, actual);
69 }
70
71 @Test
72 public void getLabJournalEntries_forInsertedLabJournals_returnsAll() throws InterruptedException {
73 List<LabJournalEntry> created = new ArrayList<>();
74 created.add(labJournalEntryService.createLabJournalEntry(dataObject.getId(), "content 1"));
75 created.add(labJournalEntryService.createLabJournalEntry(dataObject.getId(), "content 2"));
76 created.add(labJournalEntryService.createLabJournalEntry(dataObject.getId(), "content 3"));
77 created.add(labJournalEntryService.createLabJournalEntry(dataObject.getId(), "content 4"));
78
79 dataObject = dataObjectService.getDataObject(dataObject.getId());
80 List<LabJournalEntry> actual = labJournalEntryService.getLabJournalEntries(dataObject);
81
82 Collections.reverse(created);
83 assertEquals(created, actual);
84 }
85
86 @Test
87 public void getLabJournalEntries_nullDataObject_returnsEmptyList() throws InterruptedException {
88 List<LabJournalEntry> emptyList = List.of();
89 List<LabJournalEntry> actual = labJournalEntryService.getLabJournalEntries(null);
90 assertEquals(emptyList, actual);
91 }
92
93 @Test
94 public void updateLabJournalEntry_success() {
95 LabJournalEntry created = labJournalEntryService.createLabJournalEntry(dataObject.getId(), "content 1");
96 labJournalEntryService.updateLabJournalEntry(created.getId(), "content 2");
97 LabJournalEntry actual = labJournalEntryService.getLabJournalEntry(created.getId());
98 assertEquals(actual.getContent(), created.getContent());
99 }
100
101 @Test
102 public void updateLabJournalEntry_notExistsLabJournalEntry_throwsNotFound() {
103 Long largeId = Long.MAX_VALUE;
104 assertThrowsExactly(InvalidPathException.class, () ->
105 labJournalEntryService.updateLabJournalEntry(largeId, "content")
106 );
107 }
108
109 @Test
110 public void deleteLabJournalEntry_success() {
111 labJournalEntryService.createLabJournalEntry(dataObject.getId(), "content 1");
112 labJournalEntryService.createLabJournalEntry(dataObject.getId(), "content 2");
113 LabJournalEntry toBeDeleted = labJournalEntryService.createLabJournalEntry(dataObject.getId(), "content 3");
114 dataObject = dataObjectService.getDataObject(dataObject.getId());
115 labJournalEntryService.deleteLabJournalEntry(toBeDeleted.getId());
116 List<LabJournalEntry> actual = labJournalEntryService.getLabJournalEntries(dataObject);
117 assertFalse(actual.contains(toBeDeleted));
118 }
119 }