2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.actor.so;
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
34 import java.util.concurrent.CompletableFuture;
35 import java.util.concurrent.ForkJoinPool;
36 import java.util.concurrent.TimeUnit;
37 import java.util.concurrent.atomic.AtomicBoolean;
38 import org.apache.commons.lang3.tuple.Pair;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.ArgumentCaptor;
42 import org.onap.aai.domain.yang.CloudRegion;
43 import org.onap.aai.domain.yang.GenericVnf;
44 import org.onap.aai.domain.yang.ModelVer;
45 import org.onap.aai.domain.yang.ServiceInstance;
46 import org.onap.aai.domain.yang.Tenant;
47 import org.onap.policy.aai.AaiCqResponse;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
50 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
51 import org.onap.policy.controlloop.policy.PolicyResult;
52 import org.onap.policy.so.SoRequest;
54 public class VfModuleCreateTest extends BasicSoOperation {
55 private static final String MODEL_NAME2 = "my-model-name-B";
56 private static final String MODEL_VERS2 = "my-model-version-B";
57 private static final String SVC_INSTANCE_ID = "my-service-instance-id";
58 private static final String VNF_ID = "my-vnf-id";
60 private VfModuleCreate oper;
62 public VfModuleCreateTest() {
63 super(DEFAULT_ACTOR, VfModuleCreate.NAME);
68 public void setUp() throws Exception {
70 oper = new VfModuleCreate(params, config);
74 public void testConstructor() {
75 assertEquals(DEFAULT_ACTOR, oper.getActorName());
76 assertEquals(VfModuleCreate.NAME, oper.getName());
78 // verify that target validation is done
79 params = params.toBuilder().target(null).build();
80 assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleCreate(params, config))
81 .withMessageContaining("Target information");
85 public void testStartPreprocessorAsync() throws Exception {
86 // insert CQ data so it's there for the check
87 context.setProperty(AaiCqResponse.CONTEXT_KEY, makeCqResponse());
89 AtomicBoolean guardStarted = new AtomicBoolean();
91 oper = new VfModuleCreate(params, config) {
93 protected CompletableFuture<OperationOutcome> startGuardAsync() {
94 guardStarted.set(true);
95 return super.startGuardAsync();
99 CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
100 assertNotNull(future3);
101 assertTrue(guardStarted.get());
105 public void testStartGuardAsync() throws Exception {
106 // remove CQ data so it's forced to query
107 context.removeProperty(AaiCqResponse.CONTEXT_KEY);
109 CompletableFuture<OperationOutcome> future2 = oper.startPreprocessorAsync();
110 assertTrue(executor.runAll(100));
111 assertFalse(future2.isDone());
113 provideCqResponse(makeCqResponse());
114 assertTrue(executor.runAll(100));
115 assertTrue(future2.isDone());
116 assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
120 public void testMakeGuardPayload() {
121 final int origCount = 30;
122 oper.setVfCount(origCount);
124 CompletableFuture<OperationOutcome> future2 = oper.startPreprocessorAsync();
125 assertTrue(executor.runAll(100));
126 assertTrue(future2.isDone());
128 // get the payload from the request
129 ArgumentCaptor<ControlLoopOperationParams> captor = ArgumentCaptor.forClass(ControlLoopOperationParams.class);
130 verify(guardOperator).buildOperation(captor.capture());
132 Map<String, Object> payload = captor.getValue().getPayload();
133 assertNotNull(payload);
135 Integer newCount = (Integer) payload.get(VfModuleCreate.PAYLOAD_KEY_VF_COUNT);
136 assertNotNull(newCount);
137 assertEquals(origCount + 1, newCount.intValue());
141 public void testStartOperationAsync_testSuccessfulCompletion() throws Exception {
142 final int origCount = 30;
143 oper.setVfCount(origCount);
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 public long getWaitMsGet() {
157 CompletableFuture<OperationOutcome> future2 = oper.start();
159 outcome = future2.get(5, TimeUnit.SECONDS);
160 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
162 assertEquals(origCount + 1, oper.getVfCount());
166 * Tests startOperationAsync() when "get" operations are required.
169 public void testStartOperationAsyncWithGets() throws Exception {
170 when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
172 when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
173 when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
175 // use a real executor
176 params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
178 oper = new VfModuleCreate(params, config) {
180 public long getWaitMsGet() {
185 CompletableFuture<OperationOutcome> future2 = oper.start();
187 outcome = future2.get(5, TimeUnit.SECONDS);
188 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
192 public void testMakeRequest() throws CoderException {
193 Pair<String, SoRequest> pair = oper.makeRequest();
197 "/my-service-instance-id/vnfs/my-vnf-id/vfModules/scaleOut",
201 verifyRequest("vfModuleCreate.json", pair.getRight());
206 protected void makeContext() {
209 AaiCqResponse cq = mock(AaiCqResponse.class);
211 GenericVnf vnf = new GenericVnf();
212 when(cq.getGenericVnfByVfModuleModelInvariantId(MODEL_INVAR_ID)).thenReturn(vnf);
213 vnf.setVnfId(VNF_ID);
215 ServiceInstance instance = new ServiceInstance();
216 when(cq.getServiceInstance()).thenReturn(instance);
217 instance.setServiceInstanceId(SVC_INSTANCE_ID);
219 when(cq.getDefaultTenant()).thenReturn(new Tenant());
220 when(cq.getDefaultCloudRegion()).thenReturn(new CloudRegion());
222 ModelVer modelVers = new ModelVer();
223 when(cq.getModelVerByVersionId(any())).thenReturn(modelVers);
224 modelVers.setModelName(MODEL_NAME2);
225 modelVers.setModelVersion(MODEL_VERS2);
227 params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);