1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import com.fasterxml.jackson.databind.MappingIterator;
26 import com.fasterxml.jackson.dataformat.csv.CsvMapper;
27 import com.fasterxml.jackson.dataformat.csv.CsvSchema;
28 import java.io.IOException;
29 import java.io.PipedInputStream;
30 import java.io.PipedOutputStream;
31 import java.util.List;
32 import java.util.Map;
33 import org.apache.commons.lang3.StringUtils;
34 import org.apache.syncope.common.lib.SyncopeConstants;
35 import org.apache.syncope.common.lib.to.ProvisioningReport;
36 import org.apache.syncope.common.lib.to.PushTaskTO;
37 import org.apache.syncope.common.lib.types.MatchingRule;
38 import org.apache.syncope.common.lib.types.UnmatchingRule;
39 import org.apache.syncope.core.persistence.api.dao.AnyTypeDAO;
40 import org.apache.syncope.core.persistence.api.dao.UserDAO;
41 import org.apache.syncope.core.provisioning.api.pushpull.stream.SyncopeStreamPushExecutor;
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.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.beans.factory.support.AbstractBeanDefinition;
48 import org.springframework.transaction.annotation.Transactional;
49
50 @Transactional("Master")
51 public class StreamPushJobDelegateTest extends AbstractTest {
52
53 @Autowired
54 private AnyTypeDAO anyTypeDAO;
55
56 @Autowired
57 private UserDAO userDAO;
58
59 private SyncopeStreamPushExecutor executor;
60
61 private SyncopeStreamPushExecutor executor() {
62 synchronized (this) {
63 if (executor == null) {
64 executor = (SyncopeStreamPushExecutor) ApplicationContextProvider.getBeanFactory().
65 createBean(StreamPushJobDelegate.class, AbstractBeanDefinition.AUTOWIRE_BY_NAME, false);
66 }
67 }
68 return executor;
69 }
70
71 @Test
72 public void push() throws IOException {
73 PipedInputStream in = new PipedInputStream();
74 PipedOutputStream os = new PipedOutputStream(in);
75
76 PushTaskTO pushTask = new PushTaskTO();
77 pushTask.setMatchingRule(MatchingRule.UPDATE);
78 pushTask.setUnmatchingRule(UnmatchingRule.PROVISION);
79
80 List<ProvisioningReport> results = AuthContextUtils.callAsAdmin(SyncopeConstants.MASTER_DOMAIN, () -> {
81 try (CSVStreamConnector connector = new CSVStreamConnector(
82 null,
83 ";",
84 new CsvSchema.Builder().setUseHeader(true),
85 null,
86 os)) {
87
88 return executor().push(
89 anyTypeDAO.findUser(),
90 userDAO.findAll(1, 100),
91 List.of("username", "firstname", "surname", "email", "status", "loginDate"),
92 connector,
93 List.of(),
94 pushTask,
95 "user");
96 } catch (Exception e) {
97 throw new RuntimeException(e);
98 }
99 });
100 assertEquals(userDAO.count(), results.size());
101
102 MappingIterator<Map<String, String>> reader =
103 new CsvMapper().readerFor(Map.class).with(CsvSchema.emptySchema().withHeader()).readValues(in);
104
105 for (int i = 0; i < results.size() && reader.hasNext(); i++) {
106 Map<String, String> row = reader.next();
107
108 assertEquals(results.get(i).getName(), row.get("username"));
109 assertEquals(userDAO.findByUsername(row.get("username")).getStatus(), row.get("status"));
110
111 switch (row.get("username")) {
112 case "rossini":
113 assertEquals(StringUtils.EMPTY, row.get("email"));
114 assertTrue(row.get("loginDate").contains(";"));
115 break;
116
117 case "verdi":
118 assertEquals("verdi@syncope.org", row.get("email"));
119 assertEquals(StringUtils.EMPTY, row.get("loginDate"));
120 break;
121
122 case "bellini":
123 assertEquals(StringUtils.EMPTY, row.get("email"));
124 assertFalse(row.get("loginDate").contains(";"));
125 break;
126
127 default:
128 break;
129 }
130 }
131 }
132 }