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 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.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;
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;
59 @RunWith(MockitoJUnitRunner.class)
60 public class VfModuleCreateTest extends BasicSoOperation {
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";
68 private VfModuleCreate oper;
70 public VfModuleCreateTest() {
71 super(DEFAULT_ACTOR, VfModuleCreate.NAME);
75 public static void setUpBeforeClass() throws Exception {
80 public static void tearDownAfterClass() {
89 public void setUp() throws Exception {
91 oper = new VfModuleCreate(params, config);
96 * Tests "success" case with simulator.
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());
105 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
107 oper = new VfModuleCreate(params, config);
112 outcome = oper.start().get();
113 assertEquals(OperationResult.SUCCESS, outcome.getResult());
114 assertTrue(outcome.getResponse() instanceof SoResponse);
116 int count = oper.getProperty(OperationProperties.DATA_VF_COUNT);
117 assertEquals(VF_COUNT + 1, count);
121 public void testConstructor() {
122 assertEquals(DEFAULT_ACTOR, oper.getActorName());
123 assertEquals(VfModuleCreate.NAME, oper.getName());
124 assertTrue(oper.isUsePolling());
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");
133 public void testGetPropertyNames() {
135 assertThat(oper.getPropertyNames()).isEqualTo(
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));
148 public void testStartOperationAsync_testSuccessfulCompletion() throws Exception {
149 when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
151 // use a real executor
152 params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
154 oper = new VfModuleCreate(params, config) {
156 protected long getPollWaitMs() {
163 final int origCount = 30;
164 oper.setVfCount(origCount);
166 CompletableFuture<OperationOutcome> future2 = oper.start();
168 outcome = future2.get(5, TimeUnit.SECONDS);
169 assertEquals(OperationResult.SUCCESS, outcome.getResult());
171 SoResponse resp = outcome.getResponse();
173 assertEquals(REQ_ID.toString(), resp.getRequestReferences().getRequestId());
175 assertEquals(origCount + 1, oper.getVfCount());
179 * Tests startOperationAsync() when polling is required.
182 public void testStartOperationAsyncWithPolling() throws Exception {
183 when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
185 when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
186 when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
188 // use a real executor
189 params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
191 oper = new VfModuleCreate(params, config) {
193 public long getPollWaitMs() {
200 CompletableFuture<OperationOutcome> future2 = oper.start();
202 outcome = future2.get(5, TimeUnit.SECONDS);
203 assertEquals(OperationResult.SUCCESS, outcome.getResult());
207 public void testMakeRequest() throws CoderException {
208 Pair<String, SoRequest> pair = oper.makeRequest();
212 "/my-service-instance-id/vnfs/my-vnf-id/vfModules/scaleOut",
216 verifyRequest("vfModuleCreate.json", pair.getRight());
220 * Tests makeRequest() when a property is missing.
223 public void testMakeRequestMissingProperty() throws Exception {
226 ServiceInstance instance = new ServiceInstance();
227 oper.setProperty(OperationProperties.AAI_SERVICE, instance);
229 assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest())
230 .withMessageContaining("missing service instance ID");
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);
239 ModelVer modelVers = new ModelVer();
240 modelVers.setModelName(MODEL_NAME2);
241 modelVers.setModelVersion(MODEL_VERS2);
243 oper.setProperty(OperationProperties.AAI_SERVICE_MODEL, modelVers);
244 oper.setProperty(OperationProperties.AAI_VNF_MODEL, modelVers);
246 GenericVnf vnf = new GenericVnf();
247 vnf.setVnfId(VNF_ID);
248 oper.setProperty(OperationProperties.AAI_VNF, vnf);
250 CloudRegion cloudRegion = new CloudRegion();
251 cloudRegion.setCloudRegionId("my-cloud-id");
252 oper.setProperty(OperationProperties.AAI_DEFAULT_CLOUD_REGION, cloudRegion);
254 Tenant tenant = new Tenant();
255 tenant.setTenantId("my-tenant-id");
256 oper.setProperty(OperationProperties.AAI_DEFAULT_TENANT, tenant);
258 oper.setProperty(OperationProperties.DATA_VF_COUNT, VF_COUNT);