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.persistence.api.search;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  
23  import java.util.UUID;
24  import org.apache.syncope.common.lib.search.AnyObjectFiqlSearchConditionBuilder;
25  import org.apache.syncope.common.lib.search.GroupFiqlSearchConditionBuilder;
26  import org.apache.syncope.common.lib.search.SpecialAttr;
27  import org.apache.syncope.common.lib.search.UserFiqlSearchConditionBuilder;
28  import org.apache.syncope.core.persistence.api.dao.search.AnyCond;
29  import org.apache.syncope.core.persistence.api.dao.search.AnyTypeCond;
30  import org.apache.syncope.core.persistence.api.dao.search.AttrCond;
31  import org.apache.syncope.core.persistence.api.dao.search.AuxClassCond;
32  import org.apache.syncope.core.persistence.api.dao.search.DynRealmCond;
33  import org.apache.syncope.core.persistence.api.dao.search.MemberCond;
34  import org.apache.syncope.core.persistence.api.dao.search.MembershipCond;
35  import org.apache.syncope.core.persistence.api.dao.search.PrivilegeCond;
36  import org.apache.syncope.core.persistence.api.dao.search.RelationshipCond;
37  import org.apache.syncope.core.persistence.api.dao.search.RelationshipTypeCond;
38  import org.apache.syncope.core.persistence.api.dao.search.ResourceCond;
39  import org.apache.syncope.core.persistence.api.dao.search.RoleCond;
40  import org.apache.syncope.core.persistence.api.dao.search.SearchCond;
41  import org.junit.jupiter.api.Test;
42  
43  public class SearchCondConverterTest {
44  
45      private static final SearchCondVisitor VISITOR = new SearchCondVisitor();
46  
47      @Test
48      public void eq() {
49          String fiql = new UserFiqlSearchConditionBuilder().is("username").equalTo("rossini").query();
50          assertEquals("username==rossini", fiql);
51  
52          AnyCond attrCond = new AnyCond(AttrCond.Type.EQ);
53          attrCond.setSchema("username");
54          attrCond.setExpression("rossini");
55          SearchCond leaf = SearchCond.getLeaf(attrCond);
56  
57          assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
58      }
59  
60      @Test
61      public void ieq() {
62          String fiql = new UserFiqlSearchConditionBuilder().is("username").equalToIgnoreCase("rossini").query();
63          assertEquals("username=~rossini", fiql);
64  
65          AnyCond attrCond = new AnyCond(AttrCond.Type.IEQ);
66          attrCond.setSchema("username");
67          attrCond.setExpression("rossini");
68          SearchCond leaf = SearchCond.getLeaf(attrCond);
69  
70          assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
71      }
72  
73      @Test
74      public void nieq() {
75          String fiql = new UserFiqlSearchConditionBuilder().is("username").notEqualTolIgnoreCase("rossini").query();
76          assertEquals("username!~rossini", fiql);
77  
78          AnyCond anyCond = new AnyCond(AttrCond.Type.IEQ);
79          anyCond.setSchema("username");
80          anyCond.setExpression("rossini");
81          SearchCond leaf = SearchCond.getNotLeaf(anyCond);
82  
83          assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
84      }
85  
86      @Test
87      public void like() {
88          String fiql = new UserFiqlSearchConditionBuilder().is("username").equalTo("ros*").query();
89          assertEquals("username==ros*", fiql);
90  
91          AttrCond attrCond = new AnyCond(AttrCond.Type.LIKE);
92          attrCond.setSchema("username");
93          attrCond.setExpression("ros%");
94          SearchCond leaf = SearchCond.getLeaf(attrCond);
95  
96          assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
97      }
98  
99      @Test
100     public void ilike() {
101         String fiql = new UserFiqlSearchConditionBuilder().is("username").equalToIgnoreCase("ros*").query();
102         assertEquals("username=~ros*", fiql);
103 
104         AttrCond attrCond = new AnyCond(AttrCond.Type.ILIKE);
105         attrCond.setSchema("username");
106         attrCond.setExpression("ros%");
107         SearchCond leaf = SearchCond.getLeaf(attrCond);
108 
109         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
110     }
111 
112     @Test
113     public void nilike() {
114         String fiql = new UserFiqlSearchConditionBuilder().is("username").notEqualTolIgnoreCase("ros*").query();
115         assertEquals("username!~ros*", fiql);
116 
117         AttrCond attrCond = new AnyCond(AttrCond.Type.ILIKE);
118         attrCond.setSchema("username");
119         attrCond.setExpression("ros%");
120         SearchCond leaf = SearchCond.getNotLeaf(attrCond);
121 
122         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
123     }
124 
125     @Test
126     public void isNull() {
127         String fiql = new UserFiqlSearchConditionBuilder().is("loginDate").nullValue().query();
128         assertEquals("loginDate==" + SpecialAttr.NULL, fiql);
129 
130         AttrCond attrCond = new AttrCond(AttrCond.Type.ISNULL);
131         attrCond.setSchema("loginDate");
132         SearchCond leaf = SearchCond.getLeaf(attrCond);
133 
134         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
135     }
136 
137     @Test
138     public void isNotNull() {
139         String fiql = new UserFiqlSearchConditionBuilder().is("loginDate").notNullValue().query();
140         assertEquals("loginDate!=" + SpecialAttr.NULL, fiql);
141 
142         AttrCond attrCond = new AttrCond(AttrCond.Type.ISNOTNULL);
143         attrCond.setSchema("loginDate");
144         SearchCond leaf = SearchCond.getLeaf(attrCond);
145 
146         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
147     }
148 
149     @Test
150     public void relationships() {
151         String fiql = new UserFiqlSearchConditionBuilder().
152                 inRelationships("ca20ffca-1305-442f-be9a-3723a0cd88ca").query();
153         assertEquals(SpecialAttr.RELATIONSHIPS + "==ca20ffca-1305-442f-be9a-3723a0cd88ca", fiql);
154 
155         RelationshipCond relationshipCond = new RelationshipCond();
156         relationshipCond.setAnyObject("ca20ffca-1305-442f-be9a-3723a0cd88ca");
157         SearchCond leaf = SearchCond.getLeaf(relationshipCond);
158 
159         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
160     }
161 
162     @Test
163     public void relationshipTypes() {
164         String fiql = new UserFiqlSearchConditionBuilder().inRelationshipTypes("type1").query();
165         assertEquals(SpecialAttr.RELATIONSHIP_TYPES + "==type1", fiql);
166 
167         RelationshipTypeCond relationshipCond = new RelationshipTypeCond();
168         relationshipCond.setRelationshipTypeKey("type1");
169         SearchCond leaf = SearchCond.getLeaf(relationshipCond);
170 
171         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
172 
173         fiql = new AnyObjectFiqlSearchConditionBuilder("PRINTER").inRelationshipTypes("neighborhood").query();
174         assertEquals(
175                 SpecialAttr.RELATIONSHIP_TYPES + "==neighborhood;" + SpecialAttr.TYPE + "==PRINTER",
176                 fiql);
177     }
178 
179     @Test
180     public void groups() {
181         String fiql = new UserFiqlSearchConditionBuilder().
182                 inGroups("e7ff94e8-19c9-4f0a-b8b7-28327edbf6ed").query();
183         assertEquals(SpecialAttr.GROUPS + "==e7ff94e8-19c9-4f0a-b8b7-28327edbf6ed", fiql);
184 
185         MembershipCond groupCond = new MembershipCond();
186         groupCond.setGroup("e7ff94e8-19c9-4f0a-b8b7-28327edbf6ed");
187         SearchCond leaf = SearchCond.getLeaf(groupCond);
188 
189         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
190     }
191 
192     @Test
193     public void roles() {
194         String fiql = new UserFiqlSearchConditionBuilder().inRoles("User reviewer").query();
195         assertEquals(SpecialAttr.ROLES + "==User reviewer", fiql);
196 
197         RoleCond roleCond = new RoleCond();
198         roleCond.setRole("User reviewer");
199         SearchCond leaf = SearchCond.getLeaf(roleCond);
200 
201         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
202     }
203 
204     @Test
205     public void privileges() {
206         String fiql = new UserFiqlSearchConditionBuilder().withPrivileges("postMighty").query();
207         assertEquals(SpecialAttr.PRIVILEGES + "==postMighty", fiql);
208 
209         PrivilegeCond privilegeCond = new PrivilegeCond();
210         privilegeCond.setPrivilege("postMighty");
211         SearchCond leaf = SearchCond.getLeaf(privilegeCond);
212 
213         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
214     }
215 
216     @Test
217     public void dynRealms() {
218         String dynRealm = UUID.randomUUID().toString();
219         String fiql = new UserFiqlSearchConditionBuilder().inDynRealms(dynRealm).query();
220         assertEquals(SpecialAttr.DYNREALMS + "==" + dynRealm, fiql);
221 
222         DynRealmCond dynRealmCond = new DynRealmCond();
223         dynRealmCond.setDynRealm(dynRealm);
224         SearchCond leaf = SearchCond.getLeaf(dynRealmCond);
225 
226         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
227     }
228 
229     @Test
230     public void auxClasses() {
231         String fiql = new UserFiqlSearchConditionBuilder().hasAuxClasses("clazz1").query();
232         assertEquals(SpecialAttr.AUX_CLASSES + "==clazz1", fiql);
233 
234         AuxClassCond cond = new AuxClassCond();
235         cond.setAuxClass("clazz1");
236         SearchCond leaf = SearchCond.getLeaf(cond);
237 
238         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
239     }
240 
241     @Test
242     public void resources() {
243         String fiql = new UserFiqlSearchConditionBuilder().hasResources("resource-ldap").query();
244         assertEquals(SpecialAttr.RESOURCES + "==resource-ldap", fiql);
245 
246         ResourceCond resCond = new ResourceCond();
247         resCond.setResource("resource-ldap");
248         SearchCond leaf = SearchCond.getLeaf(resCond);
249 
250         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
251     }
252 
253     @Test
254     public void type() {
255         String fiql = new AnyObjectFiqlSearchConditionBuilder("PRINTER").query();
256         assertEquals(SpecialAttr.TYPE + "==PRINTER", fiql);
257 
258         AnyTypeCond acond = new AnyTypeCond();
259         acond.setAnyTypeKey("PRINTER");
260         SearchCond leaf = SearchCond.getLeaf(acond);
261 
262         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
263     }
264 
265     @Test
266     public void member() {
267         String fiql = new GroupFiqlSearchConditionBuilder().withMembers("rossini").query();
268         assertEquals(SpecialAttr.MEMBER + "==rossini", fiql);
269 
270         MemberCond mcond = new MemberCond();
271         mcond.setMember("rossini");
272         SearchCond leaf = SearchCond.getLeaf(mcond);
273 
274         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
275     }
276 
277     @Test
278     public void and() {
279         String fiql = new UserFiqlSearchConditionBuilder().
280                 is("fullname").equalTo("*o*").and("fullname").equalTo("*i*").query();
281         assertEquals("fullname==*o*;fullname==*i*", fiql);
282 
283         AttrCond fullnameLeafCond1 = new AttrCond(AttrCond.Type.LIKE);
284         fullnameLeafCond1.setSchema("fullname");
285         fullnameLeafCond1.setExpression("%o%");
286         AttrCond fullnameLeafCond2 = new AttrCond(AttrCond.Type.LIKE);
287         fullnameLeafCond2.setSchema("fullname");
288         fullnameLeafCond2.setExpression("%i%");
289         SearchCond andCond = SearchCond.getAnd(
290                 SearchCond.getLeaf(fullnameLeafCond1),
291                 SearchCond.getLeaf(fullnameLeafCond2));
292 
293         assertEquals(andCond, SearchCondConverter.convert(VISITOR, fiql));
294     }
295 
296     @Test
297     public void or() {
298         String fiql = new UserFiqlSearchConditionBuilder().
299                 is("fullname").equalTo("*o*", "*i*", "*ini").query();
300         assertEquals("fullname==*o*,fullname==*i*,fullname==*ini", fiql);
301         fiql = new UserFiqlSearchConditionBuilder().
302                 is("fullname").equalTo("*o*").or("fullname").equalTo("*i*").or("fullname").equalTo("*ini").query();
303         assertEquals("fullname==*o*,fullname==*i*,fullname==*ini", fiql);
304 
305         AttrCond fullnameLeafCond1 = new AttrCond(AttrCond.Type.LIKE);
306         fullnameLeafCond1.setSchema("fullname");
307         fullnameLeafCond1.setExpression("%o%");
308         AttrCond fullnameLeafCond2 = new AttrCond(AttrCond.Type.LIKE);
309         fullnameLeafCond2.setSchema("fullname");
310         fullnameLeafCond2.setExpression("%i%");
311         AttrCond fullnameLeafCond3 = new AttrCond(AttrCond.Type.LIKE);
312         fullnameLeafCond3.setSchema("fullname");
313         fullnameLeafCond3.setExpression("%ini");
314         SearchCond orCond = SearchCond.getOr(SearchCond.getLeaf(fullnameLeafCond1),
315                 SearchCond.getOr(
316                         SearchCond.getLeaf(fullnameLeafCond2),
317                         SearchCond.getLeaf(fullnameLeafCond3)));
318 
319         assertEquals(orCond, SearchCondConverter.convert(VISITOR, fiql));
320     }
321 
322     @Test
323     public void issueSYNCOPE1223() {
324         String fiql = new UserFiqlSearchConditionBuilder().is("ctype").equalTo("ou=sample%252Co=isp").query();
325 
326         AttrCond cond = new AttrCond(AttrCond.Type.EQ);
327         cond.setSchema("ctype");
328         cond.setExpression("ou=sample,o=isp");
329 
330         assertEquals(SearchCond.getLeaf(cond), SearchCondConverter.convert(VISITOR, fiql));
331     }
332 
333     @Test
334     public void issueSYNCOPE1779() {
335         String fiql = new UserFiqlSearchConditionBuilder().is("username").equalToIgnoreCase("ros_*").query();
336         assertEquals("username=~ros_*", fiql);
337 
338         AnyCond anyCond = new AnyCond(AttrCond.Type.ILIKE);
339         anyCond.setSchema("username");
340         anyCond.setExpression("ros\\_%");
341         SearchCond leaf = SearchCond.getLeaf(anyCond);
342 
343         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
344 
345         fiql = "name==_018c34a9-f86b-75cf-855b-a3915cc5ff44";
346 
347         anyCond = new AnyCond(AttrCond.Type.EQ);
348         anyCond.setSchema("name");
349         anyCond.setExpression("_018c34a9-f86b-75cf-855b-a3915cc5ff44");
350         leaf = SearchCond.getLeaf(anyCond);
351 
352         assertEquals(leaf, SearchCondConverter.convert(VISITOR, fiql));
353     }
354 }