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.pushpull.stream;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  
24  import com.fasterxml.jackson.dataformat.csv.CsvSchema;
25  import java.io.ByteArrayInputStream;
26  import java.io.IOException;
27  import java.util.List;
28  import java.util.stream.Collectors;
29  import org.apache.syncope.common.lib.SyncopeConstants;
30  import org.apache.syncope.common.lib.to.ProvisioningReport;
31  import org.apache.syncope.common.lib.to.PullTaskTO;
32  import org.apache.syncope.common.lib.types.AnyTypeKind;
33  import org.apache.syncope.common.lib.types.ConflictResolutionAction;
34  import org.apache.syncope.common.lib.types.MatchingRule;
35  import org.apache.syncope.common.lib.types.ResourceOperation;
36  import org.apache.syncope.common.lib.types.UnmatchingRule;
37  import org.apache.syncope.common.rest.api.beans.CSVPullSpec;
38  import org.apache.syncope.core.persistence.api.dao.AnyTypeDAO;
39  import org.apache.syncope.core.persistence.api.dao.UserDAO;
40  import org.apache.syncope.core.persistence.api.entity.user.User;
41  import org.apache.syncope.core.provisioning.api.pushpull.stream.SyncopeStreamPullExecutor;
42  import org.apache.syncope.core.provisioning.java.AbstractTest;
43  import org.apache.syncope.core.spring.ApplicationContextProvider;
44  import org.apache.syncope.core.spring.security.AuthContextUtils;
45  import org.junit.jupiter.api.Test;
46  import org.quartz.JobExecutionException;
47  import org.springframework.beans.factory.annotation.Autowired;
48  import org.springframework.beans.factory.support.AbstractBeanDefinition;
49  import org.springframework.transaction.annotation.Transactional;
50  
51  @Transactional("Master")
52  public class StreamPullJobDelegateTest extends AbstractTest {
53  
54      @Autowired
55      private AnyTypeDAO anyTypeDAO;
56  
57      @Autowired
58      private UserDAO userDAO;
59  
60      private SyncopeStreamPullExecutor executor;
61  
62      private SyncopeStreamPullExecutor executor() {
63          synchronized (this) {
64              if (executor == null) {
65                  executor = (SyncopeStreamPullExecutor) ApplicationContextProvider.getBeanFactory().
66                          createBean(StreamPullJobDelegate.class, AbstractBeanDefinition.AUTOWIRE_BY_NAME, false);
67              }
68          }
69          return executor;
70      }
71  
72      @Test
73      public void pull() throws JobExecutionException, IOException {
74          List<String> columns = List.of(
75                  "username",
76                  "email",
77                  "surname",
78                  "firstname",
79                  "fullname",
80                  "userId");
81  
82          StringBuilder csv = new StringBuilder();
83          csv.append(columns.stream().collect(Collectors.joining(",")));
84          csv.append('\n');
85          csv.append("donizetti,");
86          csv.append("donizetti@apache.org,");
87          csv.append("Donizetti,");
88          csv.append("Gaetano,");
89          csv.append("Gaetano Donizetti,");
90          csv.append("donizetti@apache.org");
91          csv.append('\n');
92  
93          PullTaskTO pullTask = new PullTaskTO();
94          pullTask.setDestinationRealm(SyncopeConstants.ROOT_REALM);
95          pullTask.setRemediation(false);
96          pullTask.setMatchingRule(MatchingRule.UPDATE);
97          pullTask.setUnmatchingRule(UnmatchingRule.PROVISION);
98  
99          List<ProvisioningReport> results = AuthContextUtils.callAsAdmin(SyncopeConstants.MASTER_DOMAIN, () -> {
100             try (CSVStreamConnector connector = new CSVStreamConnector(
101                     "username",
102                     ";",
103                     new CsvSchema.Builder().setUseHeader(true),
104                     new ByteArrayInputStream(csv.toString().getBytes()),
105                     null)) {
106 
107                 List<String> csvColumns = connector.getColumns(new CSVPullSpec());
108                 assertEquals(columns, csvColumns);
109 
110                 return executor().pull(
111                         anyTypeDAO.findUser(),
112                         "username",
113                         columns,
114                         ConflictResolutionAction.IGNORE,
115                         null,
116                         connector,
117                         pullTask,
118                         "whoever");
119             } catch (Exception e) {
120                 throw new RuntimeException(e);
121             }
122         });
123         assertEquals(1, results.size());
124 
125         assertEquals(AnyTypeKind.USER.name(), results.get(0).getAnyType());
126         assertNotNull(results.get(0).getKey());
127         assertEquals("donizetti", results.get(0).getName());
128         assertEquals("donizetti", results.get(0).getUidValue());
129         assertEquals(ResourceOperation.CREATE, results.get(0).getOperation());
130         assertEquals(ProvisioningReport.Status.SUCCESS, results.get(0).getStatus());
131 
132         User donizetti = userDAO.find(results.get(0).getKey());
133         assertNotNull(donizetti);
134         assertEquals("donizetti", donizetti.getUsername());
135         assertEquals("Gaetano", donizetti.getPlainAttr("firstname").get().getValuesAsStrings().get(0));
136     }
137 }