2 * ============LICENSE_START=======================================================
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, 2024 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.controlloop.actor.so;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
29 import static org.junit.jupiter.api.Assertions.assertNotNull;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import static org.mockito.ArgumentMatchers.any;
32 import static org.mockito.Mockito.when;
34 import java.util.List;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.concurrent.ForkJoinPool;
37 import java.util.concurrent.TimeUnit;
38 import org.apache.commons.lang3.tuple.Pair;
39 import org.junit.jupiter.api.AfterAll;
40 import org.junit.jupiter.api.BeforeAll;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.mockito.junit.jupiter.MockitoExtension;
45 import org.onap.aai.domain.yang.CloudRegion;
46 import org.onap.aai.domain.yang.GenericVnf;
47 import org.onap.aai.domain.yang.ModelVer;
48 import org.onap.aai.domain.yang.ServiceInstance;
49 import org.onap.aai.domain.yang.Tenant;
50 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
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.parameters.HttpPollingConfig;
56 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingParams;
57 import org.onap.policy.so.SoRequest;
58 import org.onap.policy.so.SoResponse;
60 @ExtendWith(MockitoExtension.class)
61 class VfModuleCreateTest extends BasicSoOperation {
64 private static final String MODEL_NAME2 = "my-model-name-B";
65 private static final String MODEL_VERS2 = "my-model-version-B";
66 private static final String SVC_INSTANCE_ID = "my-service-instance-id";
67 private static final String VNF_ID = "my-vnf-id";
69 private VfModuleCreate oper;
71 VfModuleCreateTest() {
72 super(DEFAULT_ACTOR, VfModuleCreate.NAME);
76 static void setUpBeforeClass() throws Exception {
81 static void tearDownAfterClass() {
90 void setUp() throws Exception {
92 oper = new VfModuleCreate(params, config);
97 * Tests "success" case with simulator.
100 void testSuccess() throws Exception {
101 HttpPollingParams opParams = HttpPollingParams.builder().clientName(MY_CLIENT)
102 .path("serviceInstantiation/v7/serviceInstances").pollPath("orchestrationRequests/v5/")
103 .maxPolls(2).build();
104 config = new HttpPollingConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
106 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
108 oper = new VfModuleCreate(params, config);
113 outcome = oper.start().get();
114 assertEquals(OperationResult.SUCCESS, outcome.getResult());
115 assertInstanceOf(SoResponse.class, outcome.getResponse());
117 int count = oper.getProperty(OperationProperties.DATA_VF_COUNT);
118 assertEquals(VF_COUNT + 1, count);
122 void testConstructor() {
123 assertEquals(DEFAULT_ACTOR, oper.getActorName());
124 assertEquals(VfModuleCreate.NAME, oper.getName());
125 assertTrue(oper.isUsePolling());
127 // verify that target validation is done
128 params = params.toBuilder().targetType(null).build();
129 assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleCreate(params, config))
130 .withMessageContaining("Target information");
134 void testGetPropertyNames() {
136 assertThat(oper.getPropertyNames()).isEqualTo(
138 OperationProperties.AAI_SERVICE,
139 OperationProperties.AAI_SERVICE_MODEL,
140 OperationProperties.AAI_VNF,
141 OperationProperties.AAI_VNF_MODEL,
142 OperationProperties.AAI_DEFAULT_CLOUD_REGION,
143 OperationProperties.AAI_DEFAULT_TENANT,
144 OperationProperties.DATA_VF_COUNT));
149 void testStartOperationAsync_testSuccessfulCompletion() throws Exception {
150 when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
152 // use a real executor
153 params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
155 oper = new VfModuleCreate(params, config) {
157 protected long getPollWaitMs() {
164 final int origCount = 30;
165 oper.setVfCount(origCount);
167 CompletableFuture<OperationOutcome> future2 = oper.start();
169 outcome = future2.get(5, TimeUnit.SECONDS);
170 assertEquals(OperationResult.SUCCESS, outcome.getResult());
172 SoResponse resp = outcome.getResponse();
174 assertEquals(REQ_ID.toString(), resp.getRequestReferences().getRequestId());
176 assertEquals(origCount + 1, oper.getVfCount());
180 * Tests startOperationAsync() when polling is required.
183 void testStartOperationAsyncWithPolling() throws Exception {
184 when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
186 when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
187 when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
189 // use a real executor
190 params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
192 oper = new VfModuleCreate(params, config) {
194 public long getPollWaitMs() {
201 CompletableFuture<OperationOutcome> future2 = oper.start();
203 outcome = future2.get(5, TimeUnit.SECONDS);
204 assertEquals(OperationResult.SUCCESS, outcome.getResult());
208 void testMakeRequest() throws CoderException {
209 Pair<String, SoRequest> pair = oper.makeRequest();
213 "/my-service-instance-id/vnfs/my-vnf-id/vfModules/scaleOut",
217 verifyRequest("vfModuleCreate.json", pair.getRight());
221 * Tests makeRequest() when a property is missing.
224 void testMakeRequestMissingProperty() throws Exception {
227 ServiceInstance instance = new ServiceInstance();
228 oper.setProperty(OperationProperties.AAI_SERVICE, instance);
230 assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest())
231 .withMessageContaining("missing service instance ID");
234 private void loadProperties() {
235 // set the properties
236 ServiceInstance instance = new ServiceInstance();
237 instance.setServiceInstanceId(SVC_INSTANCE_ID);
238 oper.setProperty(OperationProperties.AAI_SERVICE, instance);
240 ModelVer modelVers = new ModelVer();
241 modelVers.setModelName(MODEL_NAME2);
242 modelVers.setModelVersion(MODEL_VERS2);
244 oper.setProperty(OperationProperties.AAI_SERVICE_MODEL, modelVers);
245 oper.setProperty(OperationProperties.AAI_VNF_MODEL, modelVers);
247 GenericVnf vnf = new GenericVnf();
248 vnf.setVnfId(VNF_ID);
249 oper.setProperty(OperationProperties.AAI_VNF, vnf);
251 CloudRegion cloudRegion = new CloudRegion();
252 cloudRegion.setCloudRegionId("my-cloud-id");
253 oper.setProperty(OperationProperties.AAI_DEFAULT_CLOUD_REGION, cloudRegion);
255 Tenant tenant = new Tenant();
256 tenant.setTenantId("my-tenant-id");
257 oper.setProperty(OperationProperties.AAI_DEFAULT_TENANT, tenant);
259 oper.setProperty(OperationProperties.DATA_VF_COUNT, VF_COUNT);