From fab5309565a52653a60b52f4561696e953eabf9e Mon Sep 17 00:00:00 2001 From: Michal Kabaj Date: Mon, 26 Mar 2018 16:37:03 +0200 Subject: [PATCH 1/1] ChefAdapterImpl junits -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 --- .../appc/adapter/chef/impl/ChefAdapterImpl.java | 4 -- .../impl/ChefAdapterImplVNFCOperationsTest.java | 77 ++++++++++++++++++++++ 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java index 28facf3df..d26c85c44 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java @@ -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); diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplVNFCOperationsTest.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplVNFCOperationsTest.java index 890cfdfbd..5955d6099 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplVNFCOperationsTest.java +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplVNFCOperationsTest.java @@ -273,6 +273,83 @@ public class ChefAdapterImplVNFCOperationsTest { .startsWith(expectedErrorMessage); } + @Test + public void vnfcPushJob_shouldUpdateSvcContextWithJobId_whenPushJobWasSuccessfullyCreatedWithCallbackUrl() + throws SvcLogicException { + Map 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 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 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 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 givenInputParams(Entry... entries) { Builder paramsBuilder = ImmutableMap.builder(); paramsBuilder.put("username", USERNAME) -- 2.16.6