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.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  }