Skip to content
Browse files

Add better error messages. Unhardcode template from 10001 to Linux te…

…mplate. Add

test for getting data in the past where no data exists.
  • Loading branch information...
1 parent 4babc56 commit 8adc14046d38b1a218bcda5a04aeb79f31e774f8 Ryan Morgan committed
Showing with 77 additions and 25 deletions.
  1. +13 −7 hqu/hqapi1/app/MetricController.groovy
  2. +64 −18 src/org/hyperic/hq/hqapi1/test/MetricData_test.java
View
20 hqu/hqapi1/app/MetricController.groovy
@@ -66,7 +66,8 @@ class MetricController extends ApiController {
def failureXml = null
def templates
if (!prototype) {
- failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS)
+ failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
+ "No prototype given")
} else {
// Make sure the prototype exists.
def proto = resourceHelper.find(prototype: prototype)
@@ -98,7 +99,8 @@ class MetricController extends ApiController {
def template
def failureXml = null
if (!id) {
- failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS)
+ failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
+ "Metric template id not given")
} else {
template = metricHelper.findTemplateById(id)
if (!template) {
@@ -125,7 +127,8 @@ class MetricController extends ApiController {
def enabled = params.getOne("enabled")?.toBoolean()
if (!resourceId) {
- failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS)
+ failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
+ "Resource id not given")
} else {
def res = getResource(resourceId)
if (!res) {
@@ -164,7 +167,8 @@ class MetricController extends ApiController {
def metricId = params.getOne("id")?.toInteger()
if (!metricId) {
- failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS)
+ failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
+ "Metric id not given")
} else {
try {
metric = metricHelper.findMeasurementById(metricId);
@@ -332,12 +336,14 @@ class MetricController extends ApiController {
def end = params.getOne("end")?.toLong()
def failureXml = null
- if (!metricId || !start || !end) {
- failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS)
+ if (metricId == null || start == null || end == null) {
+ failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
+ "Missing argument")
}
if (end < start) {
- failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS)
+ failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
+ "End time cannot be < start time")
}
def metric = metricHelper.findMeasurementById(metricId)
View
82 src/org/hyperic/hq/hqapi1/test/MetricData_test.java
@@ -7,7 +7,6 @@
import org.hyperic.hq.hqapi1.types.DataPoint;
import org.hyperic.hq.hqapi1.types.Group;
import org.hyperic.hq.hqapi1.types.GroupResponse;
-import org.hyperic.hq.hqapi1.types.GroupsResponse;
import org.hyperic.hq.hqapi1.types.Metric;
import org.hyperic.hq.hqapi1.types.MetricData;
import org.hyperic.hq.hqapi1.types.MetricDataResponse;
@@ -24,6 +23,7 @@
import java.util.List;
import java.util.Random;
+import java.io.IOException;
public class MetricData_test extends MetricTestBase {
@@ -63,6 +63,38 @@ public void testGetEnabledMetricData() throws Exception {
}
}
+ public void testGetMetricDataOldRange() throws Exception {
+
+ MetricApi api = getApi().getMetricApi();
+ Resource r = getLocalPlatformResource(false, false);
+ MetricsResponse resp = api.getEnabledMetrics(r);
+ hqAssertSuccess(resp);
+
+ assertTrue("No enabled metrics found for " + r.getName(),
+ resp.getMetric().size() > 0);
+
+ Metric metric = null;
+ for (Metric m : resp.getMetric()) {
+ if (!m.getMetricTemplate().getAlias().equals("Availability")) {
+ metric = m;
+ break;
+ }
+ }
+
+ assertNotNull("No metric found for " + r.getName(), metric);
+
+ long end = 100;
+ long start = 0;
+
+ MetricDataResponse dataResponse = api.getMetricData(metric.getId(),
+ start, end);
+ hqAssertSuccess(dataResponse);
+
+ assertTrue("Wrong number of data points, expected 0, got " +
+ dataResponse.getMetricData().getDataPoint().size(),
+ dataResponse.getMetricData().getDataPoint().size() == 0);
+ }
+
public void testGetDisabledMetricData() throws Exception {
MetricApi api = getApi().getMetricApi();
@@ -124,7 +156,27 @@ public void testGetMetricDataInvalidRange() throws Exception {
hqAssertFailureInvalidParameters(dataResponse);
}
- private Group getCompatibleGroup() throws Exception {
+ // Helper method to get a MetricTemplate for the Linux ResourcePrototype
+ private MetricTemplate getLinuxTemplate() throws IOException
+ {
+ HQApi api = getApi();
+ ResourceApi resourceApi = api.getResourceApi();
+ MetricApi metricApi = api.getMetricApi();
+
+ ResourcePrototypeResponse protoResponse =
+ resourceApi.getResourcePrototype("Linux");
+ hqAssertSuccess(protoResponse);
+
+ MetricTemplatesResponse templatesResponse =
+ metricApi.getMetricTemplates(protoResponse.getResourcePrototype());
+ hqAssertSuccess(templatesResponse);
+
+ assertTrue("No templates found!", templatesResponse.getMetricTemplate().size() > 0);
+
+ return templatesResponse.getMetricTemplate().get(0);
+ }
+
+ private Group getFileServerMountCompatibleGroup() throws Exception {
HQApi api = getApi();
ResourceApi resourceApi = api.getResourceApi();
@@ -185,7 +237,7 @@ public void testGetMetricGroupData() throws Exception {
HQApi api = getApi();
- Group g = getCompatibleGroup();
+ Group g = getFileServerMountCompatibleGroup();
ResourcePrototype pt = g.getResourcePrototype();
MetricApi metricApi = api.getMetricApi();
MetricTemplatesResponse templatesResponse = metricApi.getMetricTemplates(pt);
@@ -235,7 +287,7 @@ public void testGetMetricGroupDataInvalidRange() throws Exception {
HQApi api = getApi();
- Group g = getCompatibleGroup();
+ Group g = getFileServerMountCompatibleGroup();
ResourcePrototype pt = g.getResourcePrototype();
MetricApi metricApi = api.getMetricApi();
MetricTemplatesResponse templatesResponse = metricApi.getMetricTemplates(pt);
@@ -271,7 +323,7 @@ public void testGetMetricGroupDataInvalidTemplate() throws Exception {
HQApi api = getApi();
- Group g = getCompatibleGroup();
+ Group g = getFileServerMountCompatibleGroup();
MetricApi metricApi = api.getMetricApi();
long end = System.currentTimeMillis();
long start = end - (8 * 60 * 60 * 1000);
@@ -286,13 +338,10 @@ public void testGetMetricGroupDataWrongTemplate() throws Exception {
HQApi api = getApi();
- Group g = getCompatibleGroup();
+ Group g = getFileServerMountCompatibleGroup();
MetricApi metricApi = api.getMetricApi();
- MetricTemplateResponse getTemplateResponse =
- metricApi.getMetricTemplate(10001);
- hqAssertSuccess(getTemplateResponse);
- MetricTemplate t = getTemplateResponse.getMetricTemplate();
+ MetricTemplate t = getLinuxTemplate();
long end = System.currentTimeMillis();
long start = end - (8 * 60 * 60 * 1000);
@@ -309,11 +358,7 @@ public void testGetMetricGroupDataMixedGroup() throws Exception {
Group g = getMixedGroup();
MetricApi metricApi = api.getMetricApi();
-
- MetricTemplateResponse getTemplateResponse =
- metricApi.getMetricTemplate(10001);
- hqAssertSuccess(getTemplateResponse);
- MetricTemplate t = getTemplateResponse.getMetricTemplate();
+ MetricTemplate t = getLinuxTemplate();
long end = System.currentTimeMillis();
long start = end - (8 * 60 * 60 * 1000);
@@ -329,7 +374,7 @@ public void testGetMetricGroupDataInvalidGroup() throws Exception {
HQApi api = getApi();
- Group g = getCompatibleGroup();
+ Group g = getFileServerMountCompatibleGroup();
ResourcePrototype pt = g.getResourcePrototype();
MetricApi metricApi = api.getMetricApi();
MetricTemplatesResponse templatesResponse = metricApi.getMetricTemplates(pt);
@@ -364,7 +409,7 @@ public void testGetMetricGroupDataDefaultOffTemplate() throws Exception {
HQApi api = getApi();
- Group g = getCompatibleGroup();
+ Group g = getFileServerMountCompatibleGroup();
ResourcePrototype pt = g.getResourcePrototype();
MetricApi metricApi = api.getMetricApi();
MetricTemplatesResponse templatesResponse = metricApi.getMetricTemplates(pt);
@@ -481,8 +526,9 @@ public void testResourceMetrics() throws Exception {
hqAssertFailureObjectNotFound(invalidTemplateResponse);
// Retry with valid template, but belonging to this type
+ MetricTemplate linuxTemplate = getLinuxTemplate();
MetricsDataResponse wrongTemplateResponse =
- metricApi.getMetricData(resourceIds, 10001, start, end);
+ metricApi.getMetricData(resourceIds, linuxTemplate.getId(), start, end);
hqAssertFailureInvalidParameters(wrongTemplateResponse);
// Retry with empty resources array

0 comments on commit 8adc140

Please sign in to comment.
Something went wrong with that request. Please try again.