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  
27  import java.util.List;
28  import org.apache.syncope.common.lib.types.AnyTypeKind;
29  import org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException;
30  import org.apache.syncope.core.persistence.api.dao.AnyTypeClassDAO;
31  import org.apache.syncope.core.persistence.api.dao.AnyTypeDAO;
32  import org.apache.syncope.core.persistence.api.entity.AnyType;
33  import org.apache.syncope.core.persistence.jpa.AbstractTest;
34  import org.junit.jupiter.api.Test;
35  import org.springframework.beans.factory.annotation.Autowired;
36  import org.springframework.transaction.annotation.Transactional;
37  
38  @Transactional("Master")
39  public class AnyTypeTest extends AbstractTest {
40  
41      @Autowired
42      private AnyTypeDAO anyTypeDAO;
43  
44      @Autowired
45      private AnyTypeClassDAO anyTypeClassDAO;
46  
47      @Test
48      public void find() {
49          AnyType userType = anyTypeDAO.findUser();
50          assertNotNull(userType);
51          assertEquals(AnyTypeKind.USER, userType.getKind());
52          assertEquals(AnyTypeKind.USER.name(), userType.getKey());
53          assertFalse(userType.getClasses().isEmpty());
54  
55          AnyType groupType = anyTypeDAO.findGroup();
56          assertNotNull(groupType);
57          assertEquals(AnyTypeKind.GROUP, groupType.getKind());
58          assertEquals(AnyTypeKind.GROUP.name(), groupType.getKey());
59          assertFalse(groupType.getClasses().isEmpty());
60  
61          AnyType otherType = anyTypeDAO.find("PRINTER");
62          assertNotNull(otherType);
63          assertEquals(AnyTypeKind.ANY_OBJECT, otherType.getKind());
64          assertEquals("PRINTER", otherType.getKey());
65      }
66  
67      @Test
68      public void findAll() {
69          List<AnyType> list = anyTypeDAO.findAll();
70          assertFalse(list.isEmpty());
71      }
72  
73      @Test
74      public void save() {
75          AnyType newType = entityFactory.newEntity(AnyType.class);
76          newType.setKey("new type");
77          newType.setKind(AnyTypeKind.ANY_OBJECT);
78          newType.add(anyTypeClassDAO.find("generic membership"));
79          newType.add(anyTypeClassDAO.find("csv"));
80  
81          newType = anyTypeDAO.save(newType);
82          assertNotNull(newType);
83          assertFalse(newType.getClasses().isEmpty());
84      }
85  
86      @Test
87      public void saveInvalidKind() {
88          assertThrows(InvalidEntityException.class, () -> {
89              AnyType newType = entityFactory.newEntity(AnyType.class);
90              newType.setKey("new type");
91              newType.setKind(AnyTypeKind.USER);
92              anyTypeDAO.save(newType);
93              entityManager().flush();
94          });
95      }
96  
97      @Test
98      public void saveInvalidName() {
99          assertThrows(InvalidEntityException.class, () -> {
100             AnyType newType = entityFactory.newEntity(AnyType.class);
101             newType.setKey("group");
102             newType.setKind(AnyTypeKind.ANY_OBJECT);
103             anyTypeDAO.save(newType);
104             entityManager().flush();
105         });
106     }
107 
108     @Test
109     public void delete() {
110         AnyType otherType = anyTypeDAO.find("PRINTER");
111         assertNotNull(otherType);
112 
113         anyTypeDAO.delete(otherType.getKey());
114         assertNull(anyTypeDAO.find("PRINTER"));
115     }
116 
117     @Test
118     public void deleteInvalid() {
119         assertThrows(IllegalArgumentException.class, () -> anyTypeDAO.delete(AnyTypeKind.USER.name()));
120     }
121 }