8db3438cc77b33e9e04cc408811781ce98b7176e
[policy/models.git] / models-interactions / model-impl / vfc / src / test / java / org / onap / policy / vfc / VfcManagerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * vfc
4  * ================================================================================
5  * Copyright (C) 2018-2020 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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.vfc;
24
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;
34
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.UUID;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.rest.RestManager;
41 import org.onap.policy.rest.RestManager.Pair;
42 import org.onap.policy.vfc.VfcManager.VfcCallback;
43 import org.onap.policy.vfc.util.Serialization;
44
45 public class VfcManagerTest implements VfcCallback {
46
47     private static final String SOME_URL = "http://somewhere.over.the.rainbow";
48
49     private static final String DOROTHY = "Dorothy";
50
51     private RestManager   mockedRestManager;
52
53     private Pair<Integer, String> httpResponsePutOk;
54     private Pair<Integer, String> httpResponseBadResponse;
55     private Pair<Integer, String> httpResponseErr;
56
57     private VfcRequest  request;
58     private VfcResponse response;
59
60     /**
61      * Set up the mocked REST manager.
62      */
63     @Before
64     public void setupMockedRest() {
65         mockedRestManager   = mock(RestManager.class);
66
67         httpResponsePutOk       = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
68         httpResponseBadResponse = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(null));
69         httpResponseErr         = mockedRestManager.new Pair<>(200, null);
70     }
71
72     /**
73      * Create the request and response before.
74      */
75     @Before
76     public void createRequestAndResponse() {
77         VfcHealActionVmInfo actionInfo = new VfcHealActionVmInfo();
78         actionInfo.setVmid("TheWizard");
79         actionInfo.setVmname("The Wizard of Oz");
80
81         VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
82         additionalParams.setAction("Go Home");
83         additionalParams.setActionInfo(actionInfo);
84
85         VfcHealRequest healRequest = new VfcHealRequest();
86         healRequest.setAdditionalParams(additionalParams);
87         healRequest.setCause("WestWitch");
88         healRequest.setVnfInstanceId("EmeraldCity");
89
90         final UUID requestId = UUID.randomUUID();
91         request = new VfcRequest();
92         request.setHealRequest(healRequest);
93         request.setNsInstanceId(DOROTHY);
94         request.setRequestId(requestId);
95
96         List<VfcResponseDescriptor> responseHistoryList = new ArrayList<>();;
97
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");
105
106         response = new VfcResponse();
107         response.setJobId("1234");
108         response.setRequestId(request.getRequestId().toString());
109         response.setResponseDescriptor(responseDescriptor);
110     }
111
112     @Test
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");
116
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");
119
120         assertThatIllegalArgumentException().isThrownBy(() -> new VfcManager(this, request, null, null, null))
121                         .withMessage("the \"url\" parameter on the VfcManager constructor may not be null");
122
123         new VfcManager(this, request, SOME_URL, null, null);
124
125         new VfcManager(this, request, SOME_URL, DOROTHY, "Toto");
126     }
127
128     @Test
129     public void testVfcExecutionException() throws InterruptedException {
130         VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Exception");
131         manager.setRestManager(mockedRestManager);
132
133         when(mockedRestManager.post(
134             startsWith(SOME_URL),
135             eq(DOROTHY),
136             eq("Exception"),
137             anyMap(),
138             anyString(),
139             anyString()))
140             .thenThrow(new RuntimeException("OzException"));
141
142         manager.run();
143
144         verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
145     }
146
147     @Test
148     public void testVfcExecutionNull() throws InterruptedException {
149         VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Null");
150         manager.setRestManager(mockedRestManager);
151
152         when(mockedRestManager.post(startsWith(SOME_URL),
153                 eq(DOROTHY), eq("Null"), anyMap(), anyString(), anyString()))
154                 .thenReturn(null);
155
156         manager.run();
157
158         verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
159     }
160
161     @Test
162     public void testVfcExecutionError0() throws InterruptedException {
163         VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Error0");
164         manager.setRestManager(mockedRestManager);
165
166         when(mockedRestManager.post(startsWith(SOME_URL),
167                 eq(DOROTHY), eq("Error0"), anyMap(), anyString(), anyString()))
168                 .thenReturn(httpResponseErr);
169
170         manager.run();
171
172         verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
173     }
174
175     @Test
176     public void testVfcExecutionBadResponse() throws InterruptedException {
177         VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "BadResponse");
178         manager.setRestManager(mockedRestManager);
179
180         when(mockedRestManager.post(startsWith(SOME_URL),
181                 eq(DOROTHY), eq("OK"), anyMap(), anyString(), anyString()))
182                 .thenReturn(httpResponseBadResponse);
183
184         manager.run();
185
186         verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
187     }
188
189     @Test
190     public void testVfcExecutionOk() throws InterruptedException {
191         VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Ok");
192         manager.setRestManager(mockedRestManager);
193
194         when(mockedRestManager.post(startsWith(SOME_URL),
195                 eq(DOROTHY), eq("OK"), anyMap(), anyString(), anyString()))
196                 .thenReturn(httpResponsePutOk);
197
198         manager.run();
199
200         verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
201     }
202
203     @Override
204     public void onResponse(VfcResponse responseError) {
205         //
206         // Nothing needs to be done
207         //
208     }
209 }