2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policyengine;
22 import java.io.FileNotFoundException;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.Iterator;
34 import org.json.simple.JSONArray;
35 import org.json.simple.JSONObject;
36 import org.json.simple.parser.JSONParser;
37 import org.onap.policy.api.PolicyEngine;
38 import org.onap.policy.api.PolicyEngineException;
39 import org.onap.policy.common.logging.flexlogger.FlexLogger;
40 import org.onap.policy.common.logging.flexlogger.Logger;
43 * Class reads from .testCases file and run the test cases available in the file
44 * and generates output for each test cases specifing whether is passed or fail
45 * and reason why it fails.
51 public class GeneralTestClient {
53 private static final Logger LOGGER = FlexLogger.getLogger(GeneralTestClient.class);
55 static int totalTC = 0, passTC = 0, failTC = 0;
57 public static void main(String[] args) {
59 /* command line arguments */
60 if (args.length != 0) {
61 for (int index = 0; index < args.length; index++) {
62 // System.out.println(args[index]);
63 file = Paths.get(args[index]);
64 runTestClientOnConfigFile(file);
68 file = Paths.get("input.testCases");
69 runTestClientOnConfigFile(file);
72 .println("###############################################################################################");
73 System.out.println("\n\t SUMMARY: TOTAL: " + totalTC + ",\tPASS: "
74 + passTC + ",\tFAIL: " + failTC + "\n");
76 .println("###############################################################################################");
78 System.out.println("Enter a any key to exit");
81 } catch (IOException e) {
88 * This function reads the files passed as arguments and runs the test cases
93 private static void runTestClientOnConfigFile(Path file) {
94 String resultReturned, onapComponentName;
95 int totalTCforFile = 0, passTCforFile = 0, failTCforFile = 0;
97 .println("\n###############################################################################################");
98 System.out.println("\tRuning test Client on Config file: " + file);
100 .println("###############################################################################################\n");
102 if (Files.notExists(file)) {
103 System.out.println("file doesnot exist");
105 // PolicyEngineException("File doesn't exist in the specified Path "
106 // + file.toString());
107 } else if (file.toString().endsWith(".testCases")) {
109 // read the json file
110 FileReader reader = new FileReader(file.toString());
112 JSONParser jsonParser = new JSONParser();
113 JSONArray jsonObjectArray = (JSONArray) jsonParser
115 for (Object jsonObject : jsonObjectArray) {
118 ArrayList<String> expectedResult = new ArrayList<>();
119 ArrayList<String> resultReceived = new ArrayList<>();
120 JSONObject testCase = (JSONObject) jsonObject;
121 // get a String from the JSON object
122 long id = (long) testCase.get("id");
123 String testFor = (String) testCase.get("testFor");
124 String testCaseDescription = (String) testCase
125 .get("testCaseDescription");
126 JSONArray expectedResultJson = (JSONArray) testCase
127 .get("expectedResult");
128 @SuppressWarnings("rawtypes")
129 Iterator i = expectedResultJson.iterator();
130 while (i.hasNext()) {
131 expectedResult.add((String) i.next());
133 String pdp_urlConfigFile = (String) testCase
134 .get("PDP_URLConfigFile");
135 // System.out.println(pdp_urlConfigFile);
136 PolicyEngine policyEngine;
138 policyEngine = new PolicyEngine(pdp_urlConfigFile);
143 onapComponentName = (String) testCase
145 String configName = (String) testCase
147 Map<String, String> configAttributes = new HashMap<>();
148 configAttributes.put("key", "value");
149 JSONArray configAttributesJSON = (JSONArray) testCase
150 .get("configAttributes");
151 if(configAttributesJSON!=null){
152 i = configAttributesJSON.iterator();
153 while (i.hasNext()) {
154 JSONObject innerObj = (JSONObject) i.next();
155 configAttributes.put(
156 (String) innerObj.get("key"),
157 (String) innerObj.get("value"));
161 configAttributes = null;
163 resultReceived = PolicyEngineTestClient.getConfig(
164 policyEngine, onapComponentName,
165 configName, configAttributes);
166 Collections.sort(expectedResult);
167 Collections.sort(resultReceived);
168 resultReturned = compareResults(expectedResult,resultReceived);
169 if (resultReturned.equals("PASSED")) {
170 printResult(id, testFor, testCaseDescription,
175 printResult(id, testFor, testCaseDescription,
176 "FAILED", resultReturned);
183 Map<String, String> eventAttributes = new HashMap<>();
184 eventAttributes.put("Key", "Value");
185 JSONArray eventAttributesJSON = (JSONArray) testCase
186 .get("eventAttributes");
187 if(eventAttributesJSON != null){
188 i = eventAttributesJSON.iterator();
189 while (i.hasNext()) {
190 JSONObject innerObj = (JSONObject) i.next();
192 (String) innerObj.get("key"),
193 (String) innerObj.get("value"));
196 eventAttributes=null;
198 resultReceived = PolicyEngineTestClient.getAction(
199 policyEngine, eventAttributes);
200 Collections.sort(expectedResult);
201 Collections.sort(resultReceived);
202 resultReturned = compareResults(expectedResult,
204 if (resultReturned.equals("PASSED")) {
205 printResult(id, testFor, testCaseDescription,
210 printResult(id, testFor, testCaseDescription,
211 "FAILED", resultReturned);
218 onapComponentName = (String) testCase
220 Map<String, String> decisionAttributes = new HashMap<>();
221 decisionAttributes.put("Key", "Value");
222 JSONArray decisionAttributesJSON = (JSONArray) testCase
223 .get("decisionAttributes");
224 i = decisionAttributesJSON.iterator();
225 while (i.hasNext()) {
226 JSONObject innerObj = (JSONObject) i.next();
227 decisionAttributes.put(
228 (String) innerObj.get("key"),
229 (String) innerObj.get("value"));
233 resultReceived = PolicyEngineTestClient
234 .getDecision(policyEngine,
237 Collections.sort(expectedResult);
238 Collections.sort(resultReceived);
239 resultReturned = compareResults(expectedResult,
241 if (resultReturned.equals("PASSED")) {
242 printResult(id, testFor, testCaseDescription,
247 printResult(id, testFor, testCaseDescription,
248 "FAILED", resultReturned);
254 // case "getManualNotification":
255 // PolicyEngineTestClient
256 // .getManualNotifications(org.onap.policyEngine);
258 // case "getAutoNotification":
259 // PolicyEngineTestClient
260 // .getAutoNotifications(org.onap.policyEngine);
264 printResult(id, testFor, testCaseDescription,
265 "FAILED", "\tINVAILD TEST CASE.");
271 } catch (PolicyEngineException e) {
272 printResult(id, testFor, testCaseDescription, "FAILED");
275 LOGGER.error("Exception Occured"+e);
276 } catch (Exception e) {
277 printResult(id, testFor, testCaseDescription, "FAILED");
280 LOGGER.error("Exception Occured"+e);
284 } catch (FileNotFoundException ex) {
285 LOGGER.error("Exception Occured due to File not found"+ex);
286 } catch (IOException ex) {
287 LOGGER.error("Exception Occured"+ex);
288 } catch (NullPointerException ex) {
289 LOGGER.error("Exception Occured due to Null Pointer"+ex);
290 } catch (org.json.simple.parser.ParseException e) {
291 LOGGER.error("Exception Occured while Parsing"+e);
294 System.out.println("\n\n\t Summary for the file: TOTAL: "
295 + totalTCforFile + ",\tPASS: " + passTCforFile + ",\tFAIL: "
296 + failTCforFile + "\n");
300 * This function prints the reason if test fails.
304 * @param testCaseDescription
306 * @param resultReturned
308 private static void printResult(long id, String testFor,
309 String testCaseDescription, String passFail, String resultReturned) {
310 // TODO Auto-generated method stub
311 printResult(id, testFor, testCaseDescription, passFail);
312 System.out.println(resultReturned);
317 * This function prints in output in required format.
321 * @param testCaseDescription
324 private static void printResult(long id, String testFor,
325 String testCaseDescription, String result) {
326 System.out.println(result + " - Test Case " + id + " - Test type: "
327 + testFor + " - " + testCaseDescription);
331 * This function compares the required expected output and received output
332 * and returns PASS if expected output and received output matches
334 * @param expectedResult
335 * @param resultReceived
338 private static String compareResults(ArrayList<String> expectedResult,
339 ArrayList<String> resultReceived) {
340 // TODO Auto-generated method stub
341 String returnString = "";
343 // System.out.println(expectedResult.size());
344 // System.out.println(resultReceived.size());
345 for (index = 0; index < expectedResult.size()
346 || index < resultReceived.size(); index++) {
347 if (index < expectedResult.size() && index < resultReceived.size()) {
348 if (!expectedResult.get(index)
349 .equals(resultReceived.get(index))) {
350 //returnString = "FAILED";
351 returnString += "\tExpected Output: "
352 + expectedResult.get(index)
353 + ",\n\tOutput Received: "
354 + resultReceived.get(index)+"\n";
356 //System.out.println(resultReceived.get(index));
360 if (index >= expectedResult.size()) {
361 returnString += "\tExpected Size of output: "
362 + expectedResult.size()
363 + ", Size of output received: "
364 + resultReceived.size()
365 + "\n\tExpected Output: none,\n\tOutput Received: "
366 + resultReceived.get(index)+"\n";
369 if (index >= resultReceived.size()) {
370 returnString += "\tExpected Size of output: "
371 + expectedResult.size()
372 + ", Size of output received: "
373 + resultReceived.size()
374 + "\n\tExpected Output: "
375 + expectedResult.get(index)
376 + ",\n\tOutput Received: none\n";
383 if(index==expectedResult.size() && index==resultReceived.size() && returnString.equals("")){
384 returnString="PASSED";