c997980774b0c10e2f287a2eb778e13744b3be70
[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 org.junit.AfterClass;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
32 import org.onap.policy.rest.RestManager;
33 import org.onap.policy.rest.RestManager.Pair;
34
35 public class GuardSimulatorTest {
36
37     /**
38      * Set up test class.
39      */
40     @BeforeClass
41     public static void setupSimulator() {
42         try {
43             org.onap.policy.simulators.Util.buildGuardSim();
44         } catch (Exception e) {
45             fail(e.getMessage());
46         }
47     }
48
49     @AfterClass
50     public static void tearDownSimulator() {
51         HttpServletServer.factory.destroy();
52     }
53
54     @Test
55     public void testGuard() {
56         String request = makeRequest("test_actor_id", "test_op_id", "test_target", "test_clName");
57         String url = "http://localhost:" + Util.GUARDSIM_SERVER_PORT + "/pdp/api/getDecision";
58         Pair<Integer, String> response =
59                 new RestManager().post(url, "testUname", "testPass", null, "application/json", request);
60         assertNotNull(response);
61         assertNotNull(response.first);
62         assertNotNull(response.second);
63         assertEquals("{\"decision\": \"PERMIT\", \"details\": \"Decision Permit. OK!\"}", response.second);
64
65         request = makeRequest("test_actor_id", "test_op_id", "test_target", "denyGuard");
66         response = new RestManager().post(url, "testUname", "testPass", null, "application/json", request);
67         assertNotNull(response);
68         assertNotNull(response.first);
69         assertNotNull(response.second);
70         assertEquals("{\"decision\": \"DENY\", \"details\": \"Decision Deny. You asked for it\"}", response.second);
71     }
72
73     private static String makeRequest(String actor, String recipe, String target, String clName) {
74         return "{\"decisionAttributes\": {\"actor\": \"" + actor + "\", \"recipe\": \"" + recipe + "\""
75                 + ", \"target\": \"" + target + "\", \"clname\": \"" + clName + "\"}, \"onapName\": \"PDPD\"}";
76     }
77 }