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.assertTrue;
26
27 import java.util.List;
28 import java.util.UUID;
29 import org.apache.commons.lang3.ClassUtils;
30 import org.apache.syncope.common.lib.attr.AttrRepoConf;
31 import org.apache.syncope.common.lib.attr.JDBCAttrRepoConf;
32 import org.apache.syncope.common.lib.attr.LDAPAttrRepoConf;
33 import org.apache.syncope.common.lib.attr.StubAttrRepoConf;
34 import org.apache.syncope.common.lib.attr.SyncopeAttrRepoConf;
35 import org.apache.syncope.common.lib.to.Item;
36 import org.apache.syncope.common.lib.types.AttrRepoState;
37 import org.apache.syncope.core.persistence.api.dao.AttrRepoDAO;
38 import org.apache.syncope.core.persistence.api.entity.am.AttrRepo;
39 import org.apache.syncope.core.persistence.jpa.AbstractTest;
40 import org.junit.jupiter.api.Test;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.transaction.annotation.Transactional;
43
44 @Transactional("Master")
45 public class AttrRepoTest extends AbstractTest {
46
47 @Autowired
48 private AttrRepoDAO attrRepoDAO;
49
50 @Test
51 public void findAll() {
52 List<AttrRepo> modules = attrRepoDAO.findAll();
53 assertNotNull(modules);
54 assertFalse(modules.isEmpty());
55 assertTrue(modules.size() >= 4);
56 }
57
58 @Test
59 public void find() {
60 AttrRepo attrRepo = attrRepoDAO.find("DefaultLDAPAttrRepo");
61 assertNotNull(attrRepo);
62 assertTrue(attrRepo.getConf() instanceof LDAPAttrRepoConf);
63
64 attrRepo = attrRepoDAO.find("DefaultJDBCAttrRepo");
65 assertNotNull(attrRepo);
66 assertTrue(attrRepo.getConf() instanceof JDBCAttrRepoConf);
67
68 attrRepo = attrRepoDAO.find("DefaultStubAttrRepo");
69 assertNotNull(attrRepo);
70 assertTrue(attrRepo.getConf() instanceof StubAttrRepoConf);
71 assertEquals(1, attrRepo.getItems().size());
72
73 attrRepo = attrRepoDAO.find("DefaultSyncopeAttrRepo");
74 assertNotNull(attrRepo);
75 assertTrue(attrRepo.getConf() instanceof SyncopeAttrRepoConf);
76 }
77
78 @Test
79 public void findByType() {
80 List<AttrRepo> attrRepos = attrRepoDAO.findAll();
81 assertTrue(attrRepos.stream().anyMatch(
82 attrRepo -> isSpecificConf(attrRepo.getConf(), LDAPAttrRepoConf.class)
83 && attrRepo.getKey().equals("DefaultLDAPAttrRepo")));
84 assertTrue(attrRepos.stream().anyMatch(
85 attrRepo -> isSpecificConf(attrRepo.getConf(), JDBCAttrRepoConf.class)
86 && attrRepo.getKey().equals("DefaultJDBCAttrRepo")));
87 assertTrue(attrRepos.stream().anyMatch(
88 attrRepo -> isSpecificConf(attrRepo.getConf(), SyncopeAttrRepoConf.class)
89 && attrRepo.getKey().equals("DefaultSyncopeAttrRepo")));
90 assertTrue(attrRepos.stream().anyMatch(
91 attrRepo -> isSpecificConf(attrRepo.getConf(), StubAttrRepoConf.class)
92 && attrRepo.getKey().equals("DefaultStubAttrRepo")));
93 }
94
95 @Test
96 public void saveWithStubRepo() {
97 StubAttrRepoConf conf = new StubAttrRepoConf();
98 conf.getAttributes().put("attr1", UUID.randomUUID().toString());
99 conf.getAttributes().put("attr2", UUID.randomUUID().toString());
100
101 saveAttrRepo("StaticAttrRepoTest", conf);
102 }
103
104 @Test
105 public void saveWithLdapRepo() {
106 LDAPAttrRepoConf conf = new LDAPAttrRepoConf();
107 conf.setBaseDn("dc=example,dc=org");
108 conf.setSearchFilter("cn={user}");
109 conf.setSubtreeSearch(true);
110 conf.setLdapUrl("ldap://localhost:1389");
111 conf.setBindCredential("Password");
112
113 saveAttrRepo("LDAPAttrRepoTest", conf);
114 }
115
116 @Test
117 public void saveWithJDBCRepo() {
118 JDBCAttrRepoConf conf = new JDBCAttrRepoConf();
119 conf.setSql("SELECT * FROM table WHERE name=?");
120 conf.setUrl("jdbc:h2:mem:syncopedb;DB_CLOSE_DELAY=-1");
121 conf.setUser("username");
122 conf.setPassword("password");
123
124 saveAttrRepo("JDBCAttrRepoTest", conf);
125 }
126
127 @Test
128 public void saveWithSyncopeRepo() {
129 SyncopeAttrRepoConf conf = new SyncopeAttrRepoConf();
130 conf.setDomain("Master");
131
132 saveAttrRepo("SyncopeAttrRepoTest", conf);
133 }
134
135 @Test
136 public void updateWithLDAPRepo() {
137 AttrRepo module = attrRepoDAO.find("DefaultLDAPAttrRepo");
138 assertNotNull(module);
139 AttrRepoConf conf = module.getConf();
140 LDAPAttrRepoConf.class.cast(conf).setBaseDn("dc=example2,dc=org");
141 LDAPAttrRepoConf.class.cast(conf).setSearchFilter("cn={user2}");
142 module.setConf(conf);
143
144 module = attrRepoDAO.save(module);
145 assertNotNull(module);
146 assertNotNull(module.getKey());
147
148 AttrRepo found = attrRepoDAO.find(module.getKey());
149 assertNotNull(found);
150 assertEquals("dc=example2,dc=org", LDAPAttrRepoConf.class.cast(found.getConf()).getBaseDn());
151 assertEquals("cn={user2}", LDAPAttrRepoConf.class.cast(found.getConf()).getSearchFilter());
152 }
153
154 @Test
155 public void updateWithJDBCRepo() {
156 AttrRepo module = attrRepoDAO.find("DefaultJDBCAttrRepo");
157 assertNotNull(module);
158 AttrRepoConf conf = module.getConf();
159 JDBCAttrRepoConf.class.cast(conf).setSql("SELECT * FROM otherTable WHERE name=?");
160 module.setConf(conf);
161
162 module = attrRepoDAO.save(module);
163 assertNotNull(module);
164 assertNotNull(module.getKey());
165 AttrRepo found = attrRepoDAO.find(module.getKey());
166 assertNotNull(found);
167 assertEquals("SELECT * FROM otherTable WHERE name=?", JDBCAttrRepoConf.class.cast(found.getConf()).getSql());
168 }
169
170 @Test
171 public void updateWithStubRepo() {
172 AttrRepo module = attrRepoDAO.find("DefaultStubAttrRepo");
173 assertNotNull(module);
174 assertEquals(1, StubAttrRepoConf.class.cast(module.getConf()).getAttributes().size());
175 AttrRepoConf conf = module.getConf();
176 StubAttrRepoConf.class.cast(conf).getAttributes().put("attr3", UUID.randomUUID().toString());
177 module.setConf(conf);
178
179 module = attrRepoDAO.save(module);
180 assertNotNull(module);
181 assertNotNull(module.getKey());
182 AttrRepo found = attrRepoDAO.find(module.getKey());
183 assertNotNull(found);
184 assertEquals(2, StubAttrRepoConf.class.cast(found.getConf()).getAttributes().size());
185 }
186
187 @Test
188 public void updateWithSyncopeRepo() {
189 AttrRepo module = attrRepoDAO.find("DefaultSyncopeAttrRepo");
190 assertNotNull(module);
191
192 AttrRepoConf conf = module.getConf();
193 SyncopeAttrRepoConf.class.cast(conf).setDomain("Two");
194 module.setConf(conf);
195
196 module = attrRepoDAO.save(module);
197 assertNotNull(module);
198 assertNotNull(module.getKey());
199 AttrRepo found = attrRepoDAO.find(module.getKey());
200 assertNotNull(found);
201 assertEquals("Two", SyncopeAttrRepoConf.class.cast(found.getConf()).getDomain());
202 }
203
204 @Test
205 public void delete() {
206 AttrRepo attrRepo = attrRepoDAO.find("DefaultSyncopeAttrRepo");
207 assertNotNull(attrRepo);
208
209 attrRepoDAO.delete("DefaultSyncopeAttrRepo");
210
211 attrRepo = attrRepoDAO.find("DefaultSyncopeAttrRepo");
212 assertNull(attrRepo);
213 }
214
215 private void saveAttrRepo(final String key, final AttrRepoConf conf) {
216 AttrRepo attrRepo = entityFactory.newEntity(AttrRepo.class);
217 attrRepo.setKey(key);
218 attrRepo.setDescription("An attr repo");
219 attrRepo.setState(AttrRepoState.ACTIVE);
220 attrRepo.setConf(conf);
221
222 Item keyMapping = new Item();
223 keyMapping.setIntAttrName("uid");
224 keyMapping.setExtAttrName("username");
225 attrRepo.getItems().add(keyMapping);
226
227 Item fullnameMapping = new Item();
228 fullnameMapping.setIntAttrName("cn");
229 fullnameMapping.setExtAttrName("fullname");
230 attrRepo.getItems().add(fullnameMapping);
231
232 attrRepo = attrRepoDAO.save(attrRepo);
233 entityManager().flush();
234
235 assertNotNull(attrRepo);
236 assertNotNull(attrRepo.getKey());
237 assertEquals(attrRepo, attrRepoDAO.find(attrRepo.getKey()));
238 assertEquals(2, attrRepo.getItems().size());
239 }
240
241 private static boolean isSpecificConf(final AttrRepoConf conf, final Class<? extends AttrRepoConf> clazz) {
242 return ClassUtils.isAssignable(clazz, conf.getClass());
243 }
244 }