1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.fit.core;
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.assertTrue;
25 import static org.junit.jupiter.api.Assertions.fail;
26
27 import java.io.IOException;
28 import java.util.EnumSet;
29 import java.util.List;
30 import java.util.UUID;
31 import javax.ws.rs.core.Response;
32 import org.apache.commons.lang3.ClassUtils;
33 import org.apache.commons.lang3.StringUtils;
34 import org.apache.syncope.common.lib.SyncopeClientException;
35 import org.apache.syncope.common.lib.SyncopeConstants;
36 import org.apache.syncope.common.lib.attr.AttrRepoConf;
37 import org.apache.syncope.common.lib.attr.JDBCAttrRepoConf;
38 import org.apache.syncope.common.lib.attr.LDAPAttrRepoConf;
39 import org.apache.syncope.common.lib.attr.StubAttrRepoConf;
40 import org.apache.syncope.common.lib.attr.SyncopeAttrRepoConf;
41 import org.apache.syncope.common.lib.to.AttrRepoTO;
42 import org.apache.syncope.common.lib.to.Item;
43 import org.apache.syncope.common.lib.types.AttrRepoState;
44 import org.apache.syncope.common.lib.types.CaseCanonicalizationMode;
45 import org.apache.syncope.common.rest.api.service.AttrRepoService;
46 import org.apache.syncope.fit.AbstractITCase;
47 import org.junit.jupiter.api.Test;
48
49 public class AttrRepoITCase extends AbstractITCase {
50
51 private enum AttrRepoSupportedType {
52 STUB,
53 SYNCOPE,
54 LDAP,
55 JDBC;
56
57 };
58
59 private static AttrRepoTO createAttrRepo(final AttrRepoTO attrRepo) {
60 Response response = ATTR_REPO_SERVICE.create(attrRepo);
61 if (response.getStatusInfo().getStatusCode() != Response.Status.CREATED.getStatusCode()) {
62 Exception ex = CLIENT_FACTORY.getExceptionMapper().fromResponse(response);
63 if (ex != null) {
64 throw (RuntimeException) ex;
65 }
66 }
67 return getObject(response.getLocation(), AttrRepoService.class, attrRepo.getClass());
68 }
69
70 private static AttrRepoTO buildAttrRepoTO(final AttrRepoSupportedType type) {
71 AttrRepoTO attrRepoTO = new AttrRepoTO();
72 attrRepoTO.setKey("Test" + type + "AttrRepo" + getUUIDString());
73 attrRepoTO.setDescription("A test " + type + " attr repo");
74 attrRepoTO.setState(AttrRepoState.ACTIVE);
75
76 AttrRepoConf conf;
77 switch (type) {
78 case LDAP:
79 conf = new LDAPAttrRepoConf();
80 LDAPAttrRepoConf.class.cast(conf).setBaseDn("dc=example,dc=org");
81 LDAPAttrRepoConf.class.cast(conf).setSearchFilter("cn={user}");
82 LDAPAttrRepoConf.class.cast(conf).setSubtreeSearch(true);
83 LDAPAttrRepoConf.class.cast(conf).setLdapUrl("ldap://localhost:1389");
84 LDAPAttrRepoConf.class.cast(conf).setBaseDn("cn=Directory Manager,dc=example,dc=org");
85 LDAPAttrRepoConf.class.cast(conf).setBindCredential("Password");
86 break;
87
88 case JDBC:
89 conf = new JDBCAttrRepoConf();
90 JDBCAttrRepoConf.class.cast(conf).setSql("SELECT * FROM table WHERE name=?");
91 JDBCAttrRepoConf.class.cast(conf).getUsername().add("name");
92 JDBCAttrRepoConf.class.cast(conf).getQueryAttributes().put("key1", "value1");
93 break;
94
95 case SYNCOPE:
96 conf = new SyncopeAttrRepoConf();
97 SyncopeAttrRepoConf.class.cast(conf).setDomain(SyncopeConstants.MASTER_DOMAIN);
98 break;
99
100 case STUB:
101 default:
102 conf = new StubAttrRepoConf();
103 StubAttrRepoConf.class.cast(conf).getAttributes().put("attr9", UUID.randomUUID().toString());
104 StubAttrRepoConf.class.cast(conf).getAttributes().put("attr8", UUID.randomUUID().toString());
105 break;
106 }
107 attrRepoTO.setConf(conf);
108
109 Item keyMapping = new Item();
110 keyMapping.setIntAttrName("uid");
111 keyMapping.setExtAttrName("username");
112 attrRepoTO.getItems().add(keyMapping);
113
114 Item fullnameMapping = new Item();
115 fullnameMapping.setIntAttrName("cn");
116 fullnameMapping.setExtAttrName("fullname");
117 attrRepoTO.getItems().add(fullnameMapping);
118
119 return attrRepoTO;
120 }
121
122 private static boolean isSpecificConf(final AttrRepoConf conf, final Class<? extends AttrRepoConf> clazz) {
123 return ClassUtils.isAssignable(clazz, conf.getClass());
124 }
125
126 @Test
127 public void list() {
128 List<AttrRepoTO> attrRepoTOs = ATTR_REPO_SERVICE.list();
129 assertNotNull(attrRepoTOs);
130 assertFalse(attrRepoTOs.isEmpty());
131
132 assertTrue(attrRepoTOs.stream().anyMatch(
133 attrRepo -> isSpecificConf(attrRepo.getConf(), LDAPAttrRepoConf.class)
134 && attrRepo.getKey().equals("DefaultLDAPAttrRepo")));
135 assertTrue(attrRepoTOs.stream().anyMatch(
136 attrRepo -> isSpecificConf(attrRepo.getConf(), JDBCAttrRepoConf.class)
137 && attrRepo.getKey().equals("DefaultJDBCAttrRepo")));
138 assertTrue(attrRepoTOs.stream().anyMatch(
139 attrRepo -> isSpecificConf(attrRepo.getConf(), StubAttrRepoConf.class)
140 && attrRepo.getKey().equals("DefaultStubAttrRepo")));
141 assertTrue(attrRepoTOs.stream().anyMatch(
142 attrRepo -> isSpecificConf(attrRepo.getConf(), SyncopeAttrRepoConf.class)
143 && attrRepo.getKey().equals("DefaultSyncopeAttrRepo")));
144 }
145
146 @Test
147 public void getLDAPAttrRepo() {
148 AttrRepoTO attrRepoTO = ATTR_REPO_SERVICE.read("DefaultLDAPAttrRepo");
149
150 assertNotNull(attrRepoTO);
151 assertTrue(StringUtils.isNotBlank(attrRepoTO.getDescription()));
152 assertTrue(isSpecificConf(attrRepoTO.getConf(), LDAPAttrRepoConf.class));
153 assertFalse(isSpecificConf(attrRepoTO.getConf(), JDBCAttrRepoConf.class));
154 }
155
156 @Test
157 public void getJDBCAttrRepo() {
158 AttrRepoTO attrRepoTO = ATTR_REPO_SERVICE.read("DefaultJDBCAttrRepo");
159
160 assertNotNull(attrRepoTO);
161 assertTrue(StringUtils.isNotBlank(attrRepoTO.getDescription()));
162 assertTrue(isSpecificConf(attrRepoTO.getConf(), JDBCAttrRepoConf.class));
163 assertFalse(isSpecificConf(attrRepoTO.getConf(), LDAPAttrRepoConf.class));
164 }
165
166 @Test
167 public void getStubAttrRepo() {
168 AttrRepoTO attrRepoTO = ATTR_REPO_SERVICE.read("DefaultStubAttrRepo");
169
170 assertNotNull(attrRepoTO);
171 assertTrue(StringUtils.isNotBlank(attrRepoTO.getDescription()));
172 assertTrue(isSpecificConf(attrRepoTO.getConf(), StubAttrRepoConf.class));
173 assertFalse(isSpecificConf(attrRepoTO.getConf(), SyncopeAttrRepoConf.class));
174 }
175
176 @Test
177 public void getSyncopeAttrRepo() {
178 AttrRepoTO attrRepoTO = ATTR_REPO_SERVICE.read("DefaultSyncopeAttrRepo");
179
180 assertNotNull(attrRepoTO);
181 assertTrue(StringUtils.isNotBlank(attrRepoTO.getDescription()));
182 assertTrue(isSpecificConf(attrRepoTO.getConf(), SyncopeAttrRepoConf.class));
183 assertFalse(isSpecificConf(attrRepoTO.getConf(), StubAttrRepoConf.class));
184 }
185
186 @Test
187 public void create() {
188 EnumSet.allOf(AttrRepoSupportedType.class).forEach(type -> {
189 AttrRepoTO attrRepoTO = createAttrRepo(buildAttrRepoTO(type));
190 assertNotNull(attrRepoTO);
191 assertTrue(attrRepoTO.getDescription().contains("A test " + type + " attr repo"));
192 assertEquals(2, attrRepoTO.getItems().size());
193 });
194 }
195
196 @Test
197 public void updateLDAPAttrRepo() {
198 AttrRepoTO ldapAttrRepoTO = ATTR_REPO_SERVICE.read("DefaultLDAPAttrRepo");
199 assertNotNull(ldapAttrRepoTO);
200
201 AttrRepoTO newLdapAttrRepoTO = buildAttrRepoTO(AttrRepoSupportedType.LDAP);
202 newLdapAttrRepoTO = createAttrRepo(newLdapAttrRepoTO);
203 assertNotNull(newLdapAttrRepoTO);
204
205 AttrRepoConf conf = ldapAttrRepoTO.getConf();
206 assertNotNull(conf);
207 LDAPAttrRepoConf.class.cast(conf).setSubtreeSearch(false);
208 newLdapAttrRepoTO.setConf(conf);
209
210
211 ATTR_REPO_SERVICE.update(newLdapAttrRepoTO);
212 newLdapAttrRepoTO = ATTR_REPO_SERVICE.read(newLdapAttrRepoTO.getKey());
213 assertNotNull(newLdapAttrRepoTO);
214
215 conf = newLdapAttrRepoTO.getConf();
216 assertFalse(LDAPAttrRepoConf.class.cast(conf).isSubtreeSearch());
217 }
218
219 @Test
220 public void updateJDBCAttrRepo() {
221 AttrRepoTO jdbcAttrRepoTO = ATTR_REPO_SERVICE.read("DefaultJDBCAttrRepo");
222 assertNotNull(jdbcAttrRepoTO);
223
224 AttrRepoTO newJDBCAttrRepoTO = buildAttrRepoTO(AttrRepoSupportedType.JDBC);
225 newJDBCAttrRepoTO = createAttrRepo(newJDBCAttrRepoTO);
226 assertNotNull(newJDBCAttrRepoTO);
227
228 AttrRepoConf conf = jdbcAttrRepoTO.getConf();
229 assertNotNull(conf);
230 JDBCAttrRepoConf.class.cast(conf).setCaseCanonicalization(CaseCanonicalizationMode.UPPER);
231 newJDBCAttrRepoTO.setConf(conf);
232
233
234 ATTR_REPO_SERVICE.update(newJDBCAttrRepoTO);
235 newJDBCAttrRepoTO = ATTR_REPO_SERVICE.read(newJDBCAttrRepoTO.getKey());
236 assertNotNull(newJDBCAttrRepoTO);
237
238 conf = newJDBCAttrRepoTO.getConf();
239 assertEquals(CaseCanonicalizationMode.UPPER, JDBCAttrRepoConf.class.cast(conf).getCaseCanonicalization());
240 }
241
242 @Test
243 public void updateStubAttrRepo() {
244 AttrRepoTO staticAttrRepoTO = ATTR_REPO_SERVICE.read("DefaultStubAttrRepo");
245 assertNotNull(staticAttrRepoTO);
246
247 AttrRepoTO newStubAttrRepoTO = buildAttrRepoTO(AttrRepoSupportedType.STUB);
248 newStubAttrRepoTO = createAttrRepo(newStubAttrRepoTO);
249 assertNotNull(newStubAttrRepoTO);
250
251 AttrRepoConf conf = staticAttrRepoTO.getConf();
252 assertNotNull(conf);
253 assertEquals(1, StubAttrRepoConf.class.cast(conf).getAttributes().size());
254 StubAttrRepoConf.class.cast(conf).getAttributes().put("attr3", "value3");
255 newStubAttrRepoTO.setConf(conf);
256
257
258 ATTR_REPO_SERVICE.update(newStubAttrRepoTO);
259 newStubAttrRepoTO = ATTR_REPO_SERVICE.read(newStubAttrRepoTO.getKey());
260 assertNotNull(newStubAttrRepoTO);
261
262 conf = newStubAttrRepoTO.getConf();
263 assertEquals(2, StubAttrRepoConf.class.cast(conf).getAttributes().size());
264 }
265
266 @Test
267 public void updateSyncopeAttrRepo() {
268 AttrRepoTO syncopeAttrRepoTO = ATTR_REPO_SERVICE.read("DefaultSyncopeAttrRepo");
269 assertNotNull(syncopeAttrRepoTO);
270
271 AttrRepoTO newSyncopeAttrRepoTO = buildAttrRepoTO(AttrRepoSupportedType.SYNCOPE);
272 newSyncopeAttrRepoTO = createAttrRepo(newSyncopeAttrRepoTO);
273 assertNotNull(newSyncopeAttrRepoTO);
274
275 AttrRepoConf conf = syncopeAttrRepoTO.getConf();
276 assertNotNull(conf);
277 SyncopeAttrRepoConf.class.cast(conf).setDomain("Two");
278 newSyncopeAttrRepoTO.setConf(conf);
279
280
281 ATTR_REPO_SERVICE.update(newSyncopeAttrRepoTO);
282 newSyncopeAttrRepoTO = ATTR_REPO_SERVICE.read(newSyncopeAttrRepoTO.getKey());
283 assertNotNull(newSyncopeAttrRepoTO);
284
285 conf = newSyncopeAttrRepoTO.getConf();
286 assertEquals("Two", SyncopeAttrRepoConf.class.cast(conf).getDomain());
287 }
288
289 @Test
290 public void delete() throws IOException {
291 EnumSet.allOf(AttrRepoSupportedType.class).forEach(type -> {
292 AttrRepoTO read = createAttrRepo(buildAttrRepoTO(type));
293 assertNotNull(read);
294
295 ATTR_REPO_SERVICE.delete(read.getKey());
296
297 try {
298 ATTR_REPO_SERVICE.read(read.getKey());
299 fail("This should not happen");
300 } catch (SyncopeClientException e) {
301 assertNotNull(e);
302 }
303 });
304 }
305 }