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
index 890cfdf..e79b72a 100644 (file)
@@ -3,6 +3,7 @@
  * ONAP : APPC
  * ================================================================================
  * Copyright (C) 2018 Nokia. All rights reserved.
+ * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -74,6 +75,7 @@ public class ChefAdapterImplVNFCOperationsTest {
         svcLogicContext = new SvcLogicContext();
     }
 
+    @SuppressWarnings("unchecked")
     @Test
     public void vnfcEnvironment_shouldSkipEnvironmentCreation_whenEnvParamIsEmpty() throws SvcLogicException {
         // GIVEN
@@ -89,6 +91,7 @@ public class ChefAdapterImplVNFCOperationsTest {
         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo("Skip Environment block ");
     }
 
+    @SuppressWarnings("unchecked")
     @Test
     public void vnfcEnvironment_shouldCreateNewEnvironment_forEnvParam_whenRequestedEnvDoesNotExist()
         throws SvcLogicException {
@@ -113,6 +116,7 @@ public class ChefAdapterImplVNFCOperationsTest {
         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedErrorMessage);
     }
 
+    @SuppressWarnings("unchecked")
     @Test
     public void vnfcEnvironment_shouldNotAttemptEnvCreation_andThrowException_whenPrivateKeyCheckFails() {
         // GIVEN
@@ -132,6 +136,7 @@ public class ChefAdapterImplVNFCOperationsTest {
             .isEqualTo(expectedErrorMsg + CLIENT_PRIVATE_KEY_PATH);
     }
 
+    @SuppressWarnings("unchecked")
     @Test
     public void vnfcEnvironment_shouldNotAttemptEnvCreation_andHandleJSONException_whenJSONParamsAreMalformed() {
         // GIVEN
@@ -151,6 +156,7 @@ public class ChefAdapterImplVNFCOperationsTest {
             .startsWith(expectedErrorMessage);
     }
 
+    @SuppressWarnings("unchecked")
     @Test
     public void vnfcEnvironment_shouldNotAttemptEnvCreation_andHandleException_whenExceptionOccursDuringExecution() {
         // GIVEN
@@ -163,7 +169,7 @@ public class ChefAdapterImplVNFCOperationsTest {
         // WHEN  // THEN
         assertThatExceptionOfType(SvcLogicException.class)
             .isThrownBy(() -> chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext))
-            .withMessage(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage + "Null value encountered");
+            .withMessage(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage + "vnfcEnvironmentNull value encountered");
 
         assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
@@ -194,6 +200,7 @@ public class ChefAdapterImplVNFCOperationsTest {
         assertNodeObjectsAreUpdatedFor(firstNodeResponse, secondNodeResponse, expectedHttpStatus, expectedMessage);
     }
 
+    @SuppressWarnings("unchecked")
     public void assertNodeObjectsAreUpdatedFor(ChefResponse firstNodeResponse, ChefResponse secondNodeResponse,
         int expectedHttpStatus, String expectedMessage) throws SvcLogicException {
         // GIVEN
@@ -220,6 +227,7 @@ public class ChefAdapterImplVNFCOperationsTest {
         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedMessage);
     }
 
+    @SuppressWarnings("unchecked")
     @Test
     public void vnfcNodeObjects_shouldThrowSvcLogicException_whenNodeListParamIsEmpty() {
         Map<String, String> params = givenInputParams(
@@ -228,6 +236,7 @@ public class ChefAdapterImplVNFCOperationsTest {
         checkMissingParamsAreValidated(params);
     }
 
+    @SuppressWarnings("unchecked")
     @Test
     public void vnfcNodeObjects_shouldThrowSvcLogicException_whenNodeParamIsEmpty() {
         Map<String, String> params = givenInputParams(
@@ -238,7 +247,7 @@ public class ChefAdapterImplVNFCOperationsTest {
 
     public void checkMissingParamsAreValidated(Map<String, String> params) {
         // GIVEN
-        String expectedErrorMsg = "Missing Mandatory param(s) Node , NodeList ";
+        String expectedErrorMsg = "vnfcNodeobjectsMissing Mandatory param(s) Node , NodeList ";
 
         // WHEN  // THEN
         assertThatExceptionOfType(SvcLogicException.class)
@@ -252,6 +261,7 @@ public class ChefAdapterImplVNFCOperationsTest {
             .isEqualTo("Error posting request: " + expectedErrorMsg);
     }
 
+    @SuppressWarnings("unchecked")
     @Test
     public void vnfcNodeObjects_shouldNotUpdateNodes_andHandleJSONException_whenJSONParamsAreMalformed() {
         // GIVEN
@@ -273,6 +283,179 @@ public class ChefAdapterImplVNFCOperationsTest {
             .startsWith(expectedErrorMessage);
     }
 
+    @SuppressWarnings("unchecked")
+    @Test
+    public void vnfcPushJob_shouldUpdateSvcContextWithJobId_whenPushJobWasSuccessfullyCreatedWithCallbackUrl()
+        throws SvcLogicException {
+        Map<String, String> params = givenInputParams(
+            immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"),
+            immutableEntry("CallbackCapable", "true"),
+            immutableEntry("RequestId", "666"),
+            immutableEntry("CallbackUrl", "someURLForCallback"));
+        int expectedResponseStatus = HttpStatus.SC_CREATED;
+        String expectedResponseMessage = "jobs:666-9";
+
+        assertVnfcPushJobExecutionFor(params, buildJsonRequestWithCallback(), expectedResponseStatus,
+            expectedResponseMessage);
+        assertThat(svcLogicContext.getAttribute("jobID")).isEqualTo("666");
+    }
+
+    private String buildJsonRequestWithCallback() {
+        return "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
+            + "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]" + "," + "\"env\": {\"RequestId\": \"" + "666"
+            + "\", \"CallbackUrl\": \""
+            + "someURLForCallback" + "\"}," + "\"capture_output\": true" + "}";
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void vnfcPushJob_shouldUpdateSvcContextWithJobId_whenPushJobWasSuccessfullyCreatedWithoutCallbackUrl()
+        throws SvcLogicException {
+        Map<String, String> params = givenInputParams(
+            immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"),
+            immutableEntry("RequestId", "666"));
+        int expectedResponseStatus = HttpStatus.SC_OK;
+        String expectedResponseMessage = "jobs:666-9";
+
+        assertVnfcPushJobExecutionFor(params, buildJsonRequestWithoutCallback(), expectedResponseStatus,
+            expectedResponseMessage);
+        assertThat(svcLogicContext.getAttribute("jobID")).isBlank();
+    }
+
+    private String buildJsonRequestWithoutCallback() {
+        return "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
+            + "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]" + "," + "\"env\": {}," + "\"capture_output\": true"
+            + "}";
+    }
+
+    public void assertVnfcPushJobExecutionFor(Map<String, String> params, String pushRequestWithCallback,
+        int expectedResponseStatus, String expectedResponseMessage) throws SvcLogicException {
+        // GIVEN
+        given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME,
+            CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
+        given(chefApiClient.post("/pushy/jobs", pushRequestWithCallback))
+            .willReturn(ChefResponse.create(expectedResponseStatus, expectedResponseMessage));
+
+        // WHEN
+        chefAdapterFactory.create().vnfcPushJob(params, svcLogicContext);
+
+        // THEN
+        assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
+        assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
+            .isEqualTo(Integer.toString(expectedResponseStatus));
+        assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedResponseMessage);
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void vnfcPushJob_shouldNotPushJob_andThrowException_whenNodeListParamIsEmpty() {
+        // GIVEN
+        String expectedErrorMessage = "Error posting request: vnfcPushJobMissing Mandatory param(s)  NodeList ";
+        Map<String, String> params = givenInputParams();
+        // WHEN  // THEN
+        assertThatExceptionOfType(SvcLogicException.class)
+            .isThrownBy(() -> chefAdapterFactory.create().vnfcPushJob(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_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")
     private Map<String, String> givenInputParams(Entry<String, String>... entries) {
         Builder<String, String> paramsBuilder = ImmutableMap.builder();
         paramsBuilder.put("username", USERNAME)
@@ -284,4 +467,4 @@ public class ChefAdapterImplVNFCOperationsTest {
         }
         return paramsBuilder.build();
     }
-}
\ No newline at end of file
+}