2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.fail;
28 import java.util.HashMap;
30 import org.apache.commons.lang3.tuple.Pair;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.models.decisions.concepts.DecisionRequest;
38 import org.onap.policy.models.decisions.concepts.DecisionResponse;
39 import org.onap.policy.rest.RestManager;
41 public class GuardSimulatorTest {
42 private static final StandardCoder coder = new StandardCoder();
48 public static void setupSimulator() {
50 org.onap.policy.simulators.Util.buildGuardSim();
51 } catch (Exception e) {
57 public static void tearDownSimulator() {
58 HttpServletServerFactoryInstance.getServerFactory().destroy();
62 public void testGuard() throws CoderException {
63 String request = makeRequest("test_actor_id", "test_op_id", "test_target", "test_clName");
64 String url = "http://localhost:" + Util.GUARDSIM_SERVER_PORT + "/policy/pdpx/v1/decision";
65 Pair<Integer, String> response =
66 new RestManager().post(url, "testUname", "testPass", null, "application/json", request);
67 assertNotNull(response);
68 assertNotNull(response.getLeft());
69 assertNotNull(response.getRight());
71 DecisionResponse decision = coder.decode(response.getRight(), DecisionResponse.class);
72 assertEquals("Permit", decision.getStatus());
74 request = makeRequest("test_actor_id", "test_op_id", "test_target", "denyGuard");
75 response = new RestManager().post(url, "testUname", "testPass", null, "application/json", request);
76 assertNotNull(response);
77 assertNotNull(response.getLeft());
78 assertNotNull(response.getRight());
79 decision = coder.decode(response.getRight(), DecisionResponse.class);
80 assertEquals("Deny", decision.getStatus());
83 private static String makeRequest(String actor, String recipe, String target, String clName) throws CoderException {
84 Map<String, String> guard = new HashMap<String, String>();
85 guard.put("actor", actor);
86 guard.put("recipe", recipe);
87 guard.put("target", target);
88 guard.put("clname", clName);
89 Map<String, Object> resource = new HashMap<String, Object>();
90 resource.put("guard", guard);
91 DecisionRequest request = new DecisionRequest();
92 request.setResource(resource);
94 return coder.encode(request);