From 4816b0543d14090da88559c85d645016ec6348ce Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Mon, 13 Jul 2009 16:22:57 -0700 Subject: [PATCH] Add AlertApi tests. --- .../hyperic/hq/hqapi1/test/AlertAck_test.java | 46 ++++++++++++ .../hq/hqapi1/test/AlertDelete_test.java | 46 ++++++++++++ .../hqapi1/test/AlertFindByResource_test.java | 66 +++++++++++++++++ .../hq/hqapi1/test/AlertFind_test.java | 49 ++++++++++++ .../hyperic/hq/hqapi1/test/AlertFix_test.java | 46 ++++++++++++ .../hyperic/hq/hqapi1/test/AlertTestBase.java | 74 +++++++++++++++++++ 6 files changed, 327 insertions(+) create mode 100644 src/org/hyperic/hq/hqapi1/test/AlertAck_test.java create mode 100644 src/org/hyperic/hq/hqapi1/test/AlertDelete_test.java create mode 100644 src/org/hyperic/hq/hqapi1/test/AlertFindByResource_test.java create mode 100644 src/org/hyperic/hq/hqapi1/test/AlertFind_test.java create mode 100644 src/org/hyperic/hq/hqapi1/test/AlertFix_test.java create mode 100644 src/org/hyperic/hq/hqapi1/test/AlertTestBase.java diff --git a/src/org/hyperic/hq/hqapi1/test/AlertAck_test.java b/src/org/hyperic/hq/hqapi1/test/AlertAck_test.java new file mode 100644 index 00000000..3ba531ac --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/test/AlertAck_test.java @@ -0,0 +1,46 @@ +package org.hyperic.hq.hqapi1.test; + +import org.hyperic.hq.hqapi1.types.AlertDefinition; +import org.hyperic.hq.hqapi1.types.AlertsResponse; +import org.hyperic.hq.hqapi1.types.Alert; +import org.hyperic.hq.hqapi1.types.StatusResponse; +import org.hyperic.hq.hqapi1.AlertApi; + +public class AlertAck_test extends AlertTestBase { + + public AlertAck_test(String name) { + super(name); + } + + public void testAckAlert() throws Exception { + AlertDefinition d = generateAlerts(); + AlertApi api = getAlertApi(); + + AlertsResponse response = api.findAlerts(0, System.currentTimeMillis(), + 10, 1, false, false); + hqAssertSuccess(response); + assertTrue(response.getAlert().size() <= 10); + assertTrue(response.getAlert().size() > 0); + + // Test ack + Alert a = response.getAlert().get(0); + + StatusResponse ackResponse = api.ackAlert(a.getId(), "Test ACK", 60000l); + hqAssertSuccess(ackResponse); + + // TODO: Valididate ack? Will require a getById API. + + // Cleanup + StatusResponse deleteResponse = getApi(). + getAlertDefinitionApi().deleteAlertDefinition(d.getId()); + hqAssertSuccess(deleteResponse); + } + + public void testAckInvalidAlert() throws Exception { + + AlertApi api = getAlertApi(); + + StatusResponse response = api.ackAlert(Integer.MAX_VALUE, "Test ACK", 60000l); + hqAssertFailureObjectNotFound(response); + } +} diff --git a/src/org/hyperic/hq/hqapi1/test/AlertDelete_test.java b/src/org/hyperic/hq/hqapi1/test/AlertDelete_test.java new file mode 100644 index 00000000..d3266e5a --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/test/AlertDelete_test.java @@ -0,0 +1,46 @@ +package org.hyperic.hq.hqapi1.test; + +import org.hyperic.hq.hqapi1.types.AlertDefinition; +import org.hyperic.hq.hqapi1.types.AlertsResponse; +import org.hyperic.hq.hqapi1.types.Alert; +import org.hyperic.hq.hqapi1.types.StatusResponse; +import org.hyperic.hq.hqapi1.AlertApi; + +public class AlertDelete_test extends AlertTestBase { + + public AlertDelete_test(String name) { + super(name); + } + + public void testDeleteAlert() throws Exception { + AlertDefinition d = generateAlerts(); + AlertApi api = getAlertApi(); + + AlertsResponse response = api.findAlerts(0, System.currentTimeMillis(), + 10, 1, false, false); + hqAssertSuccess(response); + assertTrue(response.getAlert().size() <= 10); + assertTrue(response.getAlert().size() > 0); + + // Test delete + Alert a = response.getAlert().get(0); + + StatusResponse deleteResponse = api.delete(a.getId()); + hqAssertSuccess(deleteResponse); + + // TODO: Valididate alert was deleted? Will require a getById API. + + // Cleanup + StatusResponse deleteDefResponse = getApi(). + getAlertDefinitionApi().deleteAlertDefinition(d.getId()); + hqAssertSuccess(deleteDefResponse); + } + + public void testDeleteInvalidAlert() throws Exception { + + AlertApi api = getAlertApi(); + + StatusResponse response = api.delete(Integer.MAX_VALUE); + hqAssertFailureObjectNotFound(response); + } +} diff --git a/src/org/hyperic/hq/hqapi1/test/AlertFindByResource_test.java b/src/org/hyperic/hq/hqapi1/test/AlertFindByResource_test.java new file mode 100644 index 00000000..1ea121c9 --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/test/AlertFindByResource_test.java @@ -0,0 +1,66 @@ +package org.hyperic.hq.hqapi1.test; + +import org.hyperic.hq.hqapi1.types.AlertDefinition; +import org.hyperic.hq.hqapi1.types.AlertsResponse; +import org.hyperic.hq.hqapi1.types.StatusResponse; +import org.hyperic.hq.hqapi1.types.Resource; +import org.hyperic.hq.hqapi1.AlertApi; + +public class AlertFindByResource_test extends AlertTestBase { + + public AlertFindByResource_test(String name) { + super(name); + } + + public void testFindValid() throws Exception { + Resource r = getLocalPlatformResource(false, false); + AlertDefinition d = generateAlerts(); + + AlertApi api = getAlertApi(); + + AlertsResponse response = api.findAlerts(r, 0, System.currentTimeMillis(), + 10, 1, false, false); + hqAssertSuccess(response); + assertTrue(response.getAlert().size() <= 10); + + // Cleanup + StatusResponse deleteResponse = getApi().getAlertDefinitionApi(). + deleteAlertDefinition(d.getId()); + hqAssertSuccess(deleteResponse); + } + + public void testFindInvalidSeverity() throws Exception { + Resource r = getLocalPlatformResource(false, false); + AlertApi api = getAlertApi(); + AlertsResponse response = api.findAlerts(r, 0, System.currentTimeMillis(), + 10, 4, false, false); + hqAssertFailureInvalidParameters(response); + } + + public void testFindInvalidCount() throws Exception { + Resource r = getLocalPlatformResource(false, false); + AlertApi api = getAlertApi(); + AlertsResponse response = api.findAlerts(r, 0, System.currentTimeMillis(), + -5, 2, false, false); + hqAssertFailureInvalidParameters(response); + } + + public void testFindInvalidRange() throws Exception { + Resource r = getLocalPlatformResource(false, false); + AlertApi api = getAlertApi(); + + AlertsResponse response = api.findAlerts(r, System.currentTimeMillis(), 0, + 10, 2, false, false); + hqAssertFailureInvalidParameters(response); + } + + public void testFindInvalidResource() throws Exception { + Resource r = new Resource(); + r.setId(Integer.MAX_VALUE); + r.setName("Invalid resource"); + AlertApi api = getAlertApi(); + AlertsResponse response = api.findAlerts(r, 0, System.currentTimeMillis(), + 10, 1, false, false); + hqAssertFailureObjectNotFound(response); + } +} diff --git a/src/org/hyperic/hq/hqapi1/test/AlertFind_test.java b/src/org/hyperic/hq/hqapi1/test/AlertFind_test.java new file mode 100644 index 00000000..dfa1996f --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/test/AlertFind_test.java @@ -0,0 +1,49 @@ +package org.hyperic.hq.hqapi1.test; + +import org.hyperic.hq.hqapi1.types.AlertDefinition; +import org.hyperic.hq.hqapi1.AlertApi; +import org.hyperic.hq.hqapi1.types.AlertsResponse; +import org.hyperic.hq.hqapi1.types.StatusResponse; + +public class AlertFind_test extends AlertTestBase { + + public AlertFind_test(String name) { + super(name); + } + + public void testFindValid() throws Exception { + AlertDefinition d = generateAlerts(); + AlertApi api = getAlertApi(); + + AlertsResponse response = api.findAlerts(0, System.currentTimeMillis(), + 10, 1, false, false); + hqAssertSuccess(response); + assertTrue(response.getAlert().size() <= 10); + + // Cleanup + StatusResponse deleteResponse = getApi(). + getAlertDefinitionApi().deleteAlertDefinition(d.getId()); + hqAssertSuccess(deleteResponse); + } + + public void testFindInvalidSeverity() throws Exception { + AlertApi api = getAlertApi(); + AlertsResponse response = api.findAlerts(0, System.currentTimeMillis(), + 10, 4, false, false); + hqAssertFailureInvalidParameters(response); + } + + public void testFindInvalidCount() throws Exception { + AlertApi api = getAlertApi(); + AlertsResponse response = api.findAlerts(0, System.currentTimeMillis(), + -5, 2, false, false); + hqAssertFailureInvalidParameters(response); + } + + public void testFindInvalidRange() throws Exception { + AlertApi api = getAlertApi(); + AlertsResponse response = api.findAlerts(System.currentTimeMillis(), 0, + 10, 2, false, false); + hqAssertFailureInvalidParameters(response); + } +} diff --git a/src/org/hyperic/hq/hqapi1/test/AlertFix_test.java b/src/org/hyperic/hq/hqapi1/test/AlertFix_test.java new file mode 100644 index 00000000..00681ba4 --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/test/AlertFix_test.java @@ -0,0 +1,46 @@ +package org.hyperic.hq.hqapi1.test; + +import org.hyperic.hq.hqapi1.types.AlertDefinition; +import org.hyperic.hq.hqapi1.types.AlertsResponse; +import org.hyperic.hq.hqapi1.types.StatusResponse; +import org.hyperic.hq.hqapi1.types.Alert; +import org.hyperic.hq.hqapi1.AlertApi; + +public class AlertFix_test extends AlertTestBase { + + public AlertFix_test(String name) { + super(name); + } + + public void testFixAlert() throws Exception { + AlertDefinition d = generateAlerts(); + AlertApi api = getAlertApi(); + + AlertsResponse response = api.findAlerts(0, System.currentTimeMillis(), + 10, 1, false, false); + hqAssertSuccess(response); + assertTrue(response.getAlert().size() <= 10); + assertTrue(response.getAlert().size() > 0); + + // Test marking fixed + Alert a = response.getAlert().get(0); + + StatusResponse fixResponse = api.fixAlert(a.getId()); + hqAssertSuccess(fixResponse); + + // TODO: Valididate fix flag was set? Will require a getById API. + + // Cleanup + StatusResponse deleteResponse = getApi(). + getAlertDefinitionApi().deleteAlertDefinition(d.getId()); + hqAssertSuccess(deleteResponse); + } + + public void testFixInvalidAlert() throws Exception { + + AlertApi api = getAlertApi(); + + StatusResponse response = api.fixAlert(Integer.MAX_VALUE); + hqAssertFailureObjectNotFound(response); + } +} diff --git a/src/org/hyperic/hq/hqapi1/test/AlertTestBase.java b/src/org/hyperic/hq/hqapi1/test/AlertTestBase.java new file mode 100644 index 00000000..0c527188 --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/test/AlertTestBase.java @@ -0,0 +1,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 definitions = new ArrayList(); + 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); + } +}