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.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertNull;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  import static org.junit.jupiter.api.Assertions.fail;
28  
29  import java.util.List;
30  import java.util.Locale;
31  import org.apache.syncope.common.lib.SyncopeConstants;
32  import org.apache.syncope.common.lib.types.AttrSchemaType;
33  import org.apache.syncope.common.lib.types.EntityViolationType;
34  import org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException;
35  import org.apache.syncope.core.persistence.api.dao.ImplementationDAO;
36  import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
37  import org.apache.syncope.core.persistence.api.entity.PlainSchema;
38  import org.apache.syncope.core.persistence.api.entity.group.GPlainAttr;
39  import org.apache.syncope.core.persistence.api.entity.user.UPlainAttr;
40  import org.apache.syncope.core.persistence.jpa.AbstractTest;
41  import org.junit.jupiter.api.Tag;
42  import org.junit.jupiter.api.Test;
43  import org.springframework.beans.factory.annotation.Autowired;
44  import org.springframework.transaction.annotation.Transactional;
45  
46  @Transactional("Master")
47  public class PlainSchemaTest extends AbstractTest {
48  
49      @Autowired
50      private PlainSchemaDAO plainSchemaDAO;
51  
52      @Autowired
53      private ImplementationDAO implementationDAO;
54  
55      @Test
56      public void findAll() {
57          List<PlainSchema> schemas = plainSchemaDAO.findAll();
58          assertEquals(27, schemas.size());
59      }
60  
61      @Test
62      public void search() {
63          List<PlainSchema> schemas = plainSchemaDAO.findByKeyword("fullna%");
64          assertEquals(1, schemas.size());
65          assertEquals(0, schemas.get(0).getLabels().size());
66      }
67  
68      @Test
69      public void findByName() {
70          PlainSchema schema = plainSchemaDAO.find("firstname");
71          assertNotNull(schema);
72  
73          assertEquals(3, schema.getLabels().size());
74          assertTrue(schema.getLabel(Locale.ITALIAN).isPresent());
75          assertFalse(schema.getLabel(Locale.KOREAN).isPresent());
76      }
77  
78      @Tag("plainAttrTable")
79      @Test
80      public void findAttrs() {
81          PlainSchema schema = plainSchemaDAO.find("icon");
82          assertNotNull(schema);
83  
84          List<GPlainAttr> gattrs = plainSchemaDAO.findAttrs(schema, GPlainAttr.class);
85          assertNotNull(gattrs);
86          assertFalse(gattrs.isEmpty());
87  
88          schema = plainSchemaDAO.find("aLong");
89          assertNotNull(schema);
90  
91          List<UPlainAttr> uattrs = plainSchemaDAO.findAttrs(schema, UPlainAttr.class);
92          assertNotNull(uattrs);
93          assertTrue(uattrs.isEmpty());
94      }
95  
96      @Test
97      public void hasAttrs() {
98          PlainSchema schema = plainSchemaDAO.find("icon");
99          assertNotNull(schema);
100         assertTrue(plainSchemaDAO.hasAttrs(schema, GPlainAttr.class));
101 
102         schema = plainSchemaDAO.find("aLong");
103         assertNotNull(schema);
104         assertFalse(plainSchemaDAO.hasAttrs(schema, UPlainAttr.class));
105     }
106 
107     @Test
108     public void save() {
109         PlainSchema schema = entityFactory.newEntity(PlainSchema.class);
110         schema.setKey("secondaryEmail");
111         schema.setType(AttrSchemaType.String);
112         schema.setValidator(implementationDAO.find("EmailAddressValidator"));
113         schema.setMandatoryCondition("false");
114         schema.setMultivalue(true);
115 
116         plainSchemaDAO.save(schema);
117 
118         PlainSchema actual = plainSchemaDAO.find("secondaryEmail");
119         assertNotNull(actual);
120         assertEquals(schema, actual);
121     }
122 
123     @Test
124     public void saveNonValid() {
125         assertThrows(InvalidEntityException.class, () -> {
126             PlainSchema schema = entityFactory.newEntity(PlainSchema.class);
127             schema.setKey("secondaryEmail");
128             schema.setType(AttrSchemaType.String);
129             schema.setValidator(implementationDAO.find("EmailAddressValidator"));
130             schema.setMandatoryCondition("false");
131             schema.setMultivalue(true);
132             schema.setUniqueConstraint(true);
133 
134             plainSchemaDAO.save(schema);
135         });
136     }
137 
138     @Test
139     public void checkForEnumType() {
140         PlainSchema schema = entityFactory.newEntity(PlainSchema.class);
141         schema.setType(AttrSchemaType.Enum);
142         schema.setKey("color");
143 
144         try {
145             plainSchemaDAO.save(schema);
146             fail("This should not happen");
147         } catch (Exception e) {
148             assertNotNull(e);
149         }
150 
151         schema.setEnumerationValues("red" + SyncopeConstants.ENUM_VALUES_SEPARATOR + "yellow");
152         schema.setEnumerationKeys('1' + SyncopeConstants.ENUM_VALUES_SEPARATOR + '2');
153 
154         plainSchemaDAO.save(schema);
155 
156         PlainSchema actual = plainSchemaDAO.find(schema.getKey());
157         assertNotNull(actual);
158         assertNotNull(actual.getEnumerationKeys());
159         assertFalse(actual.getEnumerationKeys().isEmpty());
160     }
161 
162     @Test
163     public void saveInvalidSchema() {
164         assertThrows(InvalidEntityException.class, () -> {
165             PlainSchema schema = entityFactory.newEntity(PlainSchema.class);
166             schema.setKey("username");
167             plainSchemaDAO.save(schema);
168         });
169     }
170 
171     @Test
172     public void delete() {
173         PlainSchema firstname = plainSchemaDAO.find("firstname");
174 
175         plainSchemaDAO.delete(firstname.getKey());
176 
177         PlainSchema actual = plainSchemaDAO.find("firstname");
178         assertNull(actual);
179     }
180 
181     @Test
182     public void issueSYNCOPE418() {
183         PlainSchema schema = entityFactory.newEntity(PlainSchema.class);
184         schema.setKey("http://schemas.examples.org/security/authorization/organizationUnit");
185 
186         try {
187             plainSchemaDAO.save(schema);
188             fail("This should not happen");
189         } catch (InvalidEntityException e) {
190             assertTrue(e.hasViolation(EntityViolationType.InvalidKey));
191         }
192     }
193 }