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 java.util.Locale;
29  import javax.ws.rs.core.Response;
30  import org.apache.syncope.common.lib.SyncopeClientException;
31  import org.apache.syncope.common.lib.to.DerSchemaTO;
32  import org.apache.syncope.common.lib.types.ClientExceptionType;
33  import org.apache.syncope.common.lib.types.EntityViolationType;
34  import org.apache.syncope.common.lib.types.SchemaType;
35  import org.apache.syncope.common.rest.api.beans.SchemaQuery;
36  import org.apache.syncope.fit.AbstractITCase;
37  import org.junit.jupiter.api.Assertions;
38  import org.junit.jupiter.api.Test;
39  
40  public class DerSchemaITCase extends AbstractITCase {
41  
42      @Test
43      public void search() {
44          List<DerSchemaTO> schemas = SCHEMA_SERVICE.search(new SchemaQuery.Builder().type(SchemaType.DERIVED).build());
45          assertFalse(schemas.isEmpty());
46          schemas.forEach(Assertions::assertNotNull);
47  
48          schemas = SCHEMA_SERVICE.search(new SchemaQuery.Builder().type(SchemaType.DERIVED).keyword("mder*").build());
49          assertFalse(schemas.isEmpty());
50          schemas.forEach(Assertions::assertNotNull);
51      }
52  
53      @Test
54      public void read() {
55          DerSchemaTO derivedSchemaTO = SCHEMA_SERVICE.read(SchemaType.DERIVED, "cn");
56          assertNotNull(derivedSchemaTO);
57      }
58  
59      @Test
60      public void create() {
61          DerSchemaTO schema = new DerSchemaTO();
62          schema.setKey("derived");
63          schema.setExpression("derived_sx + '_' + derived_dx");
64          schema.getLabels().put(Locale.ENGLISH, "Derived");
65          schema.getLabels().put(Locale.ITALIAN, "Derivato");
66  
67          DerSchemaTO actual = createSchema(SchemaType.DERIVED, schema);
68          assertNotNull(actual);
69          assertEquals(2, actual.getLabels().size());
70          assertEquals("Derivato", actual.getLabel(Locale.ITALIAN));
71          assertEquals(schema.getKey(), actual.getLabel(Locale.JAPANESE));
72  
73          actual = SCHEMA_SERVICE.read(SchemaType.DERIVED, actual.getKey());
74          assertNotNull(actual);
75          assertEquals(actual.getExpression(), "derived_sx + '_' + derived_dx");
76      }
77  
78      @Test
79      public void delete() {
80          DerSchemaTO schema = SCHEMA_SERVICE.read(SchemaType.DERIVED, "rderiveddata");
81          assertNotNull(schema);
82  
83          SCHEMA_SERVICE.delete(SchemaType.DERIVED, schema.getKey());
84  
85          try {
86              SCHEMA_SERVICE.read(SchemaType.DERIVED, "rderiveddata");
87              fail("This should not happen");
88          } catch (SyncopeClientException e) {
89              assertEquals(ClientExceptionType.NotFound, e.getType());
90          } finally {
91              // Recreate schema to make test re-runnable
92              schema = createSchema(SchemaType.DERIVED, schema);
93              assertNotNull(schema);
94          }
95      }
96  
97      @Test
98      public void update() {
99          DerSchemaTO schema = SCHEMA_SERVICE.read(SchemaType.DERIVED, "mderiveddata");
100         assertNotNull(schema);
101         assertEquals("mderived_sx + '-' + mderived_dx", schema.getExpression());
102         try {
103             schema.setExpression("mderived_sx + '.' + mderived_dx");
104 
105             SCHEMA_SERVICE.update(SchemaType.DERIVED, schema);
106 
107             schema = SCHEMA_SERVICE.read(SchemaType.DERIVED, "mderiveddata");
108             assertNotNull(schema);
109             assertEquals("mderived_sx + '.' + mderived_dx", schema.getExpression());
110         } finally {
111             // Set updated back to make test re-runnable
112             schema.setExpression("mderived_sx + '-' + mderived_dx");
113             SCHEMA_SERVICE.update(SchemaType.DERIVED, schema);
114         }
115     }
116 
117     @Test
118     public void issueSYNCOPE323() {
119         DerSchemaTO actual = SCHEMA_SERVICE.read(SchemaType.DERIVED, "rderiveddata");
120         assertNotNull(actual);
121 
122         try {
123             createSchema(SchemaType.DERIVED, actual);
124             fail("This should not happen");
125         } catch (SyncopeClientException e) {
126             assertEquals(Response.Status.CONFLICT, e.getType().getResponseStatus());
127             assertEquals(ClientExceptionType.EntityExists, e.getType());
128         }
129 
130         actual.setKey(null);
131         try {
132             createSchema(SchemaType.DERIVED, actual);
133             fail("This should not happen");
134         } catch (SyncopeClientException e) {
135             assertEquals(Response.Status.BAD_REQUEST, e.getType().getResponseStatus());
136             assertEquals(ClientExceptionType.RequiredValuesMissing, e.getType());
137         }
138     }
139 
140     @Test
141     public void issueSYNCOPE418() {
142         DerSchemaTO schema = new DerSchemaTO();
143         schema.setKey("http://schemas.examples.org/security/authorization/organizationUnit");
144         schema.setExpression("derived_sx + '_' + derived_dx");
145 
146         try {
147             createSchema(SchemaType.DERIVED, schema);
148             fail("This should not happen");
149         } catch (SyncopeClientException e) {
150             assertEquals(ClientExceptionType.InvalidDerSchema, e.getType());
151             assertTrue(e.getElements().iterator().next().contains(EntityViolationType.InvalidKey.name()));
152         }
153     }
154 }