Repository navigation
Expand file tree
/
Copy pathApiCommand.java
More file actions
847 lines (770 loc) · 36.9 KB
/
Copy pathApiCommand.java
File metadata and controls
847 lines (770 loc) · 36.9 KB
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
package com.cloud.test.regression;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.cloud.test.utils.UtilsForTest;
public class ApiCommand {
public static final Logger s_logger = Logger.getLogger(ApiCommand.class.getName());
public static enum CommandType {
HTTP, MYSQL, SCRIPT;
}
public static enum ResponseType {
ERROR, EMPTY;
}
private Element xmlCommand;
private String commandName;
private String testCaseInfo;
private boolean isUserCommand;
private boolean isAsync = false;
private CommandType commandType;
private ResponseType responseType;
private TreeMap<String, String> urlParam;
private HashMap<String, String> verifyParam = new HashMap<String, String>();;
private HashMap<String, String> setParam = new HashMap<String, String>();;
private int responseCode;
private Element responseBody;
private String command;
private String host;
private boolean list;
private Element listName;
private Element listId;
private boolean required = false;
private ResultSet result;
public ApiCommand(Element fstElmnt, HashMap<String, String> param, HashMap<String, String> commands) {
this.setXmlCommand(fstElmnt);
this.setCommandName();
this.setResponseType();
this.setUserCommand();
this.setCommandType();
this.setTestCaseInfo();
this.setUrlParam(param);
this.setVerifyParam(param);
this.setHost("http://" + param.get("hostip"));
this.setCommand(param);
String async = commands.get(this.getName());
if (async != null && async.equals("yes")) {
this.isAsync = true;
}
}
public Element getXmlCommand() {
return xmlCommand;
}
public void setXmlCommand(Element xmlCommand) {
this.xmlCommand = xmlCommand;
}
// ================FOLLOWING METHODS USE INPUT XML FILE=======================//
public void setCommandName() {
NodeList commandName = this.xmlCommand.getElementsByTagName("name");
Element commandElmnt = (Element)commandName.item(0);
NodeList commandNm = commandElmnt.getChildNodes();
this.commandName = (commandNm.item(0).getNodeValue());
}
public String getName() {
return commandName;
}
public void setTestCaseInfo() {
this.testCaseInfo = getElementByName("testcase");
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public void setResponseType() {
boolean result = verifyTagValue("error", "true");
if (result) {
this.responseType = ResponseType.ERROR;
return;
}
result = verifyTagValue("empty", "true");
if (result) {
this.responseType = ResponseType.EMPTY;
}
}
public void setResponseType(ResponseType responseType) {
this.responseType = responseType;
}
public ResponseType getResponseType() {
return responseType;
}
public void setUserCommand() {
boolean result = verifyTagValue("usercommand", "true");
this.isUserCommand = result;
}
public void setCommandType() {
boolean result = verifyTagValue("mysql", "true");
if (result) {
this.commandType = CommandType.MYSQL;
return;
}
result = verifyTagValue("script", "true");
if (result) {
this.commandType = CommandType.SCRIPT;
return;
}
this.commandType = CommandType.HTTP;
}
public CommandType getCommandType() {
return commandType;
}
public String getTestCaseInfo() {
return testCaseInfo;
}
public Boolean getRequired() {
return required;
}
public void setUrlParam(HashMap<String, String> param) {
this.urlParam = new TreeMap<String, String>();
NodeList parameterLst = this.xmlCommand.getElementsByTagName("parameters");
if (parameterLst != null) {
for (int j = 0; j < parameterLst.getLength(); j++) {
Element parameterElement = (Element)parameterLst.item(j);
NodeList itemLst = parameterElement.getElementsByTagName("item");
for (int k = 0; k < itemLst.getLength(); k++) {
Node item = itemLst.item(k);
if (item.getNodeType() == Node.ELEMENT_NODE) {
Element itemElement = (Element)item;
NodeList itemName = itemElement.getElementsByTagName("name");
Element itemNameElement = (Element)itemName.item(0);
// get value
Element itemValueElement = null;
if ((itemElement.getElementsByTagName("value") != null) && (itemElement.getElementsByTagName("value").getLength() != 0)) {
NodeList itemValue = itemElement.getElementsByTagName("value");
itemValueElement = (Element)itemValue.item(0);
}
Element itemParamElement = null;
// getparam
if ((itemElement.getElementsByTagName("param") != null) && (itemElement.getElementsByTagName("param").getLength() != 0)) {
NodeList itemParam = itemElement.getElementsByTagName("param");
itemParamElement = (Element)itemParam.item(0);
}
if ((itemElement.getAttribute("getparam").equals("true")) && (itemParamElement != null)) {
this.urlParam.put(itemNameElement.getTextContent(), param.get(itemParamElement.getTextContent()));
} else if (itemValueElement != null) {
this.urlParam.put(itemNameElement.getTextContent(), itemValueElement.getTextContent());
} else if (itemElement.getAttribute("random").equals("true")) {
Random ran = new Random();
String randomString = Math.abs(ran.nextInt()) + "-randomName";
this.urlParam.put(itemNameElement.getTextContent(), randomString);
if ((itemElement.getAttribute("setparam").equals("true")) && (itemParamElement != null)) {
param.put(itemParamElement.getTextContent(), randomString);
}
} else if (itemElement.getAttribute("randomnumber").equals("true")) {
Random ran = new Random();
Integer randomNumber = Math.abs(ran.nextInt(65535));
this.urlParam.put(itemNameElement.getTextContent(), randomNumber.toString());
if ((itemElement.getAttribute("setparam").equals("true")) && (itemParamElement != null)) {
param.put(itemParamElement.getTextContent(), randomNumber.toString());
}
}
}
}
}
}
}
// Set command URL
public void setCommand(HashMap<String, String> param) {
if (this.getCommandType() == CommandType.SCRIPT) {
String temp = "bash xen/" + this.commandName;
Set<?> c = this.urlParam.entrySet();
Iterator<?> it = c.iterator();
while (it.hasNext()) {
Map.Entry<?, ?> me = (Map.Entry<?, ?>)it.next();
String key = (String)me.getKey();
String value = (String)me.getValue();
try {
temp = temp + " -" + key + " " + value;
} catch (Exception ex) {
s_logger.error("Unable to set parameter " + key + " for the command " + this.getName());
}
}
this.command = temp;
} else if (this.getCommandType() == CommandType.MYSQL) {
String temp = this.commandName + " where ";
Set<?> c = this.urlParam.entrySet();
Iterator<?> it = c.iterator();
while (it.hasNext()) {
Map.Entry<?, ?> me = (Map.Entry<?, ?>)it.next();
String key = (String)me.getKey();
String value = (String)me.getValue();
try {
temp = temp + key + "=" + value;
} catch (Exception ex) {
s_logger.error("Unable to set parameter " + key + " for the command " + this.getName());
}
}
this.command = temp;
s_logger.info("The command is " + this.command);
} else {
if ((param.get("apikey") == null) || (param.get("secretkey") == null) || (this.isUserCommand == false)) {
String temp = this.host + ":8096/?command=" + this.commandName;
Set<?> c = this.urlParam.entrySet();
Iterator<?> it = c.iterator();
while (it.hasNext()) {
Map.Entry<?, ?> me = (Map.Entry<?, ?>)it.next();
String key = (String)me.getKey();
String value = (String)me.getValue();
try {
temp = temp + "&" + key + "=" + URLEncoder.encode(value, "UTF-8");
} catch (Exception ex) {
s_logger.error("Unable to set parameter " + key + " for the command " + this.getName());
}
}
this.command = temp;
} else if (isUserCommand == true) {
String apiKey = param.get("apikey");
String secretKey = param.get("secretkey");
String temp = "";
this.urlParam.put("apikey", apiKey);
this.urlParam.put("command", this.commandName);
// sort url hash map by key
Set<?> c = this.urlParam.entrySet();
Iterator<?> it = c.iterator();
while (it.hasNext()) {
Map.Entry<?, ?> me = (Map.Entry<?, ?>)it.next();
String key = (String)me.getKey();
String value = (String)me.getValue();
try {
temp = temp + key + "=" + URLEncoder.encode(value, "UTF-8") + "&";
} catch (Exception ex) {
s_logger.error("Unable to set parameter " + value + " for the command " + this.getName());
}
}
temp = temp.substring(0, temp.length() - 1);
String requestToSign = temp.toLowerCase();
String signature = UtilsForTest.signRequest(requestToSign, secretKey);
String encodedSignature = "";
try {
encodedSignature = URLEncoder.encode(signature, "UTF-8");
} catch (Exception ex) {
s_logger.error(ex);
}
this.command = this.host + ":8080/client/api/?" + temp + "&signature=" + encodedSignature;
}
}
}
public void setVerifyParam(HashMap<String, String> param) {
NodeList returnLst = this.xmlCommand.getElementsByTagName("returnvalue");
if (returnLst != null) {
for (int m = 0; m < returnLst.getLength(); m++) {
Element returnElement = (Element)returnLst.item(m);
if (returnElement.getAttribute("list").equals("true")) {
this.list = true;
NodeList elementLst = returnElement.getElementsByTagName("element");
this.listId = (Element)elementLst.item(0);
NodeList elementName = returnElement.getElementsByTagName("name");
this.listName = (Element)elementName.item(0);
} else {
this.list = false;
}
NodeList itemLst1 = returnElement.getElementsByTagName("item");
if (itemLst1 != null) {
for (int n = 0; n < itemLst1.getLength(); n++) {
Node item = itemLst1.item(n);
if (item.getNodeType() == Node.ELEMENT_NODE) {
Element itemElement = (Element)item;
// get parameter name
NodeList itemName = itemElement.getElementsByTagName("name");
Element itemNameElement = (Element)itemName.item(0);
// Get parameters for future use
if (itemElement.getAttribute("setparam").equals("true")) {
NodeList itemVariable = itemElement.getElementsByTagName("param");
Element itemVariableElement = (Element)itemVariable.item(0);
setParam.put(itemVariableElement.getTextContent(), itemNameElement.getTextContent());
this.required = true;
} else if (itemElement.getAttribute("getparam").equals("true")) {
NodeList itemVariable = itemElement.getElementsByTagName("param");
Element itemVariableElement = (Element)itemVariable.item(0);
this.verifyParam.put(itemNameElement.getTextContent(), param.get(itemVariableElement.getTextContent()));
} else if ((itemElement.getElementsByTagName("value") != null) && (itemElement.getElementsByTagName("value").getLength() != 0)) {
NodeList itemVariable = itemElement.getElementsByTagName("value");
Element itemVariableElement = (Element)itemVariable.item(0);
this.verifyParam.put(itemNameElement.getTextContent(), itemVariableElement.getTextContent());
} else {
this.verifyParam.put(itemNameElement.getTextContent(), "no value");
}
}
}
}
}
}
}
public int getResponseCode() {
return responseCode;
}
// Send api command to the server
public void sendCommand(HttpClient client, Connection conn) {
if (TestCaseEngine.s_printUrl == true) {
s_logger.info("url is " + this.command);
}
if (this.getCommandType() == CommandType.SCRIPT) {
try {
s_logger.info("Executing command " + this.command);
Runtime rtime = Runtime.getRuntime();
Process child = rtime.exec(this.command);
Thread.sleep(10000);
int retCode = child.waitFor();
if (retCode != 0) {
this.responseCode = retCode;
} else {
this.responseCode = 200;
}
} catch (Exception ex) {
s_logger.error("Unable to execute a command " + this.command, ex);
}
} else if (this.getCommandType() == CommandType.MYSQL) {
try {
Statement stmt = conn.createStatement();
this.result = stmt.executeQuery(this.command);
this.responseCode = 200;
} catch (Exception ex) {
this.responseCode = 400;
s_logger.error("Unable to execute mysql query " + this.command, ex);
}
} else {
HttpMethod method = new GetMethod(this.command);
try {
this.responseCode = client.executeMethod(method);
if (this.responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);
doc.getDocumentElement().normalize();
if (!(this.isAsync)) {
this.responseBody = doc.getDocumentElement();
} else {
// get async job result
Element jobTag = (Element)doc.getDocumentElement().getElementsByTagName("jobid").item(0);
String jobId = jobTag.getTextContent();
Element responseBodyAsyncEl = queryAsyncJobResult(jobId);
if (responseBodyAsyncEl == null) {
s_logger.error("Can't get a async result");
} else {
this.responseBody = responseBodyAsyncEl;
// get status of the job
Element jobStatusTag = (Element)responseBodyAsyncEl.getElementsByTagName("jobstatus").item(0);
String jobStatus = jobStatusTag.getTextContent();
if (!jobStatus.equals("1")) { // Need to modify with different error codes for jobAsync
// results
// set fake response code by now
this.responseCode = 400;
}
}
}
}
if (TestCaseEngine.s_printUrl == true) {
s_logger.info("Response code is " + this.responseCode);
}
} catch (Exception ex) {
s_logger.error("Command " + command + " failed with exception " + ex.getMessage());
} finally {
method.releaseConnection();
}
}
}
// verify if response is empty (contains only root element)
public boolean isEmpty() {
boolean result = false;
if (!this.responseBody.hasChildNodes())
result = true;
return result;
}
// ================FOLLOWING METHODS USE RETURN XML FILE=======================//
public boolean setParam(HashMap<String, String> param) {
if ((this.responseBody == null) && (this.commandType == CommandType.HTTP)) {
s_logger.error("Response body is empty");
return false;
}
Boolean result = true;
if (this.getCommandType() == CommandType.MYSQL) {
Set<?> set = this.setParam.entrySet();
Iterator<?> it = set.iterator();
while (it.hasNext()) {
Map.Entry<?, ?> me = (Map.Entry<?, ?>)it.next();
String key = (String)me.getKey();
String value = (String)me.getValue();
try {
String itemName = null;
while (this.result.next()) {
itemName = this.result.getString(value);
}
if (itemName != null) {
param.put(key, itemName);
} else {
s_logger.error("Following return parameter is missing: " + value);
result = false;
}
} catch (Exception ex) {
s_logger.error("Unable to set parameter " + value, ex);
}
}
} else if (this.getCommandType() == CommandType.HTTP) {
if (this.list == false) {
Set<?> set = this.setParam.entrySet();
Iterator<?> it = set.iterator();
while (it.hasNext()) {
Map.Entry<?, ?> me = (Map.Entry<?, ?>)it.next();
String key = (String)me.getKey();
String value = (String)me.getValue();
// set parameters needed for the future use
NodeList itemName = this.responseBody.getElementsByTagName(value);
if ((itemName != null) && (itemName.getLength() != 0)) {
for (int i = 0; i < itemName.getLength(); i++) {
Element itemNameElement = (Element)itemName.item(i);
if (itemNameElement.getChildNodes().getLength() <= 1) {
param.put(key, itemNameElement.getTextContent());
break;
}
}
} else {
s_logger.error("Following return parameter is missing: " + value);
result = false;
}
}
} else {
Set<?> set = this.setParam.entrySet();
Iterator<?> it = set.iterator();
NodeList returnLst = this.responseBody.getElementsByTagName(this.listName.getTextContent());
Node requiredNode = returnLst.item(Integer.parseInt(this.listId.getTextContent()));
if (requiredNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element)requiredNode;
while (it.hasNext()) {
Map.Entry<?, ?> me = (Map.Entry<?, ?>)it.next();
String key = (String)me.getKey();
String value = (String)me.getValue();
NodeList itemName = fstElmnt.getElementsByTagName(value);
if ((itemName != null) && (itemName.getLength() != 0)) {
Element itemNameElement = (Element)itemName.item(0);
if (itemNameElement.getChildNodes().getLength() <= 1) {
param.put(key, itemNameElement.getTextContent());
}
} else {
s_logger.error("Following return parameter is missing: " + value);
result = false;
}
}
}
}
}
return result;
}
public String getUrl() {
return command;
}
public boolean verifyParam() {
boolean result = true;
if (this.getCommandType() == CommandType.HTTP) {
if (this.list == false) {
Set<?> set = verifyParam.entrySet();
Iterator<?> it = set.iterator();
while (it.hasNext()) {
Map.Entry<?, ?> me = (Map.Entry<?, ?>)it.next();
String key = (String)me.getKey();
String value = (String)me.getValue();
if (value == null) {
s_logger.error("Parameter " + key + " is missing in the list of global parameters");
return false;
}
NodeList itemName = this.responseBody.getElementsByTagName(key);
if ((itemName.getLength() != 0) && (itemName != null)) {
Element itemNameElement = (Element)itemName.item(0);
if (itemNameElement.hasChildNodes()) {
continue;
}
if (!(verifyParam.get(key).equals("no value")) && !(itemNameElement.getTextContent().equals(verifyParam.get(key)))) {
s_logger.error("Incorrect value for the following tag: " + key + ". Expected value is " + verifyParam.get(key) + " while actual value is " +
itemNameElement.getTextContent());
result = false;
}
} else {
s_logger.error("Following xml element is missing in the response: " + key);
result = false;
}
}
}
// for multiple elements
else {
Set<?> set = verifyParam.entrySet();
Iterator<?> it = set.iterator();
// get list element specified by id
NodeList returnLst = this.responseBody.getElementsByTagName(this.listName.getTextContent());
Node requiredNode = returnLst.item(Integer.parseInt(this.listId.getTextContent()));
if (requiredNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element)requiredNode;
while (it.hasNext()) {
Map.Entry<?, ?> me = (Map.Entry<?, ?>)it.next();
String key = (String)me.getKey();
String value = (String)me.getValue();
if (value == null) {
s_logger.error("Parameter " + key + " is missing in the list of global parameters");
return false;
}
NodeList itemName = fstElmnt.getElementsByTagName(key);
if ((itemName.getLength() != 0) && (itemName != null)) {
Element itemNameElement = (Element)itemName.item(0);
if (!(verifyParam.get(key).equals("no value")) && !(itemNameElement.getTextContent().equals(verifyParam.get(key)))) {
s_logger.error("Incorrect value for the following tag: " + key + ". Expected value is " + verifyParam.get(key) +
" while actual value is " + itemNameElement.getTextContent());
result = false;
}
} else {
s_logger.error("Following xml element is missing in the response: " + key);
result = false;
}
}
}
}
} else if (this.getCommandType() == CommandType.MYSQL) {
Set<?> set = verifyParam.entrySet();
Iterator<?> it = set.iterator();
while (it.hasNext()) {
Map.Entry<?, ?> me = (Map.Entry<?, ?>)it.next();
String key = (String)me.getKey();
String value = (String)me.getValue();
if (value == null) {
s_logger.error("Parameter " + key + " is missing in the list of global parameters");
return false;
}
String itemName = null;
try {
while (this.result.next()) {
itemName = this.result.getString(key);
}
} catch (Exception ex) {
s_logger.error("Unable to get element from result set " + key);
}
if (!(value.equals("no value")) && !(itemName.equals(verifyParam.get(key)))) {
s_logger.error("Incorrect value for the following tag: " + key + ". Expected value is " + verifyParam.get(key) + " while actual value is " + itemName);
result = false;
}
}
}
return result;
}
public static boolean verifyEvents(String fileName, String level, String host, String account) {
boolean result = false;
HashMap<String, Integer> expectedEvents = new HashMap<String, Integer>();
HashMap<String, Integer> actualEvents = new HashMap<String, Integer>();
String key = "";
File file = new File(fileName);
if (file.exists()) {
Properties pro = new Properties();
try {
// get expected events
FileInputStream in = new FileInputStream(file);
pro.load(in);
Enumeration<?> en = pro.propertyNames();
while (en.hasMoreElements()) {
key = (String)en.nextElement();
expectedEvents.put(key, Integer.parseInt(pro.getProperty(key)));
}
// get actual events
String url = host + "/?command=listEvents&account=" + account + "&level=" + level + "&domainid=1&pagesize=100";
s_logger.info("Getting events with the following url " + url);
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
int responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
ArrayList<HashMap<String, String>> eventValues = UtilsForTest.parseMulXML(is, new String[] {"event"});
for (int i = 0; i < eventValues.size(); i++) {
HashMap<String, String> element = eventValues.get(i);
if (element.get("level").equals(level)) {
if (actualEvents.containsKey(element.get("type")) == true) {
actualEvents.put(element.get("type"), actualEvents.get(element.get("type")) + 1);
} else {
actualEvents.put(element.get("type"), 1);
}
}
}
}
method.releaseConnection();
// compare actual events with expected events
// compare expected result and actual result
Iterator<?> iterator = expectedEvents.keySet().iterator();
Integer expected;
Integer actual;
int fail = 0;
while (iterator.hasNext()) {
expected = null;
actual = null;
String type = iterator.next().toString();
expected = expectedEvents.get(type);
actual = actualEvents.get(type);
if (actual == null) {
s_logger.error("Event of type " + type + " and level " + level + " is missing in the listEvents response. Expected number of these events is " +
expected);
fail++;
} else if (expected.compareTo(actual) != 0) {
fail++;
s_logger.info("Amount of events of " + type + " type and level " + level + " is incorrect. Expected number of these events is " + expected +
", actual number is " + actual);
}
}
if (fail == 0) {
result = true;
}
} catch (Exception ex) {
s_logger.error(ex);
}
} else {
s_logger.info("File " + fileName + " not found");
}
return result;
}
public static boolean verifyEvents(HashMap<String, Integer> expectedEvents, String level, String host, String parameters) {
boolean result = false;
HashMap<String, Integer> actualEvents = new HashMap<String, Integer>();
try {
// get actual events
String url = host + "/?command=listEvents&" + parameters;
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
int responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
ArrayList<HashMap<String, String>> eventValues = UtilsForTest.parseMulXML(is, new String[] {"event"});
for (int i = 0; i < eventValues.size(); i++) {
HashMap<String, String> element = eventValues.get(i);
if (element.get("level").equals(level)) {
if (actualEvents.containsKey(element.get("type")) == true) {
actualEvents.put(element.get("type"), actualEvents.get(element.get("type")) + 1);
} else {
actualEvents.put(element.get("type"), 1);
}
}
}
}
method.releaseConnection();
} catch (Exception ex) {
s_logger.error(ex);
}
// compare actual events with expected events
Iterator<?> iterator = expectedEvents.keySet().iterator();
Integer expected;
Integer actual;
int fail = 0;
while (iterator.hasNext()) {
expected = null;
actual = null;
String type = iterator.next().toString();
expected = expectedEvents.get(type);
actual = actualEvents.get(type);
if (actual == null) {
s_logger.error("Event of type " + type + " and level " + level + " is missing in the listEvents response. Expected number of these events is " + expected);
fail++;
} else if (expected.compareTo(actual) != 0) {
fail++;
s_logger.info("Amount of events of " + type + " type and level " + level + " is incorrect. Expected number of these events is " + expected +
", actual number is " + actual);
}
}
if (fail == 0) {
result = true;
}
return result;
}
public Element queryAsyncJobResult(String jobId) {
Element returnBody = null;
int code = 400;
String resultUrl = this.host + ":8096/?command=queryAsyncJobResult&jobid=" + jobId;
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(resultUrl);
while (true) {
try {
code = client.executeMethod(method);
if (code == 200) {
InputStream is = method.getResponseBodyAsStream();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);
doc.getDocumentElement().normalize();
returnBody = doc.getDocumentElement();
Element jobStatusTag = (Element)returnBody.getElementsByTagName("jobstatus").item(0);
String jobStatus = jobStatusTag.getTextContent();
if (jobStatus.equals("0")) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
} else {
break;
}
method.releaseConnection();
} else {
s_logger.error("Error during queryJobAsync. Error code is " + code);
this.responseCode = code;
return null;
}
} catch (Exception ex) {
s_logger.error(ex);
}
}
return returnBody;
}
private String getElementByName(String elementName) {
NodeList commandName = this.xmlCommand.getElementsByTagName(elementName);
if (commandName.getLength() != 0) {
Element commandElmnt = (Element)commandName.item(0);
NodeList commandNm = commandElmnt.getChildNodes();
return commandNm.item(0).getNodeValue();
} else {
return null;
}
}
private boolean verifyTagValue(String elementName, String expectedValue) {
NodeList tag = this.xmlCommand.getElementsByTagName(elementName);
if (tag.getLength() != 0) {
Element commandElmnt = (Element)tag.item(0);
NodeList commandNm = commandElmnt.getChildNodes();
if (commandNm.item(0).getNodeValue().equals(expectedValue)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
}