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