View Javadoc
1   package de.dlr.shepard.data.structureddata.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.Mockito.verify;
7   import static org.mockito.Mockito.when;
8   
9   import com.mongodb.client.MongoCollection;
10  import de.dlr.shepard.auth.permission.services.PermissionsService;
11  import de.dlr.shepard.auth.security.AuthenticationContext;
12  import de.dlr.shepard.auth.users.entities.User;
13  import de.dlr.shepard.auth.users.services.UserService;
14  import de.dlr.shepard.common.exceptions.InvalidPathException;
15  import de.dlr.shepard.common.util.AccessType;
16  import de.dlr.shepard.common.util.DateHelper;
17  import de.dlr.shepard.common.util.PermissionType;
18  import de.dlr.shepard.data.structureddata.daos.StructuredDataContainerDAO;
19  import de.dlr.shepard.data.structureddata.entities.StructuredData;
20  import de.dlr.shepard.data.structureddata.entities.StructuredDataContainer;
21  import de.dlr.shepard.data.structureddata.entities.StructuredDataPayload;
22  import de.dlr.shepard.data.structureddata.io.StructuredDataContainerIO;
23  import io.quarkus.test.InjectMock;
24  import io.quarkus.test.component.QuarkusComponentTest;
25  import jakarta.inject.Inject;
26  import jakarta.ws.rs.InternalServerErrorException;
27  import java.util.Date;
28  import java.util.List;
29  import org.bson.Document;
30  import org.junit.jupiter.api.Test;
31  
32  @QuarkusComponentTest
33  public class StructuredDataContainerServiceTest {
34  
35    @InjectMock
36    StructuredDataContainerDAO dao;
37  
38    @InjectMock
39    PermissionsService permissionsService;
40  
41    @InjectMock
42    UserService userService;
43  
44    @InjectMock
45    AuthenticationContext authenticationContext;
46  
47    @InjectMock
48    MongoCollection<Document> mongoCollection;
49  
50    @InjectMock
51    DateHelper dateHelper;
52  
53    @Inject
54    StructuredDataContainerService service;
55  
56    @InjectMock
57    StructuredDataService structuredDataService;
58  
59    @Test
60    public void getStructuredDataContainerTest_successful() {
61      String userName = "Alice";
62      var container = new StructuredDataContainer(1L);
63  
64      when(dao.findByNeo4jId(1L)).thenReturn(container);
65      when(authenticationContext.getCurrentUserName()).thenReturn(userName);
66      when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Read, userName)).thenReturn(true);
67  
68      var actual = service.getContainer(1L);
69      assertEquals(container, actual);
70    }
71  
72    @Test
73    public void getStructuredDataContainerTest_isNull() {
74      String userName = "Alice";
75  
76      when(dao.findByNeo4jId(1L)).thenReturn(null);
77      when(authenticationContext.getCurrentUserName()).thenReturn(userName);
78      when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Read, userName)).thenReturn(true);
79  
80      assertThrows(InvalidPathException.class, () -> service.getContainer(1L));
81    }
82  
83    @Test
84    public void getStructuredDataContainerTest_isDeleted() {
85      String userName = "Alice";
86      var container = new StructuredDataContainer(1L);
87      container.setDeleted(true);
88  
89      when(dao.findByNeo4jId(1L)).thenReturn(container);
90      when(authenticationContext.getCurrentUserName()).thenReturn(userName);
91      when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Read, userName)).thenReturn(true);
92  
93      assertThrows(InvalidPathException.class, () -> service.getContainer(1L));
94    }
95  
96    @Test
97    public void getAllStructuredDataContainerTest_successful() {
98      User user = new User("Alice");
99      var container1 = new StructuredDataContainer(1L);
100     var container2 = new StructuredDataContainer(2L);
101 
102     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
103     when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Read, user.getUsername())).thenReturn(true);
104     when(dao.findAllStructuredDataContainers(null, user.getUsername())).thenReturn(List.of(container1, container2));
105     when(userService.getCurrentUser()).thenReturn(user);
106 
107     var actual = service.getAllContainers(null);
108     assertEquals(List.of(container1, container2), actual);
109   }
110 
111   @Test
112   public void createStructuredDataContainerTest() {
113     var user = new User("bob");
114     var date = new Date(32);
115 
116     var input = new StructuredDataContainerIO() {
117       {
118         setName("Name");
119       }
120     };
121 
122     var toCreate = new StructuredDataContainer() {
123       {
124         setCreatedAt(date);
125         setCreatedBy(user);
126         setMongoId("collection");
127         setName("Name");
128       }
129     };
130 
131     var created = new StructuredDataContainer() {
132       {
133         setCreatedAt(date);
134         setCreatedBy(user);
135         setMongoId("database");
136         setName("Name");
137         setId(1L);
138       }
139     };
140 
141     when(structuredDataService.createStructuredDataContainer()).thenReturn("collection");
142     when(dateHelper.getDate()).thenReturn(date);
143     when(userService.getCurrentUser()).thenReturn(user);
144     when(dao.createOrUpdate(toCreate)).thenReturn(created);
145 
146     var actual = service.createContainer(input);
147     assertEquals(created, actual);
148     verify(permissionsService).createPermissions(created, user, PermissionType.Private);
149   }
150 
151   @Test
152   public void deleteStructuredDataContainerServiceTest() {
153     var user = new User("bob");
154     var date = new Date(23);
155     var old = new StructuredDataContainer(1L);
156     old.setMongoId("XYZ");
157 
158     var expected = new StructuredDataContainer(1L) {
159       {
160         setUpdatedAt(date);
161         setUpdatedBy(user);
162         setDeleted(true);
163       }
164     };
165 
166     when(userService.getCurrentUser()).thenReturn(user);
167     when(dateHelper.getDate()).thenReturn(date);
168     when(dao.findByNeo4jId(1L)).thenReturn(old);
169     when(dao.createOrUpdate(expected)).thenReturn(expected);
170     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
171     when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Read, user.getUsername())).thenReturn(true);
172     when(permissionsService.isCurrentUserOwner(1L)).thenReturn(true);
173 
174     assertDoesNotThrow(() -> service.deleteContainer(1L));
175   }
176 
177   @Test
178   public void deleteStructuredDataContainerServiceTest_isNull() {
179     var user = new User("bob");
180     var date = new Date(23);
181 
182     when(userService.getCurrentUser()).thenReturn(user);
183     when(dateHelper.getDate()).thenReturn(date);
184     when(dao.findByNeo4jId(1L)).thenReturn(null);
185 
186     assertThrows(InvalidPathException.class, () -> service.deleteContainer(1L));
187   }
188 
189   @Test
190   public void createStructuredDataTest() {
191     var user = new User("bob");
192     var date = new Date();
193     var structuredData = new StructuredData("oid", date, "name");
194     var container = new StructuredDataContainer(1L);
195     container.setMongoId("mongoId");
196     var payload = new StructuredDataPayload(structuredData, "payload");
197 
198     var updated = new StructuredDataContainer(1L);
199     updated.setMongoId("mongoId");
200     updated.addStructuredData(structuredData);
201 
202     when(dao.findByNeo4jId(1L)).thenReturn(container);
203     when(structuredDataService.createStructuredData("mongoId", payload)).thenReturn(structuredData);
204     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
205     when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Read, user.getUsername())).thenReturn(true);
206     when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Write, user.getUsername())).thenReturn(true);
207 
208     var actual = service.createStructuredData(1L, payload);
209 
210     assertEquals(new StructuredData("oid", date, "name"), actual);
211     verify(dao).createOrUpdate(updated);
212   }
213 
214   @Test
215   public void createStructuredDataTest_containerIsNull() {
216     var user = new User("bob");
217     var structuredData = new StructuredData("oid", new Date(), "name");
218     var payload = new StructuredDataPayload(structuredData, "payload");
219 
220     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
221     when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Read, user.getUsername())).thenReturn(true);
222     when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Write, user.getUsername())).thenReturn(true);
223     when(dao.findByNeo4jId(1L)).thenReturn(null);
224 
225     assertThrows(InvalidPathException.class, () -> service.createStructuredData(1L, payload));
226   }
227 
228   @Test
229   public void createStructuredDataTest_containerIsDeleted() {
230     var user = new User("bob");
231     var structuredData = new StructuredData("oid", new Date(), "name");
232     var container = new StructuredDataContainer(1L);
233     container.setDeleted(true);
234     var payload = new StructuredDataPayload(structuredData, "payload");
235 
236     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
237     when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Read, user.getUsername())).thenReturn(true);
238     when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Write, user.getUsername())).thenReturn(true);
239     when(dao.findByNeo4jId(1L)).thenReturn(container);
240 
241     assertThrows(InvalidPathException.class, () -> service.createStructuredData(1L, payload));
242   }
243 
244   @Test
245   public void createStructuredDataTest_mongoError() {
246     var user = new User("bob");
247     var structuredData = new StructuredData("oid", new Date(), "name");
248     var container = new StructuredDataContainer(1L);
249     container.setMongoId("mongoId");
250     var payload = new StructuredDataPayload(structuredData, "payload");
251 
252     when(dao.findByNeo4jId(1L)).thenReturn(container);
253     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
254     when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Read, user.getUsername())).thenReturn(true);
255     when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Write, user.getUsername())).thenReturn(true);
256     when(structuredDataService.createStructuredData("mongoId", payload)).thenThrow(InternalServerErrorException.class);
257 
258     //TODO: as of right now, this test does not properly test anything
259     // in order to implement a mongoError, we would need to introduce partial mocking on the StructuredDataService
260 
261     assertThrows(InternalServerErrorException.class, () -> service.createStructuredData(1L, payload));
262   }
263 
264   @Test
265   public void getStructuredDataTest() {
266     var user = new User("bob");
267     var container = new StructuredDataContainer(1L);
268     var structuredData = new StructuredData("oid", new Date(), "name");
269     container.setMongoId("mongoId");
270     var result = new StructuredDataPayload(structuredData, "payload");
271 
272     when(dao.findByNeo4jId(1L)).thenReturn(container);
273     when(structuredDataService.getPayload("mongoId", "oid")).thenReturn(result);
274     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
275     when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Read, user.getUsername())).thenReturn(true);
276 
277     var actual = service.getStructuredData(1L, "oid");
278     assertEquals(result, actual);
279   }
280 
281   @Test
282   public void getStructuredDataTest_containerIsNull() {
283     when(dao.findByNeo4jId(1L)).thenReturn(null);
284     assertThrows(InvalidPathException.class, () -> service.getStructuredData(1L, "oid"));
285   }
286 
287   @Test
288   public void getStructuredDataTest_containerIsDeleted() {
289     var container = new StructuredDataContainer(1L);
290     container.setMongoId("mongoId");
291     container.setDeleted(true);
292 
293     when(dao.findLightByNeo4jId(1L)).thenReturn(container);
294 
295     assertThrows(InvalidPathException.class, () -> service.getStructuredData(1L, "oid"));
296   }
297 
298   @Test
299   public void deleteStructuredDataTest() {
300     var user = new User("bob");
301     var date = new Date();
302     var container = new StructuredDataContainer(1L);
303     container.setMongoId("mongoId");
304     container.setStructuredDatas(
305       List.of(new StructuredData("abc", new Date(), "name"), new StructuredData("123", date, "name"))
306     );
307 
308     var updated = new StructuredDataContainer(1L);
309     updated.setMongoId("mongoId");
310     updated.setStructuredDatas(List.of(new StructuredData("123", date, "name")));
311 
312     when(dao.findByNeo4jId(1L)).thenReturn(container);
313     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
314     when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Read, user.getUsername())).thenReturn(true);
315     when(permissionsService.isAccessTypeAllowedForUser(1L, AccessType.Write, user.getUsername())).thenReturn(true);
316 
317     assertDoesNotThrow(() -> service.deleteStructuredData(1L, "abc"));
318     verify(dao).createOrUpdate(updated);
319   }
320 
321   @Test
322   public void deleteStructuredDataTest_containerIsNull() {
323     when(dao.findByNeo4jId(1L)).thenReturn(null);
324 
325     assertThrows(InvalidPathException.class, () -> service.deleteStructuredData(1L, "oid"));
326   }
327 
328   @Test
329   public void deleteStructuredDataTest_containerIsDeleted() {
330     var container = new StructuredDataContainer(1L);
331     container.setMongoId("mongoId");
332     container.setDeleted(true);
333 
334     when(dao.findByNeo4jId(1L)).thenReturn(container);
335 
336     assertThrows(InvalidPathException.class, () -> service.deleteStructuredData(1L, "oid"));
337   }
338 }