2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.lenient;
29 import static org.mockito.Mockito.mock;
32 import java.util.function.Consumer;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
41 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
42 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
43 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
44 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
45 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
46 import org.onap.policy.models.decisions.concepts.DecisionRequest;
47 import org.onap.policy.models.decisions.concepts.DecisionResponse;
49 @RunWith(MockitoJUnitRunner.class)
50 public class ConfigureOperationTest extends BasicHttpOperation {
53 private Consumer<OperationOutcome> started;
55 private Consumer<OperationOutcome> completed;
57 private DecisionConfig operConfig;
58 private ConfigureOperation oper;
61 * Starts the simulator.
64 public static void setUpBeforeClass() throws Exception {
65 org.onap.policy.simulators.Util.buildXacmlSim();
67 BusTopicParams clientParams = BusTopicParams.builder().clientName(MY_CLIENT).basePath("policy/pdpx/v1/")
68 .hostname("localhost").managed(true).port(org.onap.policy.simulators.Util.XACMLSIM_SERVER_PORT)
70 HttpClientFactoryInstance.getClientFactory().build(clientParams);
74 public static void tearDownAfterClass() {
75 HttpClientFactoryInstance.getClientFactory().destroy();
76 HttpServletServerFactoryInstance.getServerFactory().destroy();
86 operConfig = mock(DecisionConfig.class);
87 lenient().when(operConfig.makeRequest()).thenAnswer(args -> {
88 DecisionRequest req = new DecisionRequest();
89 req.setAction("guard");
90 req.setOnapComponent("my-onap-component");
91 req.setOnapInstance("my-onap-instance");
92 req.setOnapName("my-onap-name");
99 params = params.toBuilder().startCallback(started).completeCallback(completed).build();
101 oper = new ConfigureOperation(params, config);
105 public void testConstructor() {
106 assertEquals(DEFAULT_ACTOR, oper.getActorName());
107 assertEquals(DEFAULT_OPERATION, oper.getName());
111 * Tests "success" case with simulator.
114 public void testSuccess() throws Exception {
115 DecisionParams opParams =
116 DecisionParams.builder().clientName(MY_CLIENT).path("decision").action("configure").build();
117 config = new DecisionConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
119 params = params.toBuilder().payload(Map.of("policy-id", "test-policy")).retry(0).timeoutSec(5)
120 .executor(blockingExecutor).build();
121 oper = new ConfigureOperation(params, config);
123 outcome = oper.start().get();
124 assertEquals(OperationResult.SUCCESS, outcome.getResult());
126 DecisionResponse response = outcome.getResponse();
127 assertTrue(response instanceof DecisionResponse);
128 assertNotNull(response.getPolicies());
129 assertThat(response.getPolicies()).containsKey("test-policy");
133 * Tests "failure" case with simulator.
136 public void testFailure() throws Exception {
137 DecisionParams opParams =
138 DecisionParams.builder().clientName(MY_CLIENT).path("decision").action("configure").build();
139 config = new DecisionConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
141 params = params.toBuilder().payload(Map.of("policy-id", "nonexistent")).retry(0).timeoutSec(5)
142 .executor(blockingExecutor).build();
143 oper = new ConfigureOperation(params, config);
145 outcome = oper.start().get();
146 assertEquals(OperationResult.FAILURE, outcome.getResult());
148 DecisionResponse response = outcome.getResponse();
149 assertTrue(response instanceof DecisionResponse);
150 assertNotNull(response.getPolicies());
151 assertThat(response.getPolicies()).isEmpty();