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.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.ArgumentMatchers.eq;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.concurrent.CompletableFuture;
39 import java.util.concurrent.atomic.AtomicBoolean;
40 import org.junit.After;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.onap.aai.domain.yang.GenericVnf;
46 import org.onap.policy.aai.AaiCqResponse;
47 import org.onap.policy.appc.Request;
48 import org.onap.policy.appc.Response;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
51 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
52 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
53 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
54 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
55 import org.onap.policy.controlloop.policy.PolicyResult;
57 public class ModifyConfigOperationTest extends BasicAppcOperation {
59 private ModifyConfigOperation oper;
61 public ModifyConfigOperationTest() {
62 super(DEFAULT_ACTOR, ModifyConfigOperation.NAME);
66 public static void setUpBeforeClass() throws Exception {
67 // use same topic name for both sides
68 initBeforeClass(MY_SINK, MY_SINK);
72 public static void tearDownAfterClass() {
78 public void setUp() throws Exception {
80 oper = new ModifyConfigOperation(params, config);
85 public void tearDown() {
90 * Tests "success" case with simulator.
93 public void testSuccess() throws Exception {
94 BidirectionalTopicParams opParams =
95 BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SINK).build();
96 config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, AppcOperation.SELECTOR_KEYS);
98 AaiCqResponse cq = mock(AaiCqResponse.class);
99 GenericVnf genvnf = mock(GenericVnf.class);
100 when(genvnf.getVnfId()).thenReturn(MY_VNF);
101 when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
103 params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
105 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
107 oper = new ModifyConfigOperation(params, config) {
109 protected CompletableFuture<OperationOutcome> startGuardAsync() {
114 outcome = oper.start().get();
115 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
116 assertTrue(outcome.getResponse() instanceof Response);
120 public void testConstructor() {
121 assertEquals(DEFAULT_ACTOR, oper.getActorName());
122 assertEquals(ModifyConfigOperation.NAME, oper.getName());
126 public void testGetPropertyNames() {
127 assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_RESOURCE_VNF));
131 public void testStartPreprocessorAsync() throws Exception {
132 CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
133 context = mock(ControlLoopEventContext.class);
134 when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
135 when(context.getEvent()).thenReturn(event);
136 params = params.toBuilder().context(context).build();
138 AtomicBoolean guardStarted = new AtomicBoolean();
140 oper = new ModifyConfigOperation(params, config) {
142 protected CompletableFuture<OperationOutcome> startGuardAsync() {
143 guardStarted.set(true);
144 return super.startGuardAsync();
148 CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
149 assertNotNull(future3);
150 assertFalse(future.isDone());
151 assertTrue(guardStarted.get());
152 verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
154 future2.complete(params.makeOutcome());
155 assertTrue(executor.runAll(100));
156 assertTrue(future3.isDone());
157 assertEquals(PolicyResult.SUCCESS, future3.get().getResult());
161 * Tests startPreprocessorAsync(), when preprocessing is disabled.
164 public void testStartPreprocessorAsyncDisabled() {
165 params = params.toBuilder().preprocessed(true).build();
166 assertNull(new ModifyConfigOperation(params, config).startPreprocessorAsync());
170 public void testMakeRequest() throws CoderException {
171 AaiCqResponse cq = new AaiCqResponse("{}");
174 params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
175 assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(1));
177 // populate the CQ data with a vnf-id
178 GenericVnf genvnf = new GenericVnf();
179 genvnf.setVnfId(MY_VNF);
180 genvnf.setModelInvariantId(RESOURCE_ID);
181 cq.setInventoryResponseItems(Arrays.asList(genvnf));
183 oper.generateSubRequestId(2);
184 Request request = oper.makeRequest(2);
185 assertNotNull(request);
186 assertEquals(MY_VNF, request.getPayload().get(ModifyConfigOperation.VNF_ID_KEY));
188 verifyRequest("modifyConfig.json", request, IGNORE_FIELDS);