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.Order;
21 import org.junit.jupiter.api.Test;
22
23 @QuarkusTest
24 public class DataObjectDAOQuarkusTest {
25
26 @Inject
27 CollectionDAO collectionDAO;
28
29 @Inject
30 CollectionService collectionService;
31
32 @Inject
33 DataObjectService dataObjectService;
34
35 @Inject
36 DataObjectDAO dataObjectDAO;
37
38 @Inject
39 UserService userService;
40
41 @Inject
42 AuthenticationContext authenticationContext;
43
44 private final String userName = "user_" + System.currentTimeMillis();
45
46 Collection collection;
47 DataObject dataObject;
48 User user;
49
50 @BeforeEach
51 public void setup() {
52 if (user == null) {
53 user = new User(userName);
54 userService.createOrUpdateUser(user);
55 }
56 authenticationContext.setPrincipal(new JWTPrincipal(userName, "key"));
57 if (collection == null) {
58 CollectionIO collectionIO = new CollectionIO();
59 collectionIO.setName("collection");
60 collection = collectionService.createCollection(collectionIO);
61 }
62 if (dataObject == null) {
63 DataObjectIO dataObjectIO = new DataObjectIO();
64 dataObjectIO.setName("dataObject");
65 HashMap<String, String> attributes = new HashMap<String, String>();
66 attributes.put("key", "value");
67 attributes.put("key1", "value1");
68 dataObjectIO.setAttributes(attributes);
69 dataObject = dataObjectService.createDataObject(collection.getShepardId(), dataObjectIO);
70 }
71 }
72
73 @Test
74 @Transactional
75 @Order(1)
76 public void attributesArePresent() {
77
78 DataObject dataObjectBeforeDeletingAttributes = dataObjectService.getDataObject(dataObject.getShepardId());
79
80 assertEquals("value", dataObjectBeforeDeletingAttributes.getAttributes().get("key"));
81 assertEquals("value1", dataObjectBeforeDeletingAttributes.getAttributes().get("key1"));
82 }
83
84 @Test
85 @Transactional
86 @Order(2)
87 public void attributesAreDeleted() {
88
89 dataObjectDAO.deleteAllAttributes(dataObject);
90
91 DataObject dataObjectAfterDeletingAttributes = dataObjectService.getDataObject(dataObject.getShepardId());
92 assertEquals(0, dataObjectAfterDeletingAttributes.getAttributes().size());
93 }
94 }