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.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.concurrent.CompletableFuture;
38 import java.util.concurrent.atomic.AtomicBoolean;
39 import org.junit.After;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.onap.aai.domain.yang.GenericVnf;
45 import org.onap.policy.aai.AaiCqResponse;
46 import org.onap.policy.appc.Request;
47 import org.onap.policy.appc.Response;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
50 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
51 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
52 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
53 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
54 import org.onap.policy.controlloop.policy.PolicyResult;
56 public class ModifyConfigOperationTest extends BasicAppcOperation {
58 private ModifyConfigOperation oper;
60 public ModifyConfigOperationTest() {
61 super(DEFAULT_ACTOR, ModifyConfigOperation.NAME);
65 public static void setUpBeforeClass() throws Exception {
66 // use same topic name for both sides
67 initBeforeClass(MY_SINK, MY_SINK);
71 public static void tearDownAfterClass() {
77 public void setUp() throws Exception {
79 oper = new ModifyConfigOperation(params, config);
84 public void tearDown() {
89 * Tests "success" case with simulator.
92 public void testSuccess() throws Exception {
93 BidirectionalTopicParams opParams =
94 BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SINK).build();
95 config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, AppcOperation.SELECTOR_KEYS);
97 AaiCqResponse cq = mock(AaiCqResponse.class);
98 GenericVnf genvnf = mock(GenericVnf.class);
99 when(genvnf.getVnfId()).thenReturn(MY_VNF);
100 when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
102 params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
104 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
106 oper = new ModifyConfigOperation(params, config) {
108 protected CompletableFuture<OperationOutcome> startGuardAsync() {
113 outcome = oper.start().get();
114 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
115 assertTrue(outcome.getResponse() instanceof Response);
119 public void testConstructor() {
120 assertEquals(DEFAULT_ACTOR, oper.getActorName());
121 assertEquals(ModifyConfigOperation.NAME, oper.getName());
125 public void testGetPropertyNames() {
126 assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_RESOURCE_VNF));
130 public void testStartPreprocessorAsync() throws Exception {
131 CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
132 context = mock(ControlLoopEventContext.class);
133 when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
134 when(context.getEvent()).thenReturn(event);
135 params = params.toBuilder().context(context).build();
137 AtomicBoolean guardStarted = new AtomicBoolean();
139 oper = new ModifyConfigOperation(params, config) {
141 protected CompletableFuture<OperationOutcome> startGuardAsync() {
142 guardStarted.set(true);
143 return super.startGuardAsync();
147 CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
148 assertNotNull(future3);
149 assertFalse(future.isDone());
150 assertTrue(guardStarted.get());
151 verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
153 future2.complete(params.makeOutcome());
154 assertTrue(executor.runAll(100));
155 assertTrue(future3.isDone());
156 assertEquals(PolicyResult.SUCCESS, future3.get().getResult());
160 public void testMakeRequest() throws CoderException {
161 AaiCqResponse cq = new AaiCqResponse("{}");
164 params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
165 assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(1));
167 // populate the CQ data with a vnf-id
168 GenericVnf genvnf = new GenericVnf();
169 genvnf.setVnfId(MY_VNF);
170 genvnf.setModelInvariantId(RESOURCE_ID);
171 cq.setInventoryResponseItems(Arrays.asList(genvnf));
173 oper.generateSubRequestId(2);
174 Request request = oper.makeRequest(2);
175 assertNotNull(request);
176 assertEquals(MY_VNF, request.getPayload().get(ModifyConfigOperation.VNF_ID_KEY));
178 verifyRequest("modifyConfig.json", request, IGNORE_FIELDS);