aboutsummaryrefslogtreecommitdiffstats
path: root/PolicyEngineClient/src/main/java/org/openecomp/policyEngine/GeneralTestClient.java
diff options
context:
space:
mode:
Diffstat (limited to 'PolicyEngineClient/src/main/java/org/openecomp/policyEngine/GeneralTestClient.java')
-rw-r--r--PolicyEngineClient/src/main/java/org/openecomp/policyEngine/GeneralTestClient.java398
1 files changed, 398 insertions, 0 deletions
diff --git a/PolicyEngineClient/src/main/java/org/openecomp/policyEngine/GeneralTestClient.java b/PolicyEngineClient/src/main/java/org/openecomp/policyEngine/GeneralTestClient.java
new file mode 100644
index 000000000..c78fc6f11
--- /dev/null
+++ b/PolicyEngineClient/src/main/java/org/openecomp/policyEngine/GeneralTestClient.java
@@ -0,0 +1,398 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * PolicyEngineClient
+ * ================================================================================
+ * Copyright (C) 2017 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+/*
+ * AT&T - PROPRIETARY
+ * THIS FILE CONTAINS PROPRIETARY INFORMATION OF
+ * AT&T AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN
+ * ACCORDANCE WITH APPLICABLE AGREEMENTS.
+ *
+ * Copyright (c) 2014 AT&T Knowledge Ventures
+ * Unpublished and Not for Publication
+ * All Rights Reserved
+ */
+package org.openecomp.policyEngine;
+
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.openecomp.policy.api.PolicyEngine;
+import org.openecomp.policy.api.PolicyEngineException;
+
+/**
+ * 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 {
+ 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, eCOMPComponentName;
+ 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<String>();
+ ArrayList<String> resultReceived = new ArrayList<String>();
+ 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":
+ eCOMPComponentName = (String) testCase
+ .get("ECOMPName");
+ String configName = (String) testCase
+ .get("ConfigName");
+ Map<String, String> configAttributes = new HashMap<String, String>();
+ 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, eCOMPComponentName,
+ 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<String, String>();
+ 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":
+ eCOMPComponentName = (String) testCase
+ .get("ECOMPName");
+ Map<String, String> decisionAttributes = new HashMap<String, String>();
+ 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,
+ eCOMPComponentName,
+ 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.openecomp.policyEngine);
+ // break;
+ // case "getAutoNotification":
+ // PolicyEngineTestClient
+ // .getAutoNotifications(org.openecomp.policyEngine);
+ // break;
+
+ default:
+ printResult(id, testFor, testCaseDescription,
+ "FAILED", "\tINVAILD TEST CASE.");
+ failTCforFile++;
+ failTC++;
+ break;
+
+ }
+ } catch (PolicyEngineException e) {
+ // TODO Auto-generated catch block
+ printResult(id, testFor, testCaseDescription, "FAILED");
+ failTCforFile++;
+ failTC++;
+ e.printStackTrace();
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ printResult(id, testFor, testCaseDescription, "FAILED");
+ failTCforFile++;
+ failTC++;
+ e.printStackTrace();
+ }
+ }
+
+ } catch (FileNotFoundException ex) {
+ ex.printStackTrace();
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ } catch (NullPointerException ex) {
+ ex.printStackTrace();
+ } catch (org.json.simple.parser.ParseException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ 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;
+
+ }
+}