From 3323c0829fa0492a5c7b3544a83cc24412c9934c Mon Sep 17 00:00:00 2001 From: Ganesh Chandrasekaran Date: Fri, 13 Jul 2018 09:41:32 +0900 Subject: saltstack adaptor fixes from Sonar Issue-ID: CCSDK-364 Change-Id: Ief9705370d84fcb99fcb718f03c31e6cbfd47363 Signed-off-by: Ganesh Chandrasekaran --- .../adaptors/saltstack/impl/ConnectionBuilder.java | 18 +- .../saltstack/impl/SaltstackAdapterImpl.java | 99 ++- .../saltstack/model/SaltstackMessageParser.java | 18 +- .../saltstack/model/SaltstackServerEmulator.java | 41 +- .../appc/adapter/impl/TestConnectionBuilder.java | 174 ----- .../adapter/impl/TestSaltstackAdapterImpl.java | 816 --------------------- ...TestSaltstackAdapterPropertiesProviderImpl.java | 323 -------- .../onap/appc/adapter/model/TestJsonParser.java | 74 -- .../appc/adapter/model/TestSaltstackAdapter.java | 80 -- .../ccsdk/adapter/impl/TestConnectionBuilder.java | 163 ++++ .../adapter/impl/TestSaltstackAdapterImpl.java | 802 ++++++++++++++++++++ ...TestSaltstackAdapterPropertiesProviderImpl.java | 318 ++++++++ .../onap/ccsdk/adapter/model/TestJsonParser.java | 74 ++ .../ccsdk/adapter/model/TestSaltstackAdapter.java | 76 ++ .../resources/org/onap/appc/default.properties | 111 --- .../resources/org/onap/ccsdk/default.properties | 111 +++ 16 files changed, 1630 insertions(+), 1668 deletions(-) delete mode 100644 saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestConnectionBuilder.java delete mode 100644 saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestSaltstackAdapterImpl.java delete mode 100644 saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java delete mode 100644 saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/model/TestJsonParser.java delete mode 100644 saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/model/TestSaltstackAdapter.java create mode 100644 saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestConnectionBuilder.java create mode 100644 saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterImpl.java create mode 100644 saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java create mode 100644 saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestJsonParser.java create mode 100644 saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestSaltstackAdapter.java delete mode 100644 saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/appc/default.properties create mode 100644 saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/ccsdk/default.properties (limited to 'saltstack-adapter/saltstack-adapter-provider/src') diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/ConnectionBuilder.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/ConnectionBuilder.java index 65ab598dd..cc4ce95c1 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/ConnectionBuilder.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/ConnectionBuilder.java @@ -84,7 +84,7 @@ public class ConnectionBuilder { * @param cmd Commands to execute * @return command execution status */ - public SaltstackResult connectNExecute(String cmd) { + public SaltstackResult connectNExecute(String cmd) throws IOException { return connectNExecute(cmd, -1, -1); } @@ -98,9 +98,12 @@ public class ConnectionBuilder { * @param retryCount number of count retry to make a SSH connection. * @return command execution status */ - public SaltstackResult connectNExecute(String cmd, int retryCount, int retryDelay) { + public SaltstackResult connectNExecute(String cmd, int retryCount, int retryDelay) + throws IOException{ SaltstackResult result = new SaltstackResult(); + OutputStream out = null; + OutputStream errs = null; try { if (retryCount != -1) { result = sshConnection.connectWithRetry(retryCount, retryDelay); @@ -112,12 +115,10 @@ public class ConnectionBuilder { } String outFilePath = "/tmp/" + RandomStringUtils.random(5, true, true); String errFilePath = "/tmp/" + RandomStringUtils.random(5, true, true); - OutputStream out = new FileOutputStream(outFilePath); - OutputStream errs = new FileOutputStream(errFilePath); + out = new FileOutputStream(outFilePath); + errs = new FileOutputStream(errFilePath); result = sshConnection.execCommand(cmd, out, errs); sshConnection.disconnect(); - out.close(); - errs.close(); if (result.getSshExitStatus() != 0) { return sortExitStatus(result.getSshExitStatus(), errFilePath, cmd); } @@ -130,6 +131,11 @@ public class ConnectionBuilder { logger.error("Caught Exception", io); result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue()); result.setStatusMessage(io.getMessage()); + } finally { + if( out != null ) + out.close(); + if( errs != null ) + errs.close(); } return result; } diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java index 84e5d4f19..0b6a5bb22 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java @@ -81,7 +81,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { private static final String SS_SERVER_HOSTNAME = "org.onap.appc.adapter.saltstack.host"; private static final String SS_SERVER_PORT = "org.onap.appc.adapter.saltstack.port"; private static final String SS_SERVER_USERNAME = "org.onap.appc.adapter.saltstack.userName"; - private static final String SS_SERVER_PASSWORD = "org.onap.appc.adapter.saltstack.userPasswd"; + private static final String SS_SERVER_PASSWD = "org.onap.appc.adapter.saltstack.userPasswd"; private static final String SS_SERVER_SSH_KEY = "org.onap.appc.adapter.saltstack.sshKey"; /** * The logger to be used @@ -186,7 +186,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { String sshHost = props.getProperty(SS_SERVER_HOSTNAME); String sshPort = props.getProperty(SS_SERVER_PORT); String sshUserName = props.getProperty(SS_SERVER_USERNAME); - String sshPassword = props.getProperty(SS_SERVER_PASSWORD); + String sshPassword = props.getProperty(SS_SERVER_PASSWD); sshClient = new ConnectionBuilder(sshHost, sshPort, sshUserName, sshPassword); } else if ("SSH_CERT".equalsIgnoreCase(clientType)) { // set path to keystore file @@ -200,13 +200,10 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { String sshKey = props.getProperty(SS_SERVER_SSH_KEY); String sshHost = props.getProperty(SS_SERVER_HOSTNAME); String sshUserName = props.getProperty(SS_SERVER_USERNAME); - String sshPassword = props.getProperty(SS_SERVER_PASSWORD); + String sshPassword = props.getProperty(SS_SERVER_PASSWD); String sshPort = props.getProperty(SS_SERVER_PORT); logger.info("Creating ssh client with ssh KEY from " + sshKey); sshClient = new ConnectionBuilder(sshHost, sshPort, sshUserName, sshPassword, sshKey); - } else if ("NONE".equalsIgnoreCase(clientType)) { - logger.info("No saltstack-adapter.properties defined so reading from DG props"); - sshClient = null; } else { logger.info("No saltstack-adapter.properties defined so reading from DG props"); sshClient = null; @@ -317,12 +314,17 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { boolean slsExec; SaltstackResult testResult; setSSHClient(params); - reqID = messageProcessor.reqId(params); - String commandToExecute = messageProcessor.reqCmd(params); - slsExec = messageProcessor.reqIsSLSExec(params); - testResult = execCommand(params, commandToExecute); - testResult = messageProcessor.parseResponse(ctx, reqID, testResult, slsExec); - checkResponseStatus(testResult, ctx, reqID, slsExec); + try { + reqID = messageProcessor.reqId(params); + String commandToExecute = messageProcessor.reqCmd(params); + slsExec = messageProcessor.reqIsSLSExec(params); + testResult = execCommand(ctx, params, commandToExecute); + testResult = messageProcessor.parseResponse(ctx, reqID, testResult, slsExec); + checkResponseStatus(testResult, ctx, reqID, slsExec); + } catch (IOException e) { + doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), + "IOException in file stream : "+ e.getMessage()); + } } /** @@ -338,13 +340,18 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { String reqID; SaltstackResult testResult; setSSHClient(params); - reqID = messageProcessor.reqId(params); - String slsName = messageProcessor.reqSlsName(params); - String applyTo = messageProcessor.reqApplyToDevices(params); - String commandToExecute = putToCommands(slsName, applyTo); - testResult = execCommand(params, commandToExecute); - testResult = messageProcessor.parseResponse(ctx, reqID, testResult, true); - checkResponseStatus(testResult, ctx, reqID, true); + try { + reqID = messageProcessor.reqId(params); + String slsName = messageProcessor.reqSlsName(params); + String applyTo = messageProcessor.reqApplyToDevices(params); + String commandToExecute = putToCommands(slsName, applyTo); + testResult = execCommand(ctx, params, commandToExecute); + testResult = messageProcessor.parseResponse(ctx, reqID, testResult, true); + checkResponseStatus(testResult, ctx, reqID, true); + } catch (IOException e) { + doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), + "IOException in file stream : "+ e.getMessage()); + } } /** @@ -360,13 +367,18 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { String reqID; SaltstackResult testResult; setSSHClient(params); - reqID = messageProcessor.reqId(params); - String slsFile = messageProcessor.reqSlsFile(params); - String applyTo = messageProcessor.reqApplyToDevices(params); - String commandToExecute = putToCommands(ctx, slsFile, applyTo); - testResult = execCommand(params, commandToExecute); - testResult = messageProcessor.parseResponse(ctx, reqID, testResult, true); - checkResponseStatus(testResult, ctx, reqID, true); + try { + reqID = messageProcessor.reqId(params); + String slsFile = messageProcessor.reqSlsFile(params); + String applyTo = messageProcessor.reqApplyToDevices(params); + String commandToExecute = putToCommands(ctx, slsFile, applyTo); + testResult = execCommand(ctx, params, commandToExecute); + testResult = messageProcessor.parseResponse(ctx, reqID, testResult, true); + checkResponseStatus(testResult, ctx, reqID, true); + } catch (IOException e) { + doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), + "IOException in file stream : "+ e.getMessage()); + } } /** @@ -382,22 +394,29 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { } - public SaltstackResult execCommand(Map params, String commandToExecute) { - SaltstackResult testResult; - if (params.get(CONNECTION_RETRY_DELAY) != null && params.get(CONNECTION_RETRY_COUNT) != null) { - int retryDelay = Integer.parseInt(params.get(CONNECTION_RETRY_DELAY)); - int retryCount = Integer.parseInt(params.get(CONNECTION_RETRY_COUNT)); - if (!testMode) { - testResult = sshClient.connectNExecute(commandToExecute, retryCount, retryDelay); - } else { - testResult = testServer.MockReqExec(params); - } - } else { - if (!testMode) { - testResult = sshClient.connectNExecute(commandToExecute); + public SaltstackResult execCommand(SvcLogicContext ctx, Map params, String commandToExecute) + throws SvcLogicException{ + + SaltstackResult testResult = new SaltstackResult(); + try { + if (params.get(CONNECTION_RETRY_DELAY) != null && params.get(CONNECTION_RETRY_COUNT) != null) { + int retryDelay = Integer.parseInt(params.get(CONNECTION_RETRY_DELAY)); + int retryCount = Integer.parseInt(params.get(CONNECTION_RETRY_COUNT)); + if (!testMode) { + testResult = sshClient.connectNExecute(commandToExecute, retryCount, retryDelay); + } else { + testResult = testServer.mockReqExec(params); + } } else { - testResult = testServer.MockReqExec(params); + if (!testMode) { + testResult = sshClient.connectNExecute(commandToExecute); + } else { + testResult = testServer.mockReqExec(params); + } } + } catch (IOException e) { + doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), + "IOException in file stream : "+ e.getMessage()); } return testResult; } diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackMessageParser.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackMessageParser.java index f282a3381..0a6e4eb89 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackMessageParser.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackMessageParser.java @@ -41,6 +41,7 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; import java.util.Collections; import java.util.HashSet; @@ -289,15 +290,16 @@ public class SaltstackMessageParser { * and returns an SaltstackResult object. */ public SaltstackResult parseResponse(SvcLogicContext ctx, String pfx, - SaltstackResult saltstackResult, boolean slsExec) { + SaltstackResult saltstackResult, boolean slsExec) throws IOException{ int code = saltstackResult.getStatusCode(); + InputStream in = null; boolean executionStatus = true, retCodeFound = false; if (code != SaltstackResultCodes.SUCCESS.getValue()) { return saltstackResult; } try { File file = new File(saltstackResult.getOutputFileName()); - InputStream in = new FileInputStream(file); + in = new FileInputStream(file); byte[] data = new byte[(int) file.length()]; in.read(data); String str = new String(data, "UTF-8"); @@ -324,6 +326,9 @@ public class SaltstackMessageParser { } catch (Exception e) { return new SaltstackResult(SaltstackResultCodes.INVALID_RESPONSE_FILE.getValue(), "error parsing response file " + saltstackResult.getOutputFileName() + " : " + e.getMessage()); + } finally { + if( in != null ) + in.close(); } if (slsExec) { if (!retCodeFound) @@ -337,10 +342,12 @@ public class SaltstackMessageParser { return saltstackResult; } - public SaltstackResult putToProperties(SvcLogicContext ctx, String pfx, SaltstackResult saltstackResult) { + public SaltstackResult putToProperties(SvcLogicContext ctx, String pfx, + SaltstackResult saltstackResult) throws IOException{ + InputStream in = null; try { File file = new File(saltstackResult.getOutputFileName()); - InputStream in = new FileInputStream(file); + in = new FileInputStream(file); Properties prop = new Properties(); prop.load(in); ctx.setAttribute(pfx + "completeResult", prop.toString()); @@ -355,6 +362,9 @@ public class SaltstackMessageParser { } catch (Exception e) { saltstackResult = new SaltstackResult(SaltstackResultCodes.INVALID_RESPONSE_FILE.getValue(), "Error parsing response file = " + saltstackResult.getOutputFileName() + ". Error = " + e.getMessage()); + } finally { + if( in != null ) + in.close(); } saltstackResult.setStatusCode(SaltstackResultCodes.FINAL_SUCCESS.getValue()); return saltstackResult; diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackServerEmulator.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackServerEmulator.java index ecb36fb83..adbf9bd9a 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackServerEmulator.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackServerEmulator.java @@ -55,7 +55,7 @@ public class SaltstackServerEmulator { * Returns an saltstack object result. The response code is always the ssh code 200 (i.e connection successful) * payload is json string as would be sent back by Saltstack Server **/ - public SaltstackResult MockReqExec(Map params) { + public SaltstackResult mockReqExec(Map params) { SaltstackResult result = new SaltstackResult(); try { @@ -75,45 +75,6 @@ public class SaltstackServerEmulator { return result; } - /** - * Method to emulate response from an Saltstack - * Server when presented with a GET request - * Returns an saltstack object result. The response code is always the ssh code 200 (i.e connection successful) - * payload is json string as would be sent back by Saltstack Server - **/ - public SaltstackResult Execute(String agentUrl) { - - Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*"); - Matcher matcher = pattern.matcher(agentUrl); - String id = StringUtils.EMPTY; - String vmAddress = "192.168.1.10"; - - if (matcher.find()) { - id = matcher.group(1); - } - - SaltstackResult getResult = new SaltstackResult(); - - JSONObject response = new JSONObject(); - response.put(STATUS_CODE, 200); - response.put(STATUS_MESSAGE, "FINISHED"); - - JSONObject results = new JSONObject(); - - JSONObject vmResults = new JSONObject(); - vmResults.put(STATUS_CODE, 200); - vmResults.put(STATUS_MESSAGE, "SUCCESS"); - vmResults.put("Id", id); - results.put(vmAddress, vmResults); - - response.put("Results", results); - - getResult.setStatusCode(200); - getResult.setStatusMessage(response.toString()); - - return getResult; - } - private SaltstackResult rejectRequest(SaltstackResult result, String Message) { result.setStatusCode(SaltstackResultCodes.REJECTED.getValue()); result.setStatusMessage("Rejected"); diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestConnectionBuilder.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestConnectionBuilder.java deleted file mode 100644 index d9a384141..000000000 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestConnectionBuilder.java +++ /dev/null @@ -1,174 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.adapter.impl; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.ccsdk.sli.adaptors.saltstack.impl.ConnectionBuilder; -import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResult; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; - -import java.util.HashMap; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - - -public class TestConnectionBuilder { - - private final String PENDING = "100"; - private final String SUCCESS = "400"; - private String message = "{\"Results\":{\"192.168.1.10\":{\"Id\":\"101\",\"StatusCode\":200,\"StatusMessage\":\"SUCCESS\"}},\"StatusCode\":200,\"StatusMessage\":\"FINISHED\"}"; - - private ConnectionBuilder connBuilder; - private String TestId; - private boolean testMode = true; - private Map params; - private SvcLogicContext svcContext; - - - @Before - public void setup() throws IllegalArgumentException { - testMode = true; - svcContext = new SvcLogicContext(); - String HostName = "test"; - String Port = "10"; - String User = "test"; - String Password = "test"; - connBuilder = new ConnectionBuilder(HostName, Port, User, Password); - - params = new HashMap<>(); - params.put("AgentUrl", "https://192.168.1.1"); - params.put("User", "test"); - params.put("Password", "test"); - } - - @After - public void tearDown() { - testMode = false; - connBuilder = null; - params = null; - svcContext = null; - } - - @Test - public void reqExecCommand_exitStatus255() { - - int exitStatus = 255; - String errFilePath = "src/test/resources/test.json"; - String command = "test"; - - SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); - int status = result.getStatusCode(); - assertEquals(698, status); - } - - @Test - public void reqExecCommand_exitStatus1() { - - int exitStatus = 1; - String errFilePath = "src/test/resources/test.json"; - String command = "test"; - - SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); - int status = result.getStatusCode(); - assertEquals(698, status); - } - - @Test - public void reqExecCommand_exitStatus5() { - - int exitStatus = 5; - String errFilePath = "src/test/resources/test.json"; - String command = "test"; - - SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); - int status = result.getStatusCode(); - assertEquals(613, status); - } - - @Test - public void reqExecCommand_exitStatus65() { - - int exitStatus = 65; - String errFilePath = "src/test/resources/test.json"; - String command = "test"; - - SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); - int status = result.getStatusCode(); - assertEquals(613, status); - } - - @Test - public void reqExecCommand_exitStatus67() { - - int exitStatus = 5; - String errFilePath = "src/test/resources/test.json"; - String command = "test"; - - SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); - int status = result.getStatusCode(); - assertEquals(613, status); - } - - @Test - public void reqExecCommand_exitStatus73() { - - int exitStatus = 65; - String errFilePath = "src/test/resources/test.json"; - String command = "test"; - - SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); - int status = result.getStatusCode(); - assertEquals(613, status); - } - - @Test - public void reqExecCommand_exitStatusUnknown() { - - int exitStatus = 5121; - String errFilePath = "src/test/resources/test.json"; - String command = "test"; - - SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); - int status = result.getStatusCode(); - assertEquals(699, status); - } - - @Test - public void reqExecCommand_exitStatusNoFile() { - - int exitStatus = 65; - String errFilePath = "src/test/resource/test.json"; - String command = "test"; - - SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); - int status = result.getStatusCode(); - assertEquals(613, status); - } -} diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestSaltstackAdapterImpl.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestSaltstackAdapterImpl.java deleted file mode 100644 index d60059e40..000000000 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestSaltstackAdapterImpl.java +++ /dev/null @@ -1,816 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.adapter.impl; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.ccsdk.sli.adaptors.saltstack.impl.SaltstackAdapterImpl; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; - -import java.util.HashMap; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - - -public class TestSaltstackAdapterImpl { - - private final String PENDING = "100"; - private final String SUCCESS = "400"; - private String message = "{\"Results\":{\"192.168.1.10\":{\"Id\":\"101\",\"StatusCode\":200,\"StatusMessage\":\"SUCCESS\"}},\"StatusCode\":200,\"StatusMessage\":\"FINISHED\"}"; - - private SaltstackAdapterImpl adapter; - private String TestId; - private boolean testMode = true; - private Map params; - private SvcLogicContext svcContext; - - - @Before - public void setup() throws IllegalArgumentException { - testMode = true; - svcContext = new SvcLogicContext(); - adapter = new SaltstackAdapterImpl(testMode); - - params = new HashMap<>(); - params.put("AgentUrl", "https://192.168.1.1"); - params.put("User", "test"); - params.put("Password", "test"); - } - - @After - public void tearDown() { - testMode = false; - adapter = null; - params = null; - svcContext = null; - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_shouldSetFailed() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "fail"); - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("101", status); - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_shouldSetUserFailed() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("Password", "test"); - params.put("Test", "fail"); - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("101", status); - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_shouldSetHostFailed() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "fail"); - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("101", status); - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_shouldSetPortFailed() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "fail"); - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("101", status); - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_shouldSetPasswordFailed() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Test", "fail"); - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("101", status); - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_shouldSetMandatoryFailed() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("Test", "fail"); - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("101", status); - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_NoResponseFile() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("cmd", "test"); - params.put("slsExec", "false"); - try { - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("400", status); - } catch (NullPointerException e) { - fail(e.getMessage() + " Unknown exception encountered "); - } - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_NoResponseFileWithRetry() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("retryDelay", "10"); - params.put("retryCount", "10"); - params.put("cmd", "test"); - params.put("slsExec", "false"); - try { - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("400", status); - } catch (NullPointerException e) { - fail(e.getMessage() + " Unknown exception encountered "); - } - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_NoResponseFileWithRetryZero() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("retryDelay", "0"); - params.put("retryCount", "0"); - params.put("cmd", "test"); - params.put("slsExec", "false"); - try { - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("400", status); - } catch (NullPointerException e) { - fail(e.getMessage() + " Unknown exception encountered "); - } - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_NoResponseFileWithNoRetry() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("retryDelay", "-1"); - params.put("retryCount", "-1"); - params.put("cmd", "test"); - params.put("slsExec", "false"); - - try { - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("400", status); - } catch (NullPointerException e) { - fail(e.getMessage() + " Unknown exception encountered "); - } - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_shouldSetFailure() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("cmd", "test"); - params.put("slsExec", "test"); - params.put("Test", "fail"); - try { - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("400", status); - } catch (NullPointerException e) { - fail(e.getMessage() + " Unknown exception encountered "); - } - } - - @Test - public void reqExecCommand_shouldSetSuccessNoSLS() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("fileName", "src/test/resources/test.json"); - params.put("Id", "test1"); - params.put("cmd", "test"); - params.put("slsExec", "false"); - - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("250", status); - assertEquals(TestId, "test1"); - } - - @Test - public void reqExecCommand_shouldSetSuccessExecSLS() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("fileName", "src/test/resources/test-sls.json"); - params.put("Id", "test1"); - params.put("cmd", "test"); - params.put("slsExec", "true"); - - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("200", status); - assertEquals(TestId, "test1"); - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_shouldSetFailExecSLS() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("fileName", "src/test/resources/test.json"); - params.put("Id", "test1"); - params.put("cmd", "test"); - params.put("slsExec", "true"); - - adapter.reqExecCommand(params, svcContext); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals(TestId, "test1"); - } - - @Test - public void reqExecCommand_shouldSetSuccessFileTxt() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("fileName", "src/test/resources/test.txt"); - params.put("Id", "txt"); - params.put("cmd", "test"); - params.put("slsExec", "false"); - - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("250", status); - assertEquals(TestId, "txt"); - } - - @Test - public void reqExecCommand_shouldSetSuccessFileNoExtension() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("fileName", "src/test/resources/test"); - params.put("Id", "txt"); - params.put("cmd", "test"); - params.put("slsExec", "false"); - - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("250", status); - assertEquals(TestId, "txt"); - } - - @Test - public void reqExecCommand_shouldSetSuccessFileInvalidJson() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("fileName", "src/test/resources/test-invalid.json"); - params.put("Id", "test1"); - params.put("cmd", "test"); - params.put("slsExec", "false"); - - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("250", status); - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_shouldSetFailFileInvalidFile() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("fileName", "src/test/resources/test-none.json"); - params.put("Id", "test1"); - - adapter.reqExecCommand(params, svcContext); - } - - @Test - public void reqExecCommand_shouldSetSuccessFileJsonNoReqID() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("fileName", "src/test/resources/test.json"); - params.put("cmd", "test"); - params.put("slsExec", "false"); - - adapter.reqExecCommand(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("250", status); - } - - @Test - public void reqExecSLSFile_shouldSetSuccessJson() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsFile", "src/test/resources/test.sls"); - params.put("fileName", "src/test/resources/test-sls.json"); - params.put("Id", "test1"); - params.put("cmd", "test"); - - adapter.reqExecSLSFile(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("200", status); - assertEquals(TestId, "test1"); - } - - @Test(expected = SvcLogicException.class) - public void reqExecSLSFile_NoSLSfile() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsFile", "src/test/resources/test-none.sls"); - params.put("fileName", "src/test/resources/test-sls.json"); - params.put("Id", "test1"); - - adapter.reqExecSLSFile(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals(TestId, "test1"); - } - - @Test(expected = SvcLogicException.class) - public void reqExecSLSFile_NoExtn() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsFile", "src/test/resources/test-none"); - params.put("fileName", "src/test/resources/test-sls.json"); - params.put("Id", "test1"); - - adapter.reqExecSLSFile(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals(TestId, "test1"); - } - - @Test(expected = SvcLogicException.class) - public void reqExecSLSFile_NoResponsefile() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsFile", "src/test/resources/test.json"); - params.put("fileName", "src/test/resources/test-none.json"); - params.put("Id", "test1"); - - adapter.reqExecSLSFile(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals(TestId, "test1"); - } - - @Test(expected = SvcLogicException.class) - public void reqExecSLSFile_WithMinionSetNotSLSType() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsFile", "src/test/resources/test.json"); - params.put("fileName", "src/test/resources/test-sls.json"); - params.put("Id", "test1"); - params.put("cmd", "test"); - params.put("applyTo", "minion1"); - - adapter.reqExecSLSFile(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("200", status); - assertEquals(TestId, "test1"); - } - - @Test - public void reqExecSLSFile_WithMinionSetSuccessSls() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsFile", "src/test/resources/test.sls"); - params.put("fileName", "src/test/resources/test-sls.json"); - params.put("Id", "test1"); - params.put("cmd", "test"); - params.put("applyTo", "minion1"); - - adapter.reqExecSLSFile(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("200", status); - assertEquals(TestId, "test1"); - } - - @Test(expected = SvcLogicException.class) - public void reqExecSLSFile_WithMinionNoSLSfile() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsFile", "src/test/resources/test-none.json"); - params.put("fileName", "src/test/resources/test-sls.json"); - params.put("Id", "test1"); - params.put("applyTo", "minion1"); - - adapter.reqExecSLSFile(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals(TestId, "test1"); - } - - @Test(expected = SvcLogicException.class) - public void reqExecSLSFile_WithMinionNoResponsefile() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsFile", "src/test/resources/test.json"); - params.put("fileName", "src/test/resources/test-none.json"); - params.put("Id", "test1"); - params.put("applyTo", "minion1"); - - adapter.reqExecSLSFile(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals(TestId, "test1"); - } - - @Test - public void reqExecSLSFile_WithAllMinionSetSuccessJson() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsFile", "src/test/resources/test.sls"); - params.put("fileName", "src/test/resources/test-sls.json"); - params.put("Id", "test1"); - params.put("cmd", "test"); - params.put("applyTo", "*"); - - adapter.reqExecSLSFile(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("200", status); - assertEquals(TestId, "test1"); - } - - @Test(expected = SvcLogicException.class) - public void reqExecSLSFile_WithAllMinionNoSLSfile() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsFile", "src/test/resources/test-none.json"); - params.put("fileName", "src/test/resources/test-sls.json"); - params.put("Id", "test1"); - params.put("applyTo", "*"); - - adapter.reqExecSLSFile(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals(TestId, "test1"); - } - - @Test(expected = SvcLogicException.class) - public void reqExecSLSFile_WithAllMinionNoResponsefile() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsFile", "src/test/resources/test.json"); - params.put("fileName", "src/test/resources/test-none.json"); - params.put("Id", "test1"); - params.put("applyTo", "*"); - - adapter.reqExecSLSFile(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals(TestId, "test1"); - } - - - @Test - public void reqExecSLS_shouldSetSuccessJson() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsName", "src/test.sls"); - params.put("fileName", "src/test/resources/test-sls.json"); - params.put("Id", "test1"); - params.put("cmd", "test"); - - adapter.reqExecSLS(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("200", status); - assertEquals(TestId, "test1"); - } - - @Test - public void reqExecSLS_shouldSetNoExtn() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsName", "src/test"); - params.put("fileName", "src/test/resources/test-sls.json"); - params.put("Id", "test1"); - params.put("cmd", "test"); - - adapter.reqExecSLS(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("200", status); - assertEquals(TestId, "test1"); - } - - @Test(expected = SvcLogicException.class) - public void reqExecSLS_NoResponsefile() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsName", "src/test/resources/test.json"); - params.put("fileName", "src/test/resources/test-none.json"); - params.put("Id", "test1"); - - adapter.reqExecSLS(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals(TestId, "test1"); - } - - - @Test - public void reqExecSLS_WithMinionSetSuccessSls() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsName", "src/test/resources/test.sls"); - params.put("fileName", "src/test/resources/test-sls.json"); - params.put("Id", "test1"); - params.put("cmd", "test"); - params.put("applyTo", "minion1"); - - adapter.reqExecSLS(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("200", status); - assertEquals(TestId, "test1"); - } - - - @Test(expected = SvcLogicException.class) - public void reqExecSLS_WithMinionNoResponsefile() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsName", "src/test/resources/test.json"); - params.put("fileName", "src/test/resources/test-none.json"); - params.put("Id", "test1"); - params.put("applyTo", "minion1"); - - adapter.reqExecSLS(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals(TestId, "test1"); - } - - @Test - public void reqExecSLS_WithAllMinionSetSuccessJson() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsName", "src/test/resources/test.sls"); - params.put("fileName", "src/test/resources/test-sls.json"); - params.put("Id", "test1"); - params.put("cmd", "test"); - params.put("applyTo", "*"); - - adapter.reqExecSLS(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals("200", status); - assertEquals(TestId, "test1"); - } - - - @Test(expected = SvcLogicException.class) - public void reqExecSLS_WithAllMinionNoResponsefile() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - - params.put("HostName", "test"); - params.put("Port", "10"); - params.put("User", "test"); - params.put("Password", "test"); - params.put("Test", "success"); - params.put("slsName", "src/test/resources/test.json"); - params.put("fileName", "src/test/resources/test-none.json"); - params.put("Id", "test1"); - params.put("applyTo", "*"); - - adapter.reqExecSLS(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); - TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); - assertEquals(TestId, "test1"); - } - - - @Test - public void reqExecLog_shouldSetMessage() throws IllegalStateException, IllegalArgumentException { - - params.put("Id", "101"); - - try { - adapter.reqExecLog(params, svcContext); - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.log"); - //assertEquals(message, status); - assertEquals(null, status); - } catch (SvcLogicException e) { - String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.log"); - fail(e.getMessage() + " Code = " + status); - } catch (Exception e) { - fail(e.getMessage() + " Unknown exception encountered "); - } - } -} diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java deleted file mode 100644 index 927591830..000000000 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java +++ /dev/null @@ -1,323 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.adapter.impl; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.ccsdk.sli.adaptors.saltstack.SaltstackAdapterPropertiesProvider; -import org.onap.ccsdk.sli.adaptors.saltstack.impl.SaltstackAdapterImpl; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; - -import java.util.Properties; - -import static org.junit.Assert.assertEquals; - -public class TestSaltstackAdapterPropertiesProviderImpl { - private final String PENDING = "100"; - private final String SUCCESS = "400"; - private String message = "{\"Results\":{\"192.168.1.10\":{\"Id\":\"101\",\"StatusCode\":200,\"StatusMessage\":\"SUCCESS\"}},\"StatusCode\":200,\"StatusMessage\":\"FINISHED\"}"; - - private SaltstackAdapterImpl adapter; - private Properties params; - private SvcLogicContext svcContext; - - - @Before - public void setup() throws IllegalArgumentException { - params = new Properties(); - } - - @After - public void tearDown() { - adapter = null; - params = null; - svcContext = null; - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_setPropertiesBasicPortNull() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("org.onap.appc.adapter.saltstack.clientType", "BASIC"); - params.put("User", "test"); - params.put("Password", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_setPropertiesBasicPortString() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("org.onap.appc.adapter.saltstack.clientType", "BASIC"); - params.put("org.onap.appc.adapter.saltstack.host", "test"); - params.put("org.onap.appc.adapter.saltstack.port", "test"); - params.put("org.onap.appc.adapter.saltstack.userName", "test"); - params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); - params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - } - - @Test - public void reqExecCommand_setPropertiesBasicSuccess() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("org.onap.appc.adapter.saltstack.clientType", "BASIC"); - params.put("org.onap.appc.adapter.saltstack.host", "test"); - params.put("org.onap.appc.adapter.saltstack.port", "10"); - params.put("org.onap.appc.adapter.saltstack.userName", "test"); - params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); - params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_setPropertiesSSH_CERTPortNull() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("org.onap.appc.adapter.saltstack.clientType", "SSH_CERT"); - params.put("User", "test"); - params.put("Password", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_setPropertiesSSH_CERTPortString() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("org.onap.appc.adapter.saltstack.clientType", "SSH_CERT"); - params.put("org.onap.appc.adapter.saltstack.host", "test"); - params.put("org.onap.appc.adapter.saltstack.port", "test"); - params.put("org.onap.appc.adapter.saltstack.userName", "test"); - params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); - params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - } - - @Test - public void reqExecCommand_setPropertiesSSH_CERTSuccess() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("org.onap.appc.adapter.saltstack.clientType", "SSH_CERT"); - params.put("org.onap.appc.adapter.saltstack.host", "test"); - params.put("org.onap.appc.adapter.saltstack.port", "10"); - params.put("org.onap.appc.adapter.saltstack.userName", "test"); - params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); - params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_setPropertiesBOTHPortNull() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("org.onap.appc.adapter.saltstack.clientType", "BOTH"); - params.put("User", "test"); - params.put("Password", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - } - - @Test(expected = SvcLogicException.class) - public void reqExecCommand_setPropertiesBOTHPortString() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("org.onap.appc.adapter.saltstack.clientType", "BOTH"); - params.put("org.onap.appc.adapter.saltstack.host", "test"); - params.put("org.onap.appc.adapter.saltstack.port", "test"); - params.put("org.onap.appc.adapter.saltstack.userName", "test"); - params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); - params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - } - - @Test - public void reqExecCommand_setPropertiesBOTHSuccess() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("org.onap.appc.adapter.saltstack.clientType", "BOTH"); - params.put("org.onap.appc.adapter.saltstack.host", "test"); - params.put("org.onap.appc.adapter.saltstack.port", "10"); - params.put("org.onap.appc.adapter.saltstack.userName", "test"); - params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); - params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - } - - @Test - public void reqExecCommand_setPropertiesNonePortNull() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("org.onap.appc.adapter.saltstack.clientType", "NONE"); - params.put("User", "test"); - params.put("Password", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - } - - @Test - public void reqExecCommand_setPropertiesNonePortString() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("org.onap.appc.adapter.saltstack.clientType", "NONE"); - params.put("org.onap.appc.adapter.saltstack.host", "test"); - params.put("org.onap.appc.adapter.saltstack.port", "test"); - params.put("org.onap.appc.adapter.saltstack.userName", "test"); - params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); - params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - } - - @Test - public void reqExecCommand_setPropertiesNoneSuccess() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("org.onap.appc.adapter.saltstack.clientType", "NONE"); - params.put("org.onap.appc.adapter.saltstack.host", "test"); - params.put("org.onap.appc.adapter.saltstack.port", "10"); - params.put("org.onap.appc.adapter.saltstack.userName", "test"); - params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); - params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - } - - - - @Test - public void reqExecCommand_setPropertiesElsePortNull() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("User", "test"); - params.put("Password", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - } - - @Test - public void reqExecCommand_setPropertiesElsePortString() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("org.onap.appc.adapter.saltstack.host", "test"); - params.put("org.onap.appc.adapter.saltstack.port", "test"); - params.put("org.onap.appc.adapter.saltstack.userName", "test"); - params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); - params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - } - - @Test - public void reqExecCommand_setPropertiesElseSuccess() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - params.put("org.onap.appc.adapter.saltstack.host", "test"); - params.put("org.onap.appc.adapter.saltstack.port", "10"); - params.put("org.onap.appc.adapter.saltstack.userName", "test"); - params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); - params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); - SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { - @Override - public Properties getProperties() { - return params; - } - }; - adapter = new SaltstackAdapterImpl(propProvider); - String adaptorName = adapter.getAdapterName(); - assertEquals("Saltstack Adapter", adaptorName); - adapter.setExecTimeout(10); - } - - @Test - public void reqExecCommand_setPropertiesDefault() throws SvcLogicException, - IllegalStateException, IllegalArgumentException { - adapter = new SaltstackAdapterImpl(); - } -} diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/model/TestJsonParser.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/model/TestJsonParser.java deleted file mode 100644 index ae13da6c8..000000000 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/model/TestJsonParser.java +++ /dev/null @@ -1,74 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * openECOMP : SDN-C - * ================================================================================ - * Copyright (C) 2017 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.adapter.model; - -import org.codehaus.jettison.json.JSONException; -import org.junit.Test; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; -import org.onap.ccsdk.sli.adaptors.saltstack.model.JsonParser; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -public class TestJsonParser { - - private static final Logger log = LoggerFactory.getLogger(TestJsonParser.class); - - @Test - public void test() throws SvcLogicException, IOException, JSONException { - BufferedReader in = new BufferedReader( - new InputStreamReader(ClassLoader.getSystemResourceAsStream("test.json")) - ); - StringBuilder b = new StringBuilder(); - String line; - while ((line = in.readLine()) != null) - b.append(line).append('\n'); - - Map mm = JsonParser.convertToProperties(b.toString()); - - logProperties(mm); - - in.close(); - } - - @Test(expected = NullPointerException.class) - public void testNullString() throws SvcLogicException, JSONException { - JsonParser.convertToProperties(null); - } - - private void logProperties(Map mm) { - List ll = new ArrayList<>(); - for (Object o : mm.keySet()) - ll.add((String) o); - Collections.sort(ll); - log.info("Properties:"); - for (String name : ll) - log.info("--- {}: {}", name, mm.get(name)); - } -} diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/model/TestSaltstackAdapter.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/model/TestSaltstackAdapter.java deleted file mode 100644 index 37b6502ca..000000000 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/model/TestSaltstackAdapter.java +++ /dev/null @@ -1,80 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ -package org.onap.appc.adapter.model; - -import org.junit.Test; -import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackMessageParser; -import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResult; -import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackServerEmulator; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -import static org.junit.Assert.assertNotNull; - -public class TestSaltstackAdapter { - - private Class[] parameterTypes; - private SaltstackMessageParser saltstackMessageParser; - private Method m; - private String name; - - @Test - public void callPrivateConstructorsMethodsForCodeCoverage() throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { - - /* test constructors */ - Class[] classesOne = {SaltstackMessageParser.class}; - for(Class clazz : classesOne) { - Constructor constructor = clazz.getDeclaredConstructor(); - name = constructor.getName(); - constructor.setAccessible(true); - assertNotNull(constructor.newInstance()); - } - Class[] classesTwo = {SaltstackServerEmulator.class}; - for(Class clazz : classesTwo) { - Constructor constructor = clazz.getDeclaredConstructor(); - name = constructor.getName(); - constructor.setAccessible(true); - assertNotNull(constructor.newInstance()); - } - Class[] classesThree = {SaltstackResult.class}; - for(Class clazz : classesThree) { - Constructor constructor = clazz.getDeclaredConstructor(); - name = constructor.getName(); - constructor.setAccessible(true); - assertNotNull(constructor.newInstance()); - } - - /* test methods */ - saltstackMessageParser = new SaltstackMessageParser(); - parameterTypes = new Class[1]; - parameterTypes[0] = String.class; - - m = saltstackMessageParser.getClass().getDeclaredMethod("getFilePayload", parameterTypes); - m.setAccessible(true); - assertNotNull(m.invoke(saltstackMessageParser,"{\"test\": test}")); - - } -} diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestConnectionBuilder.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestConnectionBuilder.java new file mode 100644 index 000000000..933f3fcf7 --- /dev/null +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestConnectionBuilder.java @@ -0,0 +1,163 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.adapter.impl; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.ccsdk.sli.adaptors.saltstack.impl.ConnectionBuilder; +import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResult; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + + +public class TestConnectionBuilder { + + private ConnectionBuilder connBuilder; + private Map params; + + + @Before + public void setup() throws IllegalArgumentException { + String HostName = "test"; + String Port = "10"; + String User = "test"; + String Password = "test"; + connBuilder = new ConnectionBuilder(HostName, Port, User, Password); + + params = new HashMap<>(); + params.put("AgentUrl", "https://192.168.1.1"); + params.put("User", "test"); + params.put("Password", "test"); + } + + @After + public void tearDown() { + connBuilder = null; + params = null; + } + + @Test + public void reqExecCommand_exitStatus255() { + + int exitStatus = 255; + String errFilePath = "src/test/resources/test.json"; + String command = "test"; + + SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); + int status = result.getStatusCode(); + assertEquals(698, status); + } + + @Test + public void reqExecCommand_exitStatus1() { + + int exitStatus = 1; + String errFilePath = "src/test/resources/test.json"; + String command = "test"; + + SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); + int status = result.getStatusCode(); + assertEquals(698, status); + } + + @Test + public void reqExecCommand_exitStatus5() { + + int exitStatus = 5; + String errFilePath = "src/test/resources/test.json"; + String command = "test"; + + SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); + int status = result.getStatusCode(); + assertEquals(613, status); + } + + @Test + public void reqExecCommand_exitStatus65() { + + int exitStatus = 65; + String errFilePath = "src/test/resources/test.json"; + String command = "test"; + + SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); + int status = result.getStatusCode(); + assertEquals(613, status); + } + + @Test + public void reqExecCommand_exitStatus67() { + + int exitStatus = 5; + String errFilePath = "src/test/resources/test.json"; + String command = "test"; + + SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); + int status = result.getStatusCode(); + assertEquals(613, status); + } + + @Test + public void reqExecCommand_exitStatus73() { + + int exitStatus = 65; + String errFilePath = "src/test/resources/test.json"; + String command = "test"; + + SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); + int status = result.getStatusCode(); + assertEquals(613, status); + } + + @Test + public void reqExecCommand_exitStatusUnknown() { + + int exitStatus = 5121; + String errFilePath = "src/test/resources/test.json"; + String command = "test"; + + SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); + int status = result.getStatusCode(); + assertEquals(699, status); + } + + @Test + public void reqExecCommand_exitStatusNoFile() { + + int exitStatus = 65; + String errFilePath = "src/test/resource/test.json"; + String command = "test"; + + SaltstackResult result = connBuilder.sortExitStatus(exitStatus, errFilePath, command); + int status = result.getStatusCode(); + assertEquals(613, status); + } +} diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterImpl.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterImpl.java new file mode 100644 index 000000000..0622a4716 --- /dev/null +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterImpl.java @@ -0,0 +1,802 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.adapter.impl; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.ccsdk.sli.adaptors.saltstack.impl.SaltstackAdapterImpl; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + + +public class TestSaltstackAdapterImpl { + + private SaltstackAdapterImpl adapter; + private String TestId; + private boolean testMode = true; + private Map params; + private SvcLogicContext svcContext; + + + @Before + public void setup() throws IllegalArgumentException { + testMode = true; + svcContext = new SvcLogicContext(); + adapter = new SaltstackAdapterImpl(testMode); + + params = new HashMap<>(); + params.put("AgentUrl", "https://192.168.1.1"); + params.put("User", "test"); + params.put("Password", "test"); + } + + @After + public void tearDown() { + testMode = false; + adapter = null; + params = null; + svcContext = null; + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_shouldSetFailed() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "fail"); + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("101", status); + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_shouldSetUserFailed() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("Password", "test"); + params.put("Test", "fail"); + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("101", status); + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_shouldSetHostFailed() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "fail"); + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("101", status); + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_shouldSetPortFailed() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "fail"); + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("101", status); + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_shouldSetPasswordFailed() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Test", "fail"); + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("101", status); + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_shouldSetMandatoryFailed() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("Test", "fail"); + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("101", status); + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_NoResponseFile() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("cmd", "test"); + params.put("slsExec", "false"); + try { + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("400", status); + } catch (NullPointerException e) { + fail(e.getMessage() + " Unknown exception encountered "); + } + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_NoResponseFileWithRetry() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("retryDelay", "10"); + params.put("retryCount", "10"); + params.put("cmd", "test"); + params.put("slsExec", "false"); + try { + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("400", status); + } catch (NullPointerException e) { + fail(e.getMessage() + " Unknown exception encountered "); + } + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_NoResponseFileWithRetryZero() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("retryDelay", "0"); + params.put("retryCount", "0"); + params.put("cmd", "test"); + params.put("slsExec", "false"); + try { + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("400", status); + } catch (NullPointerException e) { + fail(e.getMessage() + " Unknown exception encountered "); + } + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_NoResponseFileWithNoRetry() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("retryDelay", "-1"); + params.put("retryCount", "-1"); + params.put("cmd", "test"); + params.put("slsExec", "false"); + + try { + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("400", status); + } catch (NullPointerException e) { + fail(e.getMessage() + " Unknown exception encountered "); + } + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_shouldSetFailure() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("cmd", "test"); + params.put("slsExec", "test"); + params.put("Test", "fail"); + try { + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("400", status); + } catch (NullPointerException e) { + fail(e.getMessage() + " Unknown exception encountered "); + } + } + + @Test + public void reqExecCommand_shouldSetSuccessNoSLS() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("fileName", "src/test/resources/test.json"); + params.put("Id", "test1"); + params.put("cmd", "test"); + params.put("slsExec", "false"); + + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("250", status); + assertEquals(TestId, "test1"); + } + + @Test + public void reqExecCommand_shouldSetSuccessExecSLS() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("fileName", "src/test/resources/test-sls.json"); + params.put("Id", "test1"); + params.put("cmd", "test"); + params.put("slsExec", "true"); + + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_shouldSetFailExecSLS() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("fileName", "src/test/resources/test.json"); + params.put("Id", "test1"); + params.put("cmd", "test"); + params.put("slsExec", "true"); + + adapter.reqExecCommand(params, svcContext); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals(TestId, "test1"); + } + + @Test + public void reqExecCommand_shouldSetSuccessFileTxt() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("fileName", "src/test/resources/test.txt"); + params.put("Id", "txt"); + params.put("cmd", "test"); + params.put("slsExec", "false"); + + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("250", status); + assertEquals(TestId, "txt"); + } + + @Test + public void reqExecCommand_shouldSetSuccessFileNoExtension() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("fileName", "src/test/resources/test"); + params.put("Id", "txt"); + params.put("cmd", "test"); + params.put("slsExec", "false"); + + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("250", status); + assertEquals(TestId, "txt"); + } + + @Test + public void reqExecCommand_shouldSetSuccessFileInvalidJson() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("fileName", "src/test/resources/test-invalid.json"); + params.put("Id", "test1"); + params.put("cmd", "test"); + params.put("slsExec", "false"); + + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("250", status); + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_shouldSetFailFileInvalidFile() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("fileName", "src/test/resources/test-none.json"); + params.put("Id", "test1"); + + adapter.reqExecCommand(params, svcContext); + } + + @Test + public void reqExecCommand_shouldSetSuccessFileJsonNoReqID() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("fileName", "src/test/resources/test.json"); + params.put("cmd", "test"); + params.put("slsExec", "false"); + + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("250", status); + } + + @Test + public void reqExecSLSFile_shouldSetSuccessJson() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsFile", "src/test/resources/test.sls"); + params.put("fileName", "src/test/resources/test-sls.json"); + params.put("Id", "test1"); + params.put("cmd", "test"); + + adapter.reqExecSLSFile(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } + + @Test(expected = SvcLogicException.class) + public void reqExecSLSFile_NoSLSfile() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsFile", "src/test/resources/test-none.sls"); + params.put("fileName", "src/test/resources/test-sls.json"); + params.put("Id", "test1"); + + adapter.reqExecSLSFile(params, svcContext); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals(TestId, "test1"); + } + + @Test(expected = SvcLogicException.class) + public void reqExecSLSFile_NoExtn() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsFile", "src/test/resources/test-none"); + params.put("fileName", "src/test/resources/test-sls.json"); + params.put("Id", "test1"); + + adapter.reqExecSLSFile(params, svcContext); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals(TestId, "test1"); + } + + @Test(expected = SvcLogicException.class) + public void reqExecSLSFile_NoResponsefile() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsFile", "src/test/resources/test.json"); + params.put("fileName", "src/test/resources/test-none.json"); + params.put("Id", "test1"); + + adapter.reqExecSLSFile(params, svcContext); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals(TestId, "test1"); + } + + @Test(expected = SvcLogicException.class) + public void reqExecSLSFile_WithMinionSetNotSLSType() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsFile", "src/test/resources/test.json"); + params.put("fileName", "src/test/resources/test-sls.json"); + params.put("Id", "test1"); + params.put("cmd", "test"); + params.put("applyTo", "minion1"); + + adapter.reqExecSLSFile(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } + + @Test + public void reqExecSLSFile_WithMinionSetSuccessSls() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsFile", "src/test/resources/test.sls"); + params.put("fileName", "src/test/resources/test-sls.json"); + params.put("Id", "test1"); + params.put("cmd", "test"); + params.put("applyTo", "minion1"); + + adapter.reqExecSLSFile(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } + + @Test(expected = SvcLogicException.class) + public void reqExecSLSFile_WithMinionNoSLSfile() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsFile", "src/test/resources/test-none.json"); + params.put("fileName", "src/test/resources/test-sls.json"); + params.put("Id", "test1"); + params.put("applyTo", "minion1"); + + adapter.reqExecSLSFile(params, svcContext); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals(TestId, "test1"); + } + + @Test(expected = SvcLogicException.class) + public void reqExecSLSFile_WithMinionNoResponsefile() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsFile", "src/test/resources/test.json"); + params.put("fileName", "src/test/resources/test-none.json"); + params.put("Id", "test1"); + params.put("applyTo", "minion1"); + + adapter.reqExecSLSFile(params, svcContext); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals(TestId, "test1"); + } + + @Test + public void reqExecSLSFile_WithAllMinionSetSuccessJson() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsFile", "src/test/resources/test.sls"); + params.put("fileName", "src/test/resources/test-sls.json"); + params.put("Id", "test1"); + params.put("cmd", "test"); + params.put("applyTo", "*"); + + adapter.reqExecSLSFile(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } + + @Test(expected = SvcLogicException.class) + public void reqExecSLSFile_WithAllMinionNoSLSfile() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsFile", "src/test/resources/test-none.json"); + params.put("fileName", "src/test/resources/test-sls.json"); + params.put("Id", "test1"); + params.put("applyTo", "*"); + + adapter.reqExecSLSFile(params, svcContext); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals(TestId, "test1"); + } + + @Test(expected = SvcLogicException.class) + public void reqExecSLSFile_WithAllMinionNoResponsefile() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsFile", "src/test/resources/test.json"); + params.put("fileName", "src/test/resources/test-none.json"); + params.put("Id", "test1"); + params.put("applyTo", "*"); + + adapter.reqExecSLSFile(params, svcContext); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals(TestId, "test1"); + } + + + @Test + public void reqExecSLS_shouldSetSuccessJson() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsName", "src/test.sls"); + params.put("fileName", "src/test/resources/test-sls.json"); + params.put("Id", "test1"); + params.put("cmd", "test"); + + adapter.reqExecSLS(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } + + @Test + public void reqExecSLS_shouldSetNoExtn() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsName", "src/test"); + params.put("fileName", "src/test/resources/test-sls.json"); + params.put("Id", "test1"); + params.put("cmd", "test"); + + adapter.reqExecSLS(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } + + @Test(expected = SvcLogicException.class) + public void reqExecSLS_NoResponsefile() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsName", "src/test/resources/test.json"); + params.put("fileName", "src/test/resources/test-none.json"); + params.put("Id", "test1"); + + adapter.reqExecSLS(params, svcContext); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals(TestId, "test1"); + } + + + @Test + public void reqExecSLS_WithMinionSetSuccessSls() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsName", "src/test/resources/test.sls"); + params.put("fileName", "src/test/resources/test-sls.json"); + params.put("Id", "test1"); + params.put("cmd", "test"); + params.put("applyTo", "minion1"); + + adapter.reqExecSLS(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } + + + @Test(expected = SvcLogicException.class) + public void reqExecSLS_WithMinionNoResponsefile() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsName", "src/test/resources/test.json"); + params.put("fileName", "src/test/resources/test-none.json"); + params.put("Id", "test1"); + params.put("applyTo", "minion1"); + + adapter.reqExecSLS(params, svcContext); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals(TestId, "test1"); + } + + @Test + public void reqExecSLS_WithAllMinionSetSuccessJson() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsName", "src/test/resources/test.sls"); + params.put("fileName", "src/test/resources/test-sls.json"); + params.put("Id", "test1"); + params.put("cmd", "test"); + params.put("applyTo", "*"); + + adapter.reqExecSLS(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } + + + @Test(expected = SvcLogicException.class) + public void reqExecSLS_WithAllMinionNoResponsefile() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Test", "success"); + params.put("slsName", "src/test/resources/test.json"); + params.put("fileName", "src/test/resources/test-none.json"); + params.put("Id", "test1"); + params.put("applyTo", "*"); + + adapter.reqExecSLS(params, svcContext); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals(TestId, "test1"); + } + + + @Test + public void reqExecLog_shouldSetMessage() throws IllegalStateException, IllegalArgumentException { + + params.put("Id", "101"); + + try { + adapter.reqExecLog(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.log"); + //assertEquals(message, status); + assertEquals(null, status); + } catch (SvcLogicException e) { + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.log"); + fail(e.getMessage() + " Code = " + status); + } catch (Exception e) { + fail(e.getMessage() + " Unknown exception encountered "); + } + } +} diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java new file mode 100644 index 000000000..094e78c79 --- /dev/null +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java @@ -0,0 +1,318 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.adapter.impl; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.ccsdk.sli.adaptors.saltstack.SaltstackAdapterPropertiesProvider; +import org.onap.ccsdk.sli.adaptors.saltstack.impl.SaltstackAdapterImpl; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + +import java.util.Properties; + +import static org.junit.Assert.assertEquals; + +public class TestSaltstackAdapterPropertiesProviderImpl { + + private SaltstackAdapterImpl adapter; + private Properties params; + + + @Before + public void setup() throws IllegalArgumentException { + params = new Properties(); + } + + @After + public void tearDown() { + adapter = null; + params = null; + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_setPropertiesBasicPortNull() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("org.onap.appc.adapter.saltstack.clientType", "BASIC"); + params.put("User", "test"); + params.put("Password", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_setPropertiesBasicPortString() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("org.onap.appc.adapter.saltstack.clientType", "BASIC"); + params.put("org.onap.appc.adapter.saltstack.host", "test"); + params.put("org.onap.appc.adapter.saltstack.port", "test"); + params.put("org.onap.appc.adapter.saltstack.userName", "test"); + params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); + params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + } + + @Test + public void reqExecCommand_setPropertiesBasicSuccess() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("org.onap.appc.adapter.saltstack.clientType", "BASIC"); + params.put("org.onap.appc.adapter.saltstack.host", "test"); + params.put("org.onap.appc.adapter.saltstack.port", "10"); + params.put("org.onap.appc.adapter.saltstack.userName", "test"); + params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); + params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_setPropertiesSSH_CERTPortNull() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("org.onap.appc.adapter.saltstack.clientType", "SSH_CERT"); + params.put("User", "test"); + params.put("Password", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_setPropertiesSSH_CERTPortString() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("org.onap.appc.adapter.saltstack.clientType", "SSH_CERT"); + params.put("org.onap.appc.adapter.saltstack.host", "test"); + params.put("org.onap.appc.adapter.saltstack.port", "test"); + params.put("org.onap.appc.adapter.saltstack.userName", "test"); + params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); + params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + } + + @Test + public void reqExecCommand_setPropertiesSSH_CERTSuccess() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("org.onap.appc.adapter.saltstack.clientType", "SSH_CERT"); + params.put("org.onap.appc.adapter.saltstack.host", "test"); + params.put("org.onap.appc.adapter.saltstack.port", "10"); + params.put("org.onap.appc.adapter.saltstack.userName", "test"); + params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); + params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_setPropertiesBOTHPortNull() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("org.onap.appc.adapter.saltstack.clientType", "BOTH"); + params.put("User", "test"); + params.put("Password", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + } + + @Test(expected = SvcLogicException.class) + public void reqExecCommand_setPropertiesBOTHPortString() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("org.onap.appc.adapter.saltstack.clientType", "BOTH"); + params.put("org.onap.appc.adapter.saltstack.host", "test"); + params.put("org.onap.appc.adapter.saltstack.port", "test"); + params.put("org.onap.appc.adapter.saltstack.userName", "test"); + params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); + params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + } + + @Test + public void reqExecCommand_setPropertiesBOTHSuccess() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("org.onap.appc.adapter.saltstack.clientType", "BOTH"); + params.put("org.onap.appc.adapter.saltstack.host", "test"); + params.put("org.onap.appc.adapter.saltstack.port", "10"); + params.put("org.onap.appc.adapter.saltstack.userName", "test"); + params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); + params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + } + + @Test + public void reqExecCommand_setPropertiesNonePortNull() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("org.onap.appc.adapter.saltstack.clientType", "NONE"); + params.put("User", "test"); + params.put("Password", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + } + + @Test + public void reqExecCommand_setPropertiesNonePortString() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("org.onap.appc.adapter.saltstack.clientType", "NONE"); + params.put("org.onap.appc.adapter.saltstack.host", "test"); + params.put("org.onap.appc.adapter.saltstack.port", "test"); + params.put("org.onap.appc.adapter.saltstack.userName", "test"); + params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); + params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + } + + @Test + public void reqExecCommand_setPropertiesNoneSuccess() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("org.onap.appc.adapter.saltstack.clientType", "NONE"); + params.put("org.onap.appc.adapter.saltstack.host", "test"); + params.put("org.onap.appc.adapter.saltstack.port", "10"); + params.put("org.onap.appc.adapter.saltstack.userName", "test"); + params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); + params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + } + + + + @Test + public void reqExecCommand_setPropertiesElsePortNull() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("User", "test"); + params.put("Password", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + } + + @Test + public void reqExecCommand_setPropertiesElsePortString() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("org.onap.appc.adapter.saltstack.host", "test"); + params.put("org.onap.appc.adapter.saltstack.port", "test"); + params.put("org.onap.appc.adapter.saltstack.userName", "test"); + params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); + params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + } + + @Test + public void reqExecCommand_setPropertiesElseSuccess() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params.put("org.onap.appc.adapter.saltstack.host", "test"); + params.put("org.onap.appc.adapter.saltstack.port", "10"); + params.put("org.onap.appc.adapter.saltstack.userName", "test"); + params.put("org.onap.appc.adapter.saltstack.userPasswd", "test"); + params.put("org.onap.appc.adapter.saltstack.sshKey", "test"); + SaltstackAdapterPropertiesProvider propProvider = new SaltstackAdapterPropertiesProvider() { + @Override + public Properties getProperties() { + return params; + } + }; + adapter = new SaltstackAdapterImpl(propProvider); + String adaptorName = adapter.getAdapterName(); + assertEquals("Saltstack Adapter", adaptorName); + adapter.setExecTimeout(10); + } + + @Test + public void reqExecCommand_setPropertiesDefault() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + adapter = new SaltstackAdapterImpl(); + } +} diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestJsonParser.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestJsonParser.java new file mode 100644 index 000000000..74e7ed0c4 --- /dev/null +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestJsonParser.java @@ -0,0 +1,74 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : SDN-C + * ================================================================================ + * Copyright (C) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.adapter.model; + +import org.codehaus.jettison.json.JSONException; +import org.junit.Test; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.adaptors.saltstack.model.JsonParser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class TestJsonParser { + + private static final Logger log = LoggerFactory.getLogger(TestJsonParser.class); + + @Test + public void test() throws SvcLogicException, IOException, JSONException { + BufferedReader in = new BufferedReader( + new InputStreamReader(ClassLoader.getSystemResourceAsStream("test.json")) + ); + StringBuilder b = new StringBuilder(); + String line; + while ((line = in.readLine()) != null) + b.append(line).append('\n'); + + Map mm = JsonParser.convertToProperties(b.toString()); + + logProperties(mm); + + in.close(); + } + + @Test(expected = NullPointerException.class) + public void testNullString() throws SvcLogicException, JSONException { + JsonParser.convertToProperties(null); + } + + private void logProperties(Map mm) { + List ll = new ArrayList<>(); + for (Object o : mm.keySet()) + ll.add((String) o); + Collections.sort(ll); + log.info("Properties:"); + for (String name : ll) + log.info("--- {}: {}", name, mm.get(name)); + } +} diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestSaltstackAdapter.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestSaltstackAdapter.java new file mode 100644 index 000000000..0caaf3320 --- /dev/null +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestSaltstackAdapter.java @@ -0,0 +1,76 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ +package org.onap.ccsdk.adapter.model; + +import org.junit.Test; +import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackMessageParser; +import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResult; +import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackServerEmulator; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import static org.junit.Assert.assertNotNull; + +public class TestSaltstackAdapter { + + private Class[] parameterTypes; + private SaltstackMessageParser saltstackMessageParser; + private Method m; + + @Test + public void callPrivateConstructorsMethodsForCodeCoverage() throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { + + /* test constructors */ + Class[] classesOne = {SaltstackMessageParser.class}; + for(Class clazz : classesOne) { + Constructor constructor = clazz.getDeclaredConstructor(); + constructor.setAccessible(true); + assertNotNull(constructor.newInstance()); + } + Class[] classesTwo = {SaltstackServerEmulator.class}; + for(Class clazz : classesTwo) { + Constructor constructor = clazz.getDeclaredConstructor(); + constructor.setAccessible(true); + assertNotNull(constructor.newInstance()); + } + Class[] classesThree = {SaltstackResult.class}; + for(Class clazz : classesThree) { + Constructor constructor = clazz.getDeclaredConstructor(); + constructor.setAccessible(true); + assertNotNull(constructor.newInstance()); + } + + /* test methods */ + saltstackMessageParser = new SaltstackMessageParser(); + parameterTypes = new Class[1]; + parameterTypes[0] = String.class; + + m = saltstackMessageParser.getClass().getDeclaredMethod("getFilePayload", parameterTypes); + m.setAccessible(true); + assertNotNull(m.invoke(saltstackMessageParser,"{\"test\": test}")); + + } +} diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/appc/default.properties b/saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/appc/default.properties deleted file mode 100644 index 2f8fb4585..000000000 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/appc/default.properties +++ /dev/null @@ -1,111 +0,0 @@ -### -# ============LICENSE_START======================================================= -# ONAP : APPC -# ================================================================================ -# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. -# ================================================================================ -# Copyright (C) 2017 Amdocs -# ============================================================================= -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# ECOMP is a trademark and service mark of AT&T Intellectual Property. -# ============LICENSE_END========================================================= -### - -# -# Default properties for the APP-C Provider Adapter -# -# ------------------------------------------------------------------------------------------------- -# -# Define the name and path of any user-provided configuration (bootstrap) file that can be loaded -# to supply configuration options -org.onap.appc.bootstrap.file=appc.properties -org.onap.appc.bootstrap.path=/opt/onap/appc/data/properties,${user.home},. - -appc.application.name=APPC - -# -# Define the message resource bundle name to be loaded -org.onap.appc.resources=org/onap/appc/i18n/MessageResources -# -# The name of the adapter. -org.onap.appc.provider.adaptor.name=org.onap.appc.appc_provider_adapter -# -# Set up the logging environment -# -org.onap.appc.logging.file=org/onap/appc/logback.xml -org.onap.appc.logging.path=${user.home};etc;../etc -org.onap.appc.logger=org.onap.appc -org.onap.appc.security.logger=org.onap.appc.security -# -# The minimum and maximum provider/tenant context pool sizes. Min=1 means that as soon -# as the provider/tenant is referenced a Context is opened and added to the pool. Max=0 -# means that the upper bound on the pool is unbounded. -org.onap.appc.provider.min.pool=1 -org.onap.appc.provider.max.pool=0 - -# -# The following properties are used to configure the retry logic for connection to the -# IaaS provider(s). The retry delay property is the amount of time, in seconds, the -# application waits between retry attempts. The retry limit is the number of retries -# that are allowed before the request is failed. -org.onap.appc.provider.retry.delay = 30 -org.onap.appc.provider.retry.limit = 10 - -# -# The trusted hosts list for SSL access when a certificate is not provided. -# -provider.trusted.hosts=* -# -# The amount of time, in seconds, to wait for a server state change (start->stop, stop->start, etc). -# If the server does not change state to a valid state within the alloted time, the operation -# fails. -org.onap.appc.server.state.change.timeout=300 -# -# The amount of time to wait, in seconds, between subsequent polls to the OpenStack provider -# to refresh the status of a resource we are waiting on. -# -org.onap.appc.openstack.poll.interval=20 -# -# The connection information to connect to the provider we are using. These properties -# are "structured" properties, in that the name is a compound name, where the nodes -# of the name can be ordered (1, 2, 3, ...). All of the properties with the same ordinal -# position are defining the same entity. For example, provider1.type and provider1.name -# are defining the same provider, whereas provider2.name and provider2.type are defining -# the values for a different provider. Any number of providers can be defined in this -# way. -# - -# Don't change these 2 right now since they are hard coded in the DG -#provider1.type=appc -#provider1.name=appc - -#These you can change -#provider1.identity=appc -#provider1.tenant1.name=appc -#provider1.tenant1.userid=appc -#provider1.tenant1.password=appc - -# After a change to the provider make sure to recheck these values with an api call to provider1.identity/tokens -test.expected-regions=1 -test.expected-endpoints=1 - -#Your OpenStack IP -#test.ip=192.168.1.2 -# Your OpenStack Platform's Keystone Port (default is 5000) -#test.port=5000 -#test.tenantid=abcde12345fghijk6789lmnopq123rst -#test.vmid=abc12345-1234-5678-890a-abcdefg12345 -# Port 8774 below is default port for OpenStack's Nova API Service -#test.url=http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345 - diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/ccsdk/default.properties b/saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/ccsdk/default.properties new file mode 100644 index 000000000..2f8fb4585 --- /dev/null +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/ccsdk/default.properties @@ -0,0 +1,111 @@ +### +# ============LICENSE_START======================================================= +# ONAP : APPC +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Copyright (C) 2017 Amdocs +# ============================================================================= +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. +# ============LICENSE_END========================================================= +### + +# +# Default properties for the APP-C Provider Adapter +# +# ------------------------------------------------------------------------------------------------- +# +# Define the name and path of any user-provided configuration (bootstrap) file that can be loaded +# to supply configuration options +org.onap.appc.bootstrap.file=appc.properties +org.onap.appc.bootstrap.path=/opt/onap/appc/data/properties,${user.home},. + +appc.application.name=APPC + +# +# Define the message resource bundle name to be loaded +org.onap.appc.resources=org/onap/appc/i18n/MessageResources +# +# The name of the adapter. +org.onap.appc.provider.adaptor.name=org.onap.appc.appc_provider_adapter +# +# Set up the logging environment +# +org.onap.appc.logging.file=org/onap/appc/logback.xml +org.onap.appc.logging.path=${user.home};etc;../etc +org.onap.appc.logger=org.onap.appc +org.onap.appc.security.logger=org.onap.appc.security +# +# The minimum and maximum provider/tenant context pool sizes. Min=1 means that as soon +# as the provider/tenant is referenced a Context is opened and added to the pool. Max=0 +# means that the upper bound on the pool is unbounded. +org.onap.appc.provider.min.pool=1 +org.onap.appc.provider.max.pool=0 + +# +# The following properties are used to configure the retry logic for connection to the +# IaaS provider(s). The retry delay property is the amount of time, in seconds, the +# application waits between retry attempts. The retry limit is the number of retries +# that are allowed before the request is failed. +org.onap.appc.provider.retry.delay = 30 +org.onap.appc.provider.retry.limit = 10 + +# +# The trusted hosts list for SSL access when a certificate is not provided. +# +provider.trusted.hosts=* +# +# The amount of time, in seconds, to wait for a server state change (start->stop, stop->start, etc). +# If the server does not change state to a valid state within the alloted time, the operation +# fails. +org.onap.appc.server.state.change.timeout=300 +# +# The amount of time to wait, in seconds, between subsequent polls to the OpenStack provider +# to refresh the status of a resource we are waiting on. +# +org.onap.appc.openstack.poll.interval=20 +# +# The connection information to connect to the provider we are using. These properties +# are "structured" properties, in that the name is a compound name, where the nodes +# of the name can be ordered (1, 2, 3, ...). All of the properties with the same ordinal +# position are defining the same entity. For example, provider1.type and provider1.name +# are defining the same provider, whereas provider2.name and provider2.type are defining +# the values for a different provider. Any number of providers can be defined in this +# way. +# + +# Don't change these 2 right now since they are hard coded in the DG +#provider1.type=appc +#provider1.name=appc + +#These you can change +#provider1.identity=appc +#provider1.tenant1.name=appc +#provider1.tenant1.userid=appc +#provider1.tenant1.password=appc + +# After a change to the provider make sure to recheck these values with an api call to provider1.identity/tokens +test.expected-regions=1 +test.expected-endpoints=1 + +#Your OpenStack IP +#test.ip=192.168.1.2 +# Your OpenStack Platform's Keystone Port (default is 5000) +#test.port=5000 +#test.tenantid=abcde12345fghijk6789lmnopq123rst +#test.vmid=abc12345-1234-5678-890a-abcdefg12345 +# Port 8774 below is default port for OpenStack's Nova API Service +#test.url=http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345 + -- cgit 1.2.3-korg