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.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertNull;
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 org.apache.syncope.common.lib.types.EntityViolationType;
29  import org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException;
30  import org.apache.syncope.core.persistence.api.dao.DerSchemaDAO;
31  import org.apache.syncope.core.persistence.api.entity.DerSchema;
32  import org.apache.syncope.core.persistence.jpa.AbstractTest;
33  import org.junit.jupiter.api.Test;
34  import org.springframework.beans.factory.annotation.Autowired;
35  import org.springframework.transaction.annotation.Transactional;
36  
37  @Transactional("Master")
38  public class DerSchemaTest extends AbstractTest {
39  
40      @Autowired
41      private DerSchemaDAO derSchemaDAO;
42  
43      @Test
44      public void findAll() {
45          List<DerSchema> list = derSchemaDAO.findAll();
46          assertEquals(10, list.size());
47      }
48  
49      @Test
50      public void search() {
51          List<DerSchema> schemas = derSchemaDAO.findByKeyword("mderivedd%");
52          assertEquals(1, schemas.size());
53      }
54  
55      @Test
56      public void findByName() {
57          DerSchema attributeSchema = derSchemaDAO.find("cn");
58          assertNotNull(attributeSchema);
59      }
60  
61      @Test
62      public void save() {
63          DerSchema derivedAttributeSchema = entityFactory.newEntity(DerSchema.class);
64          derivedAttributeSchema.setKey("cn2");
65          derivedAttributeSchema.setExpression("firstname surname");
66  
67          derSchemaDAO.save(derivedAttributeSchema);
68  
69          DerSchema actual = derSchemaDAO.find("cn2");
70          assertNotNull(actual);
71          assertEquals(derivedAttributeSchema, actual);
72      }
73  
74      @Test
75      public void delete() {
76          DerSchema cn = derSchemaDAO.find("cn");
77          assertNotNull(cn);
78  
79          derSchemaDAO.delete(cn.getKey());
80  
81          DerSchema actual = derSchemaDAO.find("cn");
82          assertNull(actual);
83  
84          // ------------- //
85          DerSchema rderiveddata = derSchemaDAO.find("rderiveddata");
86          assertNotNull(rderiveddata);
87  
88          derSchemaDAO.delete(rderiveddata.getKey());
89  
90          actual = derSchemaDAO.find("rderiveddata");
91          assertNull(actual);
92      }
93  
94      @Test
95      public void issueSYNCOPE418() {
96          DerSchema schema = entityFactory.newEntity(DerSchema.class);
97          schema.setKey("http://schemas.examples.org/security/authorization/organizationUnit");
98  
99          try {
100             derSchemaDAO.save(schema);
101             fail("This should not happen");
102         } catch (InvalidEntityException e) {
103             assertTrue(e.hasViolation(EntityViolationType.InvalidKey));
104         }
105     }
106 }