799628d0051e63c315739450d1c19c7f41594976
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.simulator.client.main;
26
27 import com.fasterxml.jackson.databind.JsonNode;
28 import org.apache.commons.io.filefilter.WildcardFileFilter;
29 import org.junit.After;
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Matchers;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.internal.stubbing.answers.DoesNothing;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.appc.client.lcm.exceptions.AppcClientException;
41 import org.onap.appc.simulator.client.RequestHandler;
42 import org.onap.appc.simulator.client.impl.JsonRequestHandler;
43
44 import java.io.*;
45 import java.nio.file.Path;
46 import java.nio.file.Paths;
47 import java.util.ArrayList;
48 import java.util.Arrays;
49 import java.util.List;
50 import java.util.Properties;
51
52 @RunWith(MockitoJUnitRunner.class)
53
54 public class TestClientRunner {
55
56     JsonRequestHandler jsonRequestHandler;
57     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
58
59     @Before
60     public void init() throws AppcClientException{
61         System.setOut(new PrintStream(outContent));
62         jsonRequestHandler= Mockito.mock(JsonRequestHandler.class);
63     }
64
65     @After
66     public void cleanUpStreams() {
67         System.setOut(null);
68     }
69
70 /* JsinRequestHandler is a constructor
71  * will figure out how to do test without PowerMock later
72     @Test
73     public void testMain() throws java.io.IOException,java.lang.Exception{
74         String  []arguments=new String[]{"src/test/resources/data","JSON"};
75         
76         PowerMockito.whenNew(JsonRequestHandler.class).withArguments(Mockito.anyObject()).thenReturn(jsonRequestHandler);
77         Mockito.doNothing().when(jsonRequestHandler).proceedFile(Matchers.anyObject(), Matchers.anyObject());
78
79         ClientRunner.main(arguments);
80         String expectedOutput=outContent.toString();
81         Assert.assertEquals(expectedOutput,outContent.toString());
82     }
83 */
84     @Test
85     public void testGetPrperties(){
86         String folder="src/test/resources/data";
87         Properties properties=new Properties();
88         properties=getProperties(folder);
89         Assert.assertNotNull(properties);
90     }
91
92     @Test
93     public void testGetJsonFIles() throws FileNotFoundException{
94         String folder="src/test/resources/data";
95         List<File> sources = getJsonFiles(folder);
96         Assert.assertNotNull(sources);
97     }
98
99     private static Properties getProperties(String folder) {
100         Properties prop = new Properties();
101
102         InputStream conf = null;
103         try {
104             conf = new FileInputStream(folder + "client-simulator.properties");
105         } catch (FileNotFoundException e) {
106
107         }
108         if (conf != null) {
109             try {
110                 prop.load(conf);
111             } catch (IOException e) {
112                 e.printStackTrace();
113             }
114         } else {
115             try {
116                 prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("client-simulator.properties"));
117             } catch (Exception e) {
118                 throw new RuntimeException("### ERROR ### - Could not load properties to test");
119             }
120         }
121         return prop;
122     }
123
124     private static List<File> getJsonFiles(String folder) throws FileNotFoundException {
125         Path dir = Paths.get(folder);
126         FileFilter fileFilter = new WildcardFileFilter("*.json");
127         return new ArrayList<File>(Arrays.asList(dir.toFile().listFiles(fileFilter)));
128     }
129
130 }