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.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.onap.aai.domain.yang.GenericVnf;
43 import org.onap.policy.aai.AaiCqResponse;
44 import org.onap.policy.appc.Request;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
47 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
48 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
49 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
50 import org.onap.policy.controlloop.policy.PolicyResult;
52 public class ModifyConfigOperationTest extends BasicAppcOperation {
54 private ModifyConfigOperation oper;
56 public ModifyConfigOperationTest() {
57 super(DEFAULT_ACTOR, ModifyConfigOperation.NAME);
61 public static void setUpBeforeClass() throws Exception {
62 // use same topic name for both sides
63 initBeforeClass(MY_SINK, MY_SINK);
67 public static void tearDownAfterClass() {
73 public void setUp() throws Exception {
75 oper = new ModifyConfigOperation(params, config);
80 public void tearDown() {
85 * Tests "success" case with simulator.
88 public void testSuccess() throws Exception {
89 BidirectionalTopicParams opParams =
90 BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SINK).build();
91 config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, AppcOperation.SELECTOR_KEYS);
93 AaiCqResponse cq = mock(AaiCqResponse.class);
94 GenericVnf genvnf = mock(GenericVnf.class);
95 when(genvnf.getVnfId()).thenReturn(MY_VNF);
96 when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
98 params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
100 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
102 oper = new ModifyConfigOperation(params, config) {
104 protected CompletableFuture<OperationOutcome> startGuardAsync() {
109 outcome = oper.start().get();
110 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
114 public void testConstructor() {
115 assertEquals(DEFAULT_ACTOR, oper.getActorName());
116 assertEquals(ModifyConfigOperation.NAME, oper.getName());
120 public void testStartPreprocessorAsync() throws Exception {
121 CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
122 context = mock(ControlLoopEventContext.class);
123 when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
124 when(context.getEvent()).thenReturn(event);
125 params = params.toBuilder().context(context).build();
127 AtomicBoolean guardStarted = new AtomicBoolean();
129 oper = new ModifyConfigOperation(params, config) {
131 protected CompletableFuture<OperationOutcome> startGuardAsync() {
132 guardStarted.set(true);
133 return super.startGuardAsync();
137 CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
138 assertNotNull(future3);
139 assertFalse(future.isDone());
140 assertTrue(guardStarted.get());
141 verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
143 future2.complete(params.makeOutcome());
144 assertTrue(executor.runAll(100));
145 assertTrue(future3.isDone());
146 assertEquals(PolicyResult.SUCCESS, future3.get().getResult());
150 public void testMakeRequest() throws CoderException {
151 AaiCqResponse cq = new AaiCqResponse("{}");
154 params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
155 assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(1));
157 // populate the CQ data with a vnf-id
158 GenericVnf genvnf = new GenericVnf();
159 genvnf.setVnfId(MY_VNF);
160 genvnf.setModelInvariantId(RESOURCE_ID);
161 cq.setInventoryResponseItems(Arrays.asList(genvnf));
163 oper.generateSubRequestId(2);
164 Request request = oper.makeRequest(2);
165 assertNotNull(request);
166 assertEquals(MY_VNF, request.getPayload().get(ModifyConfigOperation.VNF_ID_KEY));
168 verifyRequest("modifyConfig.json", request, IGNORE_FIELDS);