From 7f42758f8292e1440a63867ec5d885abc35e38a4 Mon Sep 17 00:00:00 2001 From: Michal Kabaj Date: Wed, 21 Mar 2018 13:26:51 +0100 Subject: ChefAdapterImpl junits - Added new Junit class ChefAdapterImplVNFCOperationsTest - Added unit tests for method vnfcEnvironment - Small refactor to for exception handling in vnfcEnvironment method to improve readability and avoid catching and rethrowing same exception Change-Id: I58a43390ff45598f6d17da48eed3620667008cb7 Issue-ID: APPC-437 Signed-off-by: Michal Kabaj --- .../appc/adapter/chef/impl/ChefAdapterImpl.java | 54 ++++++++++------------ 1 file changed, 25 insertions(+), 29 deletions(-) (limited to 'appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java') 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 f689668bb..f5b6b6751 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 @@ -114,21 +114,18 @@ public class ChefAdapterImpl implements ChefAdapter { @Override public void vnfcEnvironment(Map params, SvcLogicContext ctx) throws SvcLogicException { int code; - try { - logger.info("environment of VNF-C"); - chefInfo(params, ctx); - RequestContext rc = new RequestContext(ctx); - logger.info("Context" + ctx); - rc.isAlive(); - String env = params.get("Environment"); - logger.info("Environmnet" + env); - if (env.equals(StringUtils.EMPTY)) { - chefServerResult(rc, 200, "Skip Environment block "); - } else { - JSONObject envJ = new JSONObject(env); - String envName = envJ.getString("name"); - String message; - if (privateKeyChecker.doesExist(clientPrivatekey)) { + logger.info("environment of VNF-C"); + chefInfo(params, ctx); + String env = params.get("Environment"); + logger.info("Environmnet" + env); + if (env.equals(StringUtils.EMPTY)) { + chefServerResult(new RequestContext(ctx), 200, "Skip Environment block "); + } else { + String message; + if (privateKeyChecker.doesExist(clientPrivatekey)) { + try { + JSONObject envJ = new JSONObject(env); + String envName = envJ.getString("name"); // update the details of an environment on the Chef server. ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey); ChefResponse chefResponse = chefApiClient.put("/environments/" + envName, env); @@ -141,22 +138,21 @@ public class ChefAdapterImpl implements ChefAdapter { message = chefResponse.getBody(); logger.info("requestbody {}", chefResponse.getBody()); } - - } else { - code = 500; - message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey; - doFailure(ctx, code, message); + chefServerResult(new RequestContext(ctx), code, message); + } 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); + doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage()); } - chefServerResult(rc, code, message); + } else { + code = 500; + message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey; + doFailure(ctx, code, message); } - } 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); - doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage()); } } -- cgit 1.2.3-korg