Merge "More actor clean-up"
[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 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.so;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
31
32 import java.util.concurrent.CompletableFuture;
33 import java.util.concurrent.ForkJoinPool;
34 import java.util.concurrent.TimeUnit;
35 import java.util.concurrent.atomic.AtomicBoolean;
36 import org.apache.commons.lang3.tuple.Pair;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.aai.domain.yang.CloudRegion;
40 import org.onap.aai.domain.yang.GenericVnf;
41 import org.onap.aai.domain.yang.ModelVer;
42 import org.onap.aai.domain.yang.ServiceInstance;
43 import org.onap.aai.domain.yang.Tenant;
44 import org.onap.policy.aai.AaiCqResponse;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
47 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
48 import org.onap.policy.controlloop.policy.PolicyResult;
49 import org.onap.policy.so.SoRequest;
50
51 public class VfModuleCreateTest extends BasicSoOperation {
52     private static final String MODEL_NAME2 = "my-model-name-B";
53     private static final String MODEL_VERS2 = "my-model-version-B";
54     private static final String SVC_INSTANCE_ID = "my-service-instance-id";
55     private static final String VNF_ID = "my-vnf-id";
56
57     private VfModuleCreate oper;
58
59     public VfModuleCreateTest() {
60         super(DEFAULT_ACTOR, VfModuleCreate.NAME);
61     }
62
63
64     @Before
65     public void setUp() throws Exception {
66         super.setUp();
67         oper = new VfModuleCreate(params, config);
68     }
69
70     @Test
71     public void testConstructor() {
72         assertEquals(DEFAULT_ACTOR, oper.getActorName());
73         assertEquals(VfModuleCreate.NAME, oper.getName());
74     }
75
76     @Test
77     public void testStartPreprocessorAsync() {
78         CompletableFuture<OperationOutcome> future = new CompletableFuture<>();
79         context = mock(ControlLoopEventContext.class);
80         when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future);
81         params = params.toBuilder().context(context).build();
82
83         AtomicBoolean guardStarted = new AtomicBoolean();
84
85         oper = new VfModuleCreate(params, config) {
86             @Override
87             protected CompletableFuture<OperationOutcome> startGuardAsync() {
88                 guardStarted.set(true);
89                 return super.startGuardAsync();
90             }
91         };
92
93         assertSame(future, oper.startPreprocessorAsync());
94         assertFalse(future.isDone());
95         assertTrue(guardStarted.get());
96     }
97
98     @Test
99     public void testStartOperationAsync() throws Exception {
100         when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
101
102         // use a real executor
103         params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
104
105         oper = new VfModuleCreate(params, config) {
106             @Override
107             public long getWaitMsGet() {
108                 return 1;
109             }
110         };
111
112         CompletableFuture<OperationOutcome> future2 = oper.start();
113
114         outcome = future2.get(500, TimeUnit.SECONDS);
115         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
116     }
117
118     /**
119      * Tests startOperationAsync() when "get" operations are required.
120      */
121     @Test
122     public void testStartOperationAsyncWithGets() throws Exception {
123         when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
124
125         when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
126         when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
127
128         // use a real executor
129         params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
130
131         oper = new VfModuleCreate(params, config) {
132             @Override
133             public long getWaitMsGet() {
134                 return 1;
135             }
136         };
137
138         CompletableFuture<OperationOutcome> future2 = oper.start();
139
140         outcome = future2.get(500, TimeUnit.SECONDS);
141         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
142     }
143
144     @Test
145     public void testMakeRequest() throws CoderException {
146         Pair<String, SoRequest> pair = oper.makeRequest();
147
148         // @formatter:off
149         assertEquals(
150             "/serviceInstantiation/v7/serviceInstances/my-service-instance-id/vnfs/my-vnf-id/vfModules/scaleOut",
151             pair.getLeft());
152         // @formatter:on
153
154         verifyRequest("vfModuleCreate.json", pair.getRight());
155     }
156
157
158     @Override
159     protected void makeContext() {
160         super.makeContext();
161
162         AaiCqResponse cq = mock(AaiCqResponse.class);
163
164         GenericVnf vnf = new GenericVnf();
165         when(cq.getGenericVnfByVfModuleModelInvariantId(MODEL_INVAR_ID)).thenReturn(vnf);
166         vnf.setVnfId(VNF_ID);
167
168         ServiceInstance instance = new ServiceInstance();
169         when(cq.getServiceInstance()).thenReturn(instance);
170         instance.setServiceInstanceId(SVC_INSTANCE_ID);
171
172         when(cq.getDefaultTenant()).thenReturn(new Tenant());
173         when(cq.getDefaultCloudRegion()).thenReturn(new CloudRegion());
174
175         ModelVer modelVers = new ModelVer();
176         when(cq.getModelVerByVersionId(any())).thenReturn(modelVers);
177         modelVers.setModelName(MODEL_NAME2);
178         modelVers.setModelVersion(MODEL_VERS2);
179
180         params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
181     }
182 }