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.Before;
38 import org.junit.Test;
39 import org.onap.aai.domain.yang.GenericVnf;
40 import org.onap.policy.aai.AaiCqResponse;
41 import org.onap.policy.appc.Request;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
44 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
45 import org.onap.policy.controlloop.policy.PolicyResult;
47 public class ModifyConfigOperationTest extends BasicAppcOperation {
49 private ModifyConfigOperation oper;
51 public ModifyConfigOperationTest() {
52 super(DEFAULT_ACTOR, ModifyConfigOperation.NAME);
56 public void setUp() throws Exception {
58 oper = new ModifyConfigOperation(params, config);
62 public void testModifyConfigOperation() {
63 assertEquals(DEFAULT_ACTOR, oper.getActorName());
64 assertEquals(ModifyConfigOperation.NAME, oper.getName());
68 public void testStartPreprocessorAsync() throws Exception {
69 CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
70 context = mock(ControlLoopEventContext.class);
71 when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
72 when(context.getEvent()).thenReturn(event);
73 params = params.toBuilder().context(context).build();
75 AtomicBoolean guardStarted = new AtomicBoolean();
77 oper = new ModifyConfigOperation(params, config) {
79 protected CompletableFuture<OperationOutcome> startGuardAsync() {
80 guardStarted.set(true);
81 return super.startGuardAsync();
85 CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
86 assertNotNull(future3);
87 assertFalse(future.isDone());
88 assertTrue(guardStarted.get());
89 verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
91 future2.complete(params.makeOutcome());
92 assertTrue(executor.runAll(100));
93 assertTrue(future3.isDone());
94 assertEquals(PolicyResult.SUCCESS, future3.get().getResult());
98 public void testMakeRequest() throws CoderException {
99 AaiCqResponse cq = new AaiCqResponse("{}");
102 params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
103 assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(1));
105 // populate the CQ data with a vnf-id
106 GenericVnf genvnf = new GenericVnf();
107 genvnf.setVnfId(MY_VNF);
108 genvnf.setModelInvariantId(RESOURCE_ID);
109 cq.setInventoryResponseItems(Arrays.asList(genvnf));
111 Request request = oper.makeRequest(2);
112 assertNotNull(request);
113 assertEquals(MY_VNF, request.getPayload().get(ModifyConfigOperation.VNF_ID_KEY));
115 verifyRequest("modifyConfig.json", request, IGNORE_FIELDS);