From 5c0ab1dfa4aab9cd83d5baac17710963a11b22d9 Mon Sep 17 00:00:00 2001 From: mojahidi Date: Tue, 26 Sep 2017 17:39:23 +0530 Subject: Increased test coverage for appc-java-client Added Junit test cases to increase test coverage for appc-java-client Change-Id: I31154acea348f50da5df47262611bfb108a4ed3a Issue-ID:APPC-223 Signed-off-by: mojahidi --- appc-client/client-simulator/pom.xml | 5 + .../simulator/client/impl/JsonRequestHandler.java | 6 +- .../simulator/client/impl/JsonResponseHandler.java | 4 +- .../appc/simulator/client/main/ClientRunner.java | 5 +- .../client/impl/TestJsonRequestHandler.java | 53 +++++++++++ .../client/impl/TestJsonResponseHandler.java | 46 ++++++++++ .../simulator/client/main/TestClientRunner.java | 102 +++++++++++++++++++++ .../resources/data/client-simulator.properties | 27 ++++++ .../src/test/resources/data/exceptions.txt | 1 + .../src/test/resources/data/input.json | 19 ++++ .../src/test/resources/data/output/error.json | 1 + .../src/test/resources/data/output/error.txt | 1 + 12 files changed, 265 insertions(+), 5 deletions(-) create mode 100644 appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonRequestHandler.java create mode 100644 appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonResponseHandler.java create mode 100644 appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/main/TestClientRunner.java create mode 100644 appc-client/client-simulator/src/test/resources/data/client-simulator.properties create mode 100644 appc-client/client-simulator/src/test/resources/data/exceptions.txt create mode 100644 appc-client/client-simulator/src/test/resources/data/input.json create mode 100644 appc-client/client-simulator/src/test/resources/data/output/error.json create mode 100644 appc-client/client-simulator/src/test/resources/data/output/error.txt (limited to 'appc-client/client-simulator') diff --git a/appc-client/client-simulator/pom.xml b/appc-client/client-simulator/pom.xml index 59be1263f..2fabcc5f4 100644 --- a/appc-client/client-simulator/pom.xml +++ b/appc-client/client-simulator/pom.xml @@ -90,6 +90,11 @@ jackson-databind ${jackson.version} + + com.fasterxml.jackson.core + jackson-core + ${jackson.version} + org.openecomp.appc.client client-kit diff --git a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonRequestHandler.java b/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonRequestHandler.java index 3180e7849..e3a86e01a 100644 --- a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonRequestHandler.java +++ b/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonRequestHandler.java @@ -50,7 +50,7 @@ import java.util.Properties; public class JsonRequestHandler implements RequestHandler { - private enum modeT { + private enum modeT { SYNCH, ASYNCH } @@ -79,6 +79,10 @@ public class JsonRequestHandler implements RequestHandler { exceptRpcMap = prepareExceptionsMap(); } + public JsonRequestHandler(){ + + } + private HashMap prepareExceptionsMap() { exceptRpcMap = new HashMap<>(); diff --git a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonResponseHandler.java b/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonResponseHandler.java index aef034f53..5f14bb849 100644 --- a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonResponseHandler.java +++ b/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonResponseHandler.java @@ -65,7 +65,7 @@ public class JsonResponseHandler implements ResponseHandler { int errorCode = 0; boolean isFinal = true; try { - JsonNode code = OBJECT_MAPPER.readTree(output).findValue("status").findValue("code"); + JsonNode code= OBJECT_MAPPER.readTree(output).findValue("status").findValue("code"); if (code == null) { LOG.error("Status code doesn't exist. Malformed response : " + output); @@ -130,7 +130,7 @@ public class JsonResponseHandler implements ResponseHandler { this.flushToFile(output, ".error", isFinal); } - private void flushToFile(String output, String suffix, boolean isFinal) { + private void flushToFile(String output, String suffix, boolean isFinal) { try (FileWriter fileWriter = new FileWriter(fileName + suffix);){ LOG.info("Output file : " + fileName + suffix); diff --git a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/main/ClientRunner.java b/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/main/ClientRunner.java index d28927302..704fbee80 100644 --- a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/main/ClientRunner.java +++ b/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/main/ClientRunner.java @@ -37,6 +37,7 @@ import java.util.List; import java.util.Properties; public class ClientRunner { + public static void main(String ... args) throws Exception { String folder = args[0]; if (folder == null) { @@ -66,7 +67,7 @@ public class ClientRunner { // System.exit(0); } - private static Properties getProperties(String folder) { + private static Properties getProperties(String folder) { Properties prop = new Properties(); InputStream conf = null; @@ -91,7 +92,7 @@ public class ClientRunner { return prop; } - private static List getJsonFiles(String folder) throws FileNotFoundException { + private static List getJsonFiles(String folder) throws FileNotFoundException { Path dir = Paths.get(folder); FileFilter fileFilter = new WildcardFileFilter("*.json"); return new ArrayList(Arrays.asList(dir.toFile().listFiles(fileFilter))); diff --git a/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonRequestHandler.java b/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonRequestHandler.java new file mode 100644 index 000000000..417b98d64 --- /dev/null +++ b/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonRequestHandler.java @@ -0,0 +1,53 @@ +package org.openecomp.appc.simulator.client.impl; + +import org.apache.commons.io.filefilter.WildcardFileFilter; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.openecomp.appc.client.lcm.api.LifeCycleManagerStateful; +import org.openecomp.appc.client.lcm.exceptions.AppcClientException; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.io.*; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({LifeCycleManagerStateful.class}) + +public class TestJsonRequestHandler { + + JsonResponseHandler jsonResponseHandler=new JsonResponseHandler(); + @Before + public void init(){ + jsonResponseHandler= Mockito.mock(JsonResponseHandler.class); + } + + + @Test + public void testProceedFiles() throws AppcClientException,java.io.IOException{ + String folder="src/test/resources/data"; + List sources = getJsonFiles(folder); + File source=sources.get(0); + File log = new File(folder + "/output.txt"); + JsonRequestHandler requestHandler = new JsonRequestHandler(); + Mockito.doNothing().when(jsonResponseHandler).onResponse(Matchers.anyBoolean()); + requestHandler.proceedFile(source,log); + + Assert.assertNotNull(log); + + } + + private static List getJsonFiles(String folder) throws FileNotFoundException { + Path dir = Paths.get(folder); + FileFilter fileFilter = new WildcardFileFilter("*.json"); + return new ArrayList(Arrays.asList(dir.toFile().listFiles(fileFilter))); + } + + +} \ No newline at end of file diff --git a/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonResponseHandler.java b/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonResponseHandler.java new file mode 100644 index 000000000..b477b8a61 --- /dev/null +++ b/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonResponseHandler.java @@ -0,0 +1,46 @@ +package org.openecomp.appc.simulator.client.impl; + +import org.apache.commons.io.filefilter.WildcardFileFilter; +import org.junit.Assert; +import org.junit.Ignore; +import java.io.*; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + +public class TestJsonResponseHandler { + String folder="/data/output/error.json"; + JsonResponseHandler responseHandler=new JsonResponseHandler(); + + @Ignore + public void testOnResponse() throws URISyntaxException, IOException{ + responseHandler.onResponse(getNode()); + List files=getJsonFiles(folder); + Assert.assertNotNull(files); + + } + + private String readData(String inputFile) throws URISyntaxException, IOException { + File file = new File(this.getClass().getResource(inputFile).toURI()); + + byte[] bFile = new byte[(int) file.length()]; + FileInputStream fileInputStream = new FileInputStream(file); + fileInputStream.read(bFile); + fileInputStream.close(); + return new String(bFile); + } + + private String getNode() throws java.io.IOException{ + String jsonSring="{\"status\": {\"code\": \"200\"}}"; + return jsonSring; +} + public List getJsonFiles(String folder) throws FileNotFoundException { + Path dir = Paths.get(folder); + FileFilter fileFilter = new WildcardFileFilter("*.error"); + return new ArrayList(Arrays.asList(dir.toFile().listFiles(fileFilter))); + } +} diff --git a/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/main/TestClientRunner.java b/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/main/TestClientRunner.java new file mode 100644 index 000000000..d0de0cd49 --- /dev/null +++ b/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/main/TestClientRunner.java @@ -0,0 +1,102 @@ +package org.openecomp.appc.simulator.client.main; + +import org.apache.commons.io.filefilter.WildcardFileFilter; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.openecomp.appc.client.lcm.exceptions.AppcClientException; +import org.openecomp.appc.simulator.client.impl.JsonRequestHandler; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.io.*; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({JsonRequestHandler.class,ClientRunner.class}) + +public class TestClientRunner { + + JsonRequestHandler jsonRequestHandler; + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + + @Before + public void init() throws AppcClientException{ + System.setOut(new PrintStream(outContent)); + jsonRequestHandler= Mockito.mock(JsonRequestHandler.class); + + } + + @After + public void cleanUpStreams() { + System.setOut(null); + } + + @Test + public void testMain() throws java.io.IOException,java.lang.Exception{ + String []arguments=new String[]{"src/test/resources/data","JSON"}; + PowerMockito.whenNew(JsonRequestHandler.class).withArguments(Mockito.anyObject()).thenReturn(jsonRequestHandler); + Mockito.doNothing().when(jsonRequestHandler).proceedFile(Matchers.anyObject(), Matchers.anyObject()); + + ClientRunner.main(arguments); + String expectedOutput=outContent.toString(); + Assert.assertEquals(expectedOutput,outContent.toString()); + } + + @Test + public void testGetPrperties(){ + String folder="src/test/resources/data"; + Properties properties=new Properties(); + properties=getProperties(folder); + Assert.assertNotNull(properties); + } + + @Test + public void testGetJsonFIles() throws FileNotFoundException{ + String folder="src/test/resources/data"; + List sources = getJsonFiles(folder); + Assert.assertNotNull(sources); + } + + private static Properties getProperties(String folder) { + Properties prop = new Properties(); + + InputStream conf = null; + try { + conf = new FileInputStream(folder + "client-simulator.properties"); + } catch (FileNotFoundException e) { + + } + if (conf != null) { + try { + prop.load(conf); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + try { + prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("client-simulator.properties")); + } catch (Exception e) { + throw new RuntimeException("### ERROR ### - Could not load properties to test"); + } + } + return prop; + } + + private static List getJsonFiles(String folder) throws FileNotFoundException { + Path dir = Paths.get(folder); + FileFilter fileFilter = new WildcardFileFilter("*.json"); + return new ArrayList(Arrays.asList(dir.toFile().listFiles(fileFilter))); + } + +} diff --git a/appc-client/client-simulator/src/test/resources/data/client-simulator.properties b/appc-client/client-simulator/src/test/resources/data/client-simulator.properties new file mode 100644 index 000000000..4125e2fb6 --- /dev/null +++ b/appc-client/client-simulator/src/test/resources/data/client-simulator.properties @@ -0,0 +1,27 @@ +# +# Default Properties - Configured for client simulator +# +#-------------------------------------------------------------------------------------------- +ctx.model.package=com.att.appc.client.lcm.model + +client.pool.size=10 +client.rpc.exceptions.map.file=/root/exceptions.txt +#client.response.timeout=TIMEOUT + +#------------------ +# UEB configuration +#------------------ +topic.read=async_amrita2 +topic.read.timeout=30000 +#topic.read.limit=1000 +topic.write=appc_read_oecomp2 +client.name=name1 +client.name.id=0 + +#true - force shutdown +client.force.shutdown=false +client.graceful.shutdown.timeout=60000 + +#poolMembers=uebsb92sfdc.it.att.com:3904 +poolMembers=10.147.101.7:3904 + diff --git a/appc-client/client-simulator/src/test/resources/data/exceptions.txt b/appc-client/client-simulator/src/test/resources/data/exceptions.txt new file mode 100644 index 000000000..54d433017 --- /dev/null +++ b/appc-client/client-simulator/src/test/resources/data/exceptions.txt @@ -0,0 +1 @@ +ConfigScaleOut:config-scaleout diff --git a/appc-client/client-simulator/src/test/resources/data/input.json b/appc-client/client-simulator/src/test/resources/data/input.json new file mode 100644 index 000000000..940c8424a --- /dev/null +++ b/appc-client/client-simulator/src/test/resources/data/input.json @@ -0,0 +1,19 @@ +{"input": { + "common-header": { + "timestamp": "2017-07-21T9:30:40.958Z", + "api-ver": "2.00", + "originator-id": "ORTG_1", + "request-id": "ST_122", + "sub-request-id": "12", + "flags": { + "force": "TRUE", + "ttl": 12000 + } + }, + "action" : "Start", + "action-identifiers": { + "vnf-id": "mj123!!" + }, + "payload": " {\"vnf-host-ip-address\": \"10.147.124.163\" }" + +}} diff --git a/appc-client/client-simulator/src/test/resources/data/output/error.json b/appc-client/client-simulator/src/test/resources/data/output/error.json new file mode 100644 index 000000000..75d35709e --- /dev/null +++ b/appc-client/client-simulator/src/test/resources/data/output/error.json @@ -0,0 +1 @@ +{"status": {"code": "200"}} diff --git a/appc-client/client-simulator/src/test/resources/data/output/error.txt b/appc-client/client-simulator/src/test/resources/data/output/error.txt new file mode 100644 index 000000000..8c2c9de2f --- /dev/null +++ b/appc-client/client-simulator/src/test/resources/data/output/error.txt @@ -0,0 +1 @@ +{status:"200"} \ No newline at end of file -- cgit 1.2.3-korg