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.AnyTypeDAO;
31  import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO;
32  import org.apache.syncope.core.persistence.api.dao.VirSchemaDAO;
33  import org.apache.syncope.core.persistence.api.entity.VirSchema;
34  import org.apache.syncope.core.persistence.jpa.AbstractTest;
35  import org.junit.jupiter.api.Test;
36  import org.springframework.beans.factory.annotation.Autowired;
37  import org.springframework.transaction.annotation.Transactional;
38  
39  @Transactional("Master")
40  public class VirSchemaTest extends AbstractTest {
41  
42      @Autowired
43      private VirSchemaDAO virSchemaDAO;
44  
45      @Autowired
46      private ExternalResourceDAO resourceDAO;
47  
48      @Autowired
49      private AnyTypeDAO anyTypeDAO;
50  
51      @Test
52      public void findAll() {
53          List<VirSchema> list = virSchemaDAO.findAll();
54          assertEquals(3, list.size());
55      }
56  
57      @Test
58      public void search() {
59          List<VirSchema> schemas = virSchemaDAO.findByKeyword("rvirtuald%");
60          assertEquals(1, schemas.size());
61      }
62  
63      @Test
64      public void findByName() {
65          VirSchema attributeSchema = virSchemaDAO.find("virtualdata");
66          assertNotNull(attributeSchema);
67      }
68  
69      @Test
70      public void save() {
71          VirSchema virSchema = entityFactory.newEntity(VirSchema.class);
72          virSchema.setKey("virtual");
73          virSchema.setResource(resourceDAO.find("resource-csv"));
74          virSchema.setAnyType(anyTypeDAO.findUser());
75          virSchema.setReadonly(true);
76          virSchema.setExtAttrName("EXT_ATTR");
77  
78          virSchemaDAO.save(virSchema);
79  
80          VirSchema actual = virSchemaDAO.find("virtual");
81          assertNotNull(actual);
82          assertTrue(actual.isReadonly());
83          assertEquals("EXT_ATTR", actual.getExtAttrName());
84      }
85  
86      @Test
87      public void delete() {
88          VirSchema virtualdata = virSchemaDAO.find("virtualdata");
89  
90          virSchemaDAO.delete(virtualdata.getKey());
91  
92          VirSchema actual = virSchemaDAO.find("virtualdata");
93          assertNull(actual);
94  
95          // ------------- //
96          VirSchema rvirtualdata = virSchemaDAO.find("rvirtualdata");
97          assertNotNull(rvirtualdata);
98  
99          virSchemaDAO.delete(rvirtualdata.getKey());
100 
101         actual = virSchemaDAO.find("rvirtualdata");
102         assertNull(actual);
103     }
104 
105     @Test
106     public void issueSYNCOPE418() {
107         VirSchema schema = entityFactory.newEntity(VirSchema.class);
108         schema.setKey("http://schemas.examples.org/security/authorization/organizationUnit");
109 
110         try {
111             virSchemaDAO.save(schema);
112             fail("This should not happen");
113         } catch (InvalidEntityException e) {
114             assertTrue(e.hasViolation(EntityViolationType.InvalidKey));
115         }
116     }
117 }