7168ec4493ee1fafdb8b11b9e591f89f4393ad53
[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  * ================================================================================
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.so;
23
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;
31
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;
55
56 public class VfModuleCreateTest extends BasicSoOperation {
57
58
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";
63
64     private VfModuleCreate oper;
65
66     public VfModuleCreateTest() {
67         super(DEFAULT_ACTOR, VfModuleCreate.NAME);
68     }
69
70     @BeforeClass
71     public static void setUpBeforeClass() throws Exception {
72         initBeforeClass();
73     }
74
75     @AfterClass
76     public static void tearDownAfterClass() {
77         destroyAfterClass();
78     }
79
80     /**
81      * Sets up.
82      */
83     @Before
84     @Override
85     public void setUp() throws Exception {
86         super.setUp();
87         oper = new VfModuleCreate(params, config);
88         loadProperties();
89     }
90
91     /**
92      * Tests "success" case with simulator.
93      */
94     @Test
95     public void testSuccess() throws Exception {
96         HttpPollingParams opParams = HttpPollingParams.builder().clientName(MY_CLIENT)
97                         .path("serviceInstantiation/v7/serviceInstances").pollPath("orchestrationRequests/v5/")
98                         .maxPolls(2).build();
99         config = new HttpPollingConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
100
101         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
102
103         oper = new VfModuleCreate(params, config);
104
105         loadProperties();
106
107         // run the operation
108         outcome = oper.start().get();
109         assertEquals(OperationResult.SUCCESS, outcome.getResult());
110         assertTrue(outcome.getResponse() instanceof SoResponse);
111
112         int count = oper.getProperty(OperationProperties.DATA_VF_COUNT);
113         assertEquals(VF_COUNT + 1, count);
114     }
115
116     @Test
117     public void testConstructor() {
118         assertEquals(DEFAULT_ACTOR, oper.getActorName());
119         assertEquals(VfModuleCreate.NAME, oper.getName());
120         assertTrue(oper.isUsePolling());
121
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");
126     }
127
128     @Test
129     public void testGetPropertyNames() {
130         // @formatter:off
131         assertThat(oper.getPropertyNames()).isEqualTo(
132                         List.of(
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));
140         // @formatter:on
141     }
142
143     @Test
144     public void testStartOperationAsync_testSuccessfulCompletion() throws Exception {
145         when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
146
147         // use a real executor
148         params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
149
150         oper = new VfModuleCreate(params, config) {
151             @Override
152             protected long getPollWaitMs() {
153                 return 1;
154             }
155         };
156
157         loadProperties();
158
159         final int origCount = 30;
160         oper.setVfCount(origCount);
161
162         CompletableFuture<OperationOutcome> future2 = oper.start();
163
164         outcome = future2.get(5, TimeUnit.SECONDS);
165         assertEquals(OperationResult.SUCCESS, outcome.getResult());
166
167         SoResponse resp = outcome.getResponse();
168         assertNotNull(resp);
169         assertEquals(REQ_ID.toString(), resp.getRequestReferences().getRequestId());
170
171         assertEquals(origCount + 1, oper.getVfCount());
172     }
173
174     /**
175      * Tests startOperationAsync() when polling is required.
176      */
177     @Test
178     public void testStartOperationAsyncWithPolling() throws Exception {
179         when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
180
181         when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
182         when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
183
184         // use a real executor
185         params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
186
187         oper = new VfModuleCreate(params, config) {
188             @Override
189             public long getPollWaitMs() {
190                 return 1;
191             }
192         };
193
194         loadProperties();
195
196         CompletableFuture<OperationOutcome> future2 = oper.start();
197
198         outcome = future2.get(5, TimeUnit.SECONDS);
199         assertEquals(OperationResult.SUCCESS, outcome.getResult());
200     }
201
202     @Test
203     public void testMakeRequest() throws CoderException {
204         Pair<String, SoRequest> pair = oper.makeRequest();
205
206         // @formatter:off
207         assertEquals(
208             "/my-service-instance-id/vnfs/my-vnf-id/vfModules/scaleOut",
209             pair.getLeft());
210         // @formatter:on
211
212         verifyRequest("vfModuleCreate.json", pair.getRight());
213     }
214
215     /**
216      * Tests makeRequest() when a property is missing.
217      */
218     @Test
219     public void testMakeRequestMissingProperty() throws Exception {
220         loadProperties();
221
222         ServiceInstance instance = new ServiceInstance();
223         oper.setProperty(OperationProperties.AAI_SERVICE, instance);
224
225         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest())
226                         .withMessageContaining("missing service instance ID");
227     }
228
229     private void loadProperties() {
230         // set the properties
231         ServiceInstance instance = new ServiceInstance();
232         instance.setServiceInstanceId(SVC_INSTANCE_ID);
233         oper.setProperty(OperationProperties.AAI_SERVICE, instance);
234
235         ModelVer modelVers = new ModelVer();
236         modelVers.setModelName(MODEL_NAME2);
237         modelVers.setModelVersion(MODEL_VERS2);
238
239         oper.setProperty(OperationProperties.AAI_SERVICE_MODEL, modelVers);
240         oper.setProperty(OperationProperties.AAI_VNF_MODEL, modelVers);
241
242         GenericVnf vnf = new GenericVnf();
243         vnf.setVnfId(VNF_ID);
244         oper.setProperty(OperationProperties.AAI_VNF, vnf);
245
246         CloudRegion cloudRegion = new CloudRegion();
247         cloudRegion.setCloudRegionId("my-cloud-id");
248         oper.setProperty(OperationProperties.AAI_DEFAULT_CLOUD_REGION, cloudRegion);
249
250         Tenant tenant = new Tenant();
251         tenant.setTenantId("my-tenant-id");
252         oper.setProperty(OperationProperties.AAI_DEFAULT_TENANT, tenant);
253
254         oper.setProperty(OperationProperties.DATA_VF_COUNT, VF_COUNT);
255     }
256 }