1 package de.dlr.shepard.context.references.dataobject.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.daos.CollectionDAO;
10 import de.dlr.shepard.context.collection.daos.DataObjectDAO;
11 import de.dlr.shepard.context.collection.entities.Collection;
12 import de.dlr.shepard.context.collection.entities.DataObject;
13 import de.dlr.shepard.context.collection.io.CollectionIO;
14 import de.dlr.shepard.context.collection.io.DataObjectIO;
15 import de.dlr.shepard.context.collection.services.CollectionService;
16 import de.dlr.shepard.context.collection.services.DataObjectService;
17 import de.dlr.shepard.context.references.dataobject.entities.CollectionReference;
18 import de.dlr.shepard.context.references.dataobject.io.CollectionReferenceIO;
19 import de.dlr.shepard.context.references.dataobject.services.CollectionReferenceService;
20 import io.quarkus.test.junit.QuarkusTest;
21 import jakarta.inject.Inject;
22 import jakarta.transaction.Transactional;
23 import java.util.List;
24 import org.junit.jupiter.api.Test;
25
26 @QuarkusTest
27 public class CollectionReferenceDAOQuarkusTest {
28
29 @Inject
30 CollectionDAO collectionDAO;
31
32 @Inject
33 DataObjectDAO dataObjectDAO;
34
35 @Inject
36 DataObjectService dataObjectService;
37
38 @Inject
39 CollectionReferenceDAO collectionReferenceDAO;
40
41 @Inject
42 CollectionReferenceService collectionReferenceService;
43
44 @Inject
45 CollectionService collectionService;
46
47 @Inject
48 UserService userService;
49
50 @Inject
51 AuthenticationContext authenticationContext;
52
53 private final String userName = "user_" + System.currentTimeMillis();
54 private final String userName1 = "user1_" + System.currentTimeMillis();
55
56 private Collection createCollection(CollectionIO collectionToCreate) {
57 return collectionService.createCollection(collectionToCreate);
58 }
59
60 private DataObject createDataObject(long collectionShepardId, DataObjectIO dataObjectToCreate) {
61 return dataObjectService.createDataObject(collectionShepardId, dataObjectToCreate);
62 }
63
64 private CollectionReference createCollectionReference(
65 long collectionId,
66 String name,
67 DataObject referencingDataObject,
68 Collection referencedCollection
69 ) {
70 CollectionReferenceIO crIO = new CollectionReferenceIO();
71 crIO.setName(name);
72 crIO.setReferencedCollectionId(referencedCollection.getShepardId());
73 return collectionReferenceService.createReference(collectionId, referencingDataObject.getShepardId(), crIO);
74 }
75
76 @Test
77 @Transactional
78 public void test() {
79 User user = new User(userName);
80 userService.createOrUpdateUser(user);
81 authenticationContext.setPrincipal(new JWTPrincipal(userName, "key"));
82 CollectionIO c1IO = new CollectionIO();
83 c1IO.setName("c1");
84 CollectionIO c2IO = new CollectionIO();
85 c1IO.setName("c2");
86 CollectionIO c3IO = new CollectionIO();
87 c1IO.setName("c3");
88 Collection c1 = createCollection(c1IO);
89 Collection c2 = createCollection(c2IO);
90 Collection c3 = createCollection(c3IO);
91 DataObjectIO c1d1IO = new DataObjectIO();
92 c1d1IO.setName("c1d1");
93 DataObject c1d1 = createDataObject(c1.getShepardId(), c1d1IO);
94 CollectionReference c1d1Toc2 = createCollectionReference(c1.getShepardId(), "c1d1Toc2", c1d1, c2);
95 CollectionReference c1d1Toc3 = createCollectionReference(c1.getShepardId(), "c1d1Toc2", c1d1, c3);
96 List<CollectionReference> referencesByShepardId = collectionReferenceDAO.findByDataObjectShepardId(
97 c1d1.getShepardId()
98 );
99 assertEquals(2, referencesByShepardId.size());
100 assertEquals(referencesByShepardId.contains(c1d1Toc3), true);
101 assertEquals(referencesByShepardId.contains(c1d1Toc2), true);
102 List<CollectionReference> referencesByNeo4jId = collectionReferenceDAO.findByDataObjectNeo4jId(c1d1.getShepardId());
103 assertEquals(2, referencesByNeo4jId.size());
104 assertEquals(referencesByNeo4jId.contains(c1d1Toc3), true);
105 assertEquals(referencesByNeo4jId.contains(c1d1Toc2), true);
106 }
107 }