2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019, 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.simulators;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.fail;
29 import java.util.HashMap;
31 import org.apache.commons.lang3.tuple.Pair;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.models.decisions.concepts.DecisionRequest;
39 import org.onap.policy.models.decisions.concepts.DecisionResponse;
40 import org.onap.policy.rest.RestManager;
42 public class XacmlSimulatorTest {
43 private static final StandardCoder coder = new StandardCoder();
49 public static void setupSimulator() {
51 var testServer = Util.buildXacmlSim();
52 assertNotNull(testServer);
53 } catch (Exception e) {
59 public static void tearDownSimulator() {
60 HttpServletServerFactoryInstance.getServerFactory().destroy();
64 public void testGuard() throws CoderException {
65 String request = makeGuardRequest("test_actor_id", "test_op_id", "test_target", "test_clName");
66 DecisionResponse decision = sendRequest(request);
67 assertEquals("Permit", decision.getStatus());
69 request = makeGuardRequest("test_actor_id", "test_op_id", "test_target", "denyGuard");
70 decision = sendRequest(request);
71 assertEquals("Deny", decision.getStatus());
75 public void testConfigure() throws CoderException {
76 // test retrieving a policy
77 String request = makeConfigureRequest("policy-id", "test-policy");
78 DecisionResponse decision = sendRequest(request);
79 assertNotNull(decision.getPolicies());
80 assertThat(decision.getPolicies()).containsKey("test-policy");
82 // test no policy found
83 request = makeConfigureRequest("policy-id", "nonexistent");
84 decision = sendRequest(request);
85 assertNotNull(decision.getPolicies());
86 assertThat(decision.getPolicies()).doesNotContainKey("nonexistent");
88 // test unsupported operation
89 request = makeConfigureRequest("policy-type", "test");
90 decision = sendRequest(request);
91 assertEquals("resource must contain policy-id key", decision.getMessage());
95 public void testConfigureMissingFile() throws CoderException {
96 // test retrieving a policy
97 String request = makeConfigureRequest("policy-id", "bogus-policy");
98 DecisionResponse decision = sendRequest(request);
99 assertNotNull(decision.getPolicies());
100 assertEquals("cannot read policy simulator file", decision.getMessage());
104 public void testConfigureInvalidJson() throws CoderException {
105 // test retrieving a policy
106 String request = makeConfigureRequest("policy-id", "invalid-policy");
107 DecisionResponse decision = sendRequest(request);
108 assertNotNull(decision.getPolicies());
109 assertEquals("cannot decode policy", decision.getMessage());
113 public void testUnknownAction() throws CoderException {
114 String request = makeGuardRequest("test_actor_id", "test_op_id", "test_target", "test_clName");
115 request = request.replace("guard", "bogus-action");
116 DecisionResponse decision = sendRequest(request);
117 assertThat(decision.getStatus()).isNull();
118 assertThat(decision.getMessage()).isEqualTo("unsupported action: bogus-action");
121 private DecisionResponse sendRequest(String request) throws CoderException {
122 String url = "http://localhost:" + Util.XACMLSIM_SERVER_PORT + "/policy/pdpx/v1/decision";
123 Pair<Integer, String> response =
124 new RestManager().post(url, "testUname", "testPass", null, "application/json", request);
126 // verify the response isn't null
127 assertNotNull(response);
128 assertNotNull(response.getLeft());
129 assertNotNull(response.getRight());
131 return coder.decode(response.getRight(), DecisionResponse.class);
134 private String makeGuardRequest(String actor, String recipe, String target, String clName) throws CoderException {
135 Map<String, String> guard = new HashMap<>();
136 guard.put("actor", actor);
137 guard.put("recipe", recipe);
138 guard.put("target", target);
139 guard.put("clname", clName);
141 Map<String, Object> resource = new HashMap<>();
142 resource.put("guard", guard);
144 DecisionRequest request = new DecisionRequest();
145 request.setAction("guard");
146 request.setResource(resource);
148 return coder.encode(request);
151 private String makeConfigureRequest(String key, String val) throws CoderException {
152 Map<String, Object> resource = new HashMap<>();
153 resource.put(key, val);
155 DecisionRequest request = new DecisionRequest();
156 request.setAction("configure");
157 request.setResource(resource);
159 return coder.encode(request);