Initial OpenECOMP policy/engine commit
[policy/engine.git] / PolicyEngineClient / src / main / java / org / openecomp / policyEngine / GeneralTestClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineClient
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
21 /*
22  *                        AT&T - PROPRIETARY
23  *          THIS FILE CONTAINS PROPRIETARY INFORMATION OF
24  *        AT&T AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN
25  *             ACCORDANCE WITH APPLICABLE AGREEMENTS.
26  *
27  *          Copyright (c) 2014 AT&T Knowledge Ventures
28  *              Unpublished and Not for Publication
29  *                     All Rights Reserved
30  */
31 package org.openecomp.policyEngine;
32
33 import java.io.FileNotFoundException;
34 import java.io.FileReader;
35 import java.io.IOException;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.Map;
44
45 import org.json.simple.JSONArray;
46 import org.json.simple.JSONObject;
47 import org.json.simple.parser.JSONParser;
48 import org.openecomp.policy.api.PolicyEngine;
49 import org.openecomp.policy.api.PolicyEngineException;
50
51 /**
52  * Class reads from .testCases file and run the test cases available in the file
53  * and generates output for each test cases specifing whether is passed or fail
54  * and reason why it fails.
55  * 
56  * 
57  * @version 1.0
58  *
59  */
60 public class GeneralTestClient {
61         static int totalTC = 0, passTC = 0, failTC = 0;
62
63         public static void main(String[] args) {
64                 Path file;
65                 /* command line arguments */
66                 if (args.length != 0) {
67                         for (int index = 0; index < args.length; index++) {
68                                 // System.out.println(args[index]);
69                                 file = Paths.get(args[index]);
70                                 runTestClientOnConfigFile(file);
71                         }
72                 } else {
73                         /* default file */
74                         file = Paths.get("input.testCases");
75                         runTestClientOnConfigFile(file);
76                 }
77                 System.out
78                 .println("###############################################################################################");
79                 System.out.println("\n\t SUMMARY: TOTAL: " + totalTC + ",\tPASS: "
80                                 + passTC + ",\tFAIL: " + failTC + "\n");
81                 System.out
82                 .println("###############################################################################################");
83
84                 System.out.println("Enter a any key to exit");
85                 try {
86                         System.in.read();
87                 } catch (IOException e) {
88                         //
89                 }
90
91         }
92
93         /**
94          * This function reads the files passed as arguments and runs the test cases
95          * in the file
96          *
97          * @param file
98          */
99         private static void runTestClientOnConfigFile(Path file) {
100                 String resultReturned, eCOMPComponentName;
101                 int totalTCforFile = 0, passTCforFile = 0, failTCforFile = 0;
102                 System.out
103                 .println("\n###############################################################################################");
104                 System.out.println("\tRuning test Client on Config file: " + file);
105                 System.out
106                 .println("###############################################################################################\n");
107
108                 if (Files.notExists(file)) {
109                         System.out.println("file doesnot exist");
110                         // throw new
111                         // PolicyEngineException("File doesn't exist in the specified Path "
112                         // + file.toString());
113                 } else if (file.toString().endsWith(".testCases")) {
114                         try {
115                                 // read the json file
116                                 FileReader reader = new FileReader(file.toString());
117
118                                 JSONParser jsonParser = new JSONParser();
119                                 JSONArray jsonObjectArray = (JSONArray) jsonParser
120                                                 .parse(reader);
121                                 for (Object jsonObject : jsonObjectArray) {
122                                         totalTC++;
123                                         totalTCforFile++;
124                                         ArrayList<String> expectedResult = new ArrayList<String>();
125                                         ArrayList<String> resultReceived = new ArrayList<String>();
126                                         JSONObject testCase = (JSONObject) jsonObject;
127                                         // get a String from the JSON object
128                                         long id = (long) testCase.get("id");
129                                         String testFor = (String) testCase.get("testFor");
130                                         String testCaseDescription = (String) testCase
131                                                         .get("testCaseDescription");
132                                         JSONArray expectedResultJson = (JSONArray) testCase
133                                                         .get("expectedResult");
134                                         @SuppressWarnings("rawtypes")
135                                         Iterator i = expectedResultJson.iterator();
136                                         while (i.hasNext()) {
137                                                 expectedResult.add((String) i.next());
138                                         }
139                                         String pdp_urlConfigFile = (String) testCase
140                                                         .get("PDP_URLConfigFile");
141                                         // System.out.println(pdp_urlConfigFile);
142                                         PolicyEngine policyEngine;
143                                         try {
144                                                 policyEngine = new PolicyEngine(pdp_urlConfigFile);
145
146                                                 switch (testFor) {
147
148                                                 case "getConfig":
149                                                         eCOMPComponentName = (String) testCase
150                                                         .get("ECOMPName");
151                                                         String configName = (String) testCase
152                                                                         .get("ConfigName");
153                                                         Map<String, String> configAttributes = new HashMap<String, String>();
154                                                         configAttributes.put("key", "value");
155                                                         JSONArray configAttributesJSON = (JSONArray) testCase
156                                                                         .get("configAttributes");
157                                                         if(configAttributesJSON!=null){
158                                                         i = configAttributesJSON.iterator();
159                                                         while (i.hasNext()) {
160                                                                 JSONObject innerObj = (JSONObject) i.next();
161                                                                 configAttributes.put(
162                                                                                 (String) innerObj.get("key"),
163                                                                                 (String) innerObj.get("value"));
164
165                                                         }
166                                                         }else{
167                                                                 configAttributes = null;
168                                                         }
169                                                         resultReceived = PolicyEngineTestClient.getConfig(
170                                                                         policyEngine, eCOMPComponentName,
171                                                                         configName, configAttributes);
172                                                         Collections.sort(expectedResult);
173                                                         Collections.sort(resultReceived);
174                                                         resultReturned = compareResults(expectedResult,resultReceived);
175                                                         if (resultReturned.equals("PASSED")) {
176                                                                 printResult(id, testFor, testCaseDescription,
177                                                                                 "PASSED");
178                                                                 passTCforFile++;
179                                                                 passTC++;
180                                                         } else {
181                                                                 printResult(id, testFor, testCaseDescription,
182                                                                                 "FAILED", resultReturned);
183                                                                 failTCforFile++;
184                                                                 failTC++;
185                                                         }
186                                                         break;
187
188                                                 case "getAction":
189                                                         Map<String, String> eventAttributes = new HashMap<String, String>();
190                                                         eventAttributes.put("Key", "Value");
191                                                         JSONArray eventAttributesJSON = (JSONArray) testCase
192                                                                         .get("eventAttributes");
193                                                         if(eventAttributesJSON != null){
194                                                         i = eventAttributesJSON.iterator();
195                                                         while (i.hasNext()) {
196                                                                 JSONObject innerObj = (JSONObject) i.next();
197                                                                 eventAttributes.put(
198                                                                                 (String) innerObj.get("key"),
199                                                                                 (String) innerObj.get("value"));
200                                                         }
201                                                         }else{
202                                                                 eventAttributes=null;
203                                                         }
204                                                         resultReceived = PolicyEngineTestClient.getAction(
205                                                                         policyEngine, eventAttributes);
206                                                         Collections.sort(expectedResult);
207                                                         Collections.sort(resultReceived);
208                                                         resultReturned = compareResults(expectedResult,
209                                                                         resultReceived);
210                                                         if (resultReturned.equals("PASSED")) {
211                                                                 printResult(id, testFor, testCaseDescription,
212                                                                                 "PASSED");
213                                                                 passTCforFile++;
214                                                                 passTC++;
215                                                         } else {
216                                                                 printResult(id, testFor, testCaseDescription,
217                                                                                 "FAILED", resultReturned);
218                                                                 failTCforFile++;
219                                                                 failTC++;
220                                                         }
221                                                         break;
222
223                                                 case "getDecision":
224                                                         eCOMPComponentName = (String) testCase
225                                                         .get("ECOMPName");
226                                                         Map<String, String> decisionAttributes = new HashMap<String, String>();
227                                                         decisionAttributes.put("Key", "Value");
228                                                         JSONArray decisionAttributesJSON = (JSONArray) testCase
229                                                                         .get("decisionAttributes");
230                                                         i = decisionAttributesJSON.iterator();
231                                                         while (i.hasNext()) {
232                                                                 JSONObject innerObj = (JSONObject) i.next();
233                                                                 decisionAttributes.put(
234                                                                                 (String) innerObj.get("key"),
235                                                                                 (String) innerObj.get("value"));
236
237                                                         }
238                                                         
239                                                         resultReceived = PolicyEngineTestClient
240                                                                         .getDecision(policyEngine,
241                                                                                         eCOMPComponentName,
242                                                                                         decisionAttributes);
243                                                         Collections.sort(expectedResult);
244                                                         Collections.sort(resultReceived);
245                                                         resultReturned = compareResults(expectedResult,
246                                                                         resultReceived);
247                                                         if (resultReturned.equals("PASSED")) {
248                                                                 printResult(id, testFor, testCaseDescription,
249                                                                                 "PASSED");
250                                                                 passTCforFile++;
251                                                                 passTC++;
252                                                         } else {
253                                                                 printResult(id, testFor, testCaseDescription,
254                                                                                 "FAILED", resultReturned);
255                                                                 failTCforFile++;
256                                                                 failTC++;
257                                                         }
258                                                         break;
259
260                                                         // case "getManualNotification":
261                                                         // PolicyEngineTestClient
262                                                         // .getManualNotifications(org.openecomp.policyEngine);
263                                                         // break;
264                                                         // case "getAutoNotification":
265                                                         // PolicyEngineTestClient
266                                                         // .getAutoNotifications(org.openecomp.policyEngine);
267                                                         // break;
268
269                                                 default:
270                                                         printResult(id, testFor, testCaseDescription,
271                                                                         "FAILED", "\tINVAILD TEST CASE.");
272                                                         failTCforFile++;
273                                                         failTC++;
274                                                         break;
275
276                                                 }
277                                         } catch (PolicyEngineException e) {
278                                                 // TODO Auto-generated catch block
279                                                 printResult(id, testFor, testCaseDescription, "FAILED");
280                                                 failTCforFile++;
281                                                 failTC++;
282                                                 e.printStackTrace();
283                                         } catch (Exception e) {
284                                                 // TODO Auto-generated catch block
285                                                 printResult(id, testFor, testCaseDescription, "FAILED");
286                                                 failTCforFile++;
287                                                 failTC++;
288                                                 e.printStackTrace();
289                                         }
290                                 }
291
292                         } catch (FileNotFoundException ex) {
293                                 ex.printStackTrace();
294                         } catch (IOException ex) {
295                                 ex.printStackTrace();
296                         } catch (NullPointerException ex) {
297                                 ex.printStackTrace();
298                         } catch (org.json.simple.parser.ParseException e) {
299                                 // TODO Auto-generated catch block
300                                 e.printStackTrace();
301                         }
302                 }
303                 System.out.println("\n\n\t Summary for the file: TOTAL: "
304                                 + totalTCforFile + ",\tPASS: " + passTCforFile + ",\tFAIL: "
305                                 + failTCforFile + "\n");
306         }
307
308         /**
309          * This function prints the reason if test fails.
310          * 
311          * @param id
312          * @param testFor
313          * @param testCaseDescription
314          * @param passFail
315          * @param resultReturned
316          */
317         private static void printResult(long id, String testFor,
318                         String testCaseDescription, String passFail, String resultReturned) {
319                 // TODO Auto-generated method stub
320                 printResult(id, testFor, testCaseDescription, passFail);
321                 System.out.println(resultReturned);
322
323         }
324
325         /**
326          * This function prints in output in required format.
327          * 
328          * @param id
329          * @param testFor
330          * @param testCaseDescription
331          * @param result
332          */
333         private static void printResult(long id, String testFor,
334                         String testCaseDescription, String result) {
335                 System.out.println(result + " - Test Case " + id + " - Test type: "
336                                 + testFor + " - " + testCaseDescription);
337         }
338
339         /**
340          * This function compares the required expected output and received output
341          * and returns PASS if expected output and received output matches
342          * 
343          * @param expectedResult
344          * @param resultReceived
345          * @return
346          */
347         private static String compareResults(ArrayList<String> expectedResult,
348                         ArrayList<String> resultReceived) {
349                 // TODO Auto-generated method stub
350                 String returnString = "";
351                 int index;
352 //              System.out.println(expectedResult.size());
353 //              System.out.println(resultReceived.size());
354                 for (index = 0; index < expectedResult.size()
355                                 || index < resultReceived.size(); index++) {
356                         if (index < expectedResult.size() && index < resultReceived.size()) {
357                                 if (!expectedResult.get(index)
358                                                 .equals(resultReceived.get(index))) {
359                                         //returnString = "FAILED";
360                                         returnString += "\tExpected Output: "
361                                                         + expectedResult.get(index)
362                                                         + ",\n\tOutput Received: "
363                                                         + resultReceived.get(index)+"\n";
364 //                              
365                                         //System.out.println(resultReceived.get(index));
366                                 } 
367
368                         } else {
369                                 if (index >= expectedResult.size()) {
370                                         returnString += "\tExpected Size of output: "
371                                                         + expectedResult.size()
372                                                         + ", Size of output received: "
373                                                         + resultReceived.size()
374                                                         + "\n\tExpected Output: none,\n\tOutput Received: "
375                                                         + resultReceived.get(index)+"\n";
376
377                                 } else {
378                                         if (index >= resultReceived.size()) {
379                                                 returnString += "\tExpected Size of output: "
380                                                                 + expectedResult.size()
381                                                                 + ", Size of output received: "
382                                                                 + resultReceived.size()
383                                                                 + "\n\tExpected Output: "
384                                                                 + expectedResult.get(index)
385                                                                 + ",\n\tOutput Received: none\n";
386
387                                         }
388                                 }
389                         }
390
391                 }
392                 if(index==expectedResult.size() && index==resultReceived.size() && returnString.equals("")){
393                         returnString="PASSED";
394                 }
395                 return returnString;
396
397         }
398 }