2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.. All rights reserved.
7 * Modifications Copyright (C) 2018-2019 AT&T Corporation. All rights reserved.
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.vfc;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
27 import static org.mockito.ArgumentMatchers.anyMap;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.ArgumentMatchers.endsWith;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.ArgumentMatchers.startsWith;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.UUID;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.rest.RestManager;
42 import org.onap.policy.rest.RestManager.Pair;
43 import org.onap.policy.vfc.VfcManager.VfcCallback;
44 import org.onap.policy.vfc.util.Serialization;
46 public class VfcManagerTest implements VfcCallback {
48 private RestManager mockedRestManager;
50 private Pair<Integer, String> httpResponsePutOk;
51 private Pair<Integer, String> httpResponseGetOk;
52 private Pair<Integer, String> httpResponseBadResponse;
53 private Pair<Integer, String> httpResponseErr;
55 private VfcRequest request;
56 private VfcResponse response;
59 * Set up the mocked REST manager.
62 public void setupMockedRest() {
63 mockedRestManager = mock(RestManager.class);
65 httpResponsePutOk = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
66 httpResponseGetOk = mockedRestManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
67 httpResponseBadResponse = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(null));
68 httpResponseErr = mockedRestManager.new Pair<>(200, null);
72 * Create the request and response before.
75 public void createRequestAndResponse() {
76 VfcHealActionVmInfo actionInfo = new VfcHealActionVmInfo();
77 actionInfo.setVmid("TheWizard");
78 actionInfo.setVmname("The Wizard of Oz");
80 VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
81 additionalParams.setAction("Go Home");
82 additionalParams.setActionInfo(actionInfo);
84 VfcHealRequest healRequest = new VfcHealRequest();
85 healRequest.setAdditionalParams(additionalParams);
86 healRequest.setCause("WestWitch");
87 healRequest.setVnfInstanceId("EmeraldCity");
89 final UUID requestId = UUID.randomUUID();
90 request = new VfcRequest();
91 request.setHealRequest(healRequest);
92 request.setNsInstanceId("Dorothy");
93 request.setRequestId(requestId);
95 List<VfcResponseDescriptor> responseHistoryList = new ArrayList<>();;
97 VfcResponseDescriptor responseDescriptor = new VfcResponseDescriptor();
98 responseDescriptor.setErrorCode("1234");
99 responseDescriptor.setProgress("Follow The Yellow Brick Road");
100 responseDescriptor.setResponseHistoryList(responseHistoryList);
101 responseDescriptor.setResponseId(UUID.randomUUID().toString());
102 responseDescriptor.setStatus("finished");
103 responseDescriptor.setStatusDescription("There's no place like home");
105 response = new VfcResponse();
106 response.setJobId("1234");
107 response.setRequestId(request.getRequestId().toString());
108 response.setResponseDescriptor(responseDescriptor);
112 public void testVfcInitiation() {
114 new VfcManager(null, null, null, null, null);
115 fail("test should throw an exception here");
117 catch (IllegalArgumentException e) {
118 assertEquals("the parameters \"cb\" and \"request\" on the VfcManager constructor may not be null",
123 new VfcManager(this, null, null, null, null);
124 fail("test should throw an exception here");
126 catch (IllegalArgumentException e) {
127 assertEquals("the parameters \"cb\" and \"request\" on the VfcManager constructor may not be null",
132 new VfcManager(this, request, null, null, null);
133 fail("test should throw an exception here");
135 catch (IllegalArgumentException e) {
136 assertEquals("the \"url\" parameter on the VfcManager constructor may not be null",
140 new VfcManager(this, request, "http://somewhere.over.the.rainbow", null, null);
142 new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Toto");
146 public void testVfcExecutionException() throws InterruptedException {
147 VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Exception");
148 manager.setRestManager(mockedRestManager);
150 Thread managerThread = new Thread(manager);
151 managerThread.start();
153 when(mockedRestManager.post(
154 startsWith("http://somewhere.over.the.rainbow"),
160 .thenThrow(new RuntimeException("OzException"));
162 managerThread.join();
166 public void testVfcExecutionNull() throws InterruptedException {
167 VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Null");
168 manager.setRestManager(mockedRestManager);
170 Thread managerThread = new Thread(manager);
171 managerThread.start();
173 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
174 eq("Dorothy"), eq("Null"), anyMap(), anyString(), anyString()))
177 managerThread.join();
181 public void testVfcExecutionError0() throws InterruptedException {
182 VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Error0");
183 manager.setRestManager(mockedRestManager);
185 Thread managerThread = new Thread(manager);
186 managerThread.start();
188 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
189 eq("Dorothy"), eq("Error0"), anyMap(), anyString(), anyString()))
190 .thenReturn(httpResponseErr);
192 managerThread.join();
196 public void testVfcExecutionBadResponse() throws InterruptedException {
197 VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "BadResponse");
198 manager.setRestManager(mockedRestManager);
200 Thread managerThread = new Thread(manager);
201 managerThread.start();
203 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
204 eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
205 .thenReturn(httpResponseBadResponse);
207 managerThread.join();
211 public void testVfcExecutionOk() throws InterruptedException {
212 VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Ok");
213 manager.setRestManager(mockedRestManager);
215 Thread managerThread = new Thread(manager);
216 managerThread.start();
218 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
219 eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
220 .thenReturn(httpResponsePutOk);
222 when(mockedRestManager.get(endsWith("1234"), eq("Dorothy"), eq("OK"), anyMap()))
223 .thenReturn(httpResponseGetOk);
225 managerThread.join();
229 public void onResponse(VfcResponse responseError) {
231 // Nothing needs to be done