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