Adding new LCM features to the client lib
[appc.git] / appc-client / client-simulator / src / test / java / org / onap / appc / simulator / client / main / TestClientRunner.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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.onap.appc.client.lcm.exceptions.AppcClientException;
40 import org.onap.appc.simulator.client.RequestHandler;
41 import org.onap.appc.simulator.client.impl.JsonRequestHandler;
42 import org.powermock.api.mockito.PowerMockito;
43 import org.powermock.core.classloader.annotations.PrepareForTest;
44 import org.powermock.modules.junit4.PowerMockRunner;
45
46 import java.io.*;
47 import java.nio.file.Path;
48 import java.nio.file.Paths;
49 import java.util.ArrayList;
50 import java.util.Arrays;
51 import java.util.List;
52 import java.util.Properties;
53
54 @RunWith(PowerMockRunner.class)
55 @PrepareForTest({JsonRequestHandler.class,ClientRunner.class})
56
57 public class TestClientRunner {
58
59     JsonRequestHandler jsonRequestHandler;
60     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
61
62     @Before
63     public void init() throws AppcClientException{
64         System.setOut(new PrintStream(outContent));
65         jsonRequestHandler= Mockito.mock(JsonRequestHandler.class);
66
67     }
68
69     @After
70     public void cleanUpStreams() {
71         System.setOut(null);
72     }
73
74     @Test
75     public void testMain() throws java.io.IOException,java.lang.Exception{
76         String  []arguments=new String[]{"src/test/resources/data","JSON"};
77         PowerMockito.whenNew(JsonRequestHandler.class).withArguments(Mockito.anyObject()).thenReturn(jsonRequestHandler);
78         Mockito.doNothing().when(jsonRequestHandler).proceedFile(Matchers.anyObject(), Matchers.anyObject());
79
80         ClientRunner.main(arguments);
81         String expectedOutput=outContent.toString();
82         Assert.assertEquals(expectedOutput,outContent.toString());
83     }
84
85     @Test
86     public void testGetPrperties(){
87         String folder="src/test/resources/data";
88         Properties properties=new Properties();
89         properties=getProperties(folder);
90         Assert.assertNotNull(properties);
91     }
92
93     @Test
94     public void testGetJsonFIles() throws FileNotFoundException{
95         String folder="src/test/resources/data";
96         List<File> sources = getJsonFiles(folder);
97         Assert.assertNotNull(sources);
98     }
99
100     private static Properties getProperties(String folder) {
101         Properties prop = new Properties();
102
103         InputStream conf = null;
104         try {
105             conf = new FileInputStream(folder + "client-simulator.properties");
106         } catch (FileNotFoundException e) {
107
108         }
109         if (conf != null) {
110             try {
111                 prop.load(conf);
112             } catch (IOException e) {
113                 e.printStackTrace();
114             }
115         } else {
116             try {
117                 prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("client-simulator.properties"));
118             } catch (Exception e) {
119                 throw new RuntimeException("### ERROR ### - Could not load properties to test");
120             }
121         }
122         return prop;
123     }
124
125     private static List<File> getJsonFiles(String folder) throws FileNotFoundException {
126         Path dir = Paths.get(folder);
127         FileFilter fileFilter = new WildcardFileFilter("*.json");
128         return new ArrayList<File>(Arrays.asList(dir.toFile().listFiles(fileFilter)));
129     }
130
131 }