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.logic.scim;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import org.apache.syncope.common.lib.scim.SCIMComplexConf;
27  import org.apache.syncope.common.lib.scim.SCIMConf;
28  import org.apache.syncope.common.lib.scim.SCIMUserConf;
29  import org.apache.syncope.common.lib.scim.SCIMUserNameConf;
30  import org.apache.syncope.common.lib.scim.types.EmailCanonicalType;
31  import org.apache.syncope.core.persistence.api.dao.search.AnyCond;
32  import org.apache.syncope.core.persistence.api.dao.search.AttrCond;
33  import org.apache.syncope.core.persistence.api.dao.search.SearchCond;
34  import org.apache.syncope.ext.scimv2.api.type.Resource;
35  import org.junit.jupiter.api.BeforeAll;
36  import org.junit.jupiter.api.Test;
37  
38  public class SCIMFilterTest {
39  
40      private static SearchCondVisitor VISITOR;
41  
42      @BeforeAll
43      public static void setup() {
44          SCIMConf conf = new SCIMConf();
45          conf.setUserConf(new SCIMUserConf());
46          conf.getUserConf().setTitle("title");
47          conf.getUserConf().setDisplayName("cn");
48          conf.getUserConf().setUserType("userType");
49  
50          conf.getUserConf().setName(new SCIMUserNameConf());
51          conf.getUserConf().getName().setFamilyName("surname");
52  
53          SCIMComplexConf<EmailCanonicalType> email = new SCIMComplexConf<>();
54          email.setValue("email");
55          email.setType(EmailCanonicalType.work);
56          conf.getUserConf().getEmails().add(email);
57          email = new SCIMComplexConf<>();
58          email.setValue("gmail");
59          email.setType(EmailCanonicalType.home);
60          conf.getUserConf().getEmails().add(email);
61  
62          VISITOR = new SearchCondVisitor(Resource.User, conf);
63      }
64  
65      @Test
66      public void eq() {
67          SearchCond cond = SearchCondConverter.convert(VISITOR, "userName eq \"bjensen\"");
68          assertNotNull(cond);
69          assertTrue(cond.getLeaf(AnyCond.class).isPresent());
70          assertEquals("username", cond.getLeaf(AnyCond.class).get().getSchema());
71          assertEquals(AttrCond.Type.IEQ, cond.getLeaf(AnyCond.class).get().getType());
72          assertEquals("bjensen", cond.getLeaf(AnyCond.class).get().getExpression());
73      }
74  
75      @Test
76      public void sw() {
77          SearchCond cond = SearchCondConverter.convert(VISITOR, "userName sw \"J\"");
78          assertNotNull(cond);
79          assertTrue(cond.getLeaf(AnyCond.class).isPresent());
80          assertEquals("username", cond.getLeaf(AnyCond.class).get().getSchema());
81          assertEquals(AttrCond.Type.ILIKE, cond.getLeaf(AnyCond.class).get().getType());
82          assertEquals("J%", cond.getLeaf(AnyCond.class).get().getExpression());
83  
84          SearchCond fqn = SearchCondConverter.convert(
85                  VISITOR, "urn:ietf:params:scim:schemas:core:2.0:User:userName sw \"J\"");
86          assertEquals(cond, fqn);
87      }
88  
89      @Test
90      public void pr() {
91          SearchCond cond = SearchCondConverter.convert(VISITOR, "title pr");
92          assertNotNull(cond);
93          assertTrue(cond.getLeaf(AttrCond.class).isPresent());
94          assertEquals("title", cond.getLeaf(AttrCond.class).get().getSchema());
95          assertEquals(AttrCond.Type.ISNOTNULL, cond.getLeaf(AttrCond.class).get().getType());
96          assertNull(cond.getLeaf(AttrCond.class).get().getExpression());
97      }
98  
99      @Test
100     public void gt() {
101         SearchCond cond = SearchCondConverter.convert(VISITOR, "meta.lastModified gt \"2011-05-13T04:42:34Z\"");
102         assertNotNull(cond);
103         assertTrue(cond.getLeaf(AnyCond.class).isPresent());
104         assertEquals("lastChangeDate", cond.getLeaf(AnyCond.class).get().getSchema());
105         assertEquals(AttrCond.Type.GT, cond.getLeaf(AnyCond.class).get().getType());
106         assertEquals("2011-05-13T04:42:34Z", cond.getLeaf(AnyCond.class).get().getExpression());
107     }
108 
109     @Test
110     public void not() {
111         SearchCond cond = SearchCondConverter.convert(VISITOR, "not (title pr)");
112         assertNotNull(cond);
113         assertTrue(cond.getLeaf(AttrCond.class).isPresent());
114         assertEquals("title", cond.getLeaf(AttrCond.class).get().getSchema());
115         assertEquals(AttrCond.Type.ISNULL, cond.getLeaf(AttrCond.class).get().getType());
116         assertNull(cond.getLeaf(AttrCond.class).get().getExpression());
117     }
118 
119     @Test
120     public void and() {
121         SearchCond cond = SearchCondConverter.convert(VISITOR, "title pr and userName sw \"J\"");
122         assertNotNull(cond);
123         assertEquals(SearchCond.Type.AND, cond.getType());
124 
125         SearchCond left = cond.getLeft();
126         assertNotNull(left);
127         assertTrue(left.getLeaf(AttrCond.class).isPresent());
128         assertEquals("title", left.getLeaf(AttrCond.class).get().getSchema());
129         assertEquals(AttrCond.Type.ISNOTNULL, left.getLeaf(AttrCond.class).get().getType());
130         assertNull(left.getLeaf(AttrCond.class).get().getExpression());
131 
132         SearchCond right = cond.getRight();
133         assertNotNull(right);
134         assertTrue(right.getLeaf(AnyCond.class).isPresent());
135         assertEquals("username", right.getLeaf(AnyCond.class).get().getSchema());
136         assertEquals(AttrCond.Type.ILIKE, right.getLeaf(AnyCond.class).get().getType());
137         assertEquals("J%", right.getLeaf(AnyCond.class).get().getExpression());
138     }
139 
140     @Test
141     public void or() {
142         SearchCond cond = SearchCondConverter.convert(VISITOR, "title pr or displayName eq \"Other\"");
143         assertNotNull(cond);
144         assertEquals(SearchCond.Type.OR, cond.getType());
145 
146         SearchCond left = cond.getLeft();
147         assertNotNull(left);
148         assertTrue(left.getLeaf(AttrCond.class).isPresent());
149         assertEquals("title", left.getLeaf(AttrCond.class).get().getSchema());
150         assertEquals(AttrCond.Type.ISNOTNULL, left.getLeaf(AttrCond.class).get().getType());
151         assertNull(left.getLeaf(AttrCond.class).get().getExpression());
152 
153         SearchCond right = cond.getRight();
154         assertNotNull(right);
155         assertTrue(right.getLeaf(AttrCond.class).isPresent());
156         assertEquals("cn", right.getLeaf(AttrCond.class).get().getSchema());
157         assertEquals(AttrCond.Type.IEQ, right.getLeaf(AttrCond.class).get().getType());
158         assertEquals("Other", right.getLeaf(AttrCond.class).get().getExpression());
159     }
160 
161     @Test
162     public void type() {
163         SearchCond cond = SearchCondConverter.convert(
164                 VISITOR, "userType eq \"Employee\" and (emails.type eq \"work\")");
165         assertNotNull(cond);
166         assertEquals(SearchCond.Type.AND, cond.getType());
167 
168         SearchCond left = cond.getLeft();
169         assertNotNull(left);
170         assertTrue(left.getLeaf(AttrCond.class).isPresent());
171         assertEquals("userType", left.getLeaf(AttrCond.class).get().getSchema());
172         assertEquals(AttrCond.Type.IEQ, left.getLeaf(AttrCond.class).get().getType());
173         assertEquals("Employee", left.getLeaf(AttrCond.class).get().getExpression());
174 
175         SearchCond right = cond.getRight();
176         assertNotNull(right);
177         assertTrue(right.getLeaf(AttrCond.class).isPresent());
178         assertEquals("email", right.getLeaf(AttrCond.class).get().getSchema());
179         assertEquals(AttrCond.Type.ISNOTNULL, right.getLeaf(AttrCond.class).get().getType());
180     }
181 
182     @Test
183     public void name() {
184         SearchCond cond = SearchCondConverter.convert(VISITOR, "name.familyName co \"O'Malley\"");
185         assertNotNull(cond);
186         assertEquals(SearchCond.Type.LEAF, cond.getType());
187 
188         AttrCond leaf = cond.getLeaf(AttrCond.class).get();
189         assertNotNull(leaf);
190         assertEquals("surname", leaf.getSchema());
191         assertEquals(AttrCond.Type.ILIKE, leaf.getType());
192         assertEquals("%O'Malley%", leaf.getExpression());
193     }
194 
195     @Test
196     public void emails() {
197         SearchCond cond = SearchCondConverter.convert(VISITOR,
198                 "emails co \"example.com\" or emails.value co \"example.org\"");
199         assertNotNull(cond);
200         assertEquals(SearchCond.Type.OR, cond.getType());
201 
202         SearchCond left = cond.getLeft();
203         assertNotNull(left);
204         assertEquals(SearchCond.Type.OR, left.getType());
205 
206         SearchCond left1 = left.getLeft();
207         assertNotNull(left1);
208         assertTrue(left1.getLeaf(AttrCond.class).isPresent());
209         assertEquals("email", left1.getLeaf(AttrCond.class).get().getSchema());
210         assertEquals(AttrCond.Type.ILIKE, left1.getLeaf(AttrCond.class).get().getType());
211         assertEquals("%example.com%", left1.getLeaf(AttrCond.class).get().getExpression());
212 
213         SearchCond left2 = left.getRight();
214         assertNotNull(left2);
215         assertTrue(left2.getLeaf(AttrCond.class).isPresent());
216         assertEquals("gmail", left2.getLeaf(AttrCond.class).get().getSchema());
217         assertEquals(AttrCond.Type.ILIKE, left2.getLeaf(AttrCond.class).get().getType());
218         assertEquals("%example.com%", left2.getLeaf(AttrCond.class).get().getExpression());
219 
220         SearchCond right = cond.getRight();
221         assertNotNull(right);
222         assertEquals(SearchCond.Type.OR, right.getType());
223 
224         SearchCond right1 = right.getLeft();
225         assertNotNull(right1);
226         assertTrue(right1.getLeaf(AttrCond.class).isPresent());
227         assertEquals("email", right1.getLeaf(AttrCond.class).get().getSchema());
228         assertEquals(AttrCond.Type.ILIKE, right1.getLeaf(AttrCond.class).get().getType());
229         assertEquals("%example.org%", right1.getLeaf(AttrCond.class).get().getExpression());
230 
231         SearchCond right2 = right.getRight();
232         assertNotNull(right2);
233         assertTrue(right2.getLeaf(AttrCond.class).isPresent());
234         assertEquals("gmail", right2.getLeaf(AttrCond.class).get().getSchema());
235         assertEquals(AttrCond.Type.ILIKE, right2.getLeaf(AttrCond.class).get().getType());
236         assertEquals("%example.org%", right2.getLeaf(AttrCond.class).get().getExpression());
237     }
238 }