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.utils;
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.assertTrue;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.Map;
35 import org.apache.commons.jexl3.JexlContext;
36 import org.apache.commons.lang3.StringUtils;
37 import org.apache.syncope.common.lib.Attr;
38 import org.apache.syncope.common.lib.to.AnyTO;
39 import org.apache.syncope.common.lib.to.RealmTO;
40 import org.apache.syncope.core.persistence.api.entity.Any;
41 import org.apache.syncope.core.persistence.api.entity.DerSchema;
42 import org.apache.syncope.core.persistence.api.entity.PlainAttr;
43 import org.apache.syncope.core.persistence.api.entity.Realm;
44 import org.apache.syncope.core.provisioning.api.AbstractTest;
45 import org.apache.syncope.core.provisioning.api.DerAttrHandler;
46 import org.apache.syncope.core.provisioning.api.jexl.JexlUtils;
47 import org.junit.jupiter.api.Test;
48 import org.mockito.Mock;
49
50 public class JexlUtilsTest extends AbstractTest {
51
52 @Mock
53 private JexlContext context;
54
55 @Test
56 public void isExpressionValid() {
57 String expression = "6 * 12 + 5 / 2.6";
58 assertTrue(JexlUtils.isExpressionValid(expression));
59
60 expression = "@inv4lid expression!";
61 assertFalse(JexlUtils.isExpressionValid(expression));
62 }
63
64 @Test
65 public void evaluate() {
66 String expression = null;
67 assertEquals(StringUtils.EMPTY, JexlUtils.evaluateExpr(expression, context));
68
69 expression = "6 * 12 + 5 / 2.6";
70 double result = 73.92307692307692;
71 assertEquals(result, JexlUtils.evaluateExpr(expression, context));
72 }
73
74 @Test
75 public void addFieldsToContext(
76 final @Mock Any<?> any,
77 final @Mock AnyTO anyTO,
78 final @Mock Realm realm,
79 final @Mock RealmTO realmTO) {
80
81 JexlUtils.addFieldsToContext(new Exception(), context);
82 verify(context, times(2)).set(eq("cause"), any());
83
84 String testFullPath = "testFullPath";
85 when(any.getRealm()).thenReturn(realm);
86 when(realm.getFullPath()).thenReturn(testFullPath);
87 JexlUtils.addFieldsToContext(any, context);
88 verify(context).set("realm", testFullPath);
89
90 String testRealm = "testRealm";
91 when(anyTO.getRealm()).thenReturn(testRealm);
92 JexlUtils.addFieldsToContext(anyTO, context);
93 verify(context, times(3)).set("realm", testRealm);
94
95 String fullPath = "test/full/path";
96 when(realm.getFullPath()).thenReturn(fullPath);
97 JexlUtils.addFieldsToContext(realm, context);
98 verify(context, times(2)).set("fullPath", fullPath);
99
100 fullPath = "test/full/path2";
101 when(realmTO.getFullPath()).thenReturn(fullPath);
102 JexlUtils.addFieldsToContext(realmTO, context);
103 verify(context, times(2)).set("fullPath", fullPath);
104 }
105
106 @Test
107 public void addAttrTOsToContext() {
108 String schemaName = "testSchema";
109 String value = "testValue";
110 Collection<Attr> attrs = new ArrayList<>();
111 Attr attr = new Attr.Builder(schemaName).build();
112 attrs.add(attr);
113
114 JexlUtils.addAttrsToContext(attrs, context);
115 verify(context).set(schemaName, StringUtils.EMPTY);
116
117 attr = new Attr.Builder(schemaName).value(value).build();
118 attrs.clear();
119 attrs.add(attr);
120
121 JexlUtils.addAttrsToContext(attrs, context);
122 verify(context).set(schemaName, value);
123 }
124
125 @Test
126 public void addPlainAttrsToContext(final @Mock Collection<? extends PlainAttr<?>> attrs) {
127 JexlUtils.addPlainAttrsToContext(attrs, context);
128 verify(context, times(0)).set(anyString(), any());
129 }
130
131 @Test
132 public void addDerAttrsToContext(
133 final @Mock DerAttrHandler derAttrHandler,
134 final @Mock Any<?> any,
135 final @Mock DerSchema derSchema) {
136
137 String expression = null;
138
139 Map<DerSchema, String> derAttrs = new HashMap<>();
140 derAttrs.put(derSchema, expression);
141
142 when(derAttrHandler.getValues(any())).thenReturn(derAttrs);
143 JexlUtils.addDerAttrsToContext(any, derAttrHandler, context);
144 verify(context).set(derAttrs.get(derSchema), expression);
145 }
146
147 @Test
148 public void evaluateMandatoryCondition(
149 final @Mock DerAttrHandler derAttrHandler,
150 final @Mock Any<?> any,
151 final @Mock DerSchema derSchema,
152 final @Mock Collection<? extends PlainAttr<?>> plainAttrs) {
153
154 String expression = null;
155
156 Map<DerSchema, String> derAttrs = new HashMap<>();
157 derAttrs.put(derSchema, expression);
158
159 when(any.getPlainAttrs()).thenReturn(new ArrayList<>());
160 when(derAttrHandler.getValues(any())).thenReturn(derAttrs);
161
162 assertTrue(JexlUtils.evaluateMandatoryCondition("true", any, derAttrHandler));
163 assertFalse(JexlUtils.evaluateMandatoryCondition("false", any, derAttrHandler));
164 }
165 }