Upgrade and clean up dependencies
[policy/models.git] / models-interactions / model-actors / actor.so / src / test / java / org / onap / policy / controlloop / actor / so / VfModuleCreateTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2020 Wipro Limited.
7  * Modifications Copyright (C) 2023 Nordix Foundation.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.controlloop.actor.so;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.Mockito.when;
32
33 import java.util.List;
34 import java.util.concurrent.CompletableFuture;
35 import java.util.concurrent.ForkJoinPool;
36 import java.util.concurrent.TimeUnit;
37 import org.apache.commons.lang3.tuple.Pair;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.junit.MockitoJUnitRunner;
44 import org.onap.aai.domain.yang.CloudRegion;
45 import org.onap.aai.domain.yang.GenericVnf;
46 import org.onap.aai.domain.yang.ModelVer;
47 import org.onap.aai.domain.yang.ServiceInstance;
48 import org.onap.aai.domain.yang.Tenant;
49 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
50 import org.onap.policy.common.utils.coder.CoderException;
51 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
52 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
53 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
54 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
55 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingParams;
56 import org.onap.policy.so.SoRequest;
57 import org.onap.policy.so.SoResponse;
58
59 @RunWith(MockitoJUnitRunner.class)
60 public class VfModuleCreateTest extends BasicSoOperation {
61
62
63     private static final String MODEL_NAME2 = "my-model-name-B";
64     private static final String MODEL_VERS2 = "my-model-version-B";
65     private static final String SVC_INSTANCE_ID = "my-service-instance-id";
66     private static final String VNF_ID = "my-vnf-id";
67
68     private VfModuleCreate oper;
69
70     public VfModuleCreateTest() {
71         super(DEFAULT_ACTOR, VfModuleCreate.NAME);
72     }
73
74     @BeforeClass
75     public static void setUpBeforeClass() throws Exception {
76         initBeforeClass();
77     }
78
79     @AfterClass
80     public static void tearDownAfterClass() {
81         destroyAfterClass();
82     }
83
84     /**
85      * Sets up.
86      */
87     @Before
88     @Override
89     public void setUp() throws Exception {
90         super.setUp();
91         oper = new VfModuleCreate(params, config);
92         loadProperties();
93     }
94
95     /**
96      * Tests "success" case with simulator.
97      */
98     @Test
99     public void testSuccess() throws Exception {
100         HttpPollingParams opParams = HttpPollingParams.builder().clientName(MY_CLIENT)
101                         .path("serviceInstantiation/v7/serviceInstances").pollPath("orchestrationRequests/v5/")
102                         .maxPolls(2).build();
103         config = new HttpPollingConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
104
105         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
106
107         oper = new VfModuleCreate(params, config);
108
109         loadProperties();
110
111         // run the operation
112         outcome = oper.start().get();
113         assertEquals(OperationResult.SUCCESS, outcome.getResult());
114         assertTrue(outcome.getResponse() instanceof SoResponse);
115
116         int count = oper.getProperty(OperationProperties.DATA_VF_COUNT);
117         assertEquals(VF_COUNT + 1, count);
118     }
119
120     @Test
121     public void testConstructor() {
122         assertEquals(DEFAULT_ACTOR, oper.getActorName());
123         assertEquals(VfModuleCreate.NAME, oper.getName());
124         assertTrue(oper.isUsePolling());
125
126         // verify that target validation is done
127         params = params.toBuilder().targetType(null).build();
128         assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleCreate(params, config))
129                         .withMessageContaining("Target information");
130     }
131
132     @Test
133     public void testGetPropertyNames() {
134         // @formatter:off
135         assertThat(oper.getPropertyNames()).isEqualTo(
136                         List.of(
137                             OperationProperties.AAI_SERVICE,
138                             OperationProperties.AAI_SERVICE_MODEL,
139                             OperationProperties.AAI_VNF,
140                             OperationProperties.AAI_VNF_MODEL,
141                             OperationProperties.AAI_DEFAULT_CLOUD_REGION,
142                             OperationProperties.AAI_DEFAULT_TENANT,
143                             OperationProperties.DATA_VF_COUNT));
144         // @formatter:on
145     }
146
147     @Test
148     public void testStartOperationAsync_testSuccessfulCompletion() throws Exception {
149         when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
150
151         // use a real executor
152         params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
153
154         oper = new VfModuleCreate(params, config) {
155             @Override
156             protected long getPollWaitMs() {
157                 return 1;
158             }
159         };
160
161         loadProperties();
162
163         final int origCount = 30;
164         oper.setVfCount(origCount);
165
166         CompletableFuture<OperationOutcome> future2 = oper.start();
167
168         outcome = future2.get(5, TimeUnit.SECONDS);
169         assertEquals(OperationResult.SUCCESS, outcome.getResult());
170
171         SoResponse resp = outcome.getResponse();
172         assertNotNull(resp);
173         assertEquals(REQ_ID.toString(), resp.getRequestReferences().getRequestId());
174
175         assertEquals(origCount + 1, oper.getVfCount());
176     }
177
178     /**
179      * Tests startOperationAsync() when polling is required.
180      */
181     @Test
182     public void testStartOperationAsyncWithPolling() throws Exception {
183         when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
184
185         when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
186         when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
187
188         // use a real executor
189         params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
190
191         oper = new VfModuleCreate(params, config) {
192             @Override
193             public long getPollWaitMs() {
194                 return 1;
195             }
196         };
197
198         loadProperties();
199
200         CompletableFuture<OperationOutcome> future2 = oper.start();
201
202         outcome = future2.get(5, TimeUnit.SECONDS);
203         assertEquals(OperationResult.SUCCESS, outcome.getResult());
204     }
205
206     @Test
207     public void testMakeRequest() throws CoderException {
208         Pair<String, SoRequest> pair = oper.makeRequest();
209
210         // @formatter:off
211         assertEquals(
212             "/my-service-instance-id/vnfs/my-vnf-id/vfModules/scaleOut",
213             pair.getLeft());
214         // @formatter:on
215
216         verifyRequest("vfModuleCreate.json", pair.getRight());
217     }
218
219     /**
220      * Tests makeRequest() when a property is missing.
221      */
222     @Test
223     public void testMakeRequestMissingProperty() throws Exception {
224         loadProperties();
225
226         ServiceInstance instance = new ServiceInstance();
227         oper.setProperty(OperationProperties.AAI_SERVICE, instance);
228
229         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest())
230                         .withMessageContaining("missing service instance ID");
231     }
232
233     private void loadProperties() {
234         // set the properties
235         ServiceInstance instance = new ServiceInstance();
236         instance.setServiceInstanceId(SVC_INSTANCE_ID);
237         oper.setProperty(OperationProperties.AAI_SERVICE, instance);
238
239         ModelVer modelVers = new ModelVer();
240         modelVers.setModelName(MODEL_NAME2);
241         modelVers.setModelVersion(MODEL_VERS2);
242
243         oper.setProperty(OperationProperties.AAI_SERVICE_MODEL, modelVers);
244         oper.setProperty(OperationProperties.AAI_VNF_MODEL, modelVers);
245
246         GenericVnf vnf = new GenericVnf();
247         vnf.setVnfId(VNF_ID);
248         oper.setProperty(OperationProperties.AAI_VNF, vnf);
249
250         CloudRegion cloudRegion = new CloudRegion();
251         cloudRegion.setCloudRegionId("my-cloud-id");
252         oper.setProperty(OperationProperties.AAI_DEFAULT_CLOUD_REGION, cloudRegion);
253
254         Tenant tenant = new Tenant();
255         tenant.setTenantId("my-tenant-id");
256         oper.setProperty(OperationProperties.AAI_DEFAULT_TENANT, tenant);
257
258         oper.setProperty(OperationProperties.DATA_VF_COUNT, VF_COUNT);
259     }
260 }