aboutsummaryrefslogtreecommitdiffstats
path: root/appc-dg/appc-dg-shared/appc-dg-ssh/src/main
diff options
context:
space:
mode:
authorJoss Armstrong <joss.armstrong@ericsson.com>2019-01-09 15:30:17 +0000
committerTakamune Cho <takamune.cho@att.com>2019-01-10 12:02:41 +0000
commitfede312b74f6caa7b5437a419b8d05a8eeffedf6 (patch)
tree183131c033b02ba0455eef600a916f4e3b72a5cc /appc-dg/appc-dg-shared/appc-dg-ssh/src/main
parent670feaeaa8695d139e326b635c1f6bd1aa5e0dcf (diff)
Test coverage in appc-dg-ssh
Increased line coverage from 49% to 93% Issue-ID: APPC-1313 Change-Id: I2f27742fd996cd6d07668fd61c887e29f2140818 Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
Diffstat (limited to 'appc-dg/appc-dg-shared/appc-dg-ssh/src/main')
-rw-r--r--appc-dg/appc-dg-shared/appc-dg-ssh/src/main/java/org/onap/appc/dg/ssh/impl/SshServiceImpl.java120
1 files changed, 61 insertions, 59 deletions
diff --git a/appc-dg/appc-dg-shared/appc-dg-ssh/src/main/java/org/onap/appc/dg/ssh/impl/SshServiceImpl.java b/appc-dg/appc-dg-shared/appc-dg-ssh/src/main/java/org/onap/appc/dg/ssh/impl/SshServiceImpl.java
index c1b9db354..e758f36a2 100644
--- a/appc-dg/appc-dg-shared/appc-dg-ssh/src/main/java/org/onap/appc/dg/ssh/impl/SshServiceImpl.java
+++ b/appc-dg/appc-dg-shared/appc-dg-ssh/src/main/java/org/onap/appc/dg/ssh/impl/SshServiceImpl.java
@@ -5,6 +5,8 @@
* Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications (C) 2019 Ericsson
* =============================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -41,68 +43,68 @@ import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
public class SshServiceImpl implements SshService {
- private static final EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
- private static final ObjectMapper mapper = new ObjectMapper();
+ private static final EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
+ private static final ObjectMapper mapper = new ObjectMapper();
- private SshAdapter sshAdapter;
+ private SshAdapter sshAdapter;
- public void setSshAdapter(SshAdapter sshAdapter) {
- this.sshAdapter = sshAdapter;
- }
+ public void setSshAdapter(SshAdapter sshAdapter) {
+ this.sshAdapter = sshAdapter;
+ }
- @Override
- public void exec(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
- SshConnectionDetails connectionDetails = resolveConnectionDetails(params.get(PARAM_IN_connection_details));
- String command = params.get(PARAM_IN_command);
- logger.debug("=> Connecting to SSH server...");
- SshConnection sshConnection = sshAdapter.getConnection(connectionDetails.getHost(), connectionDetails.getPort(), connectionDetails.getUsername(), connectionDetails.getPassword());
- sshConnection.connect();
- try {
- logger.debug("=> Connected to SSH server...");
- logger.debug("=> Running SSH command...");
- long timeout = DEF_timeout;
- String stimeout = params.get(PARAM_IN_timeout);
- if ((stimeout != null && !stimeout.isEmpty())) {
- timeout = Long.parseLong(stimeout);
- }
- sshConnection.setExecTimeout(timeout);
- ByteArrayOutputStream stdout = new ByteArrayOutputStream();
- ByteArrayOutputStream stderr = new ByteArrayOutputStream();
- int status = sshConnection.execCommand(command, stdout, stderr);
- String stdoutRes = stdout.toString();
- String stderrRes = stderr.toString();
- logger.debug("=> executed SSH command");
- ctx.setAttribute(PARAM_OUT_status, String.format("%01d", status));
- ctx.setAttribute(PARAM_OUT_stdout, stdoutRes);
- ctx.setAttribute(PARAM_OUT_stderr, stderrRes);
- } finally {
- sshConnection.disconnect();
- }
- }
+ @Override
+ public void exec(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
+ SshConnectionDetails connectionDetails = resolveConnectionDetails(params.get(PARAM_IN_connection_details));
+ String command = params.get(PARAM_IN_command);
+ logger.debug("=> Connecting to SSH server...");
+ SshConnection sshConnection = sshAdapter.getConnection(connectionDetails.getHost(), connectionDetails.getPort(), connectionDetails.getUsername(), connectionDetails.getPassword());
+ sshConnection.connect();
+ try {
+ logger.debug("=> Connected to SSH server...");
+ logger.debug("=> Running SSH command...");
+ long timeout = DEF_timeout;
+ String stimeout = params.get(PARAM_IN_timeout);
+ if ((stimeout != null && !stimeout.isEmpty())) {
+ timeout = Long.parseLong(stimeout);
+ }
+ sshConnection.setExecTimeout(timeout);
+ ByteArrayOutputStream stdout = new ByteArrayOutputStream();
+ ByteArrayOutputStream stderr = new ByteArrayOutputStream();
+ int status = sshConnection.execCommand(command, stdout, stderr);
+ String stdoutRes = stdout.toString();
+ String stderrRes = stderr.toString();
+ logger.debug("=> executed SSH command");
+ ctx.setAttribute(PARAM_OUT_status, String.format("%01d", status));
+ ctx.setAttribute(PARAM_OUT_stdout, stdoutRes);
+ ctx.setAttribute(PARAM_OUT_stderr, stderrRes);
+ } finally {
+ sshConnection.disconnect();
+ }
+ }
- private SshConnectionDetails resolveConnectionDetails(String connectionDetailsStr) throws APPCException {
- SshConnectionDetails connectionDetails = null;
- try {
- connectionDetails = mapper.readValue(connectionDetailsStr, SshConnectionDetails.class);
- if (0 == connectionDetails.getPort()) connectionDetails.setPort(DEF_port);
- } catch (IOException e) {
- throw new APPCException(e);
- }
- return connectionDetails;
- }
+ private SshConnectionDetails resolveConnectionDetails(String connectionDetailsStr) throws APPCException {
+ SshConnectionDetails connectionDetails = null;
+ try {
+ connectionDetails = mapper.readValue(connectionDetailsStr, SshConnectionDetails.class);
+ if (0 == connectionDetails.getPort()) connectionDetails.setPort(DEF_port);
+ } catch (IOException e) {
+ throw new APPCException(e);
+ }
+ return connectionDetails;
+ }
- @Override
- public void execWithStatusCheck(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
- exec(params, ctx);
- int status = Integer.parseInt(ctx.getAttribute(PARAM_OUT_status));
- if(status != DEF_SUCCESS_STATUS) {
- StringBuilder errmsg = new StringBuilder();
- errmsg.append("SSH command returned error status [").append(status).append(']');
- String stderr = ctx.getAttribute(PARAM_OUT_stderr);
- if((stderr != null) && !stderr.isEmpty()) {
- errmsg.append(". Error: [").append(stderr).append(']');
- }
- throw new APPCException(errmsg.toString());
- }
- }
+ @Override
+ public void execWithStatusCheck(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
+ exec(params, ctx);
+ int status = Integer.parseInt(ctx.getAttribute(PARAM_OUT_status));
+ if(status != DEF_SUCCESS_STATUS) {
+ StringBuilder errmsg = new StringBuilder();
+ errmsg.append("SSH command returned error status [").append(status).append(']');
+ String stderr = ctx.getAttribute(PARAM_OUT_stderr);
+ if((stderr != null) && !stderr.isEmpty()) {
+ errmsg.append(". Error: [").append(stderr).append(']');
+ }
+ throw new APPCException(errmsg.toString());
+ }
+ }
}