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.assertj.core.api.Assertions.assertThatIllegalArgumentException;
26 import static org.mockito.ArgumentMatchers.anyMap;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.ArgumentMatchers.endsWith;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.ArgumentMatchers.startsWith;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.UUID;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.rest.RestManager;
40 import org.onap.policy.rest.RestManager.Pair;
41 import org.onap.policy.vfc.VfcManager.VfcCallback;
42 import org.onap.policy.vfc.util.Serialization;
44 public class VfcManagerTest implements VfcCallback {
46 private static final String SOME_URL = "http://somewhere.over.the.rainbow";
48 private static final String DOROTHY = "Dorothy";
50 private RestManager mockedRestManager;
52 private Pair<Integer, String> httpResponsePutOk;
53 private Pair<Integer, String> httpResponseGetOk;
54 private Pair<Integer, String> httpResponseBadResponse;
55 private Pair<Integer, String> httpResponseErr;
57 private VfcRequest request;
58 private VfcResponse response;
61 * Set up the mocked REST manager.
64 public void setupMockedRest() {
65 mockedRestManager = mock(RestManager.class);
67 httpResponsePutOk = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
68 httpResponseGetOk = mockedRestManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
69 httpResponseBadResponse = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(null));
70 httpResponseErr = mockedRestManager.new Pair<>(200, null);
74 * Create the request and response before.
77 public void createRequestAndResponse() {
78 VfcHealActionVmInfo actionInfo = new VfcHealActionVmInfo();
79 actionInfo.setVmid("TheWizard");
80 actionInfo.setVmname("The Wizard of Oz");
82 VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
83 additionalParams.setAction("Go Home");
84 additionalParams.setActionInfo(actionInfo);
86 VfcHealRequest healRequest = new VfcHealRequest();
87 healRequest.setAdditionalParams(additionalParams);
88 healRequest.setCause("WestWitch");
89 healRequest.setVnfInstanceId("EmeraldCity");
91 final UUID requestId = UUID.randomUUID();
92 request = new VfcRequest();
93 request.setHealRequest(healRequest);
94 request.setNsInstanceId(DOROTHY);
95 request.setRequestId(requestId);
97 List<VfcResponseDescriptor> responseHistoryList = new ArrayList<>();;
99 VfcResponseDescriptor responseDescriptor = new VfcResponseDescriptor();
100 responseDescriptor.setErrorCode("1234");
101 responseDescriptor.setProgress("Follow The Yellow Brick Road");
102 responseDescriptor.setResponseHistoryList(responseHistoryList);
103 responseDescriptor.setResponseId(UUID.randomUUID().toString());
104 responseDescriptor.setStatus("finished");
105 responseDescriptor.setStatusDescription("There's no place like home");
107 response = new VfcResponse();
108 response.setJobId("1234");
109 response.setRequestId(request.getRequestId().toString());
110 response.setResponseDescriptor(responseDescriptor);
114 public void testVfcInitiation() {
115 assertThatIllegalArgumentException().isThrownBy(() -> new VfcManager(null, null, null, null, null)).withMessage(
116 "the parameters \"cb\" and \"request\" on the VfcManager constructor may not be null");
118 assertThatIllegalArgumentException().isThrownBy(() -> new VfcManager(this, null, null, null, null)).withMessage(
119 "the parameters \"cb\" and \"request\" on the VfcManager constructor may not be null");
121 assertThatIllegalArgumentException().isThrownBy(() -> new VfcManager(this, request, null, null, null))
122 .withMessage("the \"url\" parameter on the VfcManager constructor may not be null");
124 new VfcManager(this, request, SOME_URL, null, null);
126 new VfcManager(this, request, SOME_URL, DOROTHY, "Toto");
130 public void testVfcExecutionException() throws InterruptedException {
131 VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Exception");
132 manager.setRestManager(mockedRestManager);
134 Thread managerThread = new Thread(manager);
135 managerThread.start();
137 when(mockedRestManager.post(
138 startsWith(SOME_URL),
144 .thenThrow(new RuntimeException("OzException"));
146 managerThread.join();
150 public void testVfcExecutionNull() throws InterruptedException {
151 VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Null");
152 manager.setRestManager(mockedRestManager);
154 Thread managerThread = new Thread(manager);
155 managerThread.start();
157 when(mockedRestManager.post(startsWith(SOME_URL),
158 eq(DOROTHY), eq("Null"), anyMap(), anyString(), anyString()))
161 managerThread.join();
165 public void testVfcExecutionError0() throws InterruptedException {
166 VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Error0");
167 manager.setRestManager(mockedRestManager);
169 Thread managerThread = new Thread(manager);
170 managerThread.start();
172 when(mockedRestManager.post(startsWith(SOME_URL),
173 eq(DOROTHY), eq("Error0"), anyMap(), anyString(), anyString()))
174 .thenReturn(httpResponseErr);
176 managerThread.join();
180 public void testVfcExecutionBadResponse() throws InterruptedException {
181 VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "BadResponse");
182 manager.setRestManager(mockedRestManager);
184 Thread managerThread = new Thread(manager);
185 managerThread.start();
187 when(mockedRestManager.post(startsWith(SOME_URL),
188 eq(DOROTHY), eq("OK"), anyMap(), anyString(), anyString()))
189 .thenReturn(httpResponseBadResponse);
191 managerThread.join();
195 public void testVfcExecutionOk() throws InterruptedException {
196 VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Ok");
197 manager.setRestManager(mockedRestManager);
199 Thread managerThread = new Thread(manager);
200 managerThread.start();
202 when(mockedRestManager.post(startsWith(SOME_URL),
203 eq(DOROTHY), eq("OK"), anyMap(), anyString(), anyString()))
204 .thenReturn(httpResponsePutOk);
206 when(mockedRestManager.get(endsWith("1234"), eq(DOROTHY), eq("OK"), anyMap()))
207 .thenReturn(httpResponseGetOk);
209 managerThread.join();
213 public void onResponse(VfcResponse responseError) {
215 // Nothing needs to be done