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.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
34 import java.util.Arrays;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.concurrent.atomic.AtomicBoolean;
37 import org.apache.commons.lang3.tuple.Pair;
38 import org.junit.After;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.onap.aai.domain.yang.GenericVnf;
44 import org.onap.policy.aai.AaiCqResponse;
45 import org.onap.policy.appc.Request;
46 import org.onap.policy.common.utils.coder.CoderException;
47 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
48 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
49 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
50 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
51 import org.onap.policy.controlloop.policy.PolicyResult;
53 public class ModifyConfigOperationTest extends BasicAppcOperation {
55 private ModifyConfigOperation oper;
57 public ModifyConfigOperationTest() {
58 super(DEFAULT_ACTOR, ModifyConfigOperation.NAME);
62 public static void setUpBeforeClass() throws Exception {
63 // use same topic name for both sides
64 initBeforeClass(MY_SINK, MY_SINK);
68 public static void tearDownAfterClass() {
74 public void setUp() throws Exception {
76 oper = new ModifyConfigOperation(params, config);
81 public void tearDown() {
86 * Tests "success" case with simulator.
89 public void testSuccess() throws Exception {
90 BidirectionalTopicParams opParams =
91 BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SINK).build();
92 config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, AppcOperation.SELECTOR_KEYS);
94 AaiCqResponse cq = mock(AaiCqResponse.class);
95 GenericVnf genvnf = mock(GenericVnf.class);
96 when(genvnf.getVnfId()).thenReturn(MY_VNF);
97 when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
99 params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
101 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
103 oper = new ModifyConfigOperation(params, config) {
105 protected CompletableFuture<OperationOutcome> startGuardAsync() {
110 outcome = oper.start().get();
111 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
115 public void testConstructor() {
116 assertEquals(DEFAULT_ACTOR, oper.getActorName());
117 assertEquals(ModifyConfigOperation.NAME, oper.getName());
121 public void testStartPreprocessorAsync() throws Exception {
122 CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
123 context = mock(ControlLoopEventContext.class);
124 when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
125 when(context.getEvent()).thenReturn(event);
126 params = params.toBuilder().context(context).build();
128 AtomicBoolean guardStarted = new AtomicBoolean();
130 oper = new ModifyConfigOperation(params, config) {
132 protected CompletableFuture<OperationOutcome> startGuardAsync() {
133 guardStarted.set(true);
134 return super.startGuardAsync();
138 CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
139 assertNotNull(future3);
140 assertFalse(future.isDone());
141 assertTrue(guardStarted.get());
142 verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
144 future2.complete(params.makeOutcome());
145 assertTrue(executor.runAll(100));
146 assertTrue(future3.isDone());
147 assertEquals(PolicyResult.SUCCESS, future3.get().getResult());
151 public void testMakeRequest() throws CoderException {
152 AaiCqResponse cq = new AaiCqResponse("{}");
155 params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
156 assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(1));
158 // populate the CQ data with a vnf-id
159 GenericVnf genvnf = new GenericVnf();
160 genvnf.setVnfId(MY_VNF);
161 genvnf.setModelInvariantId(RESOURCE_ID);
162 cq.setInventoryResponseItems(Arrays.asList(genvnf));
164 Pair<String, Request> result = oper.makeRequest(2);
165 assertNotNull(result.getLeft());
167 Request request = result.getRight();
168 assertNotNull(request);
169 assertEquals(MY_VNF, request.getPayload().get(ModifyConfigOperation.VNF_ID_KEY));
171 verifyRequest("modifyConfig.json", request, IGNORE_FIELDS);