1 package org.onap.appc.simulator.client.main;
3 import org.apache.commons.io.filefilter.WildcardFileFilter;
4 import org.junit.After;
5 import org.junit.Assert;
6 import org.junit.Before;
8 import org.junit.runner.RunWith;
9 import org.mockito.Matchers;
10 import org.mockito.Mockito;
11 import org.onap.appc.client.lcm.exceptions.AppcClientException;
12 import org.onap.appc.simulator.client.impl.JsonRequestHandler;
13 import org.powermock.api.mockito.PowerMockito;
14 import org.powermock.core.classloader.annotations.PrepareForTest;
15 import org.powermock.modules.junit4.PowerMockRunner;
18 import java.nio.file.Path;
19 import java.nio.file.Paths;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
23 import java.util.Properties;
25 @RunWith(PowerMockRunner.class)
26 @PrepareForTest({JsonRequestHandler.class,ClientRunner.class})
28 public class TestClientRunner {
30 JsonRequestHandler jsonRequestHandler;
31 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
34 public void init() throws AppcClientException{
35 System.setOut(new PrintStream(outContent));
36 jsonRequestHandler= Mockito.mock(JsonRequestHandler.class);
41 public void cleanUpStreams() {
46 public void testMain() throws java.io.IOException,java.lang.Exception{
47 String []arguments=new String[]{"src/test/resources/data","JSON"};
48 PowerMockito.whenNew(JsonRequestHandler.class).withArguments(Mockito.anyObject()).thenReturn(jsonRequestHandler);
49 Mockito.doNothing().when(jsonRequestHandler).proceedFile(Matchers.anyObject(), Matchers.anyObject());
51 ClientRunner.main(arguments);
52 String expectedOutput=outContent.toString();
53 Assert.assertEquals(expectedOutput,outContent.toString());
57 public void testGetPrperties(){
58 String folder="src/test/resources/data";
59 Properties properties=new Properties();
60 properties=getProperties(folder);
61 Assert.assertNotNull(properties);
65 public void testGetJsonFIles() throws FileNotFoundException{
66 String folder="src/test/resources/data";
67 List<File> sources = getJsonFiles(folder);
68 Assert.assertNotNull(sources);
71 private static Properties getProperties(String folder) {
72 Properties prop = new Properties();
74 InputStream conf = null;
76 conf = new FileInputStream(folder + "client-simulator.properties");
77 } catch (FileNotFoundException e) {
83 } catch (IOException e) {
88 prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("client-simulator.properties"));
89 } catch (Exception e) {
90 throw new RuntimeException("### ERROR ### - Could not load properties to test");
96 private static List<File> getJsonFiles(String folder) throws FileNotFoundException {
97 Path dir = Paths.get(folder);
98 FileFilter fileFilter = new WildcardFileFilter("*.json");
99 return new ArrayList<File>(Arrays.asList(dir.toFile().listFiles(fileFilter)));