Fixes for Chef Adapter bundle
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / test / java / org / onap / appc / adapter / chef / impl / ChefAdapterImplVNFCOperationsTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
7  * =============================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.appc.adapter.chef.impl;
22
23
24 import static com.google.common.collect.Maps.immutableEntry;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
27 import static org.mockito.BDDMockito.given;
28
29 import com.google.common.collect.ImmutableMap;
30 import com.google.common.collect.ImmutableMap.Builder;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import org.apache.http.HttpStatus;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory;
41 import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient;
42 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class ChefAdapterImplVNFCOperationsTest {
48
49     private static final String CHEF_END_POINT = "https://localhost/organizations/onap";
50     private static final String USERNAME = "testclient";
51     private static final String ORGANIZATIONS = "onap";
52     private static final String SERVER_ADDRESS = "localhost";
53     private static final String CLIENT_PRIVATE_KEY_PATH = "/opt/onap/appc/chef/localhost/onap/testclient.pem";
54     private static final String RESULT_CODE_ATTR_KEY = "chefServerResult.code";
55     private static final String RESULT_MESSAGE_ATTR_KEY = "chefServerResult.message";
56     private static final String FAILURE_STATUS = "failure";
57     private static final String SUCCESS_STATUS = "success";
58     private static final String CHEF_ADAPTER_ERROR_PREFIX = "Chef Adapter error:";
59     private static final String ENV_PARAM_KEY = "Environment";
60     private static final String ENV_JSON_VALUE = "{name:envName}";
61
62     @Mock
63     private PrivateKeyChecker privateKeyChecker;
64     @Mock
65     private ChefApiClientFactory chefApiClientFactory;
66     @Mock
67     private ChefApiClient chefApiClient;
68
69     @InjectMocks
70     private ChefAdapterFactory chefAdapterFactory;
71     private SvcLogicContext svcLogicContext;
72
73     @Before
74     public void setUp() {
75         svcLogicContext = new SvcLogicContext();
76     }
77
78     @SuppressWarnings("unchecked")
79     @Test
80     public void vnfcEnvironment_shouldSkipEnvironmentCreation_whenEnvParamIsEmpty() throws SvcLogicException {
81         // GIVEN
82         Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ""));
83
84         // WHEN
85         chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext);
86
87         // THEN
88         assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
89         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
90             .isEqualTo(Integer.toString(HttpStatus.SC_OK));
91         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo("Skip Environment block ");
92     }
93
94     @SuppressWarnings("unchecked")
95     @Test
96     public void vnfcEnvironment_shouldCreateNewEnvironment_forEnvParam_whenRequestedEnvDoesNotExist()
97         throws SvcLogicException {
98         // GIVEN
99         String expectedErrorMessage = "New Environment Created";
100         Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ENV_JSON_VALUE));
101         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
102         given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME,
103             CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
104         given(chefApiClient.put("/environments/" + "envName", ENV_JSON_VALUE))
105             .willReturn(ChefResponse.create(HttpStatus.SC_NOT_FOUND, ""));
106         given(chefApiClient.post("/environments", ENV_JSON_VALUE))
107             .willReturn(ChefResponse.create(HttpStatus.SC_CREATED, expectedErrorMessage));
108
109         // WHEN
110         chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext);
111
112         // THEN
113         assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
114         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
115             .isEqualTo(Integer.toString(HttpStatus.SC_CREATED));
116         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedErrorMessage);
117     }
118
119     @SuppressWarnings("unchecked")
120     @Test
121     public void vnfcEnvironment_shouldNotAttemptEnvCreation_andThrowException_whenPrivateKeyCheckFails() {
122         // GIVEN
123         String expectedErrorMsg = "Cannot find the private key in the APPC file system, please load the private key to ";
124         Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ENV_JSON_VALUE));
125         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(false);
126
127         // WHEN  // THEN
128         assertThatExceptionOfType(SvcLogicException.class)
129             .isThrownBy(() -> chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext))
130             .withMessage(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMsg + CLIENT_PRIVATE_KEY_PATH);
131
132         assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
133         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
134             .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR));
135         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
136             .isEqualTo(expectedErrorMsg + CLIENT_PRIVATE_KEY_PATH);
137     }
138
139     @SuppressWarnings("unchecked")
140     @Test
141     public void vnfcEnvironment_shouldNotAttemptEnvCreation_andHandleJSONException_whenJSONParamsAreMalformed() {
142         // GIVEN
143         String expectedErrorMessage = "Error posting request due to invalid JSON block: ";
144         Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, "MALFORMED_JSON"));
145         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
146
147         // WHEN  // THEN
148         assertThatExceptionOfType(SvcLogicException.class)
149             .isThrownBy(() -> chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext))
150             .withMessageStartingWith(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage);
151
152         assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
153         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
154             .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
155         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
156             .startsWith(expectedErrorMessage);
157     }
158
159     @SuppressWarnings("unchecked")
160     @Test
161     public void vnfcEnvironment_shouldNotAttemptEnvCreation_andHandleException_whenExceptionOccursDuringExecution() {
162         // GIVEN
163         String expectedErrorMessage = "Error posting request: ";
164         Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ENV_JSON_VALUE));
165         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
166         given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME,
167             CLIENT_PRIVATE_KEY_PATH)).willThrow(new NullPointerException("Null value encountered"));
168
169         // WHEN  // THEN
170         assertThatExceptionOfType(SvcLogicException.class)
171             .isThrownBy(() -> chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext))
172             .withMessage(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage + "vnfcEnvironmentNull value encountered");
173
174         assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
175         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
176             .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
177         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).startsWith(expectedErrorMessage);
178     }
179
180     @Test
181     public void vnfcNodeObjects_shouldUpdateNodeObjects_andSetCodeAndMessageFromLastSuccessfulResponseInSvcLogicContext()
182         throws SvcLogicException {
183         // GIVEN
184         ChefResponse firstNodeResponse = ChefResponse.create(HttpStatus.SC_OK, "firstMessage");
185         ChefResponse secondNodeResponse = ChefResponse.create(HttpStatus.SC_OK, "secondMessage");
186         int expectedHttpStatus = HttpStatus.SC_OK;
187         String expectedMessage = "secondMessage";
188
189         assertNodeObjectsAreUpdatedFor(firstNodeResponse, secondNodeResponse, expectedHttpStatus, expectedMessage);
190     }
191
192     @Test
193     public void vnfcNodeObjects_shouldStopProcessingNodeObjectUpdates_whenFirstReturnedResponseIsOtherThan_200()
194         throws SvcLogicException {
195         ChefResponse firstNodeResponse = ChefResponse.create(HttpStatus.SC_ACCEPTED, "firstMessage");
196         ChefResponse secondNodeResponse = ChefResponse.create(HttpStatus.SC_OK, "secondMessage");
197         int expectedHttpStatus = HttpStatus.SC_ACCEPTED;
198         String expectedMessage = "firstMessage";
199
200         assertNodeObjectsAreUpdatedFor(firstNodeResponse, secondNodeResponse, expectedHttpStatus, expectedMessage);
201     }
202
203     @SuppressWarnings("unchecked")
204     public void assertNodeObjectsAreUpdatedFor(ChefResponse firstNodeResponse, ChefResponse secondNodeResponse,
205         int expectedHttpStatus, String expectedMessage) throws SvcLogicException {
206         // GIVEN
207         Map<String, String> params = givenInputParams(
208             immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"),
209             immutableEntry("Node", "{name:nodeName}"));
210
211         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
212         given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME,
213             CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
214         given(chefApiClient.put("/nodes/" + "test1.vnf_b.onap.com", "{\"name\":\"test1.vnf_b.onap.com\"}"))
215             .willReturn(firstNodeResponse);
216         given(chefApiClient
217             .put("/nodes/" + "test2.vnf_b.onap.com", "{\"name\":\"test2.vnf_b.onap.com\"}"))
218             .willReturn(secondNodeResponse);
219
220         // WHEN
221         chefAdapterFactory.create().vnfcNodeobjects(params, svcLogicContext);
222
223         // THEN
224         assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
225         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
226             .isEqualTo(Integer.toString(expectedHttpStatus));
227         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedMessage);
228     }
229
230     @SuppressWarnings("unchecked")
231     @Test
232     public void vnfcNodeObjects_shouldThrowSvcLogicException_whenNodeListParamIsEmpty() {
233         Map<String, String> params = givenInputParams(
234             immutableEntry("NodeList", ""),
235             immutableEntry("Node", "{name:nodeName}"));
236         checkMissingParamsAreValidated(params);
237     }
238
239     @SuppressWarnings("unchecked")
240     @Test
241     public void vnfcNodeObjects_shouldThrowSvcLogicException_whenNodeParamIsEmpty() {
242         Map<String, String> params = givenInputParams(
243             immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"),
244             immutableEntry("Node", ""));
245         checkMissingParamsAreValidated(params);
246     }
247
248     public void checkMissingParamsAreValidated(Map<String, String> params) {
249         // GIVEN
250         String expectedErrorMsg = "vnfcNodeobjectsMissing Mandatory param(s) Node , NodeList ";
251
252         // WHEN  // THEN
253         assertThatExceptionOfType(SvcLogicException.class)
254             .isThrownBy(() -> chefAdapterFactory.create().vnfcNodeobjects(params, svcLogicContext))
255             .withMessage(CHEF_ADAPTER_ERROR_PREFIX + "Error posting request: " + expectedErrorMsg);
256
257         assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
258         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
259             .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
260         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
261             .isEqualTo("Error posting request: " + expectedErrorMsg);
262     }
263
264     @SuppressWarnings("unchecked")
265     @Test
266     public void vnfcNodeObjects_shouldNotUpdateNodes_andHandleJSONException_whenJSONParamsAreMalformed() {
267         // GIVEN
268         Map<String, String> params = givenInputParams(
269             immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"),
270             immutableEntry("Node", "MALFORMED_JSON"));
271         String expectedErrorMessage = "Error posting request due to invalid JSON block: ";
272         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
273
274         // WHEN  // THEN
275         assertThatExceptionOfType(SvcLogicException.class)
276             .isThrownBy(() -> chefAdapterFactory.create().vnfcNodeobjects(params, svcLogicContext))
277             .withMessageStartingWith(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage);
278
279         assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
280         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
281             .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
282         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
283             .startsWith(expectedErrorMessage);
284     }
285
286     @SuppressWarnings("unchecked")
287     @Test
288     public void vnfcPushJob_shouldUpdateSvcContextWithJobId_whenPushJobWasSuccessfullyCreatedWithCallbackUrl()
289         throws SvcLogicException {
290         Map<String, String> params = givenInputParams(
291             immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"),
292             immutableEntry("CallbackCapable", "true"),
293             immutableEntry("RequestId", "666"),
294             immutableEntry("CallbackUrl", "someURLForCallback"));
295         int expectedResponseStatus = HttpStatus.SC_CREATED;
296         String expectedResponseMessage = "jobs:666-9";
297
298         assertVnfcPushJobExecutionFor(params, buildJsonRequestWithCallback(), expectedResponseStatus,
299             expectedResponseMessage);
300         assertThat(svcLogicContext.getAttribute("jobID")).isEqualTo("666");
301     }
302
303     private String buildJsonRequestWithCallback() {
304         return "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
305             + "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]" + "," + "\"env\": {\"RequestId\": \"" + "666"
306             + "\", \"CallbackUrl\": \""
307             + "someURLForCallback" + "\"}," + "\"capture_output\": true" + "}";
308     }
309
310     @SuppressWarnings("unchecked")
311     @Test
312     public void vnfcPushJob_shouldUpdateSvcContextWithJobId_whenPushJobWasSuccessfullyCreatedWithoutCallbackUrl()
313         throws SvcLogicException {
314         Map<String, String> params = givenInputParams(
315             immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"),
316             immutableEntry("RequestId", "666"));
317         int expectedResponseStatus = HttpStatus.SC_OK;
318         String expectedResponseMessage = "jobs:666-9";
319
320         assertVnfcPushJobExecutionFor(params, buildJsonRequestWithoutCallback(), expectedResponseStatus,
321             expectedResponseMessage);
322         assertThat(svcLogicContext.getAttribute("jobID")).isBlank();
323     }
324
325     private String buildJsonRequestWithoutCallback() {
326         return "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
327             + "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]" + "," + "\"env\": {}," + "\"capture_output\": true"
328             + "}";
329     }
330
331     public void assertVnfcPushJobExecutionFor(Map<String, String> params, String pushRequestWithCallback,
332         int expectedResponseStatus, String expectedResponseMessage) throws SvcLogicException {
333         // GIVEN
334         given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME,
335             CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
336         given(chefApiClient.post("/pushy/jobs", pushRequestWithCallback))
337             .willReturn(ChefResponse.create(expectedResponseStatus, expectedResponseMessage));
338
339         // WHEN
340         chefAdapterFactory.create().vnfcPushJob(params, svcLogicContext);
341
342         // THEN
343         assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
344         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
345             .isEqualTo(Integer.toString(expectedResponseStatus));
346         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedResponseMessage);
347     }
348
349     @SuppressWarnings("unchecked")
350     @Test
351     public void vnfcPushJob_shouldNotPushJob_andThrowException_whenNodeListParamIsEmpty() {
352         // GIVEN
353         String expectedErrorMessage = "Error posting request: vnfcPushJobMissing Mandatory param(s)  NodeList ";
354         Map<String, String> params = givenInputParams();
355         // WHEN  // THEN
356         assertThatExceptionOfType(SvcLogicException.class)
357             .isThrownBy(() -> chefAdapterFactory.create().vnfcPushJob(params, svcLogicContext))
358             .withMessageStartingWith(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage);
359
360         assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
361         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
362             .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
363         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedErrorMessage);
364     }
365
366     @SuppressWarnings("unchecked")
367     @Test
368     public void fetchResults_shouldNotFetchResults_andThrowException_whenNodeListParamIsEmpty() {
369         // GIVEN
370         String expectedErrorMessage = "Error posting request: fetchResultsMissing Mandatory param(s)  NodeList ";
371         Map<String, String> params = givenInputParams();
372         // WHEN  // THEN
373         assertThatExceptionOfType(SvcLogicException.class)
374             .isThrownBy(() -> chefAdapterFactory.create().fetchResults(params, svcLogicContext))
375             .withMessageStartingWith(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage);
376
377         assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
378         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
379             .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
380         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedErrorMessage);
381     }
382
383     @SuppressWarnings("unchecked")
384     @Test
385     public void fetchResults_shouldNotFetchResults_andThrowException_whenPrivateKeyCheckFails() {
386         // GIVEN
387         Map<String, String> params = givenInputParams(
388             immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"));
389         String expectedErrorMessage =
390             "Error posting request: fetchResults"
391                 + CHEF_ADAPTER_ERROR_PREFIX
392                 + "Cannot find the private key in the APPC file system, please load the private key to "
393                 + CLIENT_PRIVATE_KEY_PATH;
394         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(false);
395
396         // WHEN  // THEN
397         assertThatExceptionOfType(SvcLogicException.class)
398             .isThrownBy(() -> chefAdapterFactory.create().fetchResults(params, svcLogicContext))
399             .withMessage(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage);
400
401         assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
402         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
403             .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
404         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
405             .isEqualTo(expectedErrorMessage);
406     }
407
408     @SuppressWarnings("unchecked")
409     @Test
410     public void fetchResults_shouldUpdateSvcLogicContextWithJsonResponse_fromSuccessfulChefServerCall()
411         throws SvcLogicException {
412         // GIVEN
413         String json = "{normal:{PushJobOutput : \"ssh start/running, process 1090\"}}";
414         Map<String, String> params = givenInputParams(
415             immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\"]"));
416         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
417         given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME,
418             CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
419         given(chefApiClient.get("/nodes/" + "test1.vnf_b.onap.com"))
420             .willReturn(ChefResponse.create(HttpStatus.SC_OK, json));
421
422         // WHEN
423         chefAdapterFactory.create().fetchResults(params, svcLogicContext);
424
425         // THEN
426         assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
427         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
428             .isEqualTo(Integer.toString(HttpStatus.SC_OK));
429         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
430             .isEqualTo("{\"test1.vnf_b.onap.com\":{\"PushJobOutput\":\"ssh start/running, process 1090\"}}");
431     }
432
433     @SuppressWarnings("unchecked")
434     @Test
435     public void fetchResults_shouldUpdateSvcLogicContextWithFailedMessage_whenReturnedJSONMessageIsMissingAttribute()
436         throws SvcLogicException {
437         // GIVEN
438         String json = "{normal:{invalidKey : \"ssh start/running, process 1090\"}}";
439         Map<String, String> params = givenInputParams(
440             immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\"]"));
441         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
442         given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME,
443             CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
444         given(chefApiClient.get("/nodes/" + "test1.vnf_b.onap.com"))
445             .willReturn(ChefResponse.create(HttpStatus.SC_OK, json));
446
447         // WHEN
448         chefAdapterFactory.create().fetchResults(params, svcLogicContext);
449
450         // THEN
451         assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
452         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
453             .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR));
454         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
455             .isEqualTo("Cannot find PushJobOutput");
456     }
457
458     @SuppressWarnings("unchecked")
459     private Map<String, String> givenInputParams(Entry<String, String>... entries) {
460         Builder<String, String> paramsBuilder = ImmutableMap.builder();
461         paramsBuilder.put("username", USERNAME)
462             .put("serverAddress", SERVER_ADDRESS)
463             .put("organizations", ORGANIZATIONS);
464
465         for (Entry<String, String> entry : entries) {
466             paramsBuilder.put(entry);
467         }
468         return paramsBuilder.build();
469     }
470 }