2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actor.appc;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertTrue;
31 import static org.mockito.ArgumentMatchers.any;
32 import static org.mockito.ArgumentMatchers.eq;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.verify;
35 import static org.mockito.Mockito.when;
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.concurrent.CompletableFuture;
40 import java.util.concurrent.atomic.AtomicBoolean;
41 import org.junit.After;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.mockito.Mock;
47 import org.onap.aai.domain.yang.GenericVnf;
48 import org.onap.policy.aai.AaiCqResponse;
49 import org.onap.policy.appc.Request;
50 import org.onap.policy.appc.Response;
51 import org.onap.policy.common.utils.coder.CoderException;
52 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
53 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
54 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
55 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
56 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
57 import org.onap.policy.controlloop.policy.PolicyResult;
59 public class ModifyConfigOperationTest extends BasicAppcOperation {
62 private GenericVnf genvnf;
64 private AaiCqResponse cq;
66 private ModifyConfigOperation oper;
69 public ModifyConfigOperationTest() {
70 super(DEFAULT_ACTOR, ModifyConfigOperation.NAME);
74 public static void setUpBeforeClass() throws Exception {
75 // use same topic name for both sides
76 initBeforeClass(MY_SINK, MY_SINK);
80 public static void tearDownAfterClass() {
86 public void setUp() throws Exception {
88 when(genvnf.getVnfId()).thenReturn(MY_VNF);
89 when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
91 oper = new ModifyConfigOperation(params, config);
96 public void tearDown() {
101 * Tests "success" case with simulator.
104 public void testSuccess() throws Exception {
105 BidirectionalTopicParams opParams =
106 BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SINK).build();
107 config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, AppcOperation.SELECTOR_KEYS);
109 params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
111 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).preprocessed(true).build();
113 oper = new ModifyConfigOperation(params, config) {
115 protected CompletableFuture<OperationOutcome> startGuardAsync() {
120 oper.setProperty(OperationProperties.AAI_RESOURCE_VNF, genvnf);
122 outcome = oper.start().get();
123 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
124 assertTrue(outcome.getResponse() instanceof Response);
128 public void testConstructor() {
129 assertEquals(DEFAULT_ACTOR, oper.getActorName());
130 assertEquals(ModifyConfigOperation.NAME, oper.getName());
134 public void testGetPropertyNames() {
135 assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_RESOURCE_VNF));
139 public void testStartPreprocessorAsync() throws Exception {
140 CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
141 context = mock(ControlLoopEventContext.class);
142 when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
143 when(context.getEvent()).thenReturn(event);
144 params = params.toBuilder().context(context).build();
146 AtomicBoolean guardStarted = new AtomicBoolean();
148 oper = new ModifyConfigOperation(params, config) {
150 protected CompletableFuture<OperationOutcome> startGuardAsync() {
151 guardStarted.set(true);
152 return super.startGuardAsync();
156 CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
157 assertNotNull(future3);
158 assertFalse(future.isDone());
159 assertTrue(guardStarted.get());
160 verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
162 future2.complete(params.makeOutcome(null));
163 assertTrue(executor.runAll(100));
164 assertTrue(future3.isDone());
165 assertEquals(PolicyResult.SUCCESS, future3.get().getResult());
169 * Tests startPreprocessorAsync(), when preprocessing is disabled.
172 public void testStartPreprocessorAsyncDisabled() {
173 params = params.toBuilder().preprocessed(true).build();
174 assertNull(new ModifyConfigOperation(params, config).startPreprocessorAsync());
178 public void testMakeRequest() throws CoderException {
179 oper.setProperty(OperationProperties.AAI_RESOURCE_VNF, genvnf);
181 oper.generateSubRequestId(2);
182 Request request = oper.makeRequest(2);
183 assertNotNull(request);
184 assertEquals(MY_VNF, request.getPayload().get(ModifyConfigOperation.VNF_ID_KEY));
186 verifyRequest("modifyConfig.json", request, IGNORE_FIELDS);
190 public void testGetVnfIdViaProperty() throws CoderException {
191 oper.setProperty(OperationProperties.AAI_RESOURCE_VNF, genvnf);
192 assertEquals(MY_VNF, oper.getVnfId());
196 public void testGetVnfId() throws CoderException {
198 assertThatIllegalStateException().isThrownBy(() -> oper.getVnfId())
199 .withMessage("target vnf-id could not be determined");
201 cq = new AaiCqResponse("{}");
204 params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
205 assertThatIllegalArgumentException().isThrownBy(() -> oper.getVnfId())
206 .withMessage("target vnf-id could not be found");
208 // populate the CQ data with a vnf-id
209 genvnf = new GenericVnf();
210 genvnf.setVnfId(MY_VNF);
211 genvnf.setModelInvariantId(RESOURCE_ID);
212 cq.setInventoryResponseItems(Arrays.asList(genvnf));
214 assertEquals(MY_VNF, oper.getVnfId());