Upgrade and clean up dependencies
[policy/models.git] / models-interactions / model-actors / actor.xacml / src / test / java / org / onap / policy / controlloop / actor / xacml / ConfigureOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.lenient;
29 import static org.mockito.Mockito.mock;
30
31 import java.util.Map;
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;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class ConfigureOperationTest extends BasicHttpOperation {
51
52     @Mock
53     private Consumer<OperationOutcome> started;
54     @Mock
55     private Consumer<OperationOutcome> completed;
56
57     private DecisionConfig operConfig;
58     private ConfigureOperation oper;
59
60     /**
61      * Starts the simulator.
62      */
63     @BeforeClass
64     public static void setUpBeforeClass() throws Exception {
65         org.onap.policy.simulators.Util.buildXacmlSim();
66
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)
69                         .build();
70         HttpClientFactoryInstance.getClientFactory().build(clientParams);
71     }
72
73     @AfterClass
74     public static void tearDownAfterClass() {
75         HttpClientFactoryInstance.getClientFactory().destroy();
76         HttpServletServerFactoryInstance.getServerFactory().destroy();
77     }
78
79     /**
80      * Sets up.
81      */
82     @Before
83     public void setUp() {
84         super.setUpBasic();
85
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");
93             return req;
94         });
95
96         config = operConfig;
97         initConfig();
98
99         params = params.toBuilder().startCallback(started).completeCallback(completed).build();
100
101         oper = new ConfigureOperation(params, config);
102     }
103
104     @Test
105     public void testConstructor() {
106         assertEquals(DEFAULT_ACTOR, oper.getActorName());
107         assertEquals(DEFAULT_OPERATION, oper.getName());
108     }
109
110     /**
111      * Tests "success" case with simulator.
112      */
113     @Test
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());
118
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);
122
123         outcome = oper.start().get();
124         assertEquals(OperationResult.SUCCESS, outcome.getResult());
125
126         DecisionResponse response = outcome.getResponse();
127         assertTrue(response instanceof DecisionResponse);
128         assertNotNull(response.getPolicies());
129         assertThat(response.getPolicies()).containsKey("test-policy");
130     }
131
132     /**
133      * Tests "failure" case with simulator.
134      */
135     @Test
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());
140
141         params = params.toBuilder().payload(Map.of("policy-id", "nonexistent")).retry(0).timeoutSec(5)
142                         .executor(blockingExecutor).build();
143         oper = new ConfigureOperation(params, config);
144
145         outcome = oper.start().get();
146         assertEquals(OperationResult.FAILURE, outcome.getResult());
147
148         DecisionResponse response = outcome.getResponse();
149         assertTrue(response instanceof DecisionResponse);
150         assertNotNull(response.getPolicies());
151         assertThat(response.getPolicies()).isEmpty();
152     }
153
154 }