1 package de.dlr.shepard.context.references.dataobject.services;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertThrows;
5
6 import de.dlr.shepard.auth.permission.model.Permissions;
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.InvalidBodyException;
12 import de.dlr.shepard.common.exceptions.InvalidPathException;
13 import de.dlr.shepard.common.util.PermissionType;
14 import de.dlr.shepard.context.collection.daos.CollectionDAO;
15 import de.dlr.shepard.context.collection.entities.Collection;
16 import de.dlr.shepard.context.collection.entities.DataObject;
17 import de.dlr.shepard.context.collection.io.CollectionIO;
18 import de.dlr.shepard.context.collection.io.DataObjectIO;
19 import de.dlr.shepard.context.collection.services.CollectionService;
20 import de.dlr.shepard.context.collection.services.DataObjectService;
21 import de.dlr.shepard.context.references.dataobject.entities.DataObjectReference;
22 import de.dlr.shepard.context.references.dataobject.io.DataObjectReferenceIO;
23 import de.dlr.shepard.context.references.structureddata.services.StructuredDataReferenceService;
24 import de.dlr.shepard.context.version.services.VersionService;
25 import io.quarkus.test.junit.QuarkusTest;
26 import jakarta.inject.Inject;
27 import jakarta.transaction.Transactional;
28 import java.util.List;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31
32 @QuarkusTest
33 public class DataObjectReferenceServiceQuarkusTest {
34
35 @Inject
36 CollectionDAO collectionDAO;
37
38 @Inject
39 CollectionService collectionService;
40
41 @Inject
42 UserService userService;
43
44 @Inject
45 AuthenticationContext authenticationContext;
46
47 @Inject
48 StructuredDataReferenceService structuredDataReferenceService;
49
50 @Inject
51 DataObjectService dataObjectService;
52
53 @Inject
54 DataObjectReferenceService dataObjectReferenceService;
55
56 @Inject
57 VersionService versionService;
58
59 @Inject
60 CollectionReferenceService collectionReferenceService;
61
62 private final String userName = "user_" + System.currentTimeMillis();
63 private final String userName1 = "user1_" + System.currentTimeMillis();
64 Collection c1;
65 Collection c2;
66 DataObject c1do1;
67 DataObject c1do2;
68 DataObject c2do1;
69 DataObject c2do2;
70 DataObjectReference c1do1Toc1do2;
71 DataObjectReference c1do2Toc2do1;
72
73 private Collection createCollection(CollectionIO collectionToCreate) {
74 return collectionService.createCollection(collectionToCreate);
75 }
76
77 private DataObject createDataObject(Collection collection, DataObjectIO dataObjectToCreate) {
78 return dataObjectService.createDataObject(collection.getId(), dataObjectToCreate);
79 }
80
81 private DataObjectReference createDataObjectReference(
82 long collectionId,
83 String name,
84 DataObject referencingDataObject,
85 DataObject referencedDataObject
86 ) {
87 DataObjectReferenceIO dorIO = new DataObjectReferenceIO();
88 dorIO.setName(name);
89 dorIO.setReferencedDataObjectId(referencedDataObject.getShepardId());
90 return dataObjectReferenceService.createReference(collectionId, referencingDataObject.getShepardId(), dorIO);
91 }
92
93 @BeforeEach
94 public void setup() {
95 User user1 = new User(userName1);
96 userService.createOrUpdateUser(user1);
97 authenticationContext.setPrincipal(new JWTPrincipal(userName1, "key"));
98 CollectionIO c2IO = new CollectionIO();
99 c2IO.setName("c2");
100 Permissions c2Permissions = new Permissions();
101 c2Permissions.setPermissionType(PermissionType.Public);
102 c2 = collectionService.createCollection(c2IO);
103 c2.setPermissions(c2Permissions);
104 collectionDAO.createOrUpdate(c2);
105 DataObjectIO c2do1IO = new DataObjectIO();
106 c2do1IO.setName("c2do1");
107 c2do1 = createDataObject(c2, c2do1IO);
108 DataObjectIO c2do2IO = new DataObjectIO();
109 c2do2IO.setName("c2do2");
110 c2do2 = createDataObject(c2, c2do2IO);
111
112 User user = new User(userName);
113 userService.createOrUpdateUser(user);
114 authenticationContext.setPrincipal(new JWTPrincipal(userName, "key"));
115 CollectionIO c1IO = new CollectionIO();
116 c1IO.setName("c1");
117 c1 = collectionService.createCollection(c1IO);
118 DataObjectIO c1do1IO = new DataObjectIO();
119 c1do1IO.setName("c1do1");
120 c1do1 = createDataObject(c1, c1do1IO);
121 DataObjectIO c1do2IO = new DataObjectIO();
122 c1do2IO.setName("c1do2");
123 c1do2 = createDataObject(c1, c1do2IO);
124 c1do1Toc1do2 = createDataObjectReference(c1.getId(), userName, c1do1, c1do2);
125 c1do2Toc2do1 = createDataObjectReference(c1.getId(), userName, c1do2, c2do1);
126 }
127
128 @Test
129 @Transactional
130 public void findByShepardId() {
131 DataObjectReference c1do1Toc1do2Expected = dataObjectReferenceService.getReference(
132 c1.getShepardId(),
133 c1do1.getShepardId(),
134 c1do1Toc1do2.getShepardId(),
135 null
136 );
137 assertEquals(c1do1Toc1do2, c1do1Toc1do2Expected);
138 }
139
140 @Test
141 @Transactional
142 public void findByShepardIdInOtherCollection() {
143 DataObjectReference c1do2Toc2do1Expected = dataObjectReferenceService.getReference(
144 c1.getShepardId(),
145 c1do2.getShepardId(),
146 c1do2Toc2do1.getShepardId(),
147 null
148 );
149 assertEquals(c1do2Toc2do1, c1do2Toc2do1Expected);
150 }
151
152 @Test
153 @Transactional
154 public void searchNonexistingDataObjectReference() {
155 var ex = assertThrows(InvalidPathException.class, () ->
156 dataObjectReferenceService.getReference(c1.getId(), c1do1.getId(), c1.getId(), null)
157 );
158 assertEquals(ex.getMessage(), "ID ERROR - Data Object Reference with id " + c1.getId() + " is null or deleted");
159 }
160
161 @Test
162 @Transactional
163 public void searchDataObjectReferenceOfWrongDataObject() {
164 var ex = assertThrows(InvalidPathException.class, () ->
165 dataObjectReferenceService.getReference(c1.getId(), c1do2.getId(), c1do1Toc1do2.getId(), null)
166 );
167 assertEquals(ex.getMessage(), "ID ERROR - There is no association between dataObject and reference");
168 }
169
170 @Test
171 @Transactional
172 public void createReferenceNullReferenced() {
173 DataObjectReferenceIO input = new DataObjectReferenceIO() {
174 {
175 setName("MyName");
176 setReferencedDataObjectId(c1.getShepardId());
177 setRelationship("MyRelationship");
178 }
179 };
180 assertThrows(InvalidBodyException.class, () ->
181 dataObjectReferenceService.createReference(c1.getShepardId(), c1do1.getShepardId(), input)
182 );
183 }
184
185 @Test
186 @Transactional
187 public void getAllDataObjectReferencesByShepardId() {
188 List<DataObjectReference> ret = dataObjectReferenceService.getAllReferencesByDataObjectId(
189 c1.getShepardId(),
190 c1do1.getShepardId(),
191 null
192 );
193 assertEquals(1, ret.size());
194 assertEquals(c1do1Toc1do2, ret.get(0));
195 }
196
197 @Test
198 @Transactional
199 public void createReferenceToNotPermittedDataObject() {
200 authenticationContext.setPrincipal(new JWTPrincipal(userName1, "key"));
201 DataObjectReferenceIO input = new DataObjectReferenceIO() {
202 {
203 setName("MyName");
204 setReferencedDataObjectId(c1do1.getShepardId());
205 setRelationship("MyRelationship");
206 }
207 };
208 var ex = assertThrows(InvalidBodyException.class, () ->
209 dataObjectReferenceService.createReference(c2.getShepardId(), c2do1.getShepardId(), input)
210 );
211 assertEquals(
212 "You do not have permissions to access the referenced DataObject with id " + c1do1.getShepardId() + ".",
213 ex.getMessage()
214 );
215 }
216
217 @Test
218 @Transactional
219 public void createReferenceToDeletedDataObject() {
220 authenticationContext.setPrincipal(new JWTPrincipal(userName1, "key"));
221 dataObjectService.deleteDataObject(c2.getShepardId(), c2do2.getShepardId());
222 DataObjectReferenceIO input = new DataObjectReferenceIO() {
223 {
224 setName("MyName");
225 setReferencedDataObjectId(c1.getShepardId());
226 setRelationship("MyRelationship");
227 }
228 };
229 authenticationContext.setPrincipal(new JWTPrincipal(userName, "key"));
230 assertThrows(InvalidBodyException.class, () ->
231 dataObjectReferenceService.createReference(c1.getShepardId(), c1do1.getShepardId(), input)
232 );
233 }
234
235 @Test
236 @Transactional
237 public void searchDeletedDataObjectReference() {
238 dataObjectReferenceService.deleteReference(c1.getShepardId(), c1do1.getShepardId(), c1do1Toc1do2.getShepardId());
239 var ex = assertThrows(InvalidPathException.class, () ->
240 dataObjectReferenceService.getReference(c1.getId(), c1do1.getId(), c1do1Toc1do2.getId(), null)
241 );
242 assertEquals(
243 ex.getMessage(),
244 "ID ERROR - Data Object Reference with id " + c1do1Toc1do2.getId() + " is null or deleted"
245 );
246 }
247 }