Merge "Add Guard Actor"
[policy/models.git] / models-interactions / model-actors / actor.guard / src / test / java / org / onap / policy / controlloop / actor / guard / GuardOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.guard;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import java.util.LinkedHashMap;
32 import java.util.Map;
33 import java.util.TreeMap;
34 import java.util.concurrent.CompletableFuture;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
40 import org.onap.policy.controlloop.actorserviceprovider.Util;
41 import org.onap.policy.controlloop.policy.PolicyResult;
42 import org.onap.policy.models.decisions.concepts.DecisionRequest;
43 import org.onap.policy.models.decisions.concepts.DecisionResponse;
44
45 public class GuardOperationTest extends BasicHttpOperation<DecisionRequest> {
46
47     private GuardOperation oper;
48
49     /**
50      * Sets up.
51      */
52     @Before
53     public void setUp() throws Exception {
54         super.setUp();
55
56         oper = new GuardOperation(params, operator);
57     }
58
59     @Test
60     public void testConstructor() {
61         assertEquals(DEFAULT_ACTOR, oper.getActorName());
62         assertEquals(DEFAULT_OPERATION, oper.getName());
63     }
64
65     @Test
66     public void testStartOperationAsync() throws Exception {
67         CompletableFuture<OperationOutcome> future2 = oper.start();
68         executor.runAll(100);
69         assertFalse(future2.isDone());
70
71         DecisionResponse resp = new DecisionResponse();
72         resp.setStatus(GuardOperation.PERMIT);
73         when(rawResponse.readEntity(String.class)).thenReturn(Util.translate("", resp, String.class));
74
75         verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any());
76         callbackCaptor.getValue().completed(rawResponse);
77
78         executor.runAll(100);
79         assertTrue(future2.isDone());
80
81         DecisionRequest request = requestCaptor.getValue().getEntity();
82         verifyRequest("makeReqStd.json", request, "requestId");
83
84         assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
85     }
86
87     @Test
88     public void testMakeRequest() throws CoderException {
89         verifyPayload("makeReqStd.json", makePayload());
90         verifyPayload("makeReqDefault.json", new TreeMap<>());
91
92         Map<String, String> payload = new TreeMap<>();
93         payload.put("action", "some action");
94         payload.put("hello", "world");
95         payload.put("r u there?", "yes");
96         payload.put("requestId", "some request id");
97         payload.put("resource.abc", "def");
98         payload.put("resource.ghi", "jkl");
99         payload.put("some.other", "unused");
100
101         verifyPayload("makeReq.json", payload);
102
103         // null payload - start with fresh parameters and operation
104         params = params.toBuilder().payload(null).build();
105         oper = new GuardOperation(params, operator);
106         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest());
107     }
108
109     private void verifyPayload(String expectedJsonFile, Map<String, String> payload) throws CoderException {
110         params.getPayload().clear();
111         params.getPayload().putAll(payload);
112
113         Map<String, Object> requestMap = oper.makeRequest();
114
115         verifyRequest(expectedJsonFile, requestMap, "requestId");
116     }
117
118     @Test
119     public void testPostProcessResponse() {
120         DecisionResponse response = new DecisionResponse();
121
122         // null status
123         response.setStatus(null);
124         verifyOutcome(response, PolicyResult.FAILURE, "response contains no status");
125
126         // permit, mixed case
127         response.setStatus("peRmit");
128         verifyOutcome(response, PolicyResult.SUCCESS, "peRmit");
129
130         // indeterminate, mixed case
131         response.setStatus("inDETerminate");
132         verifyOutcome(response, PolicyResult.SUCCESS, "inDETerminate");
133
134         // deny, mixed case
135         response.setStatus("deNY");
136         verifyOutcome(response, PolicyResult.FAILURE, "deNY");
137
138         // unknown status
139         response.setStatus("unknown");
140         verifyOutcome(response, PolicyResult.FAILURE, "unknown");
141     }
142
143     private void verifyOutcome(DecisionResponse response, PolicyResult expectedResult, String expectedMessage) {
144         oper.postProcessResponse(outcome, BASE_URI, rawResponse, response);
145         assertEquals(expectedResult, outcome.getResult());
146         assertEquals(expectedMessage, outcome.getMessage());
147     }
148
149     @Override
150     protected Map<String, String> makePayload() {
151         DecisionRequest req = new DecisionRequest();
152         req.setAction("my-action");
153         req.setOnapComponent("my-onap-component");
154         req.setOnapInstance("my-onap-instance");
155         req.setOnapName("my-onap-name");
156         req.setRequestId("my-request-id");
157
158         @SuppressWarnings("unchecked")
159         Map<String, String> map = Util.translate("", req, LinkedHashMap.class);
160
161         // add resources
162         map.put(GuardOperation.RESOURCE_PREFIX + "actor", "resource-actor");
163         map.put(GuardOperation.RESOURCE_PREFIX + "operation", "resource-operation");
164
165         return map;
166     }
167 }