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.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertNull;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  import static org.junit.jupiter.api.Assertions.fail;
28  
29  import java.util.List;
30  import org.apache.syncope.common.lib.SyncopeConstants;
31  import org.apache.syncope.common.lib.types.EntityViolationType;
32  import org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException;
33  import org.apache.syncope.core.persistence.api.dao.MalformedPathException;
34  import org.apache.syncope.core.persistence.api.dao.PolicyDAO;
35  import org.apache.syncope.core.persistence.api.dao.RealmDAO;
36  import org.apache.syncope.core.persistence.api.entity.Realm;
37  import org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy;
38  import org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy;
39  import org.apache.syncope.core.persistence.jpa.AbstractTest;
40  import org.junit.jupiter.api.Assertions;
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 RealmTest extends AbstractTest {
47  
48      @Autowired
49      private RealmDAO realmDAO;
50  
51      @Autowired
52      private PolicyDAO policyDAO;
53  
54      @Test
55      public void getRoot() {
56          assertNotNull(realmDAO.getRoot());
57      }
58  
59      @Test
60      public void find() {
61          Realm realm = realmDAO.find("e4c28e7a-9dbf-4ee7-9441-93812a0d4a28");
62          assertNotNull(realm);
63          assertEquals(SyncopeConstants.ROOT_REALM, realm.getName());
64          assertEquals(SyncopeConstants.ROOT_REALM, realm.getFullPath());
65  
66          realm = realmDAO.find("c5b75db1-fce7-470f-b780-3b9934d82a9d");
67          assertNotNull(realm);
68          assertEquals("even", realm.getName());
69          assertEquals("/even", realm.getFullPath());
70          assertEquals("e4c28e7a-9dbf-4ee7-9441-93812a0d4a28", realm.getParent().getKey());
71          assertEquals(realmDAO.getRoot(), realm.getParent());
72  
73          realm = realmDAO.findByFullPath("/even/two");
74          assertNotNull(realm);
75          assertEquals("0679e069-7355-4b20-bd11-a5a0a5453c7c", realm.getKey());
76          assertEquals("two", realm.getName());
77          assertEquals("/even/two", realm.getFullPath());
78      }
79  
80      @Test
81      public void findInvalidPath() {
82          assertThrows(MalformedPathException.class, () -> realmDAO.findByFullPath("even/two"));
83      }
84  
85      @Test
86      public void findChildren() {
87          List<Realm> children = realmDAO.findChildren(realmDAO.findByFullPath(SyncopeConstants.ROOT_REALM));
88          assertEquals(2, children.size());
89          assertTrue(children.contains(realmDAO.findByFullPath("/odd")));
90          assertTrue(children.contains(realmDAO.findByFullPath("/even")));
91  
92          children = realmDAO.findChildren(realmDAO.findByFullPath("/odd"));
93          assertTrue(children.isEmpty());
94      }
95  
96      @Test
97      public void findAll() {
98          List<Realm> list = realmDAO.findDescendants(realmDAO.getRoot().getFullPath(), null, -1, -1);
99          assertNotNull(list);
100         assertFalse(list.isEmpty());
101         list.forEach(Assertions::assertNotNull);
102     }
103 
104     @Test
105     public void save() {
106         Realm realm = entityFactory.newEntity(Realm.class);
107         realm.setName("last");
108         realm.setParent(realmDAO.findByFullPath("/even/two"));
109 
110         Realm actual = realmDAO.save(realm);
111         assertNotNull(actual.getKey());
112         assertEquals("last", actual.getName());
113         assertEquals("/even/two/last", actual.getFullPath());
114         assertEquals(realmDAO.findByFullPath("/even/two"), actual.getParent());
115         assertEquals("20ab5a8c-4b0c-432c-b957-f7fb9784d9f7", realm.getAccountPolicy().getKey());
116         assertEquals("ce93fcda-dc3a-4369-a7b0-a6108c261c85", realm.getPasswordPolicy().getKey());
117 
118         realm = actual;
119         realm.setAccountPolicy((AccountPolicy) policyDAO.find("06e2ed52-6966-44aa-a177-a0ca7434201f"));
120         realm.setPasswordPolicy((PasswordPolicy) policyDAO.find("986d1236-3ac5-4a19-810c-5ab21d79cba1"));
121 
122         actual = realmDAO.save(realm);
123         assertEquals("06e2ed52-6966-44aa-a177-a0ca7434201f", actual.getAccountPolicy().getKey());
124         assertEquals("986d1236-3ac5-4a19-810c-5ab21d79cba1", actual.getPasswordPolicy().getKey());
125     }
126 
127     @Test
128     public void saveInvalidName() {
129         Realm realm = entityFactory.newEntity(Realm.class);
130         realm.setName(" a name");
131         realm.setParent(realmDAO.getRoot());
132 
133         try {
134             realmDAO.save(realm);
135             fail("This should not happen");
136         } catch (InvalidEntityException e) {
137             assertTrue(e.hasViolation(EntityViolationType.InvalidRealm));
138         }
139     }
140 
141     @Test
142     public void saveNullParent() {
143         Realm realm = entityFactory.newEntity(Realm.class);
144         realm.setName("name");
145         realm.setParent(null);
146 
147         try {
148             realmDAO.save(realm);
149             fail("This should not happen");
150         } catch (InvalidEntityException e) {
151             assertTrue(e.hasViolation(EntityViolationType.InvalidRealm));
152         }
153     }
154 
155     @Test
156     public void delete() {
157         Realm realm = entityFactory.newEntity(Realm.class);
158         realm.setName("name");
159         realm.setParent(realmDAO.getRoot());
160 
161         Realm actual = realmDAO.save(realm);
162         assertNotNull(actual);
163 
164         actual = realmDAO.find(actual.getKey());
165         assertNotNull(actual);
166 
167         realmDAO.delete(actual);
168         assertNull(realmDAO.find(actual.getKey()));
169     }
170 }