406cdd05dcac274a793fb1aad7aff08a60d784a6
[policy/models.git] /
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.mock;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
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.setUpBasic();
55
56         GuardConfig cguard = mock(GuardConfig.class);
57         when(cguard.makeRequest()).thenAnswer(args -> new TreeMap<>(Map.of("action", "guard")));
58
59         config = cguard;
60         initConfig();
61
62         oper = new GuardOperation(params, config);
63     }
64
65     @Test
66     public void testConstructor() {
67         assertEquals(DEFAULT_ACTOR, oper.getActorName());
68         assertEquals(DEFAULT_OPERATION, oper.getName());
69     }
70
71     @Test
72     public void testStartOperationAsync() throws Exception {
73         CompletableFuture<OperationOutcome> future2 = oper.start();
74         executor.runAll(100);
75         assertFalse(future2.isDone());
76
77         DecisionResponse resp = new DecisionResponse();
78         resp.setStatus(GuardOperation.PERMIT);
79         when(rawResponse.readEntity(String.class)).thenReturn(Util.translate("", resp, String.class));
80
81         verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any());
82         callbackCaptor.getValue().completed(rawResponse);
83
84         executor.runAll(100);
85         assertTrue(future2.isDone());
86
87         assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
88     }
89
90     @Test
91     public void testMakeRequest() throws CoderException {
92         verifyPayload("makeReqStd.json", makePayload());
93         verifyPayload("makeReqDefault.json", new TreeMap<>());
94
95         Map<String, String> payload = new TreeMap<>();
96         payload.put("action", "some action");
97         payload.put("hello", "world");
98         payload.put("r u there?", "yes");
99         payload.put("requestId", "some request id");
100         payload.put("resource.abc", "def");
101         payload.put("resource.ghi", "jkl");
102         payload.put("some.other", "unused");
103
104         verifyPayload("makeReq.json", payload);
105
106         // null payload - start with fresh parameters and operation
107         params = params.toBuilder().payload(null).build();
108         oper = new GuardOperation(params, config);
109         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest());
110     }
111
112     private void verifyPayload(String expectedJsonFile, Map<String, String> payload) throws CoderException {
113         params.getPayload().clear();
114         params.getPayload().putAll(payload);
115
116         Map<String, Object> requestMap = oper.makeRequest();
117
118         verifyRequest(expectedJsonFile, requestMap, "requestId");
119     }
120
121     @Test
122     public void testPostProcessResponse() {
123         DecisionResponse response = new DecisionResponse();
124
125         // null status
126         response.setStatus(null);
127         verifyOutcome(response, PolicyResult.FAILURE, "response contains no status");
128
129         // permit, mixed case
130         response.setStatus("peRmit");
131         verifyOutcome(response, PolicyResult.SUCCESS, "peRmit");
132
133         // indeterminate, mixed case
134         response.setStatus("inDETerminate");
135         verifyOutcome(response, PolicyResult.SUCCESS, "inDETerminate");
136
137         // deny, mixed case
138         response.setStatus("deNY");
139         verifyOutcome(response, PolicyResult.FAILURE, "deNY");
140
141         // unknown status
142         response.setStatus("unknown");
143         verifyOutcome(response, PolicyResult.FAILURE, "unknown");
144     }
145
146     private void verifyOutcome(DecisionResponse response, PolicyResult expectedResult, String expectedMessage) {
147         oper.postProcessResponse(outcome, BASE_URI, rawResponse, response);
148         assertEquals(expectedResult, outcome.getResult());
149         assertEquals(expectedMessage, outcome.getMessage());
150     }
151
152     @Override
153     protected Map<String, String> makePayload() {
154         DecisionRequest req = new DecisionRequest();
155         req.setAction("my-action");
156         req.setOnapComponent("my-onap-component");
157         req.setOnapInstance("my-onap-instance");
158         req.setOnapName("my-onap-name");
159         req.setRequestId("my-request-id");
160
161         @SuppressWarnings("unchecked")
162         Map<String, String> map = Util.translate("", req, TreeMap.class);
163
164         // add resources
165         map.put(GuardOperation.RESOURCE_PREFIX + "actor", "resource-actor");
166         map.put(GuardOperation.RESOURCE_PREFIX + "operation", "resource-operation");
167
168         return map;
169     }
170 }