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.assertTrue;
26  import static org.junit.jupiter.api.Assertions.fail;
27  
28  import java.io.File;
29  import java.util.HashSet;
30  import java.util.List;
31  import java.util.Set;
32  import java.util.stream.Collectors;
33  import org.apache.syncope.common.lib.SyncopeConstants;
34  import org.apache.syncope.common.lib.types.ConnConfPropSchema;
35  import org.apache.syncope.common.lib.types.ConnConfProperty;
36  import org.apache.syncope.common.lib.types.IdMEntitlement;
37  import org.apache.syncope.core.persistence.api.dao.ConnInstanceDAO;
38  import org.apache.syncope.core.persistence.api.entity.ConnInstance;
39  import org.apache.syncope.core.persistence.jpa.AbstractTest;
40  import org.apache.syncope.core.spring.security.DelegatedAdministrationException;
41  import org.apache.syncope.core.spring.security.SyncopeAuthenticationDetails;
42  import org.apache.syncope.core.spring.security.SyncopeGrantedAuthority;
43  import org.junit.jupiter.api.Test;
44  import org.springframework.beans.factory.annotation.Autowired;
45  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
46  import org.springframework.security.core.GrantedAuthority;
47  import org.springframework.security.core.context.SecurityContextHolder;
48  import org.springframework.transaction.annotation.Transactional;
49  
50  @Transactional("Master")
51  public class ConnInstanceTest extends AbstractTest {
52  
53      @Autowired
54      private ConnInstanceDAO connInstanceDAO;
55  
56      @Test
57      public void findAll() {
58          List<GrantedAuthority> authorities = IdMEntitlement.values().stream().
59                  map(entitlement -> new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM)).
60                  collect(Collectors.toList());
61  
62          UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
63                  new org.springframework.security.core.userdetails.User(
64                          "admin", "FAKE_PASSWORD", authorities), "FAKE_PASSWORD", authorities);
65          auth.setDetails(new SyncopeAuthenticationDetails(SyncopeConstants.MASTER_DOMAIN, null));
66          SecurityContextHolder.getContext().setAuthentication(auth);
67  
68          try {
69              List<ConnInstance> connectors = connInstanceDAO.findAll();
70              assertNotNull(connectors);
71              assertFalse(connectors.isEmpty());
72          } finally {
73              SecurityContextHolder.getContext().setAuthentication(null);
74          }
75      }
76  
77      @Test
78      public void findById() {
79          ConnInstance connInstance = connInstanceDAO.find("88a7a819-dab5-46b4-9b90-0b9769eabdb8");
80          assertNotNull(connInstance);
81          assertEquals("net.tirasa.connid.bundles.soap.WebServiceConnector", connInstance.getConnectorName());
82          assertEquals("net.tirasa.connid.bundles.soap", connInstance.getBundleName());
83  
84          try {
85              connInstanceDAO.authFind("88a7a819-dab5-46b4-9b90-0b9769eabdb8");
86              fail("This should not happen");
87          } catch (DelegatedAdministrationException e) {
88              assertNotNull(e);
89          }
90      }
91  
92      @Test
93      public void save() throws ClassNotFoundException {
94          ConnInstance connInstance = entityFactory.newEntity(ConnInstance.class);
95  
96          connInstance.setLocation(new File(System.getProperty("java.io.tmpdir")).toURI().toString());
97  
98          // set connector version
99          connInstance.setVersion("1.0");
100 
101         // set connector name
102         connInstance.setConnectorName("WebService");
103 
104         // set bundle name
105         connInstance.setBundleName("org.apache.syncope.core.persistence.test.util");
106 
107         connInstance.setDisplayName("New");
108 
109         connInstance.setConnRequestTimeout(60);
110 
111         // set the connector configuration using PropertyTO
112         Set<ConnConfProperty> conf = new HashSet<>();
113 
114         ConnConfPropSchema endpointSchema = new ConnConfPropSchema();
115         endpointSchema.setName("endpoint");
116         endpointSchema.setType(String.class.getName());
117         endpointSchema.setRequired(true);
118         ConnConfProperty endpoint = new ConnConfProperty();
119         endpoint.setSchema(endpointSchema);
120         endpoint.getValues().add("http://host.domain");
121 
122         ConnConfPropSchema servicenameSchema = new ConnConfPropSchema();
123         servicenameSchema.setName("servicename");
124         servicenameSchema.setType(String.class.getName());
125         servicenameSchema.setRequired(true);
126         ConnConfProperty servicename = new ConnConfProperty();
127         servicename.setSchema(servicenameSchema);
128         endpoint.getValues().add("Provisioning");
129 
130         conf.add(endpoint);
131         conf.add(servicename);
132 
133         // set connector configuration
134         connInstance.setConf(conf);
135         assertFalse(connInstance.getConf().isEmpty());
136 
137         // perform save operation
138         ConnInstance actual = connInstanceDAO.save(connInstance);
139 
140         assertNotNull("save did not work", actual::getKey);
141 
142         assertEquals("WebService", actual.getConnectorName());
143 
144         assertEquals("org.apache.syncope.core.persistence.test.util", actual.getBundleName());
145 
146         assertEquals("1.0", connInstance.getVersion());
147 
148         assertEquals("New", actual.getDisplayName());
149 
150         assertEquals(60, actual.getConnRequestTimeout().intValue());
151 
152         conf = connInstance.getConf();
153         assertFalse(conf.isEmpty());
154 
155         assertNotNull(conf);
156         assertTrue(conf.size() == 2);
157     }
158 
159     @Test
160     public void delete() {
161         ConnInstance connectorInstance = connInstanceDAO.find("88a7a819-dab5-46b4-9b90-0b9769eabdb8");
162         assertNotNull(connectorInstance);
163 
164         connInstanceDAO.delete(connectorInstance.getKey());
165 
166         ConnInstance actual = connInstanceDAO.find("88a7a819-dab5-46b4-9b90-0b9769eabdb8");
167         assertNull(actual);
168     }
169 }