2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 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.any;
27 import static org.mockito.ArgumentMatchers.anyMap;
28 import static org.mockito.ArgumentMatchers.anyString;
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.verify;
33 import static org.mockito.Mockito.when;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.UUID;
38 import org.apache.commons.lang3.tuple.Pair;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.rest.RestManager;
42 import org.onap.policy.vfc.VfcManager.VfcCallback;
43 import org.onap.policy.vfc.util.Serialization;
45 public class VfcManagerTest implements VfcCallback {
47 private static final String SOME_URL = "http://somewhere.over.the.rainbow";
49 private static final String DOROTHY = "Dorothy";
51 private RestManager mockedRestManager;
53 private Pair<Integer, String> httpResponsePutOk;
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 = Pair.of(202, Serialization.gsonPretty.toJson(response));
68 httpResponseBadResponse = Pair.of(202, Serialization.gsonPretty.toJson(null));
69 httpResponseErr = Pair.of(200, null);
73 * Create the request and response before.
76 public void createRequestAndResponse() {
77 VfcHealActionVmInfo actionInfo = new VfcHealActionVmInfo();
78 actionInfo.setVmid("TheWizard");
79 actionInfo.setVmname("The Wizard of Oz");
81 VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
82 additionalParams.setAction("Go Home");
83 additionalParams.setActionInfo(actionInfo);
85 VfcHealRequest healRequest = new VfcHealRequest();
86 healRequest.setAdditionalParams(additionalParams);
87 healRequest.setCause("WestWitch");
88 healRequest.setVnfInstanceId("EmeraldCity");
90 final UUID requestId = UUID.randomUUID();
91 request = new VfcRequest();
92 request.setHealRequest(healRequest);
93 request.setNsInstanceId(DOROTHY);
94 request.setRequestId(requestId);
96 List<VfcResponseDescriptor> responseHistoryList = new ArrayList<>();;
98 VfcResponseDescriptor responseDescriptor = new VfcResponseDescriptor();
99 responseDescriptor.setErrorCode("1234");
100 responseDescriptor.setProgress("Follow The Yellow Brick Road");
101 responseDescriptor.setResponseHistoryList(responseHistoryList);
102 responseDescriptor.setResponseId(UUID.randomUUID().toString());
103 responseDescriptor.setStatus("finished");
104 responseDescriptor.setStatusDescription("There's no place like home");
106 response = new VfcResponse();
107 response.setJobId("1234");
108 response.setRequestId(request.getRequestId().toString());
109 response.setResponseDescriptor(responseDescriptor);
113 public void testVfcInitiation() {
114 assertThatIllegalArgumentException().isThrownBy(() -> new VfcManager(null, null, null, null, null)).withMessage(
115 "the parameters \"cb\" and \"request\" on the VfcManager constructor may not be null");
117 assertThatIllegalArgumentException().isThrownBy(() -> new VfcManager(this, null, null, null, null)).withMessage(
118 "the parameters \"cb\" and \"request\" on the VfcManager constructor may not be null");
120 assertThatIllegalArgumentException().isThrownBy(() -> new VfcManager(this, request, null, null, null))
121 .withMessage("the \"url\" parameter on the VfcManager constructor may not be null");
123 new VfcManager(this, request, SOME_URL, null, null);
125 new VfcManager(this, request, SOME_URL, DOROTHY, "Toto");
129 public void testVfcExecutionException() throws InterruptedException {
130 VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Exception");
131 manager.setRestManager(mockedRestManager);
133 when(mockedRestManager.post(
134 startsWith(SOME_URL),
140 .thenThrow(new RuntimeException("OzException"));
144 verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
148 public void testVfcExecutionNull() throws InterruptedException {
149 VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Null");
150 manager.setRestManager(mockedRestManager);
152 when(mockedRestManager.post(startsWith(SOME_URL),
153 eq(DOROTHY), eq("Null"), anyMap(), anyString(), anyString()))
158 verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
162 public void testVfcExecutionError0() throws InterruptedException {
163 VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Error0");
164 manager.setRestManager(mockedRestManager);
166 when(mockedRestManager.post(startsWith(SOME_URL),
167 eq(DOROTHY), eq("Error0"), anyMap(), anyString(), anyString()))
168 .thenReturn(httpResponseErr);
172 verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
176 public void testVfcExecutionBadResponse() throws InterruptedException {
177 VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "BadResponse");
178 manager.setRestManager(mockedRestManager);
180 when(mockedRestManager.post(startsWith(SOME_URL),
181 eq(DOROTHY), eq("OK"), anyMap(), anyString(), anyString()))
182 .thenReturn(httpResponseBadResponse);
186 verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
190 public void testVfcExecutionOk() throws InterruptedException {
191 VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Ok");
192 manager.setRestManager(mockedRestManager);
194 when(mockedRestManager.post(startsWith(SOME_URL),
195 eq(DOROTHY), eq("OK"), anyMap(), anyString(), anyString()))
196 .thenReturn(httpResponsePutOk);
200 verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
204 public void onResponse(VfcResponse responseError) {
206 // Nothing needs to be done