2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.controlloop.actor.xacml;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.never;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
36 import java.util.List;
38 import java.util.concurrent.CompletableFuture;
39 import java.util.function.Consumer;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.junit.MockitoJUnitRunner;
47 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
48 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
49 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
50 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
51 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
52 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
53 import org.onap.policy.controlloop.actorserviceprovider.Util;
54 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
55 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
56 import org.onap.policy.models.decisions.concepts.DecisionRequest;
57 import org.onap.policy.models.decisions.concepts.DecisionResponse;
58 import org.onap.policy.simulators.XacmlSimulatorJaxRs;
60 @RunWith(MockitoJUnitRunner.class)
61 public class DecisionOperationTest extends BasicHttpOperation {
62 private static final List<String> PROPERTY_NAMES = List.of("prop-A", "prop-B");
65 private Consumer<OperationOutcome> started;
67 private Consumer<OperationOutcome> completed;
69 private DecisionConfig guardConfig;
73 * Starts the simulator.
76 public static void setUpBeforeClass() throws Exception {
77 org.onap.policy.simulators.Util.buildXacmlSim();
79 BusTopicParams clientParams = BusTopicParams.builder().clientName(MY_CLIENT).basePath("policy/pdpx/v1/")
80 .hostname("localhost").managed(true).port(org.onap.policy.simulators.Util.XACMLSIM_SERVER_PORT)
82 HttpClientFactoryInstance.getClientFactory().build(clientParams);
86 public static void tearDownAfterClass() {
87 HttpClientFactoryInstance.getClientFactory().destroy();
88 HttpServletServerFactoryInstance.getServerFactory().destroy();
95 public void setUp() throws Exception {
98 guardConfig = mock(DecisionConfig.class);
99 when(guardConfig.makeRequest()).thenAnswer(args -> {
100 DecisionRequest req = new DecisionRequest();
101 req.setAction("guard");
102 req.setOnapComponent("my-onap-component");
103 req.setOnapInstance("my-onap-instance");
104 req.setOnapName("my-onap-name");
108 config = guardConfig;
111 params = params.toBuilder().startCallback(started).completeCallback(completed).build();
113 oper = new MyOper(params, config);
117 * Tests with simulator.
120 public void testSimulator() throws Exception {
121 DecisionParams opParams = DecisionParams.builder().clientName(MY_CLIENT).path("decision").build();
122 config = new DecisionConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
124 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor)
125 .payload(Map.of("clname", XacmlSimulatorJaxRs.DENY_CLNAME)).build();
126 oper = new MyOper(params, config);
128 outcome = oper.start().get();
129 assertEquals(OperationResult.FAILURE, outcome.getResult());
130 assertTrue(outcome.getResponse() instanceof DecisionResponse);
134 public void testConstructor() {
135 assertEquals(DEFAULT_ACTOR, oper.getActorName());
136 assertEquals(DEFAULT_OPERATION, oper.getName());
140 public void testGetPropertyNames() {
141 assertThat(oper.getPropertyNames()).isEqualTo(PROPERTY_NAMES);
145 public void testStartOperationAsync() throws Exception {
146 CompletableFuture<OperationOutcome> future2 = oper.start();
147 executor.runAll(100);
148 assertFalse(future2.isDone());
150 DecisionResponse resp = new DecisionResponse();
151 resp.setStatus(GuardOperation.PERMIT);
152 when(rawResponse.readEntity(String.class)).thenReturn(Util.translate("", resp, String.class));
154 verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any());
155 callbackCaptor.getValue().completed(rawResponse);
157 executor.runAll(100);
158 assertTrue(future2.isDone());
160 outcome = future2.get();
161 assertEquals(OperationResult.SUCCESS, outcome.getResult());
162 assertEquals(resp, outcome.getResponse());
164 assertNotNull(oper.getSubRequestId());
165 assertEquals(oper.getSubRequestId(), future2.get().getSubRequestId());
169 * Tests startOperationAsync() when the guard is disabled.
172 public void testStartOperationAsyncDisabled() throws Exception {
173 // indicate that it's disabled
174 when(guardConfig.isDisabled()).thenReturn(true);
176 CompletableFuture<OperationOutcome> future2 = oper.start();
177 executor.runAll(100);
179 verify(client, never()).post(any(), any(), any(), any());
181 // should already be done
182 assertTrue(future2.isDone());
184 outcome = future2.get();
185 assertEquals(OperationResult.SUCCESS, outcome.getResult());
186 assertNull(outcome.getResponse());
188 // ensure callbacks were invoked
189 verify(started).accept(any());
190 verify(completed).accept(any());
193 private class MyOper extends DecisionOperation {
195 public MyOper(ControlLoopOperationParams params, HttpConfig config) {
196 super(params, config, PROPERTY_NAMES);
200 protected DecisionRequest makeRequest() {
201 return guardConfig.makeRequest();