Fix fieldname for guard
[policy/models.git] / models-interactions / model-simulators / src / test / java / org / onap / policy / simulators / GuardSimulatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * simulators
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.simulators;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.fail;
27
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
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;
40 import org.onap.policy.rest.RestManager.Pair;
41
42 public class GuardSimulatorTest {
43     private static final StandardCoder coder = new StandardCoder();
44
45     /**
46      * Set up test class.
47      */
48     @BeforeClass
49     public static void setupSimulator() {
50         try {
51             org.onap.policy.simulators.Util.buildGuardSim();
52         } catch (Exception e) {
53             fail(e.getMessage());
54         }
55     }
56
57     @AfterClass
58     public static void tearDownSimulator() {
59         HttpServletServer.factory.destroy();
60     }
61
62     @Test
63     public void testGuard() throws CoderException {
64         String request = makeRequest("test_actor_id", "test_op_id", "test_target", "test_clName");
65         String url = "http://localhost:" + Util.GUARDSIM_SERVER_PORT + "/policy/pdpx/v1/decision";
66         Pair<Integer, String> response =
67                 new RestManager().post(url, "testUname", "testPass", null, "application/json", request);
68         assertNotNull(response);
69         assertNotNull(response.first);
70         assertNotNull(response.second);
71
72         DecisionResponse decision = coder.decode(response.second, DecisionResponse.class);
73         assertEquals("Permit", decision.getStatus());
74
75         request = makeRequest("test_actor_id", "test_op_id", "test_target", "denyGuard");
76         response = new RestManager().post(url, "testUname", "testPass", null, "application/json", request);
77         assertNotNull(response);
78         assertNotNull(response.first);
79         assertNotNull(response.second);
80         decision = coder.decode(response.second, DecisionResponse.class);
81         assertEquals("Deny", decision.getStatus());
82     }
83
84     private static String makeRequest(String actor, String recipe, String target, String clName) throws CoderException {
85         Map<String, String> guard = new HashMap<String, String>();
86         guard.put("actor", actor);
87         guard.put("recipe", recipe);
88         guard.put("target", target);
89         guard.put("clname", clName);
90         Map<String, Object> resource = new HashMap<String, Object>();
91         resource.put("guard", guard);
92         DecisionRequest request = new DecisionRequest();
93         request.setResource(resource);
94
95         return coder.encode(request);
96     }
97 }