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.provisioning.java.data;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Set;
27  import java.util.stream.Collectors;
28  import org.apache.syncope.common.lib.SyncopeConstants;
29  import org.apache.syncope.common.lib.to.Item;
30  import org.apache.syncope.common.lib.to.Mapping;
31  import org.apache.syncope.common.lib.to.Provision;
32  import org.apache.syncope.common.lib.to.ResourceTO;
33  import org.apache.syncope.common.lib.types.AnyTypeKind;
34  import org.apache.syncope.common.lib.types.IdMEntitlement;
35  import org.apache.syncope.common.lib.types.MappingPurpose;
36  import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO;
37  import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
38  import org.apache.syncope.core.persistence.api.entity.ExternalResource;
39  import org.apache.syncope.core.persistence.api.entity.PlainSchema;
40  import org.apache.syncope.core.provisioning.api.data.ResourceDataBinder;
41  import org.apache.syncope.core.provisioning.java.AbstractTest;
42  import org.apache.syncope.core.spring.security.SyncopeAuthenticationDetails;
43  import org.apache.syncope.core.spring.security.SyncopeGrantedAuthority;
44  import org.identityconnectors.framework.common.objects.ObjectClass;
45  import org.junit.jupiter.api.AfterAll;
46  import org.junit.jupiter.api.BeforeAll;
47  import org.junit.jupiter.api.Test;
48  import org.springframework.beans.factory.annotation.Autowired;
49  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
50  import org.springframework.security.core.GrantedAuthority;
51  import org.springframework.security.core.context.SecurityContextHolder;
52  import org.springframework.transaction.annotation.Transactional;
53  
54  @Transactional("Master")
55  public class ResourceDataBinderTest extends AbstractTest {
56  
57      @BeforeAll
58      public static void setAuthContext() {
59          List<GrantedAuthority> authorities = IdMEntitlement.values().stream().
60                  map(entitlement -> new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM)).
61                  collect(Collectors.toList());
62  
63          UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
64                  new org.springframework.security.core.userdetails.User(
65                          "admin", "FAKE_PASSWORD", authorities), "FAKE_PASSWORD", authorities);
66          auth.setDetails(new SyncopeAuthenticationDetails(SyncopeConstants.MASTER_DOMAIN, null));
67          SecurityContextHolder.getContext().setAuthentication(auth);
68      }
69  
70      @AfterAll
71      public static void unsetAuthContext() {
72          SecurityContextHolder.getContext().setAuthentication(null);
73      }
74  
75      @Autowired
76      private ExternalResourceDAO resourceDAO;
77  
78      @Autowired
79      private ResourceDataBinder resourceDataBinder;
80  
81      @Autowired
82      private PlainSchemaDAO plainSchemaDAO;
83  
84      @Test
85      public void issue42() {
86          PlainSchema userId = plainSchemaDAO.find("userId");
87  
88          Set<Item> beforeUserIdMappings = new HashSet<>();
89          for (ExternalResource res : resourceDAO.findAll()) {
90              if (res.getProvisionByAnyType(AnyTypeKind.USER.name()).isPresent()
91                      && res.getProvisionByAnyType(AnyTypeKind.USER.name()).get().getMapping() != null) {
92  
93                  for (Item mapItem : res.getProvisionByAnyType(AnyTypeKind.USER.name()).get().getMapping().getItems()) {
94                      if (userId.getKey().equals(mapItem.getIntAttrName())) {
95                          beforeUserIdMappings.add(mapItem);
96                      }
97                  }
98              }
99          }
100 
101         ResourceTO resourceTO = new ResourceTO();
102         resourceTO.setKey("resource-issue42");
103         resourceTO.setConnector("88a7a819-dab5-46b4-9b90-0b9769eabdb8");
104         resourceTO.setEnforceMandatoryCondition(true);
105 
106         Provision provisionTO = new Provision();
107         provisionTO.setAnyType(AnyTypeKind.USER.name());
108         provisionTO.setObjectClass(ObjectClass.ACCOUNT_NAME);
109         resourceTO.getProvisions().add(provisionTO);
110 
111         Mapping mapping = new Mapping();
112         provisionTO.setMapping(mapping);
113 
114         Item item = new Item();
115         item.setIntAttrName("userId");
116         item.setExtAttrName("campo1");
117         item.setConnObjectKey(true);
118         item.setMandatoryCondition("false");
119         item.setPurpose(MappingPurpose.BOTH);
120         mapping.setConnObjectKeyItem(item);
121 
122         ExternalResource resource = resourceDataBinder.create(resourceTO);
123         resource = resourceDAO.save(resource);
124         entityManager().flush();
125         assertNotNull(resource);
126         assertNotNull(resource.getProvisionByAnyType(AnyTypeKind.USER.name()).get().getMapping());
127         assertEquals(1, resource.getProvisionByAnyType(AnyTypeKind.USER.name()).get().getMapping().getItems().size());
128 
129         ExternalResource actual = resourceDAO.find("resource-issue42");
130         entityManager().flush();
131         assertNotNull(actual);
132         assertEquals(resource, actual);
133 
134         userId = plainSchemaDAO.find("userId");
135 
136         Set<Item> afterUserIdMappings = new HashSet<>();
137         for (ExternalResource res : resourceDAO.findAll()) {
138             if (res.getProvisionByAnyType(AnyTypeKind.USER.name()).isPresent()
139                     && res.getProvisionByAnyType(AnyTypeKind.USER.name()).get().getMapping() != null) {
140 
141                 for (Item mapItem : res.getProvisionByAnyType(AnyTypeKind.USER.name()).get().getMapping().getItems()) {
142                     if (userId.getKey().equals(mapItem.getIntAttrName())) {
143                         afterUserIdMappings.add(mapItem);
144                     }
145                 }
146             }
147         }
148 
149         assertEquals(beforeUserIdMappings.size(), afterUserIdMappings.size() - 1);
150     }
151 }