Add property lists to Actors
[policy/models.git] / models-interactions / model-actors / actor.appc / src / test / java / org / onap / policy / controlloop / actor / appc / ModifyConfigOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.appc;
22
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;
34
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;
55
56 public class ModifyConfigOperationTest extends BasicAppcOperation {
57
58     private ModifyConfigOperation oper;
59
60     public ModifyConfigOperationTest() {
61         super(DEFAULT_ACTOR, ModifyConfigOperation.NAME);
62     }
63
64     @BeforeClass
65     public static void setUpBeforeClass() throws Exception {
66         // use same topic name for both sides
67         initBeforeClass(MY_SINK, MY_SINK);
68     }
69
70     @AfterClass
71     public static void tearDownAfterClass() {
72         destroyAfterClass();
73     }
74
75     @Before
76     @Override
77     public void setUp() throws Exception {
78         super.setUp();
79         oper = new ModifyConfigOperation(params, config);
80     }
81
82     @After
83     @Override
84     public void tearDown() {
85         super.tearDown();
86     }
87
88     /**
89      * Tests "success" case with simulator.
90      */
91     @Test
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);
96
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);
101
102         params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
103
104         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
105
106         oper = new ModifyConfigOperation(params, config) {
107             @Override
108             protected CompletableFuture<OperationOutcome> startGuardAsync() {
109                 return null;
110             }
111         };
112
113         outcome = oper.start().get();
114         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
115         assertTrue(outcome.getResponse() instanceof Response);
116     }
117
118     @Test
119     public void testConstructor() {
120         assertEquals(DEFAULT_ACTOR, oper.getActorName());
121         assertEquals(ModifyConfigOperation.NAME, oper.getName());
122     }
123
124     @Test
125     public void testGetPropertyNames() {
126         assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_RESOURCE_VNF));
127     }
128
129     @Test
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();
136
137         AtomicBoolean guardStarted = new AtomicBoolean();
138
139         oper = new ModifyConfigOperation(params, config) {
140             @Override
141             protected CompletableFuture<OperationOutcome> startGuardAsync() {
142                 guardStarted.set(true);
143                 return super.startGuardAsync();
144             }
145         };
146
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());
152
153         future2.complete(params.makeOutcome());
154         assertTrue(executor.runAll(100));
155         assertTrue(future3.isDone());
156         assertEquals(PolicyResult.SUCCESS, future3.get().getResult());
157     }
158
159     @Test
160     public void testMakeRequest() throws CoderException {
161         AaiCqResponse cq = new AaiCqResponse("{}");
162
163         // missing vnf-id
164         params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
165         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(1));
166
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));
172
173         oper.generateSubRequestId(2);
174         Request request = oper.makeRequest(2);
175         assertNotNull(request);
176         assertEquals(MY_VNF, request.getPayload().get(ModifyConfigOperation.VNF_ID_KEY));
177
178         verifyRequest("modifyConfig.json", request, IGNORE_FIELDS);
179     }
180 }