First part of onap rename
[appc.git] / appc-client / client-simulator / src / test / java / org / openecomp / appc / simulator / client / main / TestClientRunner.java
1 package org.onap.appc.simulator.client.main;
2
3 import org.apache.commons.io.filefilter.WildcardFileFilter;
4 import org.junit.After;
5 import org.junit.Assert;
6 import org.junit.Before;
7 import org.junit.Test;
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;
16
17 import java.io.*;
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;
24
25 @RunWith(PowerMockRunner.class)
26 @PrepareForTest({JsonRequestHandler.class,ClientRunner.class})
27
28 public class TestClientRunner {
29
30     JsonRequestHandler jsonRequestHandler;
31     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
32
33     @Before
34     public void init() throws AppcClientException{
35         System.setOut(new PrintStream(outContent));
36         jsonRequestHandler= Mockito.mock(JsonRequestHandler.class);
37
38     }
39
40     @After
41     public void cleanUpStreams() {
42         System.setOut(null);
43     }
44
45     @Test
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());
50
51         ClientRunner.main(arguments);
52         String expectedOutput=outContent.toString();
53         Assert.assertEquals(expectedOutput,outContent.toString());
54     }
55
56     @Test
57     public void testGetPrperties(){
58         String folder="src/test/resources/data";
59         Properties properties=new Properties();
60         properties=getProperties(folder);
61         Assert.assertNotNull(properties);
62     }
63
64     @Test
65     public void testGetJsonFIles() throws FileNotFoundException{
66         String folder="src/test/resources/data";
67         List<File> sources = getJsonFiles(folder);
68         Assert.assertNotNull(sources);
69     }
70
71     private static Properties getProperties(String folder) {
72         Properties prop = new Properties();
73
74         InputStream conf = null;
75         try {
76             conf = new FileInputStream(folder + "client-simulator.properties");
77         } catch (FileNotFoundException e) {
78
79         }
80         if (conf != null) {
81             try {
82                 prop.load(conf);
83             } catch (IOException e) {
84                 e.printStackTrace();
85             }
86         } else {
87             try {
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");
91             }
92         }
93         return prop;
94     }
95
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)));
100     }
101
102 }