0913608af2d82eded47f84912390932cedb6d1ee
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / vestest / InputJsonValidation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dcae.vestest;
21 import java.io.BufferedReader;
22 import java.io.FileNotFoundException;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26
27 import org.json.simple.JSONObject;
28 import org.json.simple.parser.JSONParser;
29 import org.junit.Test;
30 import org.onap.dcae.commonFunction.CommonStartup;
31
32 import com.google.gson.JsonIOException;
33 import com.google.gson.JsonParser;
34 import com.google.gson.JsonSyntaxException;
35
36
37 public class InputJsonValidation {
38
39
40         static String valresult = null;
41
42
43         @Test
44         public void nonvalidJSONValidation(){
45
46                 JSONObject jsonObject = null;
47                 JSONParser parser=new JSONParser();
48                 Object obj=null;
49                 //String jsonfilepath="C:/Users/vv770d/git/restfulcollector/src/test/resources/fujistu_non_valid_json.txt";
50                 String jsonfilepath="src/test/resources/VES_invalid.txt";
51                 String retValue="false";
52                 try{
53
54                         obj=parser.parse(new FileReader(jsonfilepath));
55
56                 }
57                 catch(Exception e){
58
59                         System.out.println("Exception while opening the file");
60
61                 }
62                 jsonObject=(JSONObject) obj;
63
64                 String schema=null;
65                 try {
66                         schema = new JsonParser().parse(new FileReader(VESCollectorJunitTest.schemaFile)).toString();
67                         //System.out.println("Schema value: " + schema.toString());
68                 } catch (JsonIOException e) {
69                         // TODO Auto-generated catch block
70                         e.printStackTrace();
71                 } catch (JsonSyntaxException e) {
72                         // TODO Auto-generated catch block
73                         e.printStackTrace();
74                 } catch (FileNotFoundException e) {
75                         // TODO Auto-generated catch block
76                         e.printStackTrace();
77                 }
78
79                 if (schema!=null){
80                         retValue=CommonStartup.schemavalidate(jsonObject.toString(), schema);   
81                 }
82                 //return retValue;
83                 VESCollectorJunitTest.output = retValue;
84         }
85         
86         
87         // The below test case meant for verifying json schema on provided json file
88         @Test
89         public void validJSONValidation(){
90
91                 JSONObject jsonObject = null;
92                 JSONParser parser=new JSONParser();
93                 Object obj=null;
94
95                 String jsonfilepath="src/test/resources/VES_valid.txt";
96                 String retValue="false";
97                 try{
98
99                         obj=parser.parse(new FileReader(jsonfilepath));
100
101
102                 }
103                 catch(Exception e){
104                         System.out.println("Exception while opening the file");
105
106                 }
107                 jsonObject=(JSONObject) obj;
108                 String schema=null;
109                 try {
110
111                         schema = new JsonParser().parse(new FileReader(VESCollectorJunitTest.schemaFile)).toString();
112                 } catch (JsonIOException e) {
113                         // TODO Auto-generated catch block
114                         e.printStackTrace();
115                 } catch (JsonSyntaxException e) {
116                         // TODO Auto-generated catch block
117                         e.printStackTrace();
118                 } catch (FileNotFoundException e) {
119                         // TODO Auto-generated catch block
120                         e.printStackTrace();
121                 }
122
123                 if (schema!=null){
124                         retValue=CommonStartup.schemavalidate(jsonObject.toString(), schema);   
125                 }
126                 VESCollectorJunitTest.output = retValue; 
127                 //return retValue;
128         }  
129
130         
131         
132         //validating valid json reception and its posting to DMAP.
133         @Test
134         public void eventReception(){
135
136
137                 String testCurlCommand = "curl -i -X POST -d @C:/Users/vv770d/git/restfulcollector/src/test/resources/fujistu-3.txt --header \"Content-Type: application/json\" http://localhost:8080/eventListener/v1";
138
139                 //final Process terminal = curlCommand.start();
140                 try {
141                         Process p = Runtime.getRuntime().exec(testCurlCommand);
142                         BufferedReader stdInput = new BufferedReader(new 
143                                         InputStreamReader(p.getInputStream()));
144
145                         BufferedReader stdError = new BufferedReader(new 
146                                         InputStreamReader(p.getErrorStream()));
147
148                         // read the output from the command
149
150                         String s = null;
151                         while ((s = stdInput.readLine()) != null) {
152                                 if (s.contains("HTTP/1.1 200 OK")){
153
154                                         //return "true";
155                                         VESCollectorJunitTest.output = "true";
156                                 }
157
158                         }
159                 } catch (IOException e) {
160                         // TODO Auto-generated catch block
161
162
163                         e.printStackTrace();
164                 }
165                 
166                 //return "false";
167         }
168
169 }