1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
/*-
* ============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=========================================================
*/
package org.onap.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.onap.policy.api.PolicyEngine;
import org.onap.policy.api.PolicyEngineException;
import org.onap.policy.common.logging.flexlogger.FlexLogger;
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;
}
}
|