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.fail;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.Properties;
27  import org.apache.syncope.common.lib.SyncopeClientCompositeException;
28  import org.apache.syncope.common.lib.SyncopeClientException;
29  import org.apache.syncope.common.lib.SyncopeConstants;
30  import org.apache.syncope.common.lib.request.GroupCR;
31  import org.apache.syncope.common.lib.request.UserCR;
32  import org.apache.syncope.common.lib.to.AnyTypeClassTO;
33  import org.apache.syncope.common.lib.to.Item;
34  import org.apache.syncope.common.lib.to.PlainSchemaTO;
35  import org.apache.syncope.common.lib.to.ResourceTO;
36  import org.apache.syncope.common.lib.types.AnyTypeKind;
37  import org.apache.syncope.common.lib.types.AttrSchemaType;
38  import org.apache.syncope.common.lib.types.ClientExceptionType;
39  import org.apache.syncope.common.lib.types.SchemaType;
40  import org.apache.syncope.common.lib.types.TaskType;
41  import org.apache.syncope.common.rest.api.beans.TaskQuery;
42  import org.apache.syncope.common.rest.api.service.AccessTokenService;
43  import org.apache.syncope.fit.AbstractITCase;
44  import org.junit.jupiter.api.BeforeAll;
45  import org.junit.jupiter.api.Test;
46  
47  public class ExceptionMapperITCase extends AbstractITCase {
48  
49      private static final Properties ERROR_MESSAGES = new Properties();
50  
51      @BeforeAll
52      public static void setUpErrorMessages() throws IOException {
53          try (InputStream propStream = ExceptionMapperITCase.class.getResourceAsStream("/errorMessages.properties")) {
54              ERROR_MESSAGES.load(propStream);
55          } catch (Exception e) {
56              LOG.error("Could not load /errorMessages.properties", e);
57          }
58      }
59  
60      @Test
61      public void uniqueSchemaConstraint() {
62          // 1. create an user schema with unique constraint
63          PlainSchemaTO schemaTO = new PlainSchemaTO();
64          String schemaUID = getUUIDString();
65          schemaTO.setKey("unique" + schemaUID);
66          schemaTO.setType(AttrSchemaType.String);
67          schemaTO.setUniqueConstraint(true);
68          createSchema(SchemaType.PLAIN, schemaTO);
69  
70          AnyTypeClassTO typeClass = new AnyTypeClassTO();
71          typeClass.setKey("uniqueAttribute" + getUUIDString());
72          typeClass.getPlainSchemas().add(schemaTO.getKey());
73          ANY_TYPE_CLASS_SERVICE.create(typeClass);
74  
75          // 2. create an user with mandatory attributes and unique
76          UserCR userTO1 = new UserCR();
77          userTO1.setRealm(SyncopeConstants.ROOT_REALM);
78          userTO1.getAuxClasses().add(typeClass.getKey());
79          String userId1 = getUUIDString() + "issue654_1@syncope.apache.org";
80          userTO1.setUsername(userId1);
81          userTO1.setPassword("password123");
82  
83          userTO1.getPlainAttrs().add(attr("userId", userId1));
84          userTO1.getPlainAttrs().add(attr("fullname", userId1));
85          userTO1.getPlainAttrs().add(attr("surname", userId1));
86          userTO1.getPlainAttrs().add(attr("unique" + schemaUID, "unique" + schemaUID));
87  
88          createUser(userTO1);
89  
90          // 3. create an other user with mandatory attributes and unique with the same value of userTO1
91          UserCR userTO2 = new UserCR();
92          userTO2.setRealm(SyncopeConstants.ROOT_REALM);
93          userTO2.getAuxClasses().add(typeClass.getKey());
94          String userId2 = getUUIDString() + "issue654_2@syncope.apache.org";
95          userTO2.setUsername(userId2);
96          userTO2.setPassword("password123");
97  
98          userTO2.getPlainAttrs().add(attr("userId", userId2));
99          userTO2.getPlainAttrs().add(attr("fullname", userId2));
100         userTO2.getPlainAttrs().add(attr("surname", userId2));
101         userTO2.getPlainAttrs().add(attr("unique" + schemaUID, "unique" + schemaUID));
102 
103         try {
104             createUser(userTO2);
105             fail("This should not happen");
106         } catch (Exception e) {
107             String message = ERROR_MESSAGES.getProperty("errMessage.UniqueConstraintViolation");
108             assertEquals("EntityExists [" + message + ']', e.getMessage());
109         }
110     }
111 
112     @Test
113     public void sameGroupName() {
114         String groupUUID = getUUIDString();
115 
116         // Create the first group
117         GroupCR groupTO1 = new GroupCR();
118         groupTO1.setName("child1" + groupUUID);
119         groupTO1.setRealm(SyncopeConstants.ROOT_REALM);
120         createGroup(groupTO1);
121 
122         // Create the second group, with the same name of groupTO1
123         GroupCR groupTO2 = new GroupCR();
124         groupTO2.setName("child1" + groupUUID);
125         groupTO2.setRealm(SyncopeConstants.ROOT_REALM);
126         try {
127             createGroup(groupTO2);
128             fail("This should not happen");
129         } catch (SyncopeClientException e) {
130             assertEquals(ClientExceptionType.EntityExists, e.getType());
131             String message = ERROR_MESSAGES.getProperty("errMessage.UniqueConstraintViolation");
132             assertEquals("EntityExists [" + message + ']', e.getMessage());
133         }
134     }
135 
136     @Test
137     public void headersMultiValue() {
138         UserCR userCR = new UserCR();
139         userCR.setRealm(SyncopeConstants.ROOT_REALM);
140         String userId = getUUIDString() + "issue654@syncope.apache.org";
141         userCR.setUsername(userId);
142         userCR.setPassword("password123");
143 
144         userCR.getPlainAttrs().add(attr("userId", "issue654"));
145         userCR.getPlainAttrs().add(attr("fullname", userId));
146         userCR.getPlainAttrs().add(attr("surname", userId));
147 
148         try {
149             createUser(userCR);
150             fail("This should not happen");
151         } catch (SyncopeClientCompositeException e) {
152             assertEquals(2, e.getExceptions().size());
153         }
154     }
155 
156     @Test
157     public void invalidRequests() {
158         try {
159             TASK_SERVICE.search(new TaskQuery.Builder(TaskType.NOTIFICATION).resource(RESOURCE_NAME_LDAP).build());
160             fail("This should not happen");
161         } catch (SyncopeClientException e) {
162             assertEquals(ClientExceptionType.InvalidRequest, e.getType());
163         }
164         try {
165             TASK_SERVICE.search(new TaskQuery.Builder(TaskType.PULL).anyTypeKind(AnyTypeKind.ANY_OBJECT).build());
166             fail("This should not happen");
167         } catch (SyncopeClientException e) {
168             assertEquals(ClientExceptionType.InvalidRequest, e.getType());
169         }
170         try {
171             TASK_SERVICE.search(new TaskQuery.Builder(TaskType.PULL).
172                     notification("e00945b5-1184-4d43-8e45-4318a8dcdfd4").build());
173             fail("This should not happen");
174         } catch (SyncopeClientException e) {
175             assertEquals(ClientExceptionType.InvalidRequest, e.getType());
176         }
177 
178         try {
179             ANY_TYPE_SERVICE.delete(AnyTypeKind.USER.name());
180             fail("This should not happen");
181         } catch (SyncopeClientException e) {
182             assertEquals(ClientExceptionType.InvalidRequest, e.getType());
183         }
184 
185         try {
186             ANONYMOUS_CLIENT.getService(AccessTokenService.class).login();
187             fail("This should not happen");
188         } catch (SyncopeClientException e) {
189             assertEquals(ClientExceptionType.InvalidRequest, e.getType());
190         }
191 
192         try {
193             ResourceTO ldap = RESOURCE_SERVICE.read(RESOURCE_NAME_LDAP);
194             Item mapping = ldap.getProvisions().get(0).getMapping().getItems().get(0);
195             mapping.setIntAttrName("memberships.cn");
196             RESOURCE_SERVICE.update(ldap);
197             fail("This should not happen");
198         } catch (SyncopeClientException e) {
199             assertEquals(ClientExceptionType.InvalidMapping, e.getType());
200         }
201     }
202 }