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=========================================================
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.
27 * Copyright (c) 2014 AT&T Knowledge Ventures
28 * Unpublished and Not for Publication
31 package org.onap.policyengine;
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;
45 import org.json.simple.JSONArray;
46 import org.json.simple.JSONObject;
47 import org.json.simple.parser.JSONParser;
48 import org.onap.policy.api.PolicyEngine;
49 import org.onap.policy.api.PolicyEngineException;
50 import org.onap.policy.common.logging.flexlogger.FlexLogger;
51 import org.onap.policy.common.logging.flexlogger.Logger;
54 * Class reads from .testCases file and run the test cases available in the file
55 * and generates output for each test cases specifing whether is passed or fail
56 * and reason why it fails.
62 public class GeneralTestClient {
64 private static final Logger LOGGER = FlexLogger.getLogger(GeneralTestClient.class);
66 static int totalTC = 0, passTC = 0, failTC = 0;
68 public static void main(String[] args) {
70 /* command line arguments */
71 if (args.length != 0) {
72 for (int index = 0; index < args.length; index++) {
73 // System.out.println(args[index]);
74 file = Paths.get(args[index]);
75 runTestClientOnConfigFile(file);
79 file = Paths.get("input.testCases");
80 runTestClientOnConfigFile(file);
83 .println("###############################################################################################");
84 System.out.println("\n\t SUMMARY: TOTAL: " + totalTC + ",\tPASS: "
85 + passTC + ",\tFAIL: " + failTC + "\n");
87 .println("###############################################################################################");
89 System.out.println("Enter a any key to exit");
92 } catch (IOException e) {
99 * This function reads the files passed as arguments and runs the test cases
104 private static void runTestClientOnConfigFile(Path file) {
105 String resultReturned, onapComponentName;
106 int totalTCforFile = 0, passTCforFile = 0, failTCforFile = 0;
108 .println("\n###############################################################################################");
109 System.out.println("\tRuning test Client on Config file: " + file);
111 .println("###############################################################################################\n");
113 if (Files.notExists(file)) {
114 System.out.println("file doesnot exist");
116 // PolicyEngineException("File doesn't exist in the specified Path "
117 // + file.toString());
118 } else if (file.toString().endsWith(".testCases")) {
120 // read the json file
121 FileReader reader = new FileReader(file.toString());
123 JSONParser jsonParser = new JSONParser();
124 JSONArray jsonObjectArray = (JSONArray) jsonParser
126 for (Object jsonObject : jsonObjectArray) {
129 ArrayList<String> expectedResult = new ArrayList<>();
130 ArrayList<String> resultReceived = new ArrayList<>();
131 JSONObject testCase = (JSONObject) jsonObject;
132 // get a String from the JSON object
133 long id = (long) testCase.get("id");
134 String testFor = (String) testCase.get("testFor");
135 String testCaseDescription = (String) testCase
136 .get("testCaseDescription");
137 JSONArray expectedResultJson = (JSONArray) testCase
138 .get("expectedResult");
139 @SuppressWarnings("rawtypes")
140 Iterator i = expectedResultJson.iterator();
141 while (i.hasNext()) {
142 expectedResult.add((String) i.next());
144 String pdp_urlConfigFile = (String) testCase
145 .get("PDP_URLConfigFile");
146 // System.out.println(pdp_urlConfigFile);
147 PolicyEngine policyEngine;
149 policyEngine = new PolicyEngine(pdp_urlConfigFile);
154 onapComponentName = (String) testCase
156 String configName = (String) testCase
158 Map<String, String> configAttributes = new HashMap<>();
159 configAttributes.put("key", "value");
160 JSONArray configAttributesJSON = (JSONArray) testCase
161 .get("configAttributes");
162 if(configAttributesJSON!=null){
163 i = configAttributesJSON.iterator();
164 while (i.hasNext()) {
165 JSONObject innerObj = (JSONObject) i.next();
166 configAttributes.put(
167 (String) innerObj.get("key"),
168 (String) innerObj.get("value"));
172 configAttributes = null;
174 resultReceived = PolicyEngineTestClient.getConfig(
175 policyEngine, onapComponentName,
176 configName, configAttributes);
177 Collections.sort(expectedResult);
178 Collections.sort(resultReceived);
179 resultReturned = compareResults(expectedResult,resultReceived);
180 if (resultReturned.equals("PASSED")) {
181 printResult(id, testFor, testCaseDescription,
186 printResult(id, testFor, testCaseDescription,
187 "FAILED", resultReturned);
194 Map<String, String> eventAttributes = new HashMap<>();
195 eventAttributes.put("Key", "Value");
196 JSONArray eventAttributesJSON = (JSONArray) testCase
197 .get("eventAttributes");
198 if(eventAttributesJSON != null){
199 i = eventAttributesJSON.iterator();
200 while (i.hasNext()) {
201 JSONObject innerObj = (JSONObject) i.next();
203 (String) innerObj.get("key"),
204 (String) innerObj.get("value"));
207 eventAttributes=null;
209 resultReceived = PolicyEngineTestClient.getAction(
210 policyEngine, eventAttributes);
211 Collections.sort(expectedResult);
212 Collections.sort(resultReceived);
213 resultReturned = compareResults(expectedResult,
215 if (resultReturned.equals("PASSED")) {
216 printResult(id, testFor, testCaseDescription,
221 printResult(id, testFor, testCaseDescription,
222 "FAILED", resultReturned);
229 onapComponentName = (String) testCase
231 Map<String, String> decisionAttributes = new HashMap<>();
232 decisionAttributes.put("Key", "Value");
233 JSONArray decisionAttributesJSON = (JSONArray) testCase
234 .get("decisionAttributes");
235 i = decisionAttributesJSON.iterator();
236 while (i.hasNext()) {
237 JSONObject innerObj = (JSONObject) i.next();
238 decisionAttributes.put(
239 (String) innerObj.get("key"),
240 (String) innerObj.get("value"));
244 resultReceived = PolicyEngineTestClient
245 .getDecision(policyEngine,
248 Collections.sort(expectedResult);
249 Collections.sort(resultReceived);
250 resultReturned = compareResults(expectedResult,
252 if (resultReturned.equals("PASSED")) {
253 printResult(id, testFor, testCaseDescription,
258 printResult(id, testFor, testCaseDescription,
259 "FAILED", resultReturned);
265 // case "getManualNotification":
266 // PolicyEngineTestClient
267 // .getManualNotifications(org.onap.policyEngine);
269 // case "getAutoNotification":
270 // PolicyEngineTestClient
271 // .getAutoNotifications(org.onap.policyEngine);
275 printResult(id, testFor, testCaseDescription,
276 "FAILED", "\tINVAILD TEST CASE.");
282 } catch (PolicyEngineException e) {
283 printResult(id, testFor, testCaseDescription, "FAILED");
286 LOGGER.error("Exception Occured"+e);
287 } catch (Exception e) {
288 printResult(id, testFor, testCaseDescription, "FAILED");
291 LOGGER.error("Exception Occured"+e);
295 } catch (FileNotFoundException ex) {
296 LOGGER.error("Exception Occured due to File not found"+ex);
297 } catch (IOException ex) {
298 LOGGER.error("Exception Occured"+ex);
299 } catch (NullPointerException ex) {
300 LOGGER.error("Exception Occured due to Null Pointer"+ex);
301 } catch (org.json.simple.parser.ParseException e) {
302 LOGGER.error("Exception Occured while Parsing"+e);
305 System.out.println("\n\n\t Summary for the file: TOTAL: "
306 + totalTCforFile + ",\tPASS: " + passTCforFile + ",\tFAIL: "
307 + failTCforFile + "\n");
311 * This function prints the reason if test fails.
315 * @param testCaseDescription
317 * @param resultReturned
319 private static void printResult(long id, String testFor,
320 String testCaseDescription, String passFail, String resultReturned) {
321 // TODO Auto-generated method stub
322 printResult(id, testFor, testCaseDescription, passFail);
323 System.out.println(resultReturned);
328 * This function prints in output in required format.
332 * @param testCaseDescription
335 private static void printResult(long id, String testFor,
336 String testCaseDescription, String result) {
337 System.out.println(result + " - Test Case " + id + " - Test type: "
338 + testFor + " - " + testCaseDescription);
342 * This function compares the required expected output and received output
343 * and returns PASS if expected output and received output matches
345 * @param expectedResult
346 * @param resultReceived
349 private static String compareResults(ArrayList<String> expectedResult,
350 ArrayList<String> resultReceived) {
351 // TODO Auto-generated method stub
352 String returnString = "";
354 // System.out.println(expectedResult.size());
355 // System.out.println(resultReceived.size());
356 for (index = 0; index < expectedResult.size()
357 || index < resultReceived.size(); index++) {
358 if (index < expectedResult.size() && index < resultReceived.size()) {
359 if (!expectedResult.get(index)
360 .equals(resultReceived.get(index))) {
361 //returnString = "FAILED";
362 returnString += "\tExpected Output: "
363 + expectedResult.get(index)
364 + ",\n\tOutput Received: "
365 + resultReceived.get(index)+"\n";
367 //System.out.println(resultReceived.get(index));
371 if (index >= expectedResult.size()) {
372 returnString += "\tExpected Size of output: "
373 + expectedResult.size()
374 + ", Size of output received: "
375 + resultReceived.size()
376 + "\n\tExpected Output: none,\n\tOutput Received: "
377 + resultReceived.get(index)+"\n";
380 if (index >= resultReceived.size()) {
381 returnString += "\tExpected Size of output: "
382 + expectedResult.size()
383 + ", Size of output received: "
384 + resultReceived.size()
385 + "\n\tExpected Output: "
386 + expectedResult.get(index)
387 + ",\n\tOutput Received: none\n";
394 if(index==expectedResult.size() && index==resultReceived.size() && returnString.equals("")){
395 returnString="PASSED";