2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2020 Wipro Limited.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actor.so;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.when;
32 import java.util.List;
33 import java.util.concurrent.CompletableFuture;
34 import java.util.concurrent.ForkJoinPool;
35 import java.util.concurrent.TimeUnit;
36 import org.apache.commons.lang3.tuple.Pair;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.onap.aai.domain.yang.CloudRegion;
42 import org.onap.aai.domain.yang.GenericVnf;
43 import org.onap.aai.domain.yang.ModelVer;
44 import org.onap.aai.domain.yang.ServiceInstance;
45 import org.onap.aai.domain.yang.Tenant;
46 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
49 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
50 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
51 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
52 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingParams;
53 import org.onap.policy.so.SoRequest;
54 import org.onap.policy.so.SoResponse;
56 public class VfModuleCreateTest extends BasicSoOperation {
59 private static final String MODEL_NAME2 = "my-model-name-B";
60 private static final String MODEL_VERS2 = "my-model-version-B";
61 private static final String SVC_INSTANCE_ID = "my-service-instance-id";
62 private static final String VNF_ID = "my-vnf-id";
64 private VfModuleCreate oper;
66 public VfModuleCreateTest() {
67 super(DEFAULT_ACTOR, VfModuleCreate.NAME);
71 public static void setUpBeforeClass() throws Exception {
76 public static void tearDownAfterClass() {
85 public void setUp() throws Exception {
87 oper = new VfModuleCreate(params, config);
92 * Tests "success" case with simulator.
95 public void testSuccess() throws Exception {
96 HttpPollingParams opParams = HttpPollingParams.builder().clientName(MY_CLIENT)
97 .path("serviceInstantiation/v7/serviceInstances").pollPath("orchestrationRequests/v5/")
99 config = new HttpPollingConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
101 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).preprocessed(true).build();
103 oper = new VfModuleCreate(params, config);
108 outcome = oper.start().get();
109 assertEquals(OperationResult.SUCCESS, outcome.getResult());
110 assertTrue(outcome.getResponse() instanceof SoResponse);
112 int count = oper.getProperty(OperationProperties.DATA_VF_COUNT);
113 assertEquals(VF_COUNT + 1, count);
117 public void testConstructor() {
118 assertEquals(DEFAULT_ACTOR, oper.getActorName());
119 assertEquals(VfModuleCreate.NAME, oper.getName());
120 assertTrue(oper.isUsePolling());
122 // verify that target validation is done
123 params = params.toBuilder().targetType(null).build();
124 assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleCreate(params, config))
125 .withMessageContaining("Target information");
129 public void testGetPropertyNames() {
131 assertThat(oper.getPropertyNames()).isEqualTo(
133 OperationProperties.AAI_SERVICE,
134 OperationProperties.AAI_SERVICE_MODEL,
135 OperationProperties.AAI_VNF,
136 OperationProperties.AAI_VNF_MODEL,
137 OperationProperties.AAI_DEFAULT_CLOUD_REGION,
138 OperationProperties.AAI_DEFAULT_TENANT,
139 OperationProperties.DATA_VF_COUNT));
144 public void testStartOperationAsync_testSuccessfulCompletion() throws Exception {
145 when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
147 // use a real executor
148 params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
150 oper = new VfModuleCreate(params, config) {
152 protected long getPollWaitMs() {
159 final int origCount = 30;
160 oper.setVfCount(origCount);
162 CompletableFuture<OperationOutcome> future2 = oper.start();
164 outcome = future2.get(5, TimeUnit.SECONDS);
165 assertEquals(OperationResult.SUCCESS, outcome.getResult());
167 SoResponse resp = outcome.getResponse();
169 assertEquals(REQ_ID.toString(), resp.getRequestReferences().getRequestId());
171 assertEquals(origCount + 1, oper.getVfCount());
175 * Tests startOperationAsync() when polling is required.
178 public void testStartOperationAsyncWithPolling() throws Exception {
179 when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
181 when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
182 when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
184 // use a real executor
185 params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
187 oper = new VfModuleCreate(params, config) {
189 public long getPollWaitMs() {
196 CompletableFuture<OperationOutcome> future2 = oper.start();
198 outcome = future2.get(5, TimeUnit.SECONDS);
199 assertEquals(OperationResult.SUCCESS, outcome.getResult());
203 public void testMakeRequest() throws CoderException {
204 Pair<String, SoRequest> pair = oper.makeRequest();
208 "/my-service-instance-id/vnfs/my-vnf-id/vfModules/scaleOut",
212 verifyRequest("vfModuleCreate.json", pair.getRight());
215 private void loadProperties() {
216 // set the properties
217 ServiceInstance instance = new ServiceInstance();
218 instance.setServiceInstanceId(SVC_INSTANCE_ID);
219 oper.setProperty(OperationProperties.AAI_SERVICE, instance);
221 ModelVer modelVers = new ModelVer();
222 modelVers.setModelName(MODEL_NAME2);
223 modelVers.setModelVersion(MODEL_VERS2);
225 oper.setProperty(OperationProperties.AAI_SERVICE_MODEL, modelVers);
226 oper.setProperty(OperationProperties.AAI_VNF_MODEL, modelVers);
228 GenericVnf vnf = new GenericVnf();
229 vnf.setVnfId(VNF_ID);
230 oper.setProperty(OperationProperties.AAI_VNF, vnf);
232 oper.setProperty(OperationProperties.AAI_DEFAULT_CLOUD_REGION, new CloudRegion());
233 oper.setProperty(OperationProperties.AAI_DEFAULT_TENANT, new Tenant());
235 oper.setProperty(OperationProperties.DATA_VF_COUNT, VF_COUNT);