aboutsummaryrefslogtreecommitdiffstats
path: root/appc-client/client-simulator/src/test/java/org/onap/appc/simulator/client/main/TestClientRunner.java
blob: db648f95720cb77a7a7309ace312c14e7d3795b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package org.onap.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.onap.appc.client.lcm.exceptions.AppcClientException;
import org.onap.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<File> 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<File> getJsonFiles(String folder) throws FileNotFoundException {
        Path dir = Paths.get(folder);
        FileFilter fileFilter = new WildcardFileFilter("*.json");
        return new ArrayList<File>(Arrays.asList(dir.toFile().listFiles(fileFilter)));
    }

}