[SYNCOPE-1262] Last matrix parameter still there
[syncope.git] / fit / core-reference / src / test / java / org / apache / syncope / fit / core / PolicyITCase.java
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.fit.core;
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.assertNotEquals;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.junit.jupiter.api.Assertions.fail;
27
28 import java.io.IOException;
29 import java.nio.charset.StandardCharsets;
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.Set;
33 import javax.ws.rs.core.Response;
34 import org.apache.commons.io.IOUtils;
35 import org.apache.commons.lang3.SerializationUtils;
36 import org.apache.syncope.common.lib.SyncopeClientException;
37 import org.apache.syncope.common.lib.policy.AccountPolicyTO;
38 import org.apache.syncope.common.lib.policy.PasswordPolicyTO;
39 import org.apache.syncope.common.lib.policy.PullPolicyTO;
40 import org.apache.syncope.common.lib.policy.DefaultAccountRuleConf;
41 import org.apache.syncope.common.lib.types.AnyTypeKind;
42 import org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf;
43 import org.apache.syncope.common.lib.types.PolicyType;
44 import org.apache.syncope.common.lib.to.ImplementationTO;
45 import org.apache.syncope.common.lib.types.ImplementationEngine;
46 import org.apache.syncope.common.lib.types.ImplementationType;
47 import org.apache.syncope.common.rest.api.RESTHeaders;
48 import org.apache.syncope.core.provisioning.api.serialization.POJOHelper;
49 import org.apache.syncope.fit.AbstractITCase;
50 import org.apache.syncope.fit.core.reference.DummyPullCorrelationRule;
51 import org.junit.jupiter.api.Test;
52
53 public class PolicyITCase extends AbstractITCase {
54
55 private PullPolicyTO buildPullPolicyTO() throws IOException {
56 ImplementationTO corrRule = null;
57 try {
58 corrRule = implementationService.read(ImplementationType.PULL_CORRELATION_RULE, "TestPullRule");
59 } catch (SyncopeClientException e) {
60 if (e.getType().getResponseStatus() == Response.Status.NOT_FOUND) {
61 corrRule = new ImplementationTO();
62 corrRule.setKey("TestPullRule");
63 corrRule.setEngine(ImplementationEngine.GROOVY);
64 corrRule.setType(ImplementationType.PULL_CORRELATION_RULE);
65 corrRule.setBody(IOUtils.toString(
66 getClass().getResourceAsStream("/TestPullRule.groovy"), StandardCharsets.UTF_8));
67 Response response = implementationService.create(corrRule);
68 corrRule = implementationService.read(
69 corrRule.getType(), response.getHeaderString(RESTHeaders.RESOURCE_KEY));
70 assertNotNull(corrRule);
71 }
72 }
73 assertNotNull(corrRule);
74
75 PullPolicyTO policy = new PullPolicyTO();
76 policy.getCorrelationRules().put(AnyTypeKind.USER.name(), corrRule.getKey());
77 policy.setDescription("Pull policy");
78
79 return policy;
80 }
81
82 @Test
83 public void listByType() {
84 List<PullPolicyTO> policyTOs = policyService.list(PolicyType.PULL);
85
86 assertNotNull(policyTOs);
87 assertFalse(policyTOs.isEmpty());
88 }
89
90 @Test
91 public void getAccountPolicy() {
92 AccountPolicyTO policyTO = policyService.read(PolicyType.ACCOUNT, "06e2ed52-6966-44aa-a177-a0ca7434201f");
93
94 assertNotNull(policyTO);
95 assertTrue(policyTO.getUsedByResources().isEmpty());
96 assertTrue(policyTO.getUsedByRealms().contains("/odd"));
97 }
98
99 @Test
100 public void getPasswordPolicy() {
101 PasswordPolicyTO policyTO = policyService.read(PolicyType.PASSWORD, "986d1236-3ac5-4a19-810c-5ab21d79cba1");
102
103 assertNotNull(policyTO);
104 assertTrue(policyTO.getUsedByResources().contains(RESOURCE_NAME_NOPROPAGATION));
105 assertTrue(policyTO.getUsedByRealms().containsAll(Arrays.asList("/", "/odd", "/even")));
106 }
107
108 @Test
109 public void getPullPolicy() {
110 PullPolicyTO policyTO = policyService.read(PolicyType.PULL, "66691e96-285f-4464-bc19-e68384ea4c85");
111
112 assertNotNull(policyTO);
113 assertTrue(policyTO.getUsedByRealms().isEmpty());
114 }
115
116 @Test
117 public void create() throws IOException {
118 PullPolicyTO policyTO = createPolicy(PolicyType.PULL, buildPullPolicyTO());
119 assertNotNull(policyTO);
120 assertEquals("TestPullRule", policyTO.getCorrelationRules().get(AnyTypeKind.USER.name()));
121 }
122
123 @Test
124 public void update() {
125 PasswordPolicyTO globalPolicy = policyService.read(PolicyType.PASSWORD, "ce93fcda-dc3a-4369-a7b0-a6108c261c85");
126
127 PasswordPolicyTO policy = SerializationUtils.clone(globalPolicy);
128 policy.setDescription("A simple password policy");
129
130 // create a new password policy using the former as a template
131 policy = createPolicy(PolicyType.PASSWORD, policy);
132 assertNotNull(policy);
133 assertNotEquals("ce93fcda-dc3a-4369-a7b0-a6108c261c85", policy.getKey());
134
135 ImplementationTO rule = implementationService.read(ImplementationType.PASSWORD_RULE, policy.getRules().get(0));
136 assertNotNull(rule);
137
138 DefaultPasswordRuleConf ruleConf = POJOHelper.deserialize(rule.getBody(), DefaultPasswordRuleConf.class);
139 ruleConf.setMaxLength(22);
140 rule.setBody(POJOHelper.serialize(ruleConf));
141
142 // update new password policy
143 policyService.update(PolicyType.PASSWORD, policy);
144 policy = policyService.read(PolicyType.PASSWORD, policy.getKey());
145 assertNotNull(policy);
146
147 ruleConf = POJOHelper.deserialize(rule.getBody(), DefaultPasswordRuleConf.class);
148 assertEquals(22, ruleConf.getMaxLength());
149 assertEquals(8, ruleConf.getMinLength());
150 }
151
152 @Test
153 public void delete() throws IOException {
154 PullPolicyTO policy = buildPullPolicyTO();
155
156 PullPolicyTO policyTO = createPolicy(PolicyType.PULL, policy);
157 assertNotNull(policyTO);
158
159 policyService.delete(PolicyType.PULL, policyTO.getKey());
160
161 try {
162 policyService.read(PolicyType.PULL, policyTO.getKey());
163 fail("This should not happen");
164 } catch (SyncopeClientException e) {
165 assertNotNull(e);
166 }
167 }
168
169 @Test
170 public void getPullCorrelationRuleJavaClasses() {
171 Set<String> classes = syncopeService.platform().
172 getJavaImplInfo(ImplementationType.PULL_CORRELATION_RULE).get().getClasses();
173 assertEquals(1, classes.size());
174 assertEquals(DummyPullCorrelationRule.class.getName(), classes.iterator().next());
175 }
176
177 @Test
178 public void issueSYNCOPE553() {
179 AccountPolicyTO policy = new AccountPolicyTO();
180 policy.setDescription("SYNCOPE553");
181
182 DefaultAccountRuleConf ruleConf = new DefaultAccountRuleConf();
183 ruleConf.setMinLength(3);
184 ruleConf.setMaxLength(8);
185
186 ImplementationTO rule = new ImplementationTO();
187 rule.setKey("DefaultAccountRuleConf" + getUUIDString());
188 rule.setEngine(ImplementationEngine.JAVA);
189 rule.setType(ImplementationType.ACCOUNT_RULE);
190 rule.setBody(POJOHelper.serialize(ruleConf));
191 Response response = implementationService.create(rule);
192 rule.setKey(response.getHeaderString(RESTHeaders.RESOURCE_KEY));
193
194 policy.getRules().add(rule.getKey());
195
196 policy = createPolicy(PolicyType.ACCOUNT, policy);
197 assertNotNull(policy);
198 }
199
200 @Test
201 public void issueSYNCOPE682() {
202 AccountPolicyTO policy = new AccountPolicyTO();
203 policy.setDescription("SYNCOPE682");
204 policy.getPassthroughResources().add(RESOURCE_NAME_LDAP);
205
206 DefaultAccountRuleConf ruleConf = new DefaultAccountRuleConf();
207 ruleConf.setMinLength(3);
208 ruleConf.setMaxLength(8);
209
210 ImplementationTO rule = new ImplementationTO();
211 rule.setKey("DefaultAccountRuleConf" + getUUIDString());
212 rule.setEngine(ImplementationEngine.JAVA);
213 rule.setType(ImplementationType.ACCOUNT_RULE);
214 rule.setBody(POJOHelper.serialize(ruleConf));
215 Response response = implementationService.create(rule);
216 rule.setKey(response.getHeaderString(RESTHeaders.RESOURCE_KEY));
217
218 policy.getRules().add(rule.getKey());
219
220 policy = createPolicy(PolicyType.ACCOUNT, policy);
221 assertNotNull(policy);
222 }
223 }