Fix sonar issues in models: sdc to vfc
[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-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
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.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;
33
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;
43
44 public class VfcManagerTest implements VfcCallback {
45
46     private static final String SOME_URL = "http://somewhere.over.the.rainbow";
47
48     private static final String DOROTHY = "Dorothy";
49
50     private RestManager   mockedRestManager;
51
52     private Pair<Integer, String> httpResponsePutOk;
53     private Pair<Integer, String> httpResponseGetOk;
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         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);
71     }
72
73     /**
74      * Create the request and response before.
75      */
76     @Before
77     public void createRequestAndResponse() {
78         VfcHealActionVmInfo actionInfo = new VfcHealActionVmInfo();
79         actionInfo.setVmid("TheWizard");
80         actionInfo.setVmname("The Wizard of Oz");
81
82         VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
83         additionalParams.setAction("Go Home");
84         additionalParams.setActionInfo(actionInfo);
85
86         VfcHealRequest healRequest = new VfcHealRequest();
87         healRequest.setAdditionalParams(additionalParams);
88         healRequest.setCause("WestWitch");
89         healRequest.setVnfInstanceId("EmeraldCity");
90
91         final UUID requestId = UUID.randomUUID();
92         request = new VfcRequest();
93         request.setHealRequest(healRequest);
94         request.setNsInstanceId(DOROTHY);
95         request.setRequestId(requestId);
96
97         List<VfcResponseDescriptor> responseHistoryList = new ArrayList<>();;
98
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");
106
107         response = new VfcResponse();
108         response.setJobId("1234");
109         response.setRequestId(request.getRequestId().toString());
110         response.setResponseDescriptor(responseDescriptor);
111     }
112
113     @Test
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");
117
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");
120
121         assertThatIllegalArgumentException().isThrownBy(() -> new VfcManager(this, request, null, null, null))
122                         .withMessage("the \"url\" parameter on the VfcManager constructor may not be null");
123
124         new VfcManager(this, request, SOME_URL, null, null);
125
126         new VfcManager(this, request, SOME_URL, DOROTHY, "Toto");
127     }
128
129     @Test
130     public void testVfcExecutionException() throws InterruptedException {
131         VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Exception");
132         manager.setRestManager(mockedRestManager);
133
134         Thread managerThread = new Thread(manager);
135         managerThread.start();
136
137         when(mockedRestManager.post(
138             startsWith(SOME_URL),
139             eq(DOROTHY),
140             eq("Exception"),
141             anyMap(),
142             anyString(),
143             anyString()))
144             .thenThrow(new RuntimeException("OzException"));
145
146         managerThread.join();
147     }
148
149     @Test
150     public void testVfcExecutionNull() throws InterruptedException {
151         VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Null");
152         manager.setRestManager(mockedRestManager);
153
154         Thread managerThread = new Thread(manager);
155         managerThread.start();
156
157         when(mockedRestManager.post(startsWith(SOME_URL),
158                 eq(DOROTHY), eq("Null"), anyMap(), anyString(), anyString()))
159                 .thenReturn(null);
160
161         managerThread.join();
162     }
163
164     @Test
165     public void testVfcExecutionError0() throws InterruptedException {
166         VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Error0");
167         manager.setRestManager(mockedRestManager);
168
169         Thread managerThread = new Thread(manager);
170         managerThread.start();
171
172         when(mockedRestManager.post(startsWith(SOME_URL),
173                 eq(DOROTHY), eq("Error0"), anyMap(), anyString(), anyString()))
174                 .thenReturn(httpResponseErr);
175
176         managerThread.join();
177     }
178
179     @Test
180     public void testVfcExecutionBadResponse() throws InterruptedException {
181         VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "BadResponse");
182         manager.setRestManager(mockedRestManager);
183
184         Thread managerThread = new Thread(manager);
185         managerThread.start();
186
187         when(mockedRestManager.post(startsWith(SOME_URL),
188                 eq(DOROTHY), eq("OK"), anyMap(), anyString(), anyString()))
189                 .thenReturn(httpResponseBadResponse);
190
191         managerThread.join();
192     }
193
194     @Test
195     public void testVfcExecutionOk() throws InterruptedException {
196         VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Ok");
197         manager.setRestManager(mockedRestManager);
198
199         Thread managerThread = new Thread(manager);
200         managerThread.start();
201
202         when(mockedRestManager.post(startsWith(SOME_URL),
203                 eq(DOROTHY), eq("OK"), anyMap(), anyString(), anyString()))
204                 .thenReturn(httpResponsePutOk);
205
206         when(mockedRestManager.get(endsWith("1234"), eq(DOROTHY), eq("OK"), anyMap()))
207             .thenReturn(httpResponseGetOk);
208
209         managerThread.join();
210     }
211
212     @Override
213     public void onResponse(VfcResponse responseError) {
214         //
215         // Nothing needs to be done
216         //
217     }
218 }