1 package de.dlr.shepard.context.collection.daos;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4
5 import de.dlr.shepard.auth.security.AuthenticationContext;
6 import de.dlr.shepard.auth.security.JWTPrincipal;
7 import de.dlr.shepard.auth.users.entities.User;
8 import de.dlr.shepard.auth.users.services.UserService;
9 import de.dlr.shepard.context.collection.entities.Collection;
10 import de.dlr.shepard.context.collection.entities.DataObject;
11 import de.dlr.shepard.context.collection.io.CollectionIO;
12 import de.dlr.shepard.context.collection.io.DataObjectIO;
13 import de.dlr.shepard.context.collection.services.CollectionService;
14 import de.dlr.shepard.context.collection.services.DataObjectService;
15 import io.quarkus.test.junit.QuarkusTest;
16 import jakarta.inject.Inject;
17 import jakarta.transaction.Transactional;
18 import java.util.HashMap;
19 import org.junit.jupiter.api.BeforeEach;
20 import org.junit.jupiter.api.MethodOrderer;
21 import org.junit.jupiter.api.Order;
22 import org.junit.jupiter.api.Test;
23 import org.junit.jupiter.api.TestMethodOrder;
24
25 @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
26 @QuarkusTest
27 public class DataObjectDAOQuarkusTest {
28
29 @Inject
30 CollectionDAO collectionDAO;
31
32 @Inject
33 CollectionService collectionService;
34
35 @Inject
36 DataObjectService dataObjectService;
37
38 @Inject
39 DataObjectDAO dataObjectDAO;
40
41 @Inject
42 UserService userService;
43
44 @Inject
45 AuthenticationContext authenticationContext;
46
47 private final String userName = "user_" + System.currentTimeMillis();
48
49 Collection collection;
50 DataObject dataObject;
51 User user;
52
53 @BeforeEach
54 public void setup() {
55 if (user == null) {
56 user = new User(userName);
57 userService.createOrUpdateUser(user);
58 }
59 authenticationContext.setPrincipal(new JWTPrincipal(userName, "key"));
60 if (collection == null) {
61 CollectionIO collectionIO = new CollectionIO();
62 collectionIO.setName("collection");
63 collection = collectionService.createCollection(collectionIO);
64 }
65 if (dataObject == null) {
66 DataObjectIO dataObjectIO = new DataObjectIO();
67 dataObjectIO.setName("dataObject");
68 HashMap<String, String> attributes = new HashMap<String, String>();
69 attributes.put("key", "value");
70 attributes.put("key1", "value1");
71 dataObjectIO.setAttributes(attributes);
72 dataObject = dataObjectService.createDataObject(collection.getShepardId(), dataObjectIO);
73 }
74 }
75
76 @Test
77 @Transactional
78 @Order(1)
79 public void attributesArePresent() {
80
81 DataObject dataObjectBeforeDeletingAttributes = dataObjectService.getDataObject(dataObject.getShepardId());
82
83 assertEquals("value", dataObjectBeforeDeletingAttributes.getAttributes().get("key"));
84 assertEquals("value1", dataObjectBeforeDeletingAttributes.getAttributes().get("key1"));
85 }
86
87 @Test
88 @Transactional
89 @Order(2)
90 public void attributesAreDeleted() {
91
92 dataObjectDAO.deleteAllAttributes(dataObject);
93
94 DataObject dataObjectAfterDeletingAttributes = dataObjectService.getDataObject(dataObject.getShepardId());
95 assertEquals(0, dataObjectAfterDeletingAttributes.getAttributes().size());
96 }
97 }