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.jpa.inner;
20  
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import org.apache.syncope.core.persistence.api.dao.AnyMatchDAO;
26  import org.apache.syncope.core.persistence.api.dao.AnyObjectDAO;
27  import org.apache.syncope.core.persistence.api.dao.GroupDAO;
28  import org.apache.syncope.core.persistence.api.dao.UserDAO;
29  import org.apache.syncope.core.persistence.api.dao.search.AnyCond;
30  import org.apache.syncope.core.persistence.api.dao.search.AttrCond;
31  import org.apache.syncope.core.persistence.api.dao.search.MembershipCond;
32  import org.apache.syncope.core.persistence.api.dao.search.RelationshipCond;
33  import org.apache.syncope.core.persistence.api.dao.search.RelationshipTypeCond;
34  import org.apache.syncope.core.persistence.api.dao.search.ResourceCond;
35  import org.apache.syncope.core.persistence.api.dao.search.RoleCond;
36  import org.apache.syncope.core.persistence.api.dao.search.SearchCond;
37  import org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject;
38  import org.apache.syncope.core.persistence.api.entity.group.Group;
39  import org.apache.syncope.core.persistence.api.entity.user.User;
40  import org.apache.syncope.core.persistence.jpa.AbstractTest;
41  import org.junit.jupiter.api.Test;
42  import org.springframework.beans.factory.annotation.Autowired;
43  import org.springframework.transaction.annotation.Transactional;
44  
45  @Transactional("Master")
46  public class AnyMatchTest extends AbstractTest {
47  
48      @Autowired
49      private UserDAO userDAO;
50  
51      @Autowired
52      private GroupDAO groupDAO;
53  
54      @Autowired
55      private AnyObjectDAO anyObjectDAO;
56  
57      @Autowired
58      private AnyMatchDAO anyMatcher;
59  
60      @Test
61      public void byResourceCond() {
62          User user = userDAO.find("1417acbe-cbf6-4277-9372-e75e04f97000");
63          assertNotNull(user);
64  
65          ResourceCond resourceCond = new ResourceCond();
66          resourceCond.setResource("resource-testdb2");
67          assertTrue(anyMatcher.matches(user, SearchCond.getLeaf(resourceCond)));
68  
69          resourceCond.setResource("ws-target-resource-delete");
70          assertFalse(anyMatcher.matches(user, SearchCond.getLeaf(resourceCond)));
71      }
72  
73      @Test
74      public void anyObjectMatch() {
75          AnyObject anyObject = anyObjectDAO.find("fc6dbc3a-6c07-4965-8781-921e7401a4a5");
76          assertNotNull(anyObject);
77  
78          RelationshipCond relationshipCond = new RelationshipCond();
79          relationshipCond.setAnyObject("8559d14d-58c2-46eb-a2d4-a7d35161e8f8");
80          assertTrue(anyMatcher.matches(anyObject, SearchCond.getLeaf(relationshipCond)));
81  
82          RelationshipTypeCond relationshipTypeCond = new RelationshipTypeCond();
83          relationshipTypeCond.setRelationshipTypeKey("neighborhood");
84          assertTrue(anyMatcher.matches(anyObject, SearchCond.getLeaf(relationshipTypeCond)));
85      }
86  
87      @Test
88      public void userMatch() {
89          User user = userDAO.find("1417acbe-cbf6-4277-9372-e75e04f97000");
90          assertNotNull(user);
91  
92          MembershipCond groupCond = new MembershipCond();
93          groupCond.setGroup("secretary");
94          assertFalse(anyMatcher.matches(user, SearchCond.getLeaf(groupCond)));
95  
96          groupCond.setGroup("root");
97          assertTrue(anyMatcher.matches(user, SearchCond.getLeaf(groupCond)));
98  
99          RoleCond roleCond = new RoleCond();
100         roleCond.setRole("Other");
101         assertTrue(anyMatcher.matches(user, SearchCond.getLeaf(roleCond)));
102 
103         user = userDAO.find("c9b2dec2-00a7-4855-97c0-d854842b4b24");
104         assertNotNull(user);
105 
106         RelationshipCond relationshipCond = new RelationshipCond();
107         relationshipCond.setAnyObject("fc6dbc3a-6c07-4965-8781-921e7401a4a5");
108         assertTrue(anyMatcher.matches(user, SearchCond.getLeaf(relationshipCond)));
109 
110         RelationshipTypeCond relationshipTypeCond = new RelationshipTypeCond();
111         relationshipTypeCond.setRelationshipTypeKey("neighborhood");
112         assertTrue(anyMatcher.matches(user, SearchCond.getLeaf(relationshipTypeCond)));
113     }
114 
115     @Test
116     public void groupMatch() {
117         Group group = groupDAO.find("37d15e4c-cdc1-460b-a591-8505c8133806");
118         assertNotNull(group);
119 
120         AnyCond anyCond = new AnyCond();
121         anyCond.setSchema("name");
122         anyCond.setExpression("root");
123         anyCond.setType(AttrCond.Type.EQ);
124         assertTrue(anyMatcher.matches(group, SearchCond.getLeaf(anyCond)));
125 
126         AttrCond attrCond = new AttrCond();
127         attrCond.setSchema("show");
128         attrCond.setType(AttrCond.Type.ISNOTNULL);
129         assertTrue(anyMatcher.matches(group, SearchCond.getLeaf(attrCond)));
130     }
131 }