Upgrade and clean up dependencies
[policy/models.git] / models-interactions / model-actors / actor.xacml / src / test / java / org / onap / policy / controlloop / actor / xacml / DecisionOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.controlloop.actor.xacml;
23
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;
35
36 import java.util.List;
37 import java.util.Map;
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;
59
60 @RunWith(MockitoJUnitRunner.class)
61 public class DecisionOperationTest extends BasicHttpOperation {
62     private static final List<String> PROPERTY_NAMES = List.of("prop-A", "prop-B");
63
64     @Mock
65     private Consumer<OperationOutcome> started;
66     @Mock
67     private Consumer<OperationOutcome> completed;
68
69     private DecisionConfig guardConfig;
70     private MyOper oper;
71
72     /**
73      * Starts the simulator.
74      */
75     @BeforeClass
76     public static void setUpBeforeClass() throws Exception {
77         org.onap.policy.simulators.Util.buildXacmlSim();
78
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)
81                         .build();
82         HttpClientFactoryInstance.getClientFactory().build(clientParams);
83     }
84
85     @AfterClass
86     public static void tearDownAfterClass() {
87         HttpClientFactoryInstance.getClientFactory().destroy();
88         HttpServletServerFactoryInstance.getServerFactory().destroy();
89     }
90
91     /**
92      * Sets up.
93      */
94     @Before
95     public void setUp() throws Exception {
96         super.setUpBasic();
97
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");
105             return req;
106         });
107
108         config = guardConfig;
109         initConfig();
110
111         params = params.toBuilder().startCallback(started).completeCallback(completed).build();
112
113         oper = new MyOper(params, config);
114     }
115
116     /**
117      * Tests with simulator.
118      */
119     @Test
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());
123
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);
127
128         outcome = oper.start().get();
129         assertEquals(OperationResult.FAILURE, outcome.getResult());
130         assertTrue(outcome.getResponse() instanceof DecisionResponse);
131     }
132
133     @Test
134     public void testConstructor() {
135         assertEquals(DEFAULT_ACTOR, oper.getActorName());
136         assertEquals(DEFAULT_OPERATION, oper.getName());
137     }
138
139     @Test
140     public void testGetPropertyNames() {
141         assertThat(oper.getPropertyNames()).isEqualTo(PROPERTY_NAMES);
142     }
143
144     @Test
145     public void testStartOperationAsync() throws Exception {
146         CompletableFuture<OperationOutcome> future2 = oper.start();
147         executor.runAll(100);
148         assertFalse(future2.isDone());
149
150         DecisionResponse resp = new DecisionResponse();
151         resp.setStatus(GuardOperation.PERMIT);
152         when(rawResponse.readEntity(String.class)).thenReturn(Util.translate("", resp, String.class));
153
154         verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any());
155         callbackCaptor.getValue().completed(rawResponse);
156
157         executor.runAll(100);
158         assertTrue(future2.isDone());
159
160         outcome = future2.get();
161         assertEquals(OperationResult.SUCCESS, outcome.getResult());
162         assertEquals(resp, outcome.getResponse());
163
164         assertNotNull(oper.getSubRequestId());
165         assertEquals(oper.getSubRequestId(), future2.get().getSubRequestId());
166     }
167
168     /**
169      * Tests startOperationAsync() when the guard is disabled.
170      */
171     @Test
172     public void testStartOperationAsyncDisabled() throws Exception {
173         // indicate that it's disabled
174         when(guardConfig.isDisabled()).thenReturn(true);
175
176         CompletableFuture<OperationOutcome> future2 = oper.start();
177         executor.runAll(100);
178
179         verify(client, never()).post(any(), any(), any(), any());
180
181         // should already be done
182         assertTrue(future2.isDone());
183
184         outcome = future2.get();
185         assertEquals(OperationResult.SUCCESS, outcome.getResult());
186         assertNull(outcome.getResponse());
187
188         // ensure callbacks were invoked
189         verify(started).accept(any());
190         verify(completed).accept(any());
191     }
192
193     private class MyOper extends DecisionOperation {
194
195         public MyOper(ControlLoopOperationParams params, HttpConfig config) {
196             super(params, config, PROPERTY_NAMES);
197         }
198
199         @Override
200         protected DecisionRequest makeRequest() {
201             return guardConfig.makeRequest();
202         }
203     }
204 }