aboutsummaryrefslogtreecommitdiffstats
path: root/PolicyEngineClient/src/test/java/org/onap/policyengine/GeneralTestClient.java
diff options
context:
space:
mode:
authorPamela Dragosh <pdragosh@research.att.com>2019-10-18 09:25:01 -0400
committerPamela Dragosh <pdragosh@research.att.com>2019-10-22 07:40:20 -0400
commit18998fee10abecb2ff1b2cdc3b99a3115d7de77c (patch)
tree0a0d24f34913cd66616a48799c87c4dc4827ed57 /PolicyEngineClient/src/test/java/org/onap/policyengine/GeneralTestClient.java
parent3c4b4fcb076e90f2d2a2582d27f316e60dba6ac3 (diff)
Format source code PolicyEngineClient
Formatting and sonar issues. Looks like this is all code used for demonstrating how to call the API's. Uses all main so actually no JUnit is being called. Unfortunately there are no associated test files for it. Would be interesting to see if adding a JUnit to call it with some test code would help to improve overall coverage. Future work. Issue-ID: POLICY-2145 Change-Id: I0d0f6d8898076a92d342553f45f70a56fd9d49f6 Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
Diffstat (limited to 'PolicyEngineClient/src/test/java/org/onap/policyengine/GeneralTestClient.java')
-rw-r--r--PolicyEngineClient/src/test/java/org/onap/policyengine/GeneralTestClient.java653
1 files changed, 311 insertions, 342 deletions
diff --git a/PolicyEngineClient/src/test/java/org/onap/policyengine/GeneralTestClient.java b/PolicyEngineClient/src/test/java/org/onap/policyengine/GeneralTestClient.java
index 4c5a262df..77a12279a 100644
--- a/PolicyEngineClient/src/test/java/org/onap/policyengine/GeneralTestClient.java
+++ b/PolicyEngineClient/src/test/java/org/onap/policyengine/GeneralTestClient.java
@@ -2,14 +2,14 @@
* ============LICENSE_START=======================================================
* PolicyEngineClient
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -17,6 +17,7 @@
* limitations under the License.
* ============LICENSE_END=========================================================
*/
+
package org.onap.policyengine;
import java.io.FileNotFoundException;
@@ -43,347 +44,315 @@ import org.onap.policy.common.logging.flexlogger.Logger;
* Class reads from .testCases file and run the test cases available in the file
* and generates output for each test cases specifing whether is passed or fail
* and reason why it fails.
- *
- *
+ *
+ *
* @version 1.0
*
*/
public class GeneralTestClient {
-
- private static final Logger LOGGER = FlexLogger.getLogger(GeneralTestClient.class);
-
- static int totalTC = 0, passTC = 0, failTC = 0;
-
- public static void main(String[] args) {
- Path file;
- /* command line arguments */
- if (args.length != 0) {
- for (int index = 0; index < args.length; index++) {
- // System.out.println(args[index]);
- file = Paths.get(args[index]);
- runTestClientOnConfigFile(file);
- }
- } else {
- /* default file */
- file = Paths.get("input.testCases");
- runTestClientOnConfigFile(file);
- }
- System.out
- .println("###############################################################################################");
- System.out.println("\n\t SUMMARY: TOTAL: " + totalTC + ",\tPASS: "
- + passTC + ",\tFAIL: " + failTC + "\n");
- System.out
- .println("###############################################################################################");
-
- System.out.println("Enter a any key to exit");
- try {
- System.in.read();
- } catch (IOException e) {
- //
- }
-
- }
-
- /**
- * This function reads the files passed as arguments and runs the test cases
- * in the file
- *
- * @param file
- */
- private static void runTestClientOnConfigFile(Path file) {
- String resultReturned, onapComponentName;
- int totalTCforFile = 0, passTCforFile = 0, failTCforFile = 0;
- System.out
- .println("\n###############################################################################################");
- System.out.println("\tRuning test Client on Config file: " + file);
- System.out
- .println("###############################################################################################\n");
-
- if (Files.notExists(file)) {
- System.out.println("file doesnot exist");
- // throw new
- // PolicyEngineException("File doesn't exist in the specified Path "
- // + file.toString());
- } else if (file.toString().endsWith(".testCases")) {
- try {
- // read the json file
- FileReader reader = new FileReader(file.toString());
-
- JSONParser jsonParser = new JSONParser();
- JSONArray jsonObjectArray = (JSONArray) jsonParser
- .parse(reader);
- for (Object jsonObject : jsonObjectArray) {
- totalTC++;
- totalTCforFile++;
- ArrayList<String> expectedResult = new ArrayList<>();
- ArrayList<String> resultReceived = new ArrayList<>();
- JSONObject testCase = (JSONObject) jsonObject;
- // get a String from the JSON object
- long id = (long) testCase.get("id");
- String testFor = (String) testCase.get("testFor");
- String testCaseDescription = (String) testCase
- .get("testCaseDescription");
- JSONArray expectedResultJson = (JSONArray) testCase
- .get("expectedResult");
- @SuppressWarnings("rawtypes")
- Iterator i = expectedResultJson.iterator();
- while (i.hasNext()) {
- expectedResult.add((String) i.next());
- }
- String pdp_urlConfigFile = (String) testCase
- .get("PDP_URLConfigFile");
- // System.out.println(pdp_urlConfigFile);
- PolicyEngine policyEngine;
- try {
- policyEngine = new PolicyEngine(pdp_urlConfigFile);
-
- switch (testFor) {
-
- case "getConfig":
- onapComponentName = (String) testCase
- .get("ONAPName");
- String configName = (String) testCase
- .get("ConfigName");
- Map<String, String> configAttributes = new HashMap<>();
- configAttributes.put("key", "value");
- JSONArray configAttributesJSON = (JSONArray) testCase
- .get("configAttributes");
- if(configAttributesJSON!=null){
- i = configAttributesJSON.iterator();
- while (i.hasNext()) {
- JSONObject innerObj = (JSONObject) i.next();
- configAttributes.put(
- (String) innerObj.get("key"),
- (String) innerObj.get("value"));
-
- }
- }else{
- configAttributes = null;
- }
- resultReceived = PolicyEngineTestClient.getConfig(
- policyEngine, onapComponentName,
- configName, configAttributes);
- Collections.sort(expectedResult);
- Collections.sort(resultReceived);
- resultReturned = compareResults(expectedResult,resultReceived);
- if (resultReturned.equals("PASSED")) {
- printResult(id, testFor, testCaseDescription,
- "PASSED");
- passTCforFile++;
- passTC++;
- } else {
- printResult(id, testFor, testCaseDescription,
- "FAILED", resultReturned);
- failTCforFile++;
- failTC++;
- }
- break;
-
- case "getAction":
- Map<String, String> eventAttributes = new HashMap<>();
- eventAttributes.put("Key", "Value");
- JSONArray eventAttributesJSON = (JSONArray) testCase
- .get("eventAttributes");
- if(eventAttributesJSON != null){
- i = eventAttributesJSON.iterator();
- while (i.hasNext()) {
- JSONObject innerObj = (JSONObject) i.next();
- eventAttributes.put(
- (String) innerObj.get("key"),
- (String) innerObj.get("value"));
- }
- }else{
- eventAttributes=null;
- }
- resultReceived = PolicyEngineTestClient.getAction(
- policyEngine, eventAttributes);
- Collections.sort(expectedResult);
- Collections.sort(resultReceived);
- resultReturned = compareResults(expectedResult,
- resultReceived);
- if (resultReturned.equals("PASSED")) {
- printResult(id, testFor, testCaseDescription,
- "PASSED");
- passTCforFile++;
- passTC++;
- } else {
- printResult(id, testFor, testCaseDescription,
- "FAILED", resultReturned);
- failTCforFile++;
- failTC++;
- }
- break;
-
- case "getDecision":
- onapComponentName = (String) testCase
- .get("ONAPName");
- Map<String, String> decisionAttributes = new HashMap<>();
- decisionAttributes.put("Key", "Value");
- JSONArray decisionAttributesJSON = (JSONArray) testCase
- .get("decisionAttributes");
- i = decisionAttributesJSON.iterator();
- while (i.hasNext()) {
- JSONObject innerObj = (JSONObject) i.next();
- decisionAttributes.put(
- (String) innerObj.get("key"),
- (String) innerObj.get("value"));
-
- }
-
- resultReceived = PolicyEngineTestClient
- .getDecision(policyEngine,
- onapComponentName,
- decisionAttributes);
- Collections.sort(expectedResult);
- Collections.sort(resultReceived);
- resultReturned = compareResults(expectedResult,
- resultReceived);
- if (resultReturned.equals("PASSED")) {
- printResult(id, testFor, testCaseDescription,
- "PASSED");
- passTCforFile++;
- passTC++;
- } else {
- printResult(id, testFor, testCaseDescription,
- "FAILED", resultReturned);
- failTCforFile++;
- failTC++;
- }
- break;
-
- // case "getManualNotification":
- // PolicyEngineTestClient
- // .getManualNotifications(org.onap.policyEngine);
- // break;
- // case "getAutoNotification":
- // PolicyEngineTestClient
- // .getAutoNotifications(org.onap.policyEngine);
- // break;
-
- default:
- printResult(id, testFor, testCaseDescription,
- "FAILED", "\tINVAILD TEST CASE.");
- failTCforFile++;
- failTC++;
- break;
-
- }
- } catch (PolicyEngineException e) {
- printResult(id, testFor, testCaseDescription, "FAILED");
- failTCforFile++;
- failTC++;
- LOGGER.error("Exception Occured"+e);
- } catch (Exception e) {
- printResult(id, testFor, testCaseDescription, "FAILED");
- failTCforFile++;
- failTC++;
- LOGGER.error("Exception Occured"+e);
- }
- }
-
- } catch (FileNotFoundException ex) {
- LOGGER.error("Exception Occured due to File not found"+ex);
- } catch (IOException ex) {
- LOGGER.error("Exception Occured"+ex);
- } catch (NullPointerException ex) {
- LOGGER.error("Exception Occured due to Null Pointer"+ex);
- } catch (org.json.simple.parser.ParseException e) {
- LOGGER.error("Exception Occured while Parsing"+e);
- }
- }
- System.out.println("\n\n\t Summary for the file: TOTAL: "
- + totalTCforFile + ",\tPASS: " + passTCforFile + ",\tFAIL: "
- + failTCforFile + "\n");
- }
-
- /**
- * This function prints the reason if test fails.
- *
- * @param id
- * @param testFor
- * @param testCaseDescription
- * @param passFail
- * @param resultReturned
- */
- private static void printResult(long id, String testFor,
- String testCaseDescription, String passFail, String resultReturned) {
- // TODO Auto-generated method stub
- printResult(id, testFor, testCaseDescription, passFail);
- System.out.println(resultReturned);
-
- }
-
- /**
- * This function prints in output in required format.
- *
- * @param id
- * @param testFor
- * @param testCaseDescription
- * @param result
- */
- private static void printResult(long id, String testFor,
- String testCaseDescription, String result) {
- System.out.println(result + " - Test Case " + id + " - Test type: "
- + testFor + " - " + testCaseDescription);
- }
-
- /**
- * This function compares the required expected output and received output
- * and returns PASS if expected output and received output matches
- *
- * @param expectedResult
- * @param resultReceived
- * @return
- */
- private static String compareResults(ArrayList<String> expectedResult,
- ArrayList<String> resultReceived) {
- // TODO Auto-generated method stub
- String returnString = "";
- int index;
-// System.out.println(expectedResult.size());
-// System.out.println(resultReceived.size());
- for (index = 0; index < expectedResult.size()
- || index < resultReceived.size(); index++) {
- if (index < expectedResult.size() && index < resultReceived.size()) {
- if (!expectedResult.get(index)
- .equals(resultReceived.get(index))) {
- //returnString = "FAILED";
- returnString += "\tExpected Output: "
- + expectedResult.get(index)
- + ",\n\tOutput Received: "
- + resultReceived.get(index)+"\n";
-//
- //System.out.println(resultReceived.get(index));
- }
-
- } else {
- if (index >= expectedResult.size()) {
- returnString += "\tExpected Size of output: "
- + expectedResult.size()
- + ", Size of output received: "
- + resultReceived.size()
- + "\n\tExpected Output: none,\n\tOutput Received: "
- + resultReceived.get(index)+"\n";
-
- } else {
- if (index >= resultReceived.size()) {
- returnString += "\tExpected Size of output: "
- + expectedResult.size()
- + ", Size of output received: "
- + resultReceived.size()
- + "\n\tExpected Output: "
- + expectedResult.get(index)
- + ",\n\tOutput Received: none\n";
-
- }
- }
- }
-
- }
- if(index==expectedResult.size() && index==resultReceived.size() && returnString.equals("")){
- returnString="PASSED";
- }
- return returnString;
-
- }
+
+ private static final Logger LOGGER = FlexLogger.getLogger(GeneralTestClient.class);
+
+ static int totalTC = 0;
+ static int passTC = 0;
+ static int failTC = 0;
+
+ /**
+ * main.
+ *
+ * @param args String[] args
+ */
+ public static void main(String[] args) {
+ Path file;
+ /* command line arguments */
+ if (args.length != 0) {
+ for (int index = 0; index < args.length; index++) {
+ // System.out.println(args[index]);
+ file = Paths.get(args[index]);
+ runTestClientOnConfigFile(file);
+ }
+ } else {
+ /* default file */
+ file = Paths.get("input.testCases");
+ runTestClientOnConfigFile(file);
+ }
+ System.out.println(
+ "###############################################################################################");
+ System.out.println("\n\t SUMMARY: TOTAL: " + totalTC + ",\tPASS: " + passTC + ",\tFAIL: " + failTC + "\n");
+ System.out.println(
+ "###############################################################################################");
+
+ System.out.println("Enter a any key to exit");
+ try {
+ System.in.read();
+ } catch (IOException e) {
+ //
+ }
+
+ }
+
+ /**
+ * This function reads the files passed as arguments and runs the test cases
+ * in the file.
+ *
+ * @param file Path
+ */
+ private static void runTestClientOnConfigFile(Path file) {
+ String resultReturned;
+ String onapComponentName;
+ int totalTCforFile = 0;
+ int passTCforFile = 0;
+ int failTCforFile = 0;
+ System.out.println(
+ "\n###############################################################################################");
+ System.out.println("\tRuning test Client on Config file: " + file);
+ System.out.println(
+ "###############################################################################################\n");
+
+ if (Files.notExists(file)) {
+ System.out.println("file doesnot exist");
+ // throw new
+ // PolicyEngineException("File doesn't exist in the specified Path "
+ // + file.toString());
+ } else if (file.toString().endsWith(".testCases")) {
+ try {
+ // read the json file
+ FileReader reader = new FileReader(file.toString());
+
+ JSONParser jsonParser = new JSONParser();
+ JSONArray jsonObjectArray = (JSONArray) jsonParser.parse(reader);
+ for (Object jsonObject : jsonObjectArray) {
+ totalTC++;
+ totalTCforFile++;
+ ArrayList<String> expectedResult = new ArrayList<>();
+ ArrayList<String> resultReceived = new ArrayList<>();
+ JSONObject testCase = (JSONObject) jsonObject;
+ // get a String from the JSON object
+ long id = (long) testCase.get("id");
+ String testFor = (String) testCase.get("testFor");
+ String testCaseDescription = (String) testCase.get("testCaseDescription");
+ JSONArray expectedResultJson = (JSONArray) testCase.get("expectedResult");
+ @SuppressWarnings("rawtypes")
+ Iterator iter = expectedResultJson.iterator();
+ while (iter.hasNext()) {
+ expectedResult.add((String) iter.next());
+ }
+ String pdpUrlConfigFile = (String) testCase.get("PDP_URLConfigFile");
+ // System.out.println(pdp_urlConfigFile);
+ PolicyEngine policyEngine;
+ try {
+ policyEngine = new PolicyEngine(pdpUrlConfigFile);
+
+ switch (testFor) {
+
+ case "getConfig":
+ onapComponentName = (String) testCase.get("ONAPName");
+ String configName = (String) testCase.get("ConfigName");
+ Map<String, String> configAttributes = new HashMap<>();
+ configAttributes.put("key", "value");
+ JSONArray configAttributesJson = (JSONArray) testCase.get("configAttributes");
+ if (configAttributesJson != null) {
+ iter = configAttributesJson.iterator();
+ while (iter.hasNext()) {
+ JSONObject innerObj = (JSONObject) iter.next();
+ configAttributes.put((String) innerObj.get("key"),
+ (String) innerObj.get("value"));
+
+ }
+ } else {
+ configAttributes = null;
+ }
+ resultReceived = PolicyEngineTestClient.getConfig(policyEngine, onapComponentName,
+ configName, configAttributes);
+ Collections.sort(expectedResult);
+ Collections.sort(resultReceived);
+ resultReturned = compareResults(expectedResult, resultReceived);
+ if (resultReturned.equals("PASSED")) {
+ printResult(id, testFor, testCaseDescription, "PASSED");
+ passTCforFile++;
+ passTC++;
+ } else {
+ printResult(id, testFor, testCaseDescription, "FAILED", resultReturned);
+ failTCforFile++;
+ failTC++;
+ }
+ break;
+
+ case "getAction":
+ Map<String, String> eventAttributes = new HashMap<>();
+ eventAttributes.put("Key", "Value");
+ JSONArray eventAttributesJson = (JSONArray) testCase.get("eventAttributes");
+ if (eventAttributesJson != null) {
+ iter = eventAttributesJson.iterator();
+ while (iter.hasNext()) {
+ JSONObject innerObj = (JSONObject) iter.next();
+ eventAttributes.put((String) innerObj.get("key"),
+ (String) innerObj.get("value"));
+ }
+ } else {
+ eventAttributes = null;
+ }
+ resultReceived = PolicyEngineTestClient.getAction(policyEngine, eventAttributes);
+ Collections.sort(expectedResult);
+ Collections.sort(resultReceived);
+ resultReturned = compareResults(expectedResult, resultReceived);
+ if (resultReturned.equals("PASSED")) {
+ printResult(id, testFor, testCaseDescription, "PASSED");
+ passTCforFile++;
+ passTC++;
+ } else {
+ printResult(id, testFor, testCaseDescription, "FAILED", resultReturned);
+ failTCforFile++;
+ failTC++;
+ }
+ break;
+
+ case "getDecision":
+ onapComponentName = (String) testCase.get("ONAPName");
+ Map<String, String> decisionAttributes = new HashMap<>();
+ decisionAttributes.put("Key", "Value");
+ JSONArray decisionAttributesJson = (JSONArray) testCase.get("decisionAttributes");
+ iter = decisionAttributesJson.iterator();
+ while (iter.hasNext()) {
+ JSONObject innerObj = (JSONObject) iter.next();
+ decisionAttributes.put((String) innerObj.get("key"),
+ (String) innerObj.get("value"));
+
+ }
+
+ resultReceived = PolicyEngineTestClient.getDecision(policyEngine, onapComponentName,
+ decisionAttributes);
+ Collections.sort(expectedResult);
+ Collections.sort(resultReceived);
+ resultReturned = compareResults(expectedResult, resultReceived);
+ if (resultReturned.equals("PASSED")) {
+ printResult(id, testFor, testCaseDescription, "PASSED");
+ passTCforFile++;
+ passTC++;
+ } else {
+ printResult(id, testFor, testCaseDescription, "FAILED", resultReturned);
+ failTCforFile++;
+ failTC++;
+ }
+ break;
+
+ // case "getManualNotification":
+ // PolicyEngineTestClient
+ // .getManualNotifications(org.onap.policyEngine);
+ // break;
+ // case "getAutoNotification":
+ // PolicyEngineTestClient
+ // .getAutoNotifications(org.onap.policyEngine);
+ // break;
+
+ default:
+ printResult(id, testFor, testCaseDescription, "FAILED", "\tINVAILD TEST CASE.");
+ failTCforFile++;
+ failTC++;
+ break;
+
+ }
+ } catch (PolicyEngineException e) {
+ printResult(id, testFor, testCaseDescription, "FAILED");
+ failTCforFile++;
+ failTC++;
+ LOGGER.error("Exception Occured" + e);
+ } catch (Exception e) {
+ printResult(id, testFor, testCaseDescription, "FAILED");
+ failTCforFile++;
+ failTC++;
+ LOGGER.error("Exception Occured" + e);
+ }
+ }
+
+ } catch (FileNotFoundException ex) {
+ LOGGER.error("Exception Occured due to File not found" + ex);
+ } catch (IOException ex) {
+ LOGGER.error("Exception Occured" + ex);
+ } catch (NullPointerException ex) {
+ LOGGER.error("Exception Occured due to Null Pointer" + ex);
+ } catch (org.json.simple.parser.ParseException e) {
+ LOGGER.error("Exception Occured while Parsing" + e);
+ }
+ }
+ System.out.println("\n\n\t Summary for the file: TOTAL: " + totalTCforFile + ",\tPASS: " + passTCforFile
+ + ",\tFAIL: " + failTCforFile + "\n");
+ }
+
+ /**
+ * This function prints the reason if test fails.
+ *
+ * @param id long
+ * @param testFor String
+ * @param testCaseDescription String
+ * @param passFail String
+ * @param resultReturned String
+ */
+ private static void printResult(long id, String testFor, String testCaseDescription, String passFail,
+ String resultReturned) {
+ // TODO Auto-generated method stub
+ printResult(id, testFor, testCaseDescription, passFail);
+ System.out.println(resultReturned);
+
+ }
+
+ /**
+ * This function prints in output in required format.
+ *
+ * @param id long
+ * @param testFor String
+ * @param testCaseDescription String
+ * @param result String
+ */
+ private static void printResult(long id, String testFor, String testCaseDescription, String result) {
+ System.out.println(result + " - Test Case " + id + " - Test type: " + testFor + " - " + testCaseDescription);
+ }
+
+ /**
+ * This function compares the required expected output and received output
+ * and returns PASS if expected output and received output matches.
+ *
+ * @param expectedResult ArrayList of Strings
+ * @param resultReceived ArrayList of Strings
+ * @return String
+ */
+ private static String compareResults(ArrayList<String> expectedResult, ArrayList<String> resultReceived) {
+ // TODO Auto-generated method stub
+ String returnString = "";
+ int index;
+ // System.out.println(expectedResult.size());
+ // System.out.println(resultReceived.size());
+ for (index = 0; index < expectedResult.size() || index < resultReceived.size(); index++) {
+ if (index < expectedResult.size() && index < resultReceived.size()) {
+ if (!expectedResult.get(index).equals(resultReceived.get(index))) {
+ // returnString = "FAILED";
+ returnString += "\tExpected Output: " + expectedResult.get(index) + ",\n\tOutput Received: "
+ + resultReceived.get(index) + "\n";
+ //
+ // System.out.println(resultReceived.get(index));
+ }
+
+ } else {
+ if (index >= expectedResult.size()) {
+ returnString += "\tExpected Size of output: " + expectedResult.size()
+ + ", Size of output received: " + resultReceived.size()
+ + "\n\tExpected Output: none,\n\tOutput Received: " + resultReceived.get(index) + "\n";
+
+ } else {
+ if (index >= resultReceived.size()) {
+ returnString += "\tExpected Size of output: " + expectedResult.size()
+ + ", Size of output received: " + resultReceived.size() + "\n\tExpected Output: "
+ + expectedResult.get(index) + ",\n\tOutput Received: none\n";
+
+ }
+ }
+ }
+
+ }
+ if (index == expectedResult.size() && index == resultReceived.size() && returnString.equals("")) {
+ returnString = "PASSED";
+ }
+ return returnString;
+
+ }
}