From 0190a9cfa67b3470d6cbdab2277f1b68a53d84bb Mon Sep 17 00:00:00 2001 From: Joss Armstrong Date: Thu, 6 Dec 2018 17:17:46 +0000 Subject: [PATCH] Fix for APPC-1270 Reduced testing time from 90 seconds to 2 seconds Removed code smell for multiple instance/static references Added assertions and coverage for success scenarios for all methods Line coverage increased from 55% to 84% Moved construction of objects to protected methods to facilitate testing Issue-ID: APPC-1270 Change-Id: I7945c069c703d2ea3fac9ffa33927856170656c9 Signed-off-by: Joss Armstrong --- .../appc/ccadaptor/ConfigComponentAdaptor.java | 69 +-- .../appc/ccadaptor/ConfigComponentAdaptorTest.java | 468 ++++++++++++--------- 2 files changed, 318 insertions(+), 219 deletions(-) diff --git a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/ConfigComponentAdaptor.java b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/ConfigComponentAdaptor.java index a224b7f65..62e1cbc8f 100644 --- a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/ConfigComponentAdaptor.java +++ b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/ConfigComponentAdaptor.java @@ -6,6 +6,8 @@ * ============================================================================= * Modifications Copyright (C) 2018 IBM. * ============================================================================= + * Modifications Copyright (C) 2018 Ericsson + * ============================================================================= * 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 @@ -35,11 +37,9 @@ import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.UncheckedIOException; import java.net.HttpURLConnection; import java.util.HashMap; import java.util.Map; -import java.util.Map.Entry; import java.util.NoSuchElementException; import java.util.Properties; import java.util.StringTokenizer; @@ -121,12 +121,12 @@ public ConfigStatus configure(String key, Map parameters, SvcLog HttpResponse r = new HttpResponse(); r.code = 200; log.debug("ConfigComponentAdaptor.configure - key = " + key); - debugLog.printRTAriDebug(fnName, "key = " + key); + DebugLog.printRTAriDebug(fnName, "key = " + key); log.debug("Parameters:"); - debugLog.printRTAriDebug(fnName, "Parameters:"); + DebugLog.printRTAriDebug(fnName, "Parameters:"); for (String parmName : parameters.keySet()) { log.debug(" " + parmName + " = " + parameters.get(parmName)); - debugLog.printRTAriDebug(fnName, " " + parmName + " = " + parameters.get(parmName)); + DebugLog.printRTAriDebug(fnName, " " + parmName + " = " + parameters.get(parmName)); } String parmval = parameters.get("config-component-configUrl"); @@ -198,22 +198,22 @@ public ConfigStatus configure(String key, Map parameters, SvcLog String fullPathFileName = parameters.get("fullPathFileName"); String data = null; - SshJcraftWrapper sshJcraftWrapper = new SshJcraftWrapper(); + SshJcraftWrapper sshJcraftWrapper = getSshJcraftWrapper(); log.debug("SCP: SshJcraftWrapper has been instantiated"); - debugLog.printRTAriDebug(fnName, "SCP: SshJcraftWrapper has been instantiated"); + DebugLog.printRTAriDebug(fnName, "SCP: SshJcraftWrapper has been instantiated"); try { if (key.equals("put")) { data = parameters.get("data"); if (data != null) { - debugLog.printRTAriDebug(fnName, "Command is for put: Length of data is: " + data.length()); + DebugLog.printRTAriDebug(fnName, "Command is for put: Length of data is: " + data.length()); InputStream is = new ByteArrayInputStream(data.getBytes()); log.debug("SCP: Doing a put: fullPathFileName=" + fullPathFileName); - debugLog.printRTAriDebug(fnName, "SCP: Doing a put: fullPathFileName=" + fullPathFileName); + DebugLog.printRTAriDebug(fnName, "SCP: Doing a put: fullPathFileName=" + fullPathFileName); sshJcraftWrapper.put(is, fullPathFileName, host, loginId, password); try { - debugLog.printRTAriDebug(fnName, "Sleeping for 180 seconds...."); + DebugLog.printRTAriDebug(fnName, "Sleeping for 180 seconds...."); Thread.sleep(1000 * 180); - debugLog.printRTAriDebug(fnName, "Woke up...."); + DebugLog.printRTAriDebug(fnName, "Woke up...."); } catch (java.lang.InterruptedException ee) { boolean ignore = true; } @@ -224,9 +224,9 @@ public ConfigStatus configure(String key, Map parameters, SvcLog } else // Must be a get { log.debug("SCP: Doing a get: fullPathFileName=" + fullPathFileName); - debugLog.printRTAriDebug(fnName, "SCP: Doing a get: fullPathFileName=" + fullPathFileName); + DebugLog.printRTAriDebug(fnName, "SCP: Doing a get: fullPathFileName=" + fullPathFileName); String response = sshJcraftWrapper.get(fullPathFileName, host, loginId, password); - debugLog.printRTAriDebug(fnName, "Got the response and putting into the ctx object"); + DebugLog.printRTAriDebug(fnName, "Got the response and putting into the ctx object"); ctx.setAttribute("fileContents", response); log.debug("SCP: Closing the SFTP connection"); } @@ -248,11 +248,11 @@ public ConfigStatus configure(String key, Map parameters, SvcLog password = EncryptionTool.getInstance().decrypt(password); String cliCommand = parameters.get("cli"); String portNumber = parameters.get("portNumber"); - SshJcraftWrapper sshJcraftWrapper = new SshJcraftWrapper(); + SshJcraftWrapper sshJcraftWrapper = getSshJcraftWrapper(); try { log.debug("CLI: Attempting to login: host=" + host + " loginId=" + loginId + " portNumber=" + portNumber); - debugLog.printRTAriDebug(fnName, "CLI: Attempting to login: host=" + host + " loginId=" + loginId + + DebugLog.printRTAriDebug(fnName, "CLI: Attempting to login: host=" + host + " loginId=" + loginId + " portNumber=" + portNumber); sshJcraftWrapper.connect(host, loginId, password, Integer.parseInt(portNumber)); @@ -293,7 +293,7 @@ public ConfigStatus configure(String key, Map parameters, SvcLog return (setResponseStatus(ctx, r)); } if (key.equals("GetCliRunningConfig")) { - debugLog.printRTAriDebug(fnName, "key was: GetCliRunningConfig: "); + DebugLog.printRTAriDebug(fnName, "key was: GetCliRunningConfig: "); log.debug("key was: GetCliRunningConfig: "); String User_name = parameters.get("User_name"); String Host_ip_address = parameters.get("Host_ip_address"); @@ -301,9 +301,9 @@ public ConfigStatus configure(String key, Map parameters, SvcLog Password = EncryptionTool.getInstance().decrypt(Password); String Port_number = parameters.get("Port_number"); String Get_config_template = parameters.get("Get_config_template"); - SshJcraftWrapper sshJcraftWrapper = new SshJcraftWrapper(); + SshJcraftWrapper sshJcraftWrapper = getSshJcraftWrapper(); log.debug("GetCliRunningConfig: sshJcraftWrapper was instantiated"); - debugLog.printRTAriDebug(fnName, "GetCliRunningConfig: sshJcraftWrapper was instantiated"); + DebugLog.printRTAriDebug(fnName, "GetCliRunningConfig: sshJcraftWrapper was instantiated"); try { DebugLog.printAriDebug(fnName, "GetCliRunningConfig: User_name=" + User_name + " Host_ip_address=" + Host_ip_address + " Port_number=" @@ -395,7 +395,7 @@ public ConfigStatus configure(String key, Map parameters, SvcLog log(fnName, "xml-download: User_name=" + User_name + " Host_ip_address=" + Host_ip_address + " Port_number=" + Port_number); - SshJcraftWrapper sshJcraftWrapper = new SshJcraftWrapper(); + SshJcraftWrapper sshJcraftWrapper = getSshJcraftWrapper(); try { sshJcraftWrapper.connect(Host_ip_address, User_name, @@ -472,7 +472,7 @@ public ConfigStatus configure(String key, Map parameters, SvcLog log(fnName, "xml-getrunningconfig: User_name=" + User_name + " Host_ip_address=" + Host_ip_address + " Port_number=" + Port_number); - SshJcraftWrapper sshJcraftWrapper = new SshJcraftWrapper(); + SshJcraftWrapper sshJcraftWrapper = getSshJcraftWrapper(); try { String NetconfHelloCmd = netconfHelloCmd; sshJcraftWrapper.connect(Host_ip_address, @@ -509,7 +509,7 @@ public ConfigStatus configure(String key, Map parameters, SvcLog } } if (key.equals("DownloadCliConfig")) { - debugLog.printRTAriDebug(fnName, "key was: DownloadCliConfig: "); + DebugLog.printRTAriDebug(fnName, "key was: DownloadCliConfig: "); log.debug("key was: DownloadCliConfig: "); String User_name = parameters.get("User_name"); String Host_ip_address = parameters.get("Host_ip_address"); @@ -519,9 +519,9 @@ public ConfigStatus configure(String key, Map parameters, SvcLog String Download_config_template = parameters.get("Download_config_template"); String Config_contents = parameters.get("Config_contents"); DebugLog.printAriDebug(fnName, "Contents of the 'Config_contents' are: " + Config_contents); - SshJcraftWrapper sshJcraftWrapper = new SshJcraftWrapper(); + SshJcraftWrapper sshJcraftWrapper = getSshJcraftWrapper(); log.debug("DownloadCliConfig: sshJcraftWrapper was instantiated"); - debugLog.printRTAriDebug(fnName, "DownloadCliConfig: sshJcraftWrapper was instantiated"); + DebugLog.printRTAriDebug(fnName, "DownloadCliConfig: sshJcraftWrapper was instantiated"); int timeout = 4 * 60 * 1000; try { DebugLog.printAriDebug(fnName, "DownloadCliConfig: User_name=" + User_name + @@ -601,13 +601,13 @@ public ConfigStatus configure(String key, Map parameters, SvcLog } } - debugLog.printRTAriDebug(fnName, "Unsupported action - " + action); + DebugLog.printRTAriDebug(fnName, "Unsupported action - " + action); log.error("Unsupported action - " + action); return ConfigStatus.FAILURE; } private void log(String fileName, String messg) { - debugLog.printRTAriDebug(fileName, messg); + DebugLog.printRTAriDebug(fileName, messg); log.debug(fileName + ": " + messg); } @@ -910,7 +910,7 @@ private String expandRepeats(SvcLogicContext ctx, String template, int level) { private HttpResponse sendXmlRequest(String xmlRequest, String url, String user, String password) { try { - Client client = Client.create(); + Client client = getClient(); client.setConnectTimeout(5000); WebResource webResource = client.resource(url); @@ -922,9 +922,7 @@ private HttpResponse sendXmlRequest(String xmlRequest, String url, String user, String authStringEnc = new String(authEncBytes); authString = "Basic " + authStringEnc; - ClientResponse response = - webResource.header("Authorization", authString).accept("UTF-8").type("application/xml").post( - ClientResponse.class, xmlRequest); + ClientResponse response = getClientResponse(webResource, authString, xmlRequest); int code = response.getStatus(); String message = null; @@ -1038,4 +1036,17 @@ public static void main(String args[]) throws Exception { System.out.println("*************************TRACE 1*****************************"); cca.configure(key, parameters, ctx); } + +protected SshJcraftWrapper getSshJcraftWrapper() { + return new SshJcraftWrapper(); +} + +protected Client getClient() { + return Client.create(); +} + +protected ClientResponse getClientResponse(WebResource webResource, String authString, String xmlRequest) { + return webResource.header("Authorization", authString).accept("UTF-8").type("application/xml").post( + ClientResponse.class, xmlRequest); +} } diff --git a/appc-config/appc-config-adaptor/provider/src/test/java/org/onap/appc/ccadaptor/ConfigComponentAdaptorTest.java b/appc-config/appc-config-adaptor/provider/src/test/java/org/onap/appc/ccadaptor/ConfigComponentAdaptorTest.java index 1cf9b8ecf..eae53a836 100644 --- a/appc-config/appc-config-adaptor/provider/src/test/java/org/onap/appc/ccadaptor/ConfigComponentAdaptorTest.java +++ b/appc-config/appc-config-adaptor/provider/src/test/java/org/onap/appc/ccadaptor/ConfigComponentAdaptorTest.java @@ -8,6 +8,8 @@ * ============================================================================= * Modification Copyright (C) 2018 IBM. * ============================================================================= + * Modifications Copyright (C) 2018 Ericsson + * ============================================================================= * 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 @@ -25,288 +27,357 @@ package org.onap.appc.ccadaptor; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Properties; -import org.junit.Assert; +import org.hamcrest.CoreMatchers; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor.ConfigStatus; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.powermock.reflect.Whitebox; +@RunWith(MockitoJUnitRunner.class) public class ConfigComponentAdaptorTest { + private static final String TERMINATE_COMMAND = "" + + "\n \n \n \n ]]>]]>"; + private SshJcraftWrapper mockWrapper; + + @Before + public void setupForTests() { + mockWrapper = Mockito.mock(SshJcraftWrapper.class); + } @Test - public void testGetCliRunningConfig() { + public void testGet() throws TimedOutException, IOException { Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); - String Get_config_template = ("get_config_template"); - String key = "GetCliRunningConfig"; + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props)); + Mockito.doReturn("TEST\nDATA").when(mockWrapper).receiveUntil(Mockito.anyString(), + Mockito.anyInt(), Mockito.anyString()); + Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper(); + String key = "get"; Map parameters = new HashMap<>(); parameters.put("Host_ip_address", "test"); - parameters.put("User_name", "test"); - parameters.put("Password", "password"); - parameters.put("Port_number", "22"); - parameters.put("Get_config_template", Get_config_template); - parameters.put("config-component-configUrl","testUrl"); - parameters.put("config-component-configPassword","testPassword"); - parameters.put("config-component-configUser","testUser"); - SvcLogicContext ctx = new SvcLogicContext(); ctx.setAttribute( - "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", - "test"); - cca.configure(key, parameters, ctx); + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", + "test"); + assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx)); } @Test - public void testDownloadCliConfig() { + public void testPutExceptionFlow() throws TimedOutException, IOException { Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); - String Get_config_template = ("get_config_template"); - String key = "DownloadCliConfig"; + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props)); + Mockito.doThrow(new IOException()).when(mockWrapper).put(Mockito.anyObject(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString()); + Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper(); + String key = "put"; Map parameters = new HashMap<>(); - parameters.put("Host_ip_address", "test"); - parameters.put("User_name", "test"); - parameters.put("Password", "password"); - parameters.put("Port_number", "22"); - parameters.put("Get_config_template", Get_config_template); - + parameters.put("data", "test"); SvcLogicContext ctx = new SvcLogicContext(); ctx.setAttribute( - "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", - "test"); - cca.configure(key, parameters, ctx); + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", + "test"); + assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx)); } @Test - public void testXmlDownload() { + public void testCli() throws TimedOutException, IOException { Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); - String Get_config_template = ("get_config_template"); - String key = "xml-download"; + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props)); + Mockito.doReturn("TEST\nDATA").when(mockWrapper).receiveUntil(Mockito.anyString(), + Mockito.anyInt(), Mockito.anyString()); + Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper(); + String Get_config_template = + ("get_config_template\nRequest: \"show config\"\nResponse: Ends_With \"RESPONSE\""); + String key = "cli"; Map parameters = new HashMap<>(); - parameters.put("Host_ip_address", "test"); - parameters.put("User_name", "test"); - parameters.put("Password", "password"); - parameters.put("Port_number", "22"); + loadSshParameters(parameters); parameters.put("Get_config_template", Get_config_template); - + parameters.put("config-component-configUrl", "testUrl"); + parameters.put("config-component-configPassword", "testPassword"); + parameters.put("config-component-configUser", "testUser"); SvcLogicContext ctx = new SvcLogicContext(); ctx.setAttribute( - "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", - "test"); - cca.configure(key, parameters, ctx); + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", + "test"); + assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx)); } @Test - public void testXmlGetrunningconfig() { + public void testCliExceptionFlow() throws TimedOutException, IOException { Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); - String Get_config_template = ("get_config_template"); - String key = "xml-getrunningconfig"; + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props)); + Mockito.doThrow(new IOException()).when(mockWrapper).connect(Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyInt()); + Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper(); + String Get_config_template = + ("get_config_template\nRequest: \"show config\"\nResponse: Ends_With \"RESPONSE\""); + String key = "cli"; Map parameters = new HashMap<>(); - parameters.put("Host_ip_address", "test"); - parameters.put("User_name", "test"); - parameters.put("Password", "password"); - parameters.put("Port_number", "22"); + loadSshParameters(parameters); parameters.put("Get_config_template", Get_config_template); - + parameters.put("config-component-configUrl", "testUrl"); + parameters.put("config-component-configPassword", "testPassword"); + parameters.put("config-component-configUser", "testUser"); SvcLogicContext ctx = new SvcLogicContext(); ctx.setAttribute( - "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", - "test"); - cca.configure(key, parameters, ctx); + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", + "test"); + assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx)); } @Test public void testEscapeSql() { Properties props = null; ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); - String Get_config_template = ("get_config_template"); + String testArtifactContents = ("\\ \\\\"); String key = "escapeSql"; Map parameters = new HashMap<>(); - parameters.put("Host_ip_address", "test"); - parameters.put("User_name", "test"); - parameters.put("Password", "password"); - parameters.put("Port_number", "22"); - parameters.put("Get_config_template", Get_config_template); - + loadSshParameters(parameters); + parameters.put("artifactContents", testArtifactContents); SvcLogicContext ctx = new SvcLogicContext(); ctx.setAttribute( - "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", - "test"); - cca.configure(key, parameters, ctx); + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", + "test"); + assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx)); + assertEquals("\\" + testArtifactContents + "\\\\", ctx.getAttribute("escapedData")); } @Test - public void testAll() { + public void testGetCliRunningConfig() throws TimedOutException, IOException { Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); - String Get_config_template = ("test"); - String Download_config_template = ("test"); + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props)); + Mockito.doReturn("TEST\nDATA").when(mockWrapper).receiveUntil(Mockito.anyString(), + Mockito.anyInt(), Mockito.anyString()); + Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper(); + String Get_config_template = + ("get_config_template\nRequest: \"show config\"\nResponse: Ends_With \"RESPONSE\""); String key = "GetCliRunningConfig"; Map parameters = new HashMap<>(); - parameters.put("Host_ip_address", "test"); - parameters.put("User_name", "test"); - parameters.put("Password", "password"); - parameters.put("Port_number", "22"); + loadSshParameters(parameters); parameters.put("Get_config_template", Get_config_template); - parameters.put("Protocol", "netconf"); - parameters.put("Contents", "Contents"); - parameters.put("Download_config_template", Download_config_template); - parameters.put("Config_contents", "test"); - + parameters.put("config-component-configUrl", "testUrl"); + parameters.put("config-component-configPassword", "testPassword"); + parameters.put("config-component-configUser", "testUser"); SvcLogicContext ctx = new SvcLogicContext(); ctx.setAttribute( - "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", - "test"); - cca.configure(key, parameters, ctx); + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", + "test"); + assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx)); } @Test - public void testAll1() { + public void testGetCliRunningConfigExceptionFlow() throws TimedOutException, IOException { Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); - String key = "get"; + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props)); + Mockito.doThrow(new IOException()).when(mockWrapper).receiveUntil(Mockito.anyString(), + Mockito.anyInt(), Mockito.anyString()); + Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper(); + String Get_config_template = + ("get_config_template\nRequest: \"show config\"\nResponse: Ends_With \"RESPONSE\""); + String key = "GetCliRunningConfig"; Map parameters = new HashMap<>(); - parameters.put("Host_ip_address", "test"); + loadSshParameters(parameters); + parameters.put("Get_config_template", Get_config_template); + parameters.put("config-component-configUrl", "testUrl"); + parameters.put("config-component-configPassword", "testPassword"); + parameters.put("config-component-configUser", "testUser"); SvcLogicContext ctx = new SvcLogicContext(); ctx.setAttribute( - "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", - "test"); - cca.configure(key, parameters, ctx); + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", + "test"); + assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx)); } - //@Test - public void testAll2() { + @Test + public void testXmlDownload() throws TimedOutException, IOException { Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); - String key = "cli"; - Map parameters = new HashMap(); - parameters.put("Host_ip_address", "test"); + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props)); + Mockito.doReturn("TEST\nDATA").when(mockWrapper).receiveUntil(Mockito.anyString(), + Mockito.anyInt(), Mockito.anyString()); + Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper(); + String Get_config_template = ("get_config_template"); + String key = "xml-download"; + Map parameters = new HashMap<>(); + loadSshParameters(parameters); + parameters.put("Get_config_template", Get_config_template); + SvcLogicContext ctx = new SvcLogicContext(); ctx.setAttribute( - "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", - "test"); - cca.configure(key, parameters, ctx); + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", + "test"); + assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx)); } @Test - public void testGetStringBetweenQuotes() throws Exception { + public void testXmlDownloadExceptionFlow() throws TimedOutException, IOException { Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); - String result = Whitebox.invokeMethod(cca, "getStringBetweenQuotes", "\"testvalue\""); - Assert.assertEquals("testvalue", result); + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props)); + Mockito.doReturn("rpc-error").when(mockWrapper).receiveUntil("", 600000, ""); + Mockito.doThrow(new IOException()).when(mockWrapper).send(TERMINATE_COMMAND); + Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper(); + String Get_config_template = ("get_config_template"); + String key = "xml-download"; + Map parameters = new HashMap<>(); + loadSshParameters(parameters); + parameters.put("Get_config_template", Get_config_template); + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute( + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", + "test"); + assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx)); } @Test - public void testBuildXmlRequest() throws Exception { + public void testXmlGetrunningconfig() throws TimedOutException, IOException { Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); - Map param = new HashMap<>(); - Whitebox.invokeMethod(cca, "buildXmlRequest", param, "template"); + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props)); + Mockito.doReturn("\n\n") + .when(mockWrapper) + .receiveUntil(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString()); + Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper(); + String key = "xml-getrunningconfig"; + Map parameters = new HashMap<>(); + loadSshParameters(parameters); + SvcLogicContext ctx = new SvcLogicContext(); + assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx)); } @Test - public void testTrimResponse() throws Exception { - Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); - String result = Whitebox.invokeMethod(cca, "trimResponse", "testData"); - Assert.assertEquals("", result); - - - String withConfigData = "\n" + - "\n" + - "1.1X.0\n" + - "\n" + - "node0\n" + - "\n" + - "\n" + - "\n" + - "]]>]]>\n" + - ""; - String compareString = "\n" - + "1.1X.0\n" - + "\n" - + "node0\n" - + "\n"; - - String result3 = Whitebox.invokeMethod(cca, "trimResponse", withConfigData); - Assert.assertEquals(compareString, result3); - - String withData = "]]>]]>\n" + - "\n" + - "\n" + - "\n" + - "\n" + - "1\n" + - "\n" + - "\n" + - "\n" + - "]]>]]"; - String compareStringWithData = "\n" + - "\n" + - "1\n" + - "\n"; - String result2 = Whitebox.invokeMethod(cca, "trimResponse", withData); - Assert.assertEquals(compareStringWithData,result2); - - + public void testXmlGetrunningconfigExceptionFlow() throws TimedOutException, IOException { + Properties props = new Properties(); + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props)); + Mockito.doThrow(new IOException()).when(mockWrapper).connect(Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyInt(), + Mockito.anyInt(), Mockito.anyString()); + Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper(); + String key = "xml-getrunningconfig"; + Map parameters = new HashMap<>(); + loadSshParameters(parameters); + SvcLogicContext ctx = new SvcLogicContext(); + assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx)); } @Test - public void testBuildNetworkData2() throws Exception { + public void testDownloadCliConfig() throws TimedOutException, IOException { Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props)); + Mockito.doReturn("TEST\nDATA").when(mockWrapper).receiveUntil(Mockito.anyString(), + Mockito.anyInt(), Mockito.anyString()); + Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper(); + String Download_config_template = + ("get_config_template\nRequest: \"show config\"\nResponse: Ends_With \"RESPONSE\""); + String key = "DownloadCliConfig"; + Map parameters = new HashMap<>(); + loadSshParameters(parameters); + parameters.put("Download_config_template", Download_config_template); SvcLogicContext ctx = new SvcLogicContext(); - String result = Whitebox.invokeMethod(cca, "buildNetworkData2", ctx, "template", "operation"); - Assert.assertEquals("template", result); + ctx.setAttribute( + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", + "test"); + assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx)); } - //@Test - public void testGetLastFewLinesOfFile() throws Exception { + @Test + public void testDownloadCliConfigExceptionFlow() throws TimedOutException, IOException { Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); - Whitebox.invokeMethod(cca, "readFile", "test"); + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props)); + Mockito.doThrow(new IOException("ExceptionFromDownloadCli")).when(mockWrapper) + .receiveUntil(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString()); + Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper(); + String Download_config_template = ("get_config_template\nRequest: \"show config\"" + + "\n Execute_config_contents Response: Ends_With\" \"RESPONSE\"\n"); + String key = "DownloadCliConfig"; + Map parameters = new HashMap<>(); + loadSshParameters(parameters); + parameters.put("Config_contents", "TEST\nDATA"); + parameters.put("Download_config_template", Download_config_template); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute( + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", + "test"); + assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx)); } @Test - public void testConnect() throws Exception { - Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); + public void testPrepare() { + Client mockClient = Mockito.mock(Client.class); + WebResource mockWebResource = Mockito.mock(WebResource.class); + ClientResponse mockClientResponse = Mockito.mock(ClientResponse.class); + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(null)); + Mockito.doReturn(mockClientResponse).when(cca).getClientResponse(Mockito.anyObject(), + Mockito.anyString(), Mockito.anyString()); + Mockito.doReturn(mockWebResource).when(mockClient).resource(Mockito.anyString()); + Mockito.doReturn(mockClient).when(cca).getClient(); + Map parameters = new HashMap<>(); + parameters.put("action", "prepare"); SvcLogicContext ctx = new SvcLogicContext(); - cca.activate("key", ctx); - + ctx.setAttribute( + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", + "test"); + assertEquals(ConfigStatus.SUCCESS, cca.configure("", parameters, ctx)); } - @Test(expected = Exception.class) - public void testActivate() throws Exception { - Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); + @Test + public void testPrepareExceptionFlow() { + ConfigComponentAdaptor cca = new ConfigComponentAdaptor(null); + Map parameters = new HashMap<>(); + parameters.put("action", "prepare"); SvcLogicContext ctx = new SvcLogicContext(); - String result = Whitebox.invokeMethod(cca, "activate", ctx, true); - Assert.assertEquals("template", result); + ctx.setAttribute( + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].scale-configuration[0].network-type", + "test"); + assertEquals(ConfigStatus.FAILURE, cca.configure("", parameters, ctx)); + assertEquals("500", ctx.getAttribute("error-code")); } - @Test(expected = Exception.class) - public void testAudit() throws Exception { - Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); + @Test + public void testAudit() { + Client mockClient = Mockito.mock(Client.class); + WebResource mockWebResource = Mockito.mock(WebResource.class); + ClientResponse mockClientResponse = Mockito.mock(ClientResponse.class); + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(null)); + Mockito.doReturn(mockClientResponse).when(cca).getClientResponse(Mockito.anyObject(), + Mockito.anyString(), Mockito.anyString()); + Mockito.doReturn(mockWebResource).when(mockClient).resource(Mockito.anyString()); + Mockito.doReturn(mockClient).when(cca).getClient(); + Map parameters = new HashMap<>(); + parameters.put("action", "audit"); SvcLogicContext ctx = new SvcLogicContext(); - String result = Whitebox.invokeMethod(cca, "audit", ctx, "test"); - Assert.assertEquals("template", result); + ctx.setAttribute( + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", + "test"); + assertEquals(ConfigStatus.SUCCESS, cca.configure("", parameters, ctx)); } - @Test(expected = Exception.class) - public void testPrepare() throws Exception { - Properties props = null; - ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); + @Test + public void testActivate() { + ClientResponse mockClientResponse = Mockito.mock(ClientResponse.class); + ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(null)); + Mockito.doReturn(mockClientResponse).when(cca).getClientResponse(Mockito.anyObject(), + Mockito.anyString(), Mockito.anyString()); + Map parameters = new HashMap<>(); + parameters.put("action", "activate"); SvcLogicContext ctx = new SvcLogicContext(); - String result = Whitebox.invokeMethod(cca, "prepare", ctx, "test", "test"); - Assert.assertEquals("template", result); + ctx.setAttribute( + "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name", + "test"); + assertEquals(ConfigStatus.SUCCESS, cca.configure("", parameters, ctx)); } - + @Test public void testConstructorForNonNullProperties() throws Exception { Properties props = new Properties(); @@ -318,17 +389,34 @@ public class ConfigComponentAdaptorTest { props.setProperty("auditComponent.passwd", "testAuditPwd"); props.setProperty("service-configuration-notification-url", "testServiceNotificationUrl"); props.setProperty("audit-configuration-notification-url", "testAuditNotificationUrl"); - + ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props); - Assert.assertEquals("testConfigUrl", cca.getConfigUrl()); - Assert.assertEquals("testConfigUser", cca.getConfigUser()); - Assert.assertEquals("testConfigPwd", cca.getConfigPassword()); - Assert.assertEquals("testAuditUrl", cca.getAuditUrl()); - Assert.assertEquals("testAuditUser", cca.getAuditUser()); - Assert.assertEquals("testAuditPwd", cca.getAuditPassword()); - Assert.assertEquals("testServiceNotificationUrl", cca.getConfigCallbackUrl()); - Assert.assertEquals("testAuditNotificationUrl", cca.getAuditCallbackUrl()); - + assertEquals("testConfigUrl", cca.getConfigUrl()); + assertEquals("testConfigUser", cca.getConfigUser()); + assertEquals("testConfigPwd", cca.getConfigPassword()); + assertEquals("testAuditUrl", cca.getAuditUrl()); + assertEquals("testAuditUser", cca.getAuditUser()); + assertEquals("testAuditPwd", cca.getAuditPassword()); + assertEquals("testServiceNotificationUrl", cca.getConfigCallbackUrl()); + assertEquals("testAuditNotificationUrl", cca.getAuditCallbackUrl()); } + @Test + public void testStaticReadFile() { + assertThat(ConfigComponentAdaptor._readFile("src/main/resources/config-base.xml"), + CoreMatchers.containsString("")); + } + + @Test + public void testStaticReadFileExceptionFlow() { + assertEquals("", ConfigComponentAdaptor._readFile("NON_EXISTENT_FILE")); + } + + private void loadSshParameters(Map map) { + map.put("Host_ip_address", "test"); + map.put("User_name", "test"); + map.put("Password", "password"); + map.put("Port_number", "22"); + map.put("portNumber", "22"); + } } -- 2.16.6