1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.persistence.jpa.inner;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import java.util.List;
29 import org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException;
30 import org.apache.syncope.core.persistence.api.dao.AnyObjectDAO;
31 import org.apache.syncope.core.persistence.api.dao.RelationshipTypeDAO;
32 import org.apache.syncope.core.persistence.api.entity.RelationshipType;
33 import org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject;
34 import org.apache.syncope.core.persistence.jpa.AbstractTest;
35 import org.junit.jupiter.api.Test;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.transaction.annotation.Transactional;
38
39 @Transactional("Master")
40 public class RelationshipTypeTest extends AbstractTest {
41
42 @Autowired
43 private RelationshipTypeDAO relationshipTypeDAO;
44
45 @Autowired
46 private AnyObjectDAO anyObjectDAO;
47
48 @Test
49 public void find() {
50 RelationshipType inclusion = relationshipTypeDAO.find("inclusion");
51 assertNotNull(inclusion);
52 assertEquals("inclusion", inclusion.getKey());
53 }
54
55 @Test
56 public void findAll() {
57 List<RelationshipType> list = relationshipTypeDAO.findAll();
58 assertFalse(list.isEmpty());
59 }
60
61 @Test
62 public void save() {
63 RelationshipType newType = entityFactory.newEntity(RelationshipType.class);
64 newType.setKey("new type");
65 newType.setDescription("description");
66
67 newType = relationshipTypeDAO.save(newType);
68 assertNotNull(newType);
69 assertEquals("description", newType.getDescription());
70 }
71
72 @Test
73 public void saveInvalidName() {
74 assertThrows(InvalidEntityException.class, () -> {
75 RelationshipType newType = entityFactory.newEntity(RelationshipType.class);
76 newType.setKey("membership");
77 relationshipTypeDAO.save(newType);
78 });
79 }
80
81 @Test
82 public void delete() {
83 RelationshipType type = relationshipTypeDAO.find("neighborhood");
84 assertNotNull(type);
85
86 relationshipTypeDAO.delete(type.getKey());
87 assertNull(relationshipTypeDAO.find("neighborhood"));
88 }
89
90 @Test
91 public void deleteOnAnyObject() {
92 RelationshipType neighborhood = relationshipTypeDAO.find("neighborhood");
93 assertNotNull(neighborhood);
94
95 AnyObject anyObject = anyObjectDAO.find("fc6dbc3a-6c07-4965-8781-921e7401a4a5");
96 assertNotNull(anyObject);
97 assertNotNull(anyObject.getRelationships(neighborhood));
98 assertFalse(anyObject.getRelationships(neighborhood).isEmpty());
99
100 relationshipTypeDAO.delete("neighborhood");
101
102 entityManager().flush();
103
104 anyObject = anyObjectDAO.find("fc6dbc3a-6c07-4965-8781-921e7401a4a5");
105 assertNotNull(anyObject);
106 assertTrue(anyObject.getRelationships().isEmpty());
107 }
108 }