+ @SuppressWarnings("unchecked")
+ @Test
+ public void fetchResults_shouldNotFetchResults_andThrowException_whenNodeListParamIsEmpty() {
+ // GIVEN
+ String expectedErrorMessage = "Error posting request: fetchResultsMissing Mandatory param(s) NodeList ";
+ Map<String, String> params = givenInputParams();
+ // WHEN // THEN
+ assertThatExceptionOfType(SvcLogicException.class)
+ .isThrownBy(() -> chefAdapterFactory.create().fetchResults(params, svcLogicContext))
+ .withMessageStartingWith(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage);
+
+ assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
+ assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
+ .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
+ assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedErrorMessage);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void fetchResults_shouldNotFetchResults_andThrowException_whenPrivateKeyCheckFails() {
+ // GIVEN
+ Map<String, String> params = givenInputParams(
+ immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"));
+ String expectedErrorMessage =
+ "Error posting request: fetchResults"
+ + CHEF_ADAPTER_ERROR_PREFIX
+ + "Cannot find the private key in the APPC file system, please load the private key to "
+ + CLIENT_PRIVATE_KEY_PATH;
+ given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(false);
+
+ // WHEN // THEN
+ assertThatExceptionOfType(SvcLogicException.class)
+ .isThrownBy(() -> chefAdapterFactory.create().fetchResults(params, svcLogicContext))
+ .withMessage(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage);
+
+ assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
+ assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
+ .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
+ assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
+ .isEqualTo(expectedErrorMessage);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void fetchResults_shouldUpdateSvcLogicContextWithJsonResponse_fromSuccessfulChefServerCall()
+ throws SvcLogicException {
+ // GIVEN
+ String json = "{normal:{PushJobOutput : \"ssh start/running, process 1090\"}}";
+ Map<String, String> params = givenInputParams(
+ immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\"]"));
+ given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
+ given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME,
+ CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
+ given(chefApiClient.get("/nodes/" + "test1.vnf_b.onap.com"))
+ .willReturn(ChefResponse.create(HttpStatus.SC_OK, json));
+
+ // WHEN
+ chefAdapterFactory.create().fetchResults(params, svcLogicContext);
+
+ // THEN
+ assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
+ assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
+ .isEqualTo(Integer.toString(HttpStatus.SC_OK));
+ assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
+ .isEqualTo("{\"test1.vnf_b.onap.com\":{\"PushJobOutput\":\"ssh start/running, process 1090\"}}");
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void fetchResults_shouldUpdateSvcLogicContextWithFailedMessage_whenReturnedJSONMessageIsMissingAttribute()
+ throws SvcLogicException {
+ // GIVEN
+ String json = "{normal:{invalidKey : \"ssh start/running, process 1090\"}}";
+ Map<String, String> params = givenInputParams(
+ immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\"]"));
+ given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
+ given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME,
+ CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
+ given(chefApiClient.get("/nodes/" + "test1.vnf_b.onap.com"))
+ .willReturn(ChefResponse.create(HttpStatus.SC_OK, json));
+
+ // WHEN
+ chefAdapterFactory.create().fetchResults(params, svcLogicContext);
+
+ // THEN
+ assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
+ assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
+ .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR));
+ assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
+ .isEqualTo("Cannot find PushJobOutput");
+ }
+
+ @SuppressWarnings("unchecked")