2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actor.guard;
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.mock;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
34 import java.util.TreeMap;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.function.Consumer;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
42 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
43 import org.onap.policy.controlloop.actorserviceprovider.Util;
44 import org.onap.policy.controlloop.policy.PolicyResult;
45 import org.onap.policy.models.decisions.concepts.DecisionRequest;
46 import org.onap.policy.models.decisions.concepts.DecisionResponse;
48 public class GuardOperationTest extends BasicHttpOperation<DecisionRequest> {
51 private Consumer<OperationOutcome> started;
53 private Consumer<OperationOutcome> completed;
55 private GuardConfig guardConfig;
56 private GuardOperation oper;
62 public void setUp() throws Exception {
65 guardConfig = mock(GuardConfig.class);
66 when(guardConfig.makeRequest()).thenAnswer(args -> new TreeMap<>(Map.of("action", "guard")));
71 params = params.toBuilder().startCallback(started).completeCallback(completed).build();
73 oper = new GuardOperation(params, config);
77 public void testConstructor() {
78 assertEquals(DEFAULT_ACTOR, oper.getActorName());
79 assertEquals(DEFAULT_OPERATION, oper.getName());
83 public void testStartOperationAsync() throws Exception {
84 CompletableFuture<OperationOutcome> future2 = oper.start();
86 assertFalse(future2.isDone());
88 DecisionResponse resp = new DecisionResponse();
89 resp.setStatus(GuardOperation.PERMIT);
90 when(rawResponse.readEntity(String.class)).thenReturn(Util.translate("", resp, String.class));
92 verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any());
93 callbackCaptor.getValue().completed(rawResponse);
96 assertTrue(future2.isDone());
98 assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
102 * Tests startOperationAsync() when the guard is disabled.
105 public void testStartOperationAsyncDisabled() throws Exception {
106 // indicate that it's disabled
107 when(guardConfig.isDisabled()).thenReturn(true);
109 CompletableFuture<OperationOutcome> future2 = oper.start();
110 executor.runAll(100);
112 verify(client, never()).post(any(), any(), any(), any());
114 // should already be done
115 assertTrue(future2.isDone());
117 assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
119 // ensure callbacks were invoked
120 verify(started).accept(any());
121 verify(completed).accept(any());
125 public void testMakeRequest() throws CoderException {
126 verifyPayload("makeReqStd.json", makePayload());
127 verifyPayload("makeReqDefault.json", new TreeMap<>());
129 Map<String, Object> payload = new TreeMap<>();
130 payload.put("action", "some action");
131 payload.put("hello", "world");
132 payload.put("r u there?", "yes");
133 payload.put("requestId", "some request id");
135 Map<String, Object> resource = new TreeMap<>();
136 payload.put("resource", resource);
137 resource.put("abc", "def");
138 resource.put("ghi", "jkl");
140 verifyPayload("makeReq.json", payload);
142 // null payload - start with fresh parameters and operation
143 params = params.toBuilder().payload(null).build();
144 oper = new GuardOperation(params, config);
145 assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest());
148 private void verifyPayload(String expectedJsonFile, Map<String, Object> payload) throws CoderException {
149 params.getPayload().clear();
150 params.getPayload().putAll(payload);
152 Map<String, Object> requestMap = oper.makeRequest();
154 verifyRequest(expectedJsonFile, requestMap, "requestId");
158 public void testPostProcessResponse() {
159 DecisionResponse response = new DecisionResponse();
162 response.setStatus(null);
163 verifyOutcome(response, PolicyResult.FAILURE, "response contains no status");
165 // permit, mixed case
166 response.setStatus("peRmit");
167 verifyOutcome(response, PolicyResult.SUCCESS, "peRmit");
169 // indeterminate, mixed case
170 response.setStatus("inDETerminate");
171 verifyOutcome(response, PolicyResult.SUCCESS, "inDETerminate");
174 response.setStatus("deNY");
175 verifyOutcome(response, PolicyResult.FAILURE, "deNY");
178 response.setStatus("unknown");
179 verifyOutcome(response, PolicyResult.FAILURE, "unknown");
182 private void verifyOutcome(DecisionResponse response, PolicyResult expectedResult, String expectedMessage) {
183 oper.postProcessResponse(outcome, BASE_URI, rawResponse, response);
184 assertEquals(expectedResult, outcome.getResult());
185 assertEquals(expectedMessage, outcome.getMessage());
189 protected Map<String, Object> makePayload() {
190 DecisionRequest req = new DecisionRequest();
191 req.setAction("my-action");
192 req.setOnapComponent("my-onap-component");
193 req.setOnapInstance("my-onap-instance");
194 req.setOnapName("my-onap-name");
195 req.setRequestId("my-request-id");
198 Map<String, Object> resource = new TreeMap<>();
199 req.setResource(resource);
200 resource.put("actor", "resource-actor");
201 resource.put("operation", "resource-operation");
203 @SuppressWarnings("unchecked")
204 Map<String, Object> map = Util.translate("", req, TreeMap.class);