721cb6a6dde59f4e7bd8ad2a6704eb548653d51c
[policy/models.git] /
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, 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
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.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;
33
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;
59
60 @ExtendWith(MockitoExtension.class)
61 class VfModuleCreateTest extends BasicSoOperation {
62
63
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";
68
69     private VfModuleCreate oper;
70
71     VfModuleCreateTest() {
72         super(DEFAULT_ACTOR, VfModuleCreate.NAME);
73     }
74
75     @BeforeAll
76     static void setUpBeforeClass() throws Exception {
77         initBeforeClass();
78     }
79
80     @AfterAll
81     static void tearDownAfterClass() {
82         destroyAfterClass();
83     }
84
85     /**
86      * Sets up.
87      */
88     @BeforeEach
89     @Override
90     void setUp() throws Exception {
91         super.setUp();
92         oper = new VfModuleCreate(params, config);
93         loadProperties();
94     }
95
96     /**
97      * Tests "success" case with simulator.
98      */
99     @Test
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());
105
106         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
107
108         oper = new VfModuleCreate(params, config);
109
110         loadProperties();
111
112         // run the operation
113         outcome = oper.start().get();
114         assertEquals(OperationResult.SUCCESS, outcome.getResult());
115         assertInstanceOf(SoResponse.class, outcome.getResponse());
116
117         int count = oper.getProperty(OperationProperties.DATA_VF_COUNT);
118         assertEquals(VF_COUNT + 1, count);
119     }
120
121     @Test
122     void testConstructor() {
123         assertEquals(DEFAULT_ACTOR, oper.getActorName());
124         assertEquals(VfModuleCreate.NAME, oper.getName());
125         assertTrue(oper.isUsePolling());
126
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");
131     }
132
133     @Test
134     void testGetPropertyNames() {
135         // @formatter:off
136         assertThat(oper.getPropertyNames()).isEqualTo(
137             List.of(
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));
145         // @formatter:on
146     }
147
148     @Test
149     void testStartOperationAsync_testSuccessfulCompletion() throws Exception {
150         when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
151
152         // use a real executor
153         params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
154
155         oper = new VfModuleCreate(params, config) {
156             @Override
157             protected long getPollWaitMs() {
158                 return 1;
159             }
160         };
161
162         loadProperties();
163
164         final int origCount = 30;
165         oper.setVfCount(origCount);
166
167         CompletableFuture<OperationOutcome> future2 = oper.start();
168
169         outcome = future2.get(5, TimeUnit.SECONDS);
170         assertEquals(OperationResult.SUCCESS, outcome.getResult());
171
172         SoResponse resp = outcome.getResponse();
173         assertNotNull(resp);
174         assertEquals(REQ_ID.toString(), resp.getRequestReferences().getRequestId());
175
176         assertEquals(origCount + 1, oper.getVfCount());
177     }
178
179     /**
180      * Tests startOperationAsync() when polling is required.
181      */
182     @Test
183     void testStartOperationAsyncWithPolling() throws Exception {
184         when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
185
186         when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
187         when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
188
189         // use a real executor
190         params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
191
192         oper = new VfModuleCreate(params, config) {
193             @Override
194             public long getPollWaitMs() {
195                 return 1;
196             }
197         };
198
199         loadProperties();
200
201         CompletableFuture<OperationOutcome> future2 = oper.start();
202
203         outcome = future2.get(5, TimeUnit.SECONDS);
204         assertEquals(OperationResult.SUCCESS, outcome.getResult());
205     }
206
207     @Test
208     void testMakeRequest() throws CoderException {
209         Pair<String, SoRequest> pair = oper.makeRequest();
210
211         // @formatter:off
212         assertEquals(
213             "/my-service-instance-id/vnfs/my-vnf-id/vfModules/scaleOut",
214             pair.getLeft());
215         // @formatter:on
216
217         verifyRequest("vfModuleCreate.json", pair.getRight());
218     }
219
220     /**
221      * Tests makeRequest() when a property is missing.
222      */
223     @Test
224     void testMakeRequestMissingProperty() throws Exception {
225         loadProperties();
226
227         ServiceInstance instance = new ServiceInstance();
228         oper.setProperty(OperationProperties.AAI_SERVICE, instance);
229
230         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest())
231             .withMessageContaining("missing service instance ID");
232     }
233
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);
239
240         ModelVer modelVers = new ModelVer();
241         modelVers.setModelName(MODEL_NAME2);
242         modelVers.setModelVersion(MODEL_VERS2);
243
244         oper.setProperty(OperationProperties.AAI_SERVICE_MODEL, modelVers);
245         oper.setProperty(OperationProperties.AAI_VNF_MODEL, modelVers);
246
247         GenericVnf vnf = new GenericVnf();
248         vnf.setVnfId(VNF_ID);
249         oper.setProperty(OperationProperties.AAI_VNF, vnf);
250
251         CloudRegion cloudRegion = new CloudRegion();
252         cloudRegion.setCloudRegionId("my-cloud-id");
253         oper.setProperty(OperationProperties.AAI_DEFAULT_CLOUD_REGION, cloudRegion);
254
255         Tenant tenant = new Tenant();
256         tenant.setTenantId("my-tenant-id");
257         oper.setProperty(OperationProperties.AAI_DEFAULT_TENANT, tenant);
258
259         oper.setProperty(OperationProperties.DATA_VF_COUNT, VF_COUNT);
260     }
261 }