View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  import static org.mockito.ArgumentMatchers.any;
26  import static org.mockito.ArgumentMatchers.anyString;
27  import static org.mockito.ArgumentMatchers.eq;
28  import static org.mockito.Mockito.times;
29  import static org.mockito.Mockito.verify;
30  import static org.mockito.Mockito.when;
31  
32  import java.util.ArrayList;
33  import java.util.Collection;
34  import java.util.HashMap;
35  import java.util.Map;
36  import org.apache.commons.jexl3.JexlContext;
37  import org.apache.commons.jexl3.JxltEngine;
38  import org.apache.commons.lang3.StringUtils;
39  import org.apache.syncope.common.lib.Attr;
40  import org.apache.syncope.common.lib.to.AnyTO;
41  import org.apache.syncope.common.lib.to.RealmTO;
42  import org.apache.syncope.core.persistence.api.entity.Any;
43  import org.apache.syncope.core.persistence.api.entity.DerSchema;
44  import org.apache.syncope.core.persistence.api.entity.PlainAttr;
45  import org.apache.syncope.core.persistence.api.entity.Realm;
46  import org.apache.syncope.core.provisioning.api.AbstractTest;
47  import org.apache.syncope.core.provisioning.api.DerAttrHandler;
48  import org.apache.syncope.core.provisioning.api.jexl.JexlUtils;
49  import org.junit.jupiter.api.Test;
50  import org.mockito.Mock;
51  
52  public class JexlUtilsTest extends AbstractTest {
53  
54      @Mock
55      private JexlContext context;
56  
57      @Test
58      public void newJxltEngine() {
59          JxltEngine engine = JexlUtils.newJxltEngine();
60          assertNotNull(engine);
61      }
62  
63      @Test
64      public void isExpressionValid() {
65          String expression = "6 * 12 + 5 / 2.6";
66          assertTrue(JexlUtils.isExpressionValid(expression));
67  
68          expression = "@inv4lid expression!";
69          assertFalse(JexlUtils.isExpressionValid(expression));
70      }
71  
72      @Test
73      public void evaluate() {
74          String expression = null;
75          assertEquals(StringUtils.EMPTY, JexlUtils.evaluate(expression, context));
76  
77          expression = "6 * 12 + 5 / 2.6";
78          double result = 73.92307692307692;
79          assertEquals(result, JexlUtils.evaluate(expression, context));
80      }
81  
82      @Test
83      public void addFieldsToContext(
84              final @Mock Any<?> any,
85              final @Mock AnyTO anyTO,
86              final @Mock Realm realm,
87              final @Mock RealmTO realmTO) {
88  
89          JexlUtils.addFieldsToContext(new Exception(), context);
90          verify(context, times(2)).set(eq("cause"), any());
91  
92          String testFullPath = "testFullPath";
93          when(any.getRealm()).thenReturn(realm);
94          when(realm.getFullPath()).thenReturn(testFullPath);
95          JexlUtils.addFieldsToContext(any, context);
96          verify(context).set("realm", testFullPath);
97  
98          String testRealm = "testRealm";
99          when(anyTO.getRealm()).thenReturn(testRealm);
100         JexlUtils.addFieldsToContext(anyTO, context);
101         verify(context, times(3)).set("realm", testRealm);
102 
103         String fullPath = "test/full/path";
104         when(realm.getFullPath()).thenReturn(fullPath);
105         JexlUtils.addFieldsToContext(realm, context);
106         verify(context, times(2)).set("fullPath", fullPath);
107 
108         fullPath = "test/full/path2";
109         when(realmTO.getFullPath()).thenReturn(fullPath);
110         JexlUtils.addFieldsToContext(realmTO, context);
111         verify(context, times(2)).set("fullPath", fullPath);
112     }
113 
114     @Test
115     public void addAttrTOsToContext() {
116         String schemaName = "testSchema";
117         String value = "testValue";
118         Collection<Attr> attrs = new ArrayList<>();
119         Attr attr = new Attr.Builder(schemaName).build();
120         attrs.add(attr);
121 
122         JexlUtils.addAttrsToContext(attrs, context);
123         verify(context).set(schemaName, StringUtils.EMPTY);
124 
125         attr = new Attr.Builder(schemaName).value(value).build();
126         attrs.clear();
127         attrs.add(attr);
128 
129         JexlUtils.addAttrsToContext(attrs, context);
130         verify(context).set(schemaName, value);
131     }
132 
133     @Test
134     public void addPlainAttrsToContext(final @Mock Collection<? extends PlainAttr<?>> attrs) {
135         JexlUtils.addPlainAttrsToContext(attrs, context);
136         verify(context, times(0)).set(anyString(), any());
137     }
138 
139     @Test
140     public void addDerAttrsToContext(
141             final @Mock DerAttrHandler derAttrHandler,
142             final @Mock Any<?> any,
143             final @Mock DerSchema derSchema) {
144 
145         String expression = null;
146 
147         Map<DerSchema, String> derAttrs = new HashMap<>();
148         derAttrs.put(derSchema, expression);
149 
150         when(derAttrHandler.getValues(any())).thenReturn(derAttrs);
151         JexlUtils.addDerAttrsToContext(any, derAttrHandler, context);
152         verify(context).set(derAttrs.get(derSchema), expression);
153     }
154 
155     @Test
156     public void evaluateMandatoryCondition(
157             final @Mock DerAttrHandler derAttrHandler,
158             final @Mock Any<?> any,
159             final @Mock DerSchema derSchema,
160             final @Mock Collection<? extends PlainAttr<?>> plainAttrs) {
161 
162         String expression = null;
163 
164         Map<DerSchema, String> derAttrs = new HashMap<>();
165         derAttrs.put(derSchema, expression);
166 
167         when(any.getPlainAttrs()).thenReturn(new ArrayList<>());
168         when(derAttrHandler.getValues(any())).thenReturn(derAttrs);
169 
170         assertTrue(JexlUtils.evaluateMandatoryCondition("true", any, derAttrHandler));
171         assertFalse(JexlUtils.evaluateMandatoryCondition("false", any, derAttrHandler));
172     }
173 }