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  
28  import java.util.List;
29  import org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException;
30  import org.apache.syncope.core.persistence.api.dao.AnyObjectDAO;
31  import org.apache.syncope.core.persistence.api.dao.RelationshipTypeDAO;
32  import org.apache.syncope.core.persistence.api.entity.RelationshipType;
33  import org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject;
34  import org.apache.syncope.core.persistence.jpa.AbstractTest;
35  import org.junit.jupiter.api.Test;
36  import org.springframework.beans.factory.annotation.Autowired;
37  import org.springframework.transaction.annotation.Transactional;
38  
39  @Transactional("Master")
40  public class RelationshipTypeTest extends AbstractTest {
41  
42      @Autowired
43      private RelationshipTypeDAO relationshipTypeDAO;
44  
45      @Autowired
46      private AnyObjectDAO anyObjectDAO;
47  
48      @Test
49      public void find() {
50          RelationshipType inclusion = relationshipTypeDAO.find("inclusion");
51          assertNotNull(inclusion);
52          assertEquals("inclusion", inclusion.getKey());
53      }
54  
55      @Test
56      public void findAll() {
57          List<RelationshipType> list = relationshipTypeDAO.findAll();
58          assertFalse(list.isEmpty());
59      }
60  
61      @Test
62      public void save() {
63          RelationshipType newType = entityFactory.newEntity(RelationshipType.class);
64          newType.setKey("new type");
65          newType.setDescription("description");
66  
67          newType = relationshipTypeDAO.save(newType);
68          assertNotNull(newType);
69          assertEquals("description", newType.getDescription());
70      }
71  
72      @Test
73      public void saveInvalidName() {
74          assertThrows(InvalidEntityException.class, () -> {
75              RelationshipType newType = entityFactory.newEntity(RelationshipType.class);
76              newType.setKey("membership");
77              relationshipTypeDAO.save(newType);
78          });
79      }
80  
81      @Test
82      public void delete() {
83          RelationshipType type = relationshipTypeDAO.find("neighborhood");
84          assertNotNull(type);
85  
86          relationshipTypeDAO.delete(type.getKey());
87          assertNull(relationshipTypeDAO.find("neighborhood"));
88      }
89  
90      @Test
91      public void deleteOnAnyObject() {
92          RelationshipType neighborhood = relationshipTypeDAO.find("neighborhood");
93          assertNotNull(neighborhood);
94  
95          AnyObject anyObject = anyObjectDAO.find("fc6dbc3a-6c07-4965-8781-921e7401a4a5");
96          assertNotNull(anyObject);
97          assertNotNull(anyObject.getRelationships(neighborhood));
98          assertFalse(anyObject.getRelationships(neighborhood).isEmpty());
99  
100         relationshipTypeDAO.delete("neighborhood");
101 
102         entityManager().flush();
103 
104         anyObject = anyObjectDAO.find("fc6dbc3a-6c07-4965-8781-921e7401a4a5");
105         assertNotNull(anyObject);
106         assertTrue(anyObject.getRelationships().isEmpty());
107     }
108 }