Clean up of Pair classes - models
[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-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
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 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;
40
41 public class GuardSimulatorTest {
42     private static final StandardCoder coder = new StandardCoder();
43
44     /**
45      * Set up test class.
46      */
47     @BeforeClass
48     public static void setupSimulator() {
49         try {
50             org.onap.policy.simulators.Util.buildGuardSim();
51         } catch (Exception e) {
52             fail(e.getMessage());
53         }
54     }
55
56     @AfterClass
57     public static void tearDownSimulator() {
58         HttpServletServerFactoryInstance.getServerFactory().destroy();
59     }
60
61     @Test
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());
70
71         DecisionResponse decision = coder.decode(response.getRight(), DecisionResponse.class);
72         assertEquals("Permit", decision.getStatus());
73
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());
81     }
82
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);
93
94         return coder.encode(request);
95     }
96 }