ChefAdapterImpl junits 75/38575/2
authorMichal Kabaj <michal.kabaj@nokia.com>
Mon, 26 Mar 2018 14:37:03 +0000 (16:37 +0200)
committerTakamune Cho <tc012c@att.com>
Mon, 26 Mar 2018 21:03:33 +0000 (21:03 +0000)
-Add new JUnits to ChefAdapterImplVNFCOperationsTest
-remove unused JSONException catch clause from ChefAdapterImpl,
when there is no jackson lib used

Change-Id: I96b8d6155ca13087b697a19e1fcbba2ee82a8291
Issue-ID: APPC-437
Signed-off-by: Michal Kabaj <michal.kabaj@nokia.com>
appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java
appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplVNFCOperationsTest.java

index 28facf3..d26c85c 100644 (file)
@@ -247,10 +247,6 @@ public class ChefAdapterImpl implements ChefAdapter {
             } else {
                 throw new SvcLogicException("Missing Mandatory param(s)  NodeList ");
             }
-        } catch (JSONException e) {
-            code = 401;
-            logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
-            doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
         } catch (Exception e) {
             code = 401;
             logger.error(POSTING_REQUEST_ERROR_STR, e);
index 890cfdf..5955d60 100644 (file)
@@ -273,6 +273,83 @@ public class ChefAdapterImplVNFCOperationsTest {
             .startsWith(expectedErrorMessage);
     }
 
+    @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" + "}";
+    }
+
+    @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);
+    }
+
+    @Test
+    public void vnfcPushJob_shouldNotPushJob_andThrowException_whenNodeListParamIsEmpty() {
+        // GIVEN
+        String expectedErrorMessage = "Error posting request: Missing 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);
+    }
+
     private Map<String, String> givenInputParams(Entry<String, String>... entries) {
         Builder<String, String> paramsBuilder = ImmutableMap.builder();
         paramsBuilder.put("username", USERNAME)