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.outer;
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  import static org.junit.jupiter.api.Assertions.fail;
25  
26  import java.util.List;
27  import org.apache.syncope.common.lib.types.AnyTypeKind;
28  import org.apache.syncope.core.persistence.api.dao.AnyMatchDAO;
29  import org.apache.syncope.core.persistence.api.dao.AnySearchDAO;
30  import org.apache.syncope.core.persistence.api.dao.AnyTypeDAO;
31  import org.apache.syncope.core.persistence.api.dao.DynRealmDAO;
32  import org.apache.syncope.core.persistence.api.dao.UserDAO;
33  import org.apache.syncope.core.persistence.api.dao.search.DynRealmCond;
34  import org.apache.syncope.core.persistence.api.dao.search.SearchCond;
35  import org.apache.syncope.core.persistence.api.entity.DynRealm;
36  import org.apache.syncope.core.persistence.api.entity.DynRealmMembership;
37  import org.apache.syncope.core.persistence.api.entity.user.User;
38  import org.apache.syncope.core.persistence.jpa.AbstractTest;
39  import org.junit.jupiter.api.Test;
40  import org.springframework.beans.factory.annotation.Autowired;
41  import org.springframework.transaction.annotation.Transactional;
42  
43  @Transactional("Master")
44  public class DynRealmTest extends AbstractTest {
45  
46      @Autowired
47      private AnyMatchDAO anyMatcher;
48  
49      @Autowired
50      private AnyTypeDAO anyTypeDAO;
51  
52      @Autowired
53      private DynRealmDAO dynRealmDAO;
54  
55      @Autowired
56      private AnySearchDAO searchDAO;
57  
58      @Autowired
59      private UserDAO userDAO;
60  
61      @Test
62      public void misc() {
63          DynRealm dynRealm = entityFactory.newEntity(DynRealm.class);
64          dynRealm.setKey("/name");
65  
66          DynRealmMembership memb = entityFactory.newEntity(DynRealmMembership.class);
67          memb.setDynRealm(dynRealm);
68          memb.setAnyType(anyTypeDAO.findUser());
69          memb.setFIQLCond("cool==true");
70  
71          dynRealm.add(memb);
72          memb.setDynRealm(dynRealm);
73  
74          // invalid key (starts with /)
75          try {
76              dynRealmDAO.save(dynRealm);
77              fail("This should not happen");
78          } catch (Exception e) {
79              assertNotNull(e);
80          }
81  
82          dynRealm.setKey("name");
83          DynRealm actual = dynRealmDAO.saveAndRefreshDynMemberships(dynRealm);
84          assertNotNull(actual);
85  
86          entityManager().flush();
87  
88          DynRealmCond dynRealmCond = new DynRealmCond();
89          dynRealmCond.setDynRealm(actual.getKey());
90          List<User> matching = searchDAO.search(SearchCond.getLeaf(dynRealmCond), AnyTypeKind.USER);
91          assertNotNull(matching);
92          assertFalse(matching.isEmpty());
93  
94          User user = matching.get(0);
95          assertTrue(anyMatcher.matches(user, SearchCond.getLeaf(dynRealmCond)));
96  
97          assertTrue(userDAO.findDynRealms(user.getKey()).contains(actual.getKey()));
98      }
99  }