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.fit.core;
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.assertTrue;
25  import static org.junit.jupiter.api.Assertions.fail;
26  
27  import java.util.List;
28  import javax.ws.rs.core.Response;
29  import org.apache.syncope.common.lib.SyncopeClientException;
30  import org.apache.syncope.common.lib.to.AnyTypeClassTO;
31  import org.apache.syncope.common.lib.to.AnyTypeTO;
32  import org.apache.syncope.common.lib.types.AnyTypeKind;
33  import org.apache.syncope.common.lib.types.ClientExceptionType;
34  import org.apache.syncope.common.rest.api.service.AnyTypeClassService;
35  import org.apache.syncope.common.rest.api.service.AnyTypeService;
36  import org.apache.syncope.fit.AbstractITCase;
37  import org.junit.jupiter.api.Test;
38  
39  public class AnyTypeITCase extends AbstractITCase {
40  
41      @Test
42      public void read() {
43          AnyTypeTO userType = ANY_TYPE_SERVICE.read(AnyTypeKind.USER.name());
44          assertNotNull(userType);
45          assertEquals(AnyTypeKind.USER, userType.getKind());
46          assertEquals(AnyTypeKind.USER.name(), userType.getKey());
47          assertFalse(userType.getClasses().isEmpty());
48  
49          AnyTypeTO groupType = ANY_TYPE_SERVICE.read(AnyTypeKind.GROUP.name());
50          assertNotNull(groupType);
51          assertEquals(AnyTypeKind.GROUP, groupType.getKind());
52          assertEquals(AnyTypeKind.GROUP.name(), groupType.getKey());
53          assertFalse(groupType.getClasses().isEmpty());
54  
55          AnyTypeTO otherType = ANY_TYPE_SERVICE.read(PRINTER);
56          assertNotNull(otherType);
57          assertEquals(AnyTypeKind.ANY_OBJECT, otherType.getKind());
58          assertEquals(PRINTER, otherType.getKey());
59      }
60  
61      @Test
62      public void list() {
63          List<AnyTypeTO> list = ANY_TYPE_SERVICE.list();
64          assertFalse(list.isEmpty());
65      }
66  
67      @Test
68      public void crud() {
69          AnyTypeTO newType = new AnyTypeTO();
70          newType.setKey("new type");
71          newType.setKind(AnyTypeKind.ANY_OBJECT);
72          newType.getClasses().add("generic membership");
73          newType.getClasses().add("csv");
74  
75          Response response = ANY_TYPE_SERVICE.create(newType);
76          assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatusInfo().getStatusCode());
77  
78          newType = getObject(response.getLocation(), AnyTypeService.class, AnyTypeTO.class);
79          assertNotNull(newType);
80          assertEquals(2, newType.getClasses().size());
81          assertTrue(newType.getClasses().contains("generic membership"));
82          assertTrue(newType.getClasses().contains("csv"));
83  
84          newType.getClasses().remove("generic membership");
85          ANY_TYPE_SERVICE.update(newType);
86  
87          newType = ANY_TYPE_SERVICE.read(newType.getKey());
88          assertNotNull(newType);
89          assertEquals(1, newType.getClasses().size());
90          assertTrue(newType.getClasses().contains("csv"));
91  
92          ANY_TYPE_SERVICE.delete(newType.getKey());
93  
94          try {
95              ANY_TYPE_SERVICE.read(newType.getKey());
96              fail("This should not happen");
97          } catch (SyncopeClientException e) {
98              assertEquals(ClientExceptionType.NotFound, e.getType());
99          }
100     }
101 
102     @Test
103     public void createInvalidKind() {
104         AnyTypeTO newType = new AnyTypeTO();
105         newType.setKey("new type");
106         newType.setKind(AnyTypeKind.USER);
107         try {
108             ANY_TYPE_SERVICE.create(newType);
109             fail("This should not happen");
110         } catch (SyncopeClientException e) {
111             assertEquals(ClientExceptionType.InvalidAnyType, e.getType());
112         }
113     }
114 
115     @Test
116     public void createInvalidName() {
117         AnyTypeTO newType = new AnyTypeTO();
118         newType.setKey("GROUP");
119         newType.setKind(AnyTypeKind.GROUP);
120         try {
121             ANY_TYPE_SERVICE.create(newType);
122             fail("This should not happen");
123         } catch (SyncopeClientException e) {
124             assertEquals(ClientExceptionType.EntityExists, e.getType());
125         }
126     }
127 
128     @Test
129     public void deleteTypeClass() {
130         AnyTypeClassTO newClass = new AnyTypeClassTO();
131         newClass.setKey("new class" + getUUIDString());
132         newClass.getDerSchemas().add("cn");
133 
134         Response response = ANY_TYPE_CLASS_SERVICE.create(newClass);
135         assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatusInfo().getStatusCode());
136 
137         newClass = getObject(response.getLocation(), AnyTypeClassService.class, AnyTypeClassTO.class);
138         assertNotNull(newClass);
139 
140         AnyTypeTO other = ANY_TYPE_SERVICE.read(PRINTER);
141         assertNotNull(other);
142 
143         other.getClasses().add(newClass.getKey());
144         ANY_TYPE_SERVICE.update(other);
145 
146         other = ANY_TYPE_SERVICE.read(other.getKey());
147         assertNotNull(other);
148         assertTrue(other.getClasses().contains(newClass.getKey()));
149 
150         ANY_TYPE_CLASS_SERVICE.delete(newClass.getKey());
151 
152         other = ANY_TYPE_SERVICE.read(other.getKey());
153         assertNotNull(other);
154         assertFalse(other.getClasses().contains(newClass.getKey()));
155     }
156 
157     @Test
158     public void issueSYNCOPE754() {
159         AnyTypeClassTO other = ANY_TYPE_CLASS_SERVICE.read("other");
160         assertNotNull(other);
161 
162         AnyTypeTO group = ANY_TYPE_SERVICE.read(AnyTypeKind.GROUP.name());
163         try {
164             assertFalse(group.getClasses().contains("other"));
165             group.getClasses().add("other");
166 
167             ANY_TYPE_SERVICE.update(group);
168 
169             group = ANY_TYPE_SERVICE.read(AnyTypeKind.GROUP.name());
170             assertTrue(group.getClasses().contains("other"));
171 
172             other = ANY_TYPE_CLASS_SERVICE.read("other");
173             assertEquals(2, other.getInUseByTypes().size());
174             assertTrue(other.getInUseByTypes().contains(AnyTypeKind.USER.name()));
175             assertTrue(other.getInUseByTypes().contains(AnyTypeKind.GROUP.name()));
176         } finally {
177             group.getClasses().remove("other");
178             ANY_TYPE_SERVICE.update(group);
179         }
180     }
181 
182     @Test
183     public void issueSYNCOPE1472() {
184         // 1. add any type class csv twice to PRINTER any type
185         AnyTypeTO anyTypeTO = ANY_TYPE_SERVICE.read(PRINTER);
186         anyTypeTO.getClasses().clear();
187         anyTypeTO.getClasses().add("minimal printer");
188         anyTypeTO.getClasses().add("csv");
189         anyTypeTO.getClasses().add("csv");
190         ANY_TYPE_SERVICE.update(anyTypeTO);
191 
192         // 2. read again and remove any type class
193         anyTypeTO = ANY_TYPE_SERVICE.read(PRINTER);
194         anyTypeTO.getClasses().remove("csv");
195         ANY_TYPE_SERVICE.update(anyTypeTO);
196 
197         assertFalse(ANY_TYPE_SERVICE.read(PRINTER).getClasses().contains("csv"), 
198                 "Should not contain removed any type classes");
199     }
200 }