-
Notifications
You must be signed in to change notification settings - Fork 19
/
AlertTestBase.java
74 lines (62 loc) · 2.65 KB
/
AlertTestBase.java
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
package org.hyperic.hq.hqapi1.test;
import org.hyperic.hq.hqapi1.AlertApi;
import org.hyperic.hq.hqapi1.AlertDefinitionBuilder;
import org.hyperic.hq.hqapi1.AlertDefinitionApi;
import org.hyperic.hq.hqapi1.types.AlertDefinition;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.types.AlertDefinitionsResponse;
import org.hyperic.hq.hqapi1.types.AlertsResponse;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
public class AlertTestBase extends HQApiTestBase {
public AlertTestBase(String name) {
super(name);
}
protected AlertApi getAlertApi() {
return getApi().getAlertApi();
}
/**
* Setup an AlertDefinition that will fire Alert instances waiting for
* at least 1 alert to be generated.
*
* @return The AlertDefinition that was created to generate the alerts.
*/
protected AlertDefinition generateAlerts() throws Exception {
Resource platform = getLocalPlatformResource(false, false);
AlertDefinition d = new AlertDefinition();
Random r = new Random();
d.setName("Test Alert Definition" + r.nextInt());
d.setDescription("Definition that will always fire, allowing for testing of Alerts");
d.setPriority(AlertDefinitionBuilder.AlertPriority.MEDIUM.getPriority());
d.setActive(true);
d.setResource(platform);
d.getAlertCondition().add(AlertDefinitionBuilder.
createThresholdCondition(true, "Availability",
AlertDefinitionBuilder.AlertComparator.GREATER_THAN, -1));
AlertDefinitionApi defApi = getApi().getAlertDefinitionApi();
List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
definitions.add(d);
AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
hqAssertSuccess(response);
// Now we wait..
for (int i = 0; i < 120; i++) {
// Wait for alerts
System.out.println("Waiting for alerts...");
AlertsResponse alerts = getAlertApi().findAlerts(platform, 0,
System.currentTimeMillis(),
10, 1, false, false);
hqAssertSuccess(alerts);
if (alerts.getAlert().size() > 0) {
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// Ignore
}
}
return response.getAlertDefinition().get(0);
}
}