Skip preprocessor step in 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.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.ArgumentMatchers.eq;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.concurrent.CompletableFuture;
39 import java.util.concurrent.atomic.AtomicBoolean;
40 import org.junit.After;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.onap.aai.domain.yang.GenericVnf;
46 import org.onap.policy.aai.AaiCqResponse;
47 import org.onap.policy.appc.Request;
48 import org.onap.policy.appc.Response;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
51 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
52 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
53 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
54 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
55 import org.onap.policy.controlloop.policy.PolicyResult;
56
57 public class ModifyConfigOperationTest extends BasicAppcOperation {
58
59     private ModifyConfigOperation oper;
60
61     public ModifyConfigOperationTest() {
62         super(DEFAULT_ACTOR, ModifyConfigOperation.NAME);
63     }
64
65     @BeforeClass
66     public static void setUpBeforeClass() throws Exception {
67         // use same topic name for both sides
68         initBeforeClass(MY_SINK, MY_SINK);
69     }
70
71     @AfterClass
72     public static void tearDownAfterClass() {
73         destroyAfterClass();
74     }
75
76     @Before
77     @Override
78     public void setUp() throws Exception {
79         super.setUp();
80         oper = new ModifyConfigOperation(params, config);
81     }
82
83     @After
84     @Override
85     public void tearDown() {
86         super.tearDown();
87     }
88
89     /**
90      * Tests "success" case with simulator.
91      */
92     @Test
93     public void testSuccess() throws Exception {
94         BidirectionalTopicParams opParams =
95                         BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SINK).build();
96         config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, AppcOperation.SELECTOR_KEYS);
97
98         AaiCqResponse cq = mock(AaiCqResponse.class);
99         GenericVnf genvnf = mock(GenericVnf.class);
100         when(genvnf.getVnfId()).thenReturn(MY_VNF);
101         when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
102
103         params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
104
105         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
106
107         oper = new ModifyConfigOperation(params, config) {
108             @Override
109             protected CompletableFuture<OperationOutcome> startGuardAsync() {
110                 return null;
111             }
112         };
113
114         outcome = oper.start().get();
115         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
116         assertTrue(outcome.getResponse() instanceof Response);
117     }
118
119     @Test
120     public void testConstructor() {
121         assertEquals(DEFAULT_ACTOR, oper.getActorName());
122         assertEquals(ModifyConfigOperation.NAME, oper.getName());
123     }
124
125     @Test
126     public void testGetPropertyNames() {
127         assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_RESOURCE_VNF));
128     }
129
130     @Test
131     public void testStartPreprocessorAsync() throws Exception {
132         CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
133         context = mock(ControlLoopEventContext.class);
134         when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
135         when(context.getEvent()).thenReturn(event);
136         params = params.toBuilder().context(context).build();
137
138         AtomicBoolean guardStarted = new AtomicBoolean();
139
140         oper = new ModifyConfigOperation(params, config) {
141             @Override
142             protected CompletableFuture<OperationOutcome> startGuardAsync() {
143                 guardStarted.set(true);
144                 return super.startGuardAsync();
145             }
146         };
147
148         CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
149         assertNotNull(future3);
150         assertFalse(future.isDone());
151         assertTrue(guardStarted.get());
152         verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
153
154         future2.complete(params.makeOutcome());
155         assertTrue(executor.runAll(100));
156         assertTrue(future3.isDone());
157         assertEquals(PolicyResult.SUCCESS, future3.get().getResult());
158     }
159
160     /**
161      * Tests startPreprocessorAsync(), when preprocessing is disabled.
162      */
163     @Test
164     public void testStartPreprocessorAsyncDisabled() {
165         params = params.toBuilder().preprocessed(true).build();
166         assertNull(new ModifyConfigOperation(params, config).startPreprocessorAsync());
167     }
168
169     @Test
170     public void testMakeRequest() throws CoderException {
171         AaiCqResponse cq = new AaiCqResponse("{}");
172
173         // missing vnf-id
174         params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
175         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(1));
176
177         // populate the CQ data with a vnf-id
178         GenericVnf genvnf = new GenericVnf();
179         genvnf.setVnfId(MY_VNF);
180         genvnf.setModelInvariantId(RESOURCE_ID);
181         cq.setInventoryResponseItems(Arrays.asList(genvnf));
182
183         oper.generateSubRequestId(2);
184         Request request = oper.makeRequest(2);
185         assertNotNull(request);
186         assertEquals(MY_VNF, request.getPayload().get(ModifyConfigOperation.VNF_ID_KEY));
187
188         verifyRequest("modifyConfig.json", request, IGNORE_FIELDS);
189     }
190 }