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.outer;
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.assertTrue;
24  
25  import org.apache.syncope.common.lib.types.AttrSchemaType;
26  import org.apache.syncope.core.persistence.api.dao.AnyTypeClassDAO;
27  import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
28  import org.apache.syncope.core.persistence.api.entity.AnyTypeClass;
29  import org.apache.syncope.core.persistence.api.entity.PlainSchema;
30  import org.apache.syncope.core.persistence.jpa.AbstractTest;
31  import org.junit.jupiter.api.Test;
32  import org.springframework.beans.factory.annotation.Autowired;
33  import org.springframework.transaction.annotation.Transactional;
34  
35  @Transactional("Master")
36  public class AnyTypeClassTest extends AbstractTest {
37  
38      @Autowired
39      private PlainSchemaDAO plainSchemaDAO;
40  
41      @Autowired
42      private AnyTypeClassDAO anyTypeClassDAO;
43  
44      @Test
45      public void create() {
46          PlainSchema newSchema = entityFactory.newEntity(PlainSchema.class);
47          newSchema.setKey("new_plain_schema");
48          newSchema.setType(AttrSchemaType.String);
49  
50          plainSchemaDAO.save(newSchema);
51  
52          entityManager().flush();
53  
54          newSchema = plainSchemaDAO.find(newSchema.getKey());
55          assertNotNull(newSchema);
56  
57          AnyTypeClass newClass = entityFactory.newEntity(AnyTypeClass.class);
58          newClass.setKey("new class");
59          newClass.add(newSchema);
60  
61          anyTypeClassDAO.save(newClass);
62  
63          entityManager().flush();
64  
65          newClass = anyTypeClassDAO.find(newClass.getKey());
66          assertNotNull(newClass);
67          assertEquals(1, newClass.getPlainSchemas().size());
68          assertEquals(newSchema, newClass.getPlainSchemas().get(0));
69          assertEquals(newClass, newClass.getPlainSchemas().get(0).getAnyTypeClass());
70  
71          newSchema = plainSchemaDAO.find(newSchema.getKey());
72          assertNotNull(newSchema.getAnyTypeClass());
73      }
74  
75      @Test
76      public void delete() {
77          AnyTypeClass minimalUser = anyTypeClassDAO.find("minimal user");
78          assertNotNull(minimalUser);
79  
80          PlainSchema surname = plainSchemaDAO.find("surname");
81          assertNotNull(surname);
82          assertTrue(minimalUser.getPlainSchemas().contains(surname));
83          int before = minimalUser.getPlainSchemas().size();
84  
85          plainSchemaDAO.delete("surname");
86  
87          entityManager().flush();
88  
89          minimalUser = anyTypeClassDAO.find("minimal user");
90          assertNotNull(minimalUser);
91          assertEquals(before, minimalUser.getPlainSchemas().size() + 1);
92      }
93  }