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.openecomp.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.openecomp.policy.api.PolicyEngine;
49 import org.openecomp.policy.api.PolicyEngineException;
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.
60 public class GeneralTestClient {
61 static int totalTC = 0, passTC = 0, failTC = 0;
63 public static void main(String[] args) {
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);
74 file = Paths.get("input.testCases");
75 runTestClientOnConfigFile(file);
78 .println("###############################################################################################");
79 System.out.println("\n\t SUMMARY: TOTAL: " + totalTC + ",\tPASS: "
80 + passTC + ",\tFAIL: " + failTC + "\n");
82 .println("###############################################################################################");
84 System.out.println("Enter a any key to exit");
87 } catch (IOException e) {
94 * This function reads the files passed as arguments and runs the test cases
99 private static void runTestClientOnConfigFile(Path file) {
100 String resultReturned, eCOMPComponentName;
101 int totalTCforFile = 0, passTCforFile = 0, failTCforFile = 0;
103 .println("\n###############################################################################################");
104 System.out.println("\tRuning test Client on Config file: " + file);
106 .println("###############################################################################################\n");
108 if (Files.notExists(file)) {
109 System.out.println("file doesnot exist");
111 // PolicyEngineException("File doesn't exist in the specified Path "
112 // + file.toString());
113 } else if (file.toString().endsWith(".testCases")) {
115 // read the json file
116 FileReader reader = new FileReader(file.toString());
118 JSONParser jsonParser = new JSONParser();
119 JSONArray jsonObjectArray = (JSONArray) jsonParser
121 for (Object jsonObject : jsonObjectArray) {
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());
139 String pdp_urlConfigFile = (String) testCase
140 .get("PDP_URLConfigFile");
141 // System.out.println(pdp_urlConfigFile);
142 PolicyEngine policyEngine;
144 policyEngine = new PolicyEngine(pdp_urlConfigFile);
149 eCOMPComponentName = (String) testCase
151 String configName = (String) testCase
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"));
167 configAttributes = null;
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,
181 printResult(id, testFor, testCaseDescription,
182 "FAILED", resultReturned);
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();
198 (String) innerObj.get("key"),
199 (String) innerObj.get("value"));
202 eventAttributes=null;
204 resultReceived = PolicyEngineTestClient.getAction(
205 policyEngine, eventAttributes);
206 Collections.sort(expectedResult);
207 Collections.sort(resultReceived);
208 resultReturned = compareResults(expectedResult,
210 if (resultReturned.equals("PASSED")) {
211 printResult(id, testFor, testCaseDescription,
216 printResult(id, testFor, testCaseDescription,
217 "FAILED", resultReturned);
224 eCOMPComponentName = (String) testCase
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"));
239 resultReceived = PolicyEngineTestClient
240 .getDecision(policyEngine,
243 Collections.sort(expectedResult);
244 Collections.sort(resultReceived);
245 resultReturned = compareResults(expectedResult,
247 if (resultReturned.equals("PASSED")) {
248 printResult(id, testFor, testCaseDescription,
253 printResult(id, testFor, testCaseDescription,
254 "FAILED", resultReturned);
260 // case "getManualNotification":
261 // PolicyEngineTestClient
262 // .getManualNotifications(org.openecomp.policyEngine);
264 // case "getAutoNotification":
265 // PolicyEngineTestClient
266 // .getAutoNotifications(org.openecomp.policyEngine);
270 printResult(id, testFor, testCaseDescription,
271 "FAILED", "\tINVAILD TEST CASE.");
277 } catch (PolicyEngineException e) {
278 // TODO Auto-generated catch block
279 printResult(id, testFor, testCaseDescription, "FAILED");
283 } catch (Exception e) {
284 // TODO Auto-generated catch block
285 printResult(id, testFor, testCaseDescription, "FAILED");
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
303 System.out.println("\n\n\t Summary for the file: TOTAL: "
304 + totalTCforFile + ",\tPASS: " + passTCforFile + ",\tFAIL: "
305 + failTCforFile + "\n");
309 * This function prints the reason if test fails.
313 * @param testCaseDescription
315 * @param resultReturned
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);
326 * This function prints in output in required format.
330 * @param testCaseDescription
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);
340 * This function compares the required expected output and received output
341 * and returns PASS if expected output and received output matches
343 * @param expectedResult
344 * @param resultReceived
347 private static String compareResults(ArrayList<String> expectedResult,
348 ArrayList<String> resultReceived) {
349 // TODO Auto-generated method stub
350 String returnString = "";
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";
365 //System.out.println(resultReceived.get(index));
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";
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";
392 if(index==expectedResult.size() && index==resultReceived.size() && returnString.equals("")){
393 returnString="PASSED";