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.logic;
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.assertTrue;
25  
26  import com.fasterxml.jackson.databind.MappingIterator;
27  import com.fasterxml.jackson.dataformat.csv.CsvMapper;
28  import com.fasterxml.jackson.dataformat.csv.CsvSchema;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.PipedInputStream;
32  import java.io.PipedOutputStream;
33  import java.util.List;
34  import java.util.Map;
35  import org.apache.syncope.common.lib.SyncopeConstants;
36  import org.apache.syncope.common.lib.to.ProvisioningReport;
37  import org.apache.syncope.common.lib.to.UserTO;
38  import org.apache.syncope.common.lib.types.AnyTypeKind;
39  import org.apache.syncope.common.lib.types.ResourceOperation;
40  import org.apache.syncope.common.rest.api.beans.CSVPullSpec;
41  import org.apache.syncope.common.rest.api.beans.CSVPushSpec;
42  import org.apache.syncope.core.spring.security.AuthContextUtils;
43  import org.junit.jupiter.api.Test;
44  import org.springframework.beans.factory.annotation.Autowired;
45  import org.springframework.data.domain.Page;
46  import org.springframework.data.domain.PageRequest;
47  import org.springframework.transaction.annotation.Transactional;
48  
49  @Transactional
50  public class ReconciliationLogicTest extends AbstractTest {
51  
52      @Autowired
53      private ReconciliationLogic reconciliationLogic;
54  
55      @Autowired
56      private UserLogicOp userLogic;
57  
58      @Test
59      public void pullFromCSV() {
60          CSVPullSpec spec = new CSVPullSpec.Builder(AnyTypeKind.USER.name(), "username").build();
61          InputStream csv = getClass().getResourceAsStream("/test1.csv");
62  
63          List<ProvisioningReport> results = AuthContextUtils.callAsAdmin(
64                  SyncopeConstants.MASTER_DOMAIN, () -> reconciliationLogic.pull(spec, csv));
65          assertEquals(2, results.size());
66  
67          assertEquals(AnyTypeKind.USER.name(), results.getFirst().getAnyType());
68          assertNotNull(results.getFirst().getKey());
69          assertEquals("donizetti", results.getFirst().getName());
70          assertEquals("donizetti", results.getFirst().getUidValue());
71          assertEquals(ResourceOperation.CREATE, results.get(0).getOperation());
72          assertEquals(ProvisioningReport.Status.SUCCESS, results.get(0).getStatus());
73  
74          AuthContextUtils.runAsAdmin(SyncopeConstants.MASTER_DOMAIN, () -> {
75              UserTO donizetti = userLogic.read(results.getFirst().getKey());
76              assertNotNull(donizetti);
77              assertEquals("Gaetano", donizetti.getPlainAttr("firstname").get().getValues().getFirst());
78              assertEquals(1, donizetti.getPlainAttr("loginDate").get().getValues().size());
79  
80              UserTO cimarosa = userLogic.read(results.get(1).getKey());
81              assertNotNull(cimarosa);
82              assertEquals("Domenico Cimarosa", cimarosa.getPlainAttr("fullname").get().getValues().getFirst());
83              assertEquals(2, cimarosa.getPlainAttr("loginDate").get().getValues().size());
84          });
85      }
86  
87      @Test
88      public void pushToCSV() throws IOException {
89          Page<UserTO> search = AuthContextUtils.callAsAdmin(SyncopeConstants.MASTER_DOMAIN,
90                  () -> userLogic.search(null, PageRequest.of(0, 100), SyncopeConstants.ROOT_REALM, true, false));
91          assertNotNull(search);
92  
93          CSVPushSpec spec = new CSVPushSpec.Builder(AnyTypeKind.USER.name()).ignorePaging(true).
94                  field("username").
95                  field("status").
96                  plainAttr("firstname").
97                  plainAttr("surname").
98                  plainAttr("email").
99                  plainAttr("loginDate").
100                 build();
101 
102         PipedInputStream in = new PipedInputStream();
103         PipedOutputStream os = new PipedOutputStream(in);
104 
105         List<ProvisioningReport> results = AuthContextUtils.callAsAdmin(
106                 SyncopeConstants.MASTER_DOMAIN,
107                 () -> reconciliationLogic.push(null, PageRequest.of(0, 1), SyncopeConstants.ROOT_REALM, spec, os));
108         assertEquals(search.getTotalElements(), results.size());
109 
110         MappingIterator<Map<String, String>> reader =
111                 new CsvMapper().readerFor(Map.class).with(CsvSchema.emptySchema().withHeader()).readValues(in);
112 
113         for (int i = 0; i < results.size() && reader.hasNext(); i++) {
114             Map<String, String> row = reader.next();
115 
116             assertEquals(results.get(i).getName(), row.get("username"));
117             assertEquals(search.get().filter(user -> row.get("username").equals(user.getUsername())).
118                     findFirst().get().getStatus(),
119                     row.get("status"));
120 
121             switch (row.get("username")) {
122                 case "rossini" -> {
123                     assertEquals(spec.getNullValue(), row.get("email"));
124                     assertTrue(row.get("loginDate").contains(spec.getArrayElementSeparator()));
125                 }
126 
127                 case "verdi" -> {
128                     assertEquals("verdi@syncope.org", row.get("email"));
129                     assertEquals(spec.getNullValue(), row.get("loginDate"));
130                 }
131 
132                 case "bellini" -> {
133                     assertEquals(spec.getNullValue(), row.get("email"));
134                     assertFalse(row.get("loginDate").contains(spec.getArrayElementSeparator()));
135                 }
136 
137                 default -> {
138                 }
139             }
140         }
141     }
142 }