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.outer;
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.assertTrue;
26 import static org.junit.jupiter.api.Assertions.fail;
27
28 import java.util.List;
29 import java.util.UUID;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
32 import javax.persistence.EntityExistsException;
33 import org.apache.syncope.common.lib.SyncopeConstants;
34 import org.apache.syncope.common.lib.to.Item;
35 import org.apache.syncope.common.lib.types.AnyTypeKind;
36 import org.apache.syncope.common.lib.types.AttrSchemaType;
37 import org.apache.syncope.common.lib.types.IdMEntitlement;
38 import org.apache.syncope.common.lib.types.IdRepoEntitlement;
39 import org.apache.syncope.core.persistence.api.dao.DerSchemaDAO;
40 import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO;
41 import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
42 import org.apache.syncope.core.persistence.api.dao.UserDAO;
43 import org.apache.syncope.core.persistence.api.entity.PlainSchema;
44 import org.apache.syncope.core.persistence.api.entity.user.UPlainAttr;
45 import org.apache.syncope.core.persistence.jpa.AbstractTest;
46 import org.apache.syncope.core.spring.security.SyncopeAuthenticationDetails;
47 import org.apache.syncope.core.spring.security.SyncopeGrantedAuthority;
48 import org.junit.jupiter.api.AfterAll;
49 import org.junit.jupiter.api.BeforeAll;
50 import org.junit.jupiter.api.Test;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
53 import org.springframework.security.core.GrantedAuthority;
54 import org.springframework.security.core.context.SecurityContextHolder;
55 import org.springframework.transaction.annotation.Transactional;
56
57 @Transactional("Master")
58 public class PlainSchemaTest extends AbstractTest {
59
60 @Autowired
61 private UserDAO userDAO;
62
63 @Autowired
64 private PlainSchemaDAO plainSchemaDAO;
65
66 @Autowired
67 private DerSchemaDAO derSchemaDAO;
68
69 @Autowired
70 private ExternalResourceDAO resourceDAO;
71
72 @BeforeAll
73 public static void setAuthContext() {
74 List<GrantedAuthority> authorities = Stream.concat(
75 IdRepoEntitlement.values().stream(), IdMEntitlement.values().stream()).
76 map(entitlement -> new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM)).
77 collect(Collectors.toList());
78
79 UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
80 new org.springframework.security.core.userdetails.User(
81 "admin", "FAKE_PASSWORD", authorities), "FAKE_PASSWORD", authorities);
82 auth.setDetails(new SyncopeAuthenticationDetails(SyncopeConstants.MASTER_DOMAIN, null));
83 SecurityContextHolder.getContext().setAuthentication(auth);
84 }
85
86 @AfterAll
87 public static void unsetAuthContext() {
88 SecurityContextHolder.getContext().setAuthentication(null);
89 }
90
91 @Test
92 public void checkIdUniqueness() {
93 assertNotNull(derSchemaDAO.find("cn"));
94
95 PlainSchema schema = entityFactory.newEntity(PlainSchema.class);
96 schema.setKey("cn");
97 schema.setType(AttrSchemaType.String);
98 plainSchemaDAO.save(schema);
99
100 try {
101 entityManager().flush();
102 fail("This should not happen");
103 } catch (Exception e) {
104 assertTrue(e instanceof EntityExistsException || e.getCause() instanceof EntityExistsException);
105 }
106 }
107
108 private List<Item> getMappingItems(final String intAttrName) {
109 return resourceDAO.findAll().stream().
110 flatMap(resource -> resource.getProvisions().stream()).
111 flatMap(provision -> provision.getMapping().getItems().stream()).
112 filter(item -> intAttrName.equals(item.getIntAttrName())).
113 collect(Collectors.toList());
114 }
115
116 @Test
117 public void deleteFullname() {
118
119
120 resourceDAO.find("ws-target-resource-2").
121 getProvisionByAnyType(AnyTypeKind.USER.name()).get().getMapping().getItems().
122 forEach(item -> {
123 if ("fullname".equals(item.getIntAttrName())) {
124 item.setConnObjectKey(false);
125 } else if ("surname".equals(item.getIntAttrName())) {
126 item.setConnObjectKey(true);
127 }
128 });
129
130
131 PlainSchema schema = plainSchemaDAO.find("fullname");
132 assertNotNull(schema);
133
134
135 List<Item> mapItems = getMappingItems("fullname");
136 assertFalse(mapItems.isEmpty());
137
138
139 plainSchemaDAO.delete("fullname");
140
141 entityManager().flush();
142
143
144 schema = plainSchemaDAO.find("fullname");
145 assertNull(schema);
146
147
148 mapItems = getMappingItems("fullname");
149 assertTrue(mapItems.isEmpty());
150
151 assertNull(findPlainAttr("01f22fbd-b672-40af-b528-686d9b27ebc4", UPlainAttr.class));
152 assertNull(findPlainAttr(UUID.randomUUID().toString(), UPlainAttr.class));
153 assertFalse(userDAO.findByUsername("rossini").getPlainAttr("fullname").isPresent());
154 assertFalse(userDAO.findByUsername("vivaldi").getPlainAttr("fullname").isPresent());
155 }
156
157 @Test
158 public void deleteSurname() {
159
160 PlainSchema schema = plainSchemaDAO.find("surname");
161 assertNotNull(schema);
162
163
164 List<Item> mapItems = getMappingItems("surname");
165 assertFalse(mapItems.isEmpty());
166
167
168 assertEquals(2, schema.getLabels().size());
169
170
171 plainSchemaDAO.delete("surname");
172
173 entityManager().flush();
174
175
176 schema = plainSchemaDAO.find("surname");
177 assertNull(schema);
178 }
179
180 @Test
181 public void deleteFirstname() {
182 int pre = resourceDAO.find("resource-db-pull").
183 getProvisionByAnyType(AnyTypeKind.USER.name()).get().getMapping().getItems().size();
184
185 plainSchemaDAO.delete("firstname");
186 assertNull(plainSchemaDAO.find("firstname"));
187
188 assertEquals(pre - 1, resourceDAO.find("resource-db-pull").
189 getProvisionByAnyType(AnyTypeKind.USER.name()).get().getMapping().getItems().size());
190 }
191 }