1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.provisioning.api.jexl;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertNotNull;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import java.time.OffsetDateTime;
27 import org.apache.commons.jexl3.JexlContext;
28 import org.apache.commons.jexl3.MapContext;
29 import org.apache.syncope.core.persistence.api.entity.Realm;
30 import org.apache.syncope.core.persistence.api.entity.user.User;
31 import org.apache.syncope.core.provisioning.api.AbstractTest;
32 import org.junit.jupiter.api.Test;
33
34 public class MappingTest extends AbstractTest {
35
36 @Test
37 public void anyConnObjectLink() {
38 Realm realm = mock(Realm.class);
39 when(realm.getFullPath()).thenReturn("/even");
40
41 User user = mock(User.class);
42 when(user.getUsername()).thenReturn("rossini");
43 when(user.getRealm()).thenReturn(realm);
44 assertNotNull(user);
45
46 JexlContext jexlContext = new MapContext();
47 JexlUtils.addFieldsToContext(user, jexlContext);
48
49 String connObjectLink = "'uid=' + username + ',ou=people,o=isp'";
50 assertEquals("uid=rossini,ou=people,o=isp", JexlUtils.evaluate(connObjectLink, jexlContext));
51
52 connObjectLink = "'uid=' + username + realm.replaceAll('/', ',o=') + ',ou=people,o=isp'";
53 assertEquals("uid=rossini,o=even,ou=people,o=isp", JexlUtils.evaluate(connObjectLink, jexlContext));
54 }
55
56 @Test
57 public void realmConnObjectLink() {
58 Realm realm = mock(Realm.class);
59 when(realm.getFullPath()).thenReturn("/even/two");
60 assertNotNull(realm);
61
62 JexlContext jexlContext = new MapContext();
63 JexlUtils.addFieldsToContext(realm, jexlContext);
64
65 String connObjectLink = "syncope:fullPath2Dn(fullPath, 'ou') + ',o=isp'";
66 assertEquals("ou=two,ou=even,o=isp", JexlUtils.evaluate(connObjectLink, jexlContext));
67
68 when(realm.getFullPath()).thenReturn("/even");
69 assertNotNull(realm);
70
71 jexlContext = new MapContext();
72 JexlUtils.addFieldsToContext(realm, jexlContext);
73
74 assertEquals("ou=even,o=isp", JexlUtils.evaluate(connObjectLink, jexlContext));
75 }
76
77 @Test
78 public void datetime() {
79 OffsetDateTime now = OffsetDateTime.now();
80
81 JexlContext jexlContext = new MapContext();
82 jexlContext.set("value", now);
83
84 String expression = "value.toInstant().toEpochMilli()";
85 assertEquals(now.toInstant().toEpochMilli(), JexlUtils.evaluate(expression, jexlContext));
86 }
87 }