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.appc.Response;
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());
112 assertTrue(outcome.getResponse() instanceof Response);
116 public void testConstructor() {
117 assertEquals(DEFAULT_ACTOR, oper.getActorName());
118 assertEquals(ModifyConfigOperation.NAME, oper.getName());
122 public void testStartPreprocessorAsync() throws Exception {
123 CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
124 context = mock(ControlLoopEventContext.class);
125 when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
126 when(context.getEvent()).thenReturn(event);
127 params = params.toBuilder().context(context).build();
129 AtomicBoolean guardStarted = new AtomicBoolean();
131 oper = new ModifyConfigOperation(params, config) {
133 protected CompletableFuture<OperationOutcome> startGuardAsync() {
134 guardStarted.set(true);
135 return super.startGuardAsync();
139 CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
140 assertNotNull(future3);
141 assertFalse(future.isDone());
142 assertTrue(guardStarted.get());
143 verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
145 future2.complete(params.makeOutcome());
146 assertTrue(executor.runAll(100));
147 assertTrue(future3.isDone());
148 assertEquals(PolicyResult.SUCCESS, future3.get().getResult());
152 public void testMakeRequest() throws CoderException {
153 AaiCqResponse cq = new AaiCqResponse("{}");
156 params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
157 assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(1));
159 // populate the CQ data with a vnf-id
160 GenericVnf genvnf = new GenericVnf();
161 genvnf.setVnfId(MY_VNF);
162 genvnf.setModelInvariantId(RESOURCE_ID);
163 cq.setInventoryResponseItems(Arrays.asList(genvnf));
165 oper.generateSubRequestId(2);
166 Request request = oper.makeRequest(2);
167 assertNotNull(request);
168 assertEquals(MY_VNF, request.getPayload().get(ModifyConfigOperation.VNF_ID_KEY));
170 verifyRequest("modifyConfig.json", request, IGNORE_FIELDS);