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.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.assertTrue;
26  import static org.junit.jupiter.api.Assertions.fail;
27  
28  import java.util.List;
29  import java.util.UUID;
30  import java.util.stream.Collectors;
31  import java.util.stream.Stream;
32  import javax.persistence.EntityExistsException;
33  import org.apache.syncope.common.lib.SyncopeConstants;
34  import org.apache.syncope.common.lib.to.Item;
35  import org.apache.syncope.common.lib.types.AnyTypeKind;
36  import org.apache.syncope.common.lib.types.AttrSchemaType;
37  import org.apache.syncope.common.lib.types.IdMEntitlement;
38  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
39  import org.apache.syncope.core.persistence.api.dao.DerSchemaDAO;
40  import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO;
41  import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
42  import org.apache.syncope.core.persistence.api.dao.UserDAO;
43  import org.apache.syncope.core.persistence.api.entity.PlainSchema;
44  import org.apache.syncope.core.persistence.api.entity.user.UPlainAttr;
45  import org.apache.syncope.core.persistence.jpa.AbstractTest;
46  import org.apache.syncope.core.spring.security.SyncopeAuthenticationDetails;
47  import org.apache.syncope.core.spring.security.SyncopeGrantedAuthority;
48  import org.junit.jupiter.api.AfterAll;
49  import org.junit.jupiter.api.BeforeAll;
50  import org.junit.jupiter.api.Test;
51  import org.springframework.beans.factory.annotation.Autowired;
52  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
53  import org.springframework.security.core.GrantedAuthority;
54  import org.springframework.security.core.context.SecurityContextHolder;
55  import org.springframework.transaction.annotation.Transactional;
56  
57  @Transactional("Master")
58  public class PlainSchemaTest extends AbstractTest {
59  
60      @Autowired
61      private UserDAO userDAO;
62  
63      @Autowired
64      private PlainSchemaDAO plainSchemaDAO;
65  
66      @Autowired
67      private DerSchemaDAO derSchemaDAO;
68  
69      @Autowired
70      private ExternalResourceDAO resourceDAO;
71  
72      @BeforeAll
73      public static void setAuthContext() {
74          List<GrantedAuthority> authorities = Stream.concat(
75                  IdRepoEntitlement.values().stream(), IdMEntitlement.values().stream()).
76                  map(entitlement -> new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM)).
77                  collect(Collectors.toList());
78  
79          UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
80                  new org.springframework.security.core.userdetails.User(
81                          "admin", "FAKE_PASSWORD", authorities), "FAKE_PASSWORD", authorities);
82          auth.setDetails(new SyncopeAuthenticationDetails(SyncopeConstants.MASTER_DOMAIN, null));
83          SecurityContextHolder.getContext().setAuthentication(auth);
84      }
85  
86      @AfterAll
87      public static void unsetAuthContext() {
88          SecurityContextHolder.getContext().setAuthentication(null);
89      }
90  
91      @Test
92      public void checkIdUniqueness() {
93          assertNotNull(derSchemaDAO.find("cn"));
94  
95          PlainSchema schema = entityFactory.newEntity(PlainSchema.class);
96          schema.setKey("cn");
97          schema.setType(AttrSchemaType.String);
98          plainSchemaDAO.save(schema);
99  
100         try {
101             entityManager().flush();
102             fail("This should not happen");
103         } catch (Exception e) {
104             assertTrue(e instanceof EntityExistsException || e.getCause() instanceof EntityExistsException);
105         }
106     }
107 
108     private List<Item> getMappingItems(final String intAttrName) {
109         return resourceDAO.findAll().stream().
110                 flatMap(resource -> resource.getProvisions().stream()).
111                 flatMap(provision -> provision.getMapping().getItems().stream()).
112                 filter(item -> intAttrName.equals(item.getIntAttrName())).
113                 collect(Collectors.toList());
114     }
115 
116     @Test
117     public void deleteFullname() {
118         // fullname is mapped as ConnObjectKey for ws-target-resource-2, need to swap it otherwise validation errors 
119         // will be raised
120         resourceDAO.find("ws-target-resource-2").
121                 getProvisionByAnyType(AnyTypeKind.USER.name()).get().getMapping().getItems().
122                 forEach(item -> {
123                     if ("fullname".equals(item.getIntAttrName())) {
124                         item.setConnObjectKey(false);
125                     } else if ("surname".equals(item.getIntAttrName())) {
126                         item.setConnObjectKey(true);
127                     }
128                 });
129 
130         // search for user schema fullname
131         PlainSchema schema = plainSchemaDAO.find("fullname");
132         assertNotNull(schema);
133 
134         // check for associated mappings
135         List<Item> mapItems = getMappingItems("fullname");
136         assertFalse(mapItems.isEmpty());
137 
138         // delete user schema fullname
139         plainSchemaDAO.delete("fullname");
140 
141         entityManager().flush();
142 
143         // check for schema deletion
144         schema = plainSchemaDAO.find("fullname");
145         assertNull(schema);
146 
147         // check for mappings deletion
148         mapItems = getMappingItems("fullname");
149         assertTrue(mapItems.isEmpty());
150 
151         assertNull(findPlainAttr("01f22fbd-b672-40af-b528-686d9b27ebc4", UPlainAttr.class));
152         assertNull(findPlainAttr(UUID.randomUUID().toString(), UPlainAttr.class));
153         assertFalse(userDAO.findByUsername("rossini").getPlainAttr("fullname").isPresent());
154         assertFalse(userDAO.findByUsername("vivaldi").getPlainAttr("fullname").isPresent());
155     }
156 
157     @Test
158     public void deleteSurname() {
159         // search for user schema surname
160         PlainSchema schema = plainSchemaDAO.find("surname");
161         assertNotNull(schema);
162 
163         // check for associated mappings
164         List<Item> mapItems = getMappingItems("surname");
165         assertFalse(mapItems.isEmpty());
166 
167         // check for labels
168         assertEquals(2, schema.getLabels().size());
169 
170         // delete user schema surname
171         plainSchemaDAO.delete("surname");
172 
173         entityManager().flush();
174 
175         // check for schema deletion
176         schema = plainSchemaDAO.find("surname");
177         assertNull(schema);
178     }
179 
180     @Test
181     public void deleteFirstname() {
182         int pre = resourceDAO.find("resource-db-pull").
183                 getProvisionByAnyType(AnyTypeKind.USER.name()).get().getMapping().getItems().size();
184 
185         plainSchemaDAO.delete("firstname");
186         assertNull(plainSchemaDAO.find("firstname"));
187 
188         assertEquals(pre - 1, resourceDAO.find("resource-db-pull").
189                 getProvisionByAnyType(AnyTypeKind.USER.name()).get().getMapping().getItems().size());
190     }
191 }