Update alarm resource test to green

This commit is contained in:
Jonathan Halterman 2014-03-24 15:42:25 -07:00
parent 1d5847562e
commit 3e4b697a50
3 changed files with 39 additions and 49 deletions

View File

@ -57,20 +57,22 @@ public class CreateAlarmCommand {
public void validate() { public void validate() {
if (name.length() > 250) if (name.length() > 250)
Exceptions.unprocessableEntity("Name %s must be 250 characters or less", name); throw Exceptions.unprocessableEntity("Name %s must be 250 characters or less", name);
if (description != null && description.length() > 250) if (description != null && description.length() > 250)
Exceptions.unprocessableEntity("Description %s must be 250 characters or less", description); throw Exceptions.unprocessableEntity("Description %s must be 250 characters or less",
description);
for (String action : alarmActions) for (String action : alarmActions)
if (action.length() > 50) if (action.length() > 50)
Exceptions.unprocessableEntity("Alarm action %s must be 50 characters or less", action); throw Exceptions.unprocessableEntity("Alarm action %s must be 50 characters or less",
action);
if (okActions != null) if (okActions != null)
for (String action : okActions) for (String action : okActions)
if (action.length() > 50) if (action.length() > 50)
Exceptions.unprocessableEntity("Ok action %s must be 50 characters or less", action); throw Exceptions.unprocessableEntity("Ok action %s must be 50 characters or less", action);
if (undeterminedActions != null) if (undeterminedActions != null)
for (String action : undeterminedActions) for (String action : undeterminedActions)
if (action.length() > 50) if (action.length() > 50)
Exceptions.unprocessableEntity("Undetermined action %s must be 50 characters or less", throw Exceptions.unprocessableEntity(
action); "Undetermined action %s must be 50 characters or less", action);
} }
} }

View File

@ -1,7 +1,5 @@
package com.hpcloud.mon.resource; package com.hpcloud.mon.resource;
import io.dropwizard.jersey.jackson.JsonProcessingExceptionMapper;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.hpcloud.dropwizard.AbstractResourceTest; import com.hpcloud.dropwizard.AbstractResourceTest;
@ -11,6 +9,7 @@ import com.hpcloud.mon.resource.exception.EntityNotFoundExceptionMapper;
import com.hpcloud.mon.resource.exception.IllegalArgumentExceptionMapper; import com.hpcloud.mon.resource.exception.IllegalArgumentExceptionMapper;
import com.hpcloud.mon.resource.exception.InvalidEntityExceptionMapper; import com.hpcloud.mon.resource.exception.InvalidEntityExceptionMapper;
import com.hpcloud.mon.resource.exception.JsonMappingExceptionManager; import com.hpcloud.mon.resource.exception.JsonMappingExceptionManager;
import com.hpcloud.mon.resource.exception.JsonProcessingExceptionMapper;
import com.hpcloud.mon.resource.exception.ResourceNotFoundExceptionMapper; import com.hpcloud.mon.resource.exception.ResourceNotFoundExceptionMapper;
import com.hpcloud.mon.resource.exception.ThrowableExceptionMapper; import com.hpcloud.mon.resource.exception.ThrowableExceptionMapper;

View File

@ -37,6 +37,7 @@ import com.sun.jersey.api.client.GenericType;
*/ */
@Test @Test
public class AlarmResourceTest extends AbstractMonApiResourceTest { public class AlarmResourceTest extends AbstractMonApiResourceTest {
private String expression;
private AlarmDetail alarm; private AlarmDetail alarm;
private Alarm alarmItem; private Alarm alarmItem;
private AlarmService service; private AlarmService service;
@ -47,8 +48,8 @@ public class AlarmResourceTest extends AbstractMonApiResourceTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected void setupResources() throws Exception { protected void setupResources() throws Exception {
super.setupResources(); super.setupResources();
String expression = "avg(hpcs.compute{instance_id=937, az=2, instance_uuid=0ff588fc-d298-482f-bb11-4b52d56801a4, metric_name=disk_read_ops}) >= 90";
expression = "avg(disk_read_ops{service=hpcs.compute, instance_id=937}) >= 90";
alarmItem = new Alarm("123", "Disk Exceeds 1k Operations", null, expression, AlarmState.OK); alarmItem = new Alarm("123", "Disk Exceeds 1k Operations", null, expression, AlarmState.OK);
alarmActions = new ArrayList<String>(); alarmActions = new ArrayList<String>();
alarmActions.add("29387234"); alarmActions.add("29387234");
@ -71,27 +72,35 @@ public class AlarmResourceTest extends AbstractMonApiResourceTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void shouldCreate() { public void shouldCreate() {
String expression = "avg(hpcs.compute{instance_id=937, az=2, instance_uuid=0ff588fc-d298-482f-bb11-4b52d56801a4, metric_name=disk_read_ops}) >= 90";
ClientResponse response = createResponseFor(new CreateAlarmCommand( ClientResponse response = createResponseFor(new CreateAlarmCommand(
"Disk Exceeds 1k Operations", null, expression, alarmActions)); "Disk Exceeds 1k Operations", null, expression, alarmActions));
assertEquals(response.getStatus(), 201); assertEquals(response.getStatus(), 201);
AlarmDetail newAlarm = response.getEntity(AlarmDetail.class); AlarmDetail newAlarm = response.getEntity(AlarmDetail.class);
String location = response.getHeaders().get("Location").get(0); String location = response.getHeaders().get("Location").get(0);
assertEquals(location, "/v1.1/alarms/" + newAlarm.getId()); assertEquals(location, "/v2.0/alarms/" + newAlarm.getId());
assertEquals(newAlarm, alarm); assertEquals(newAlarm, alarm);
verify(service).create(eq("abc"), eq("Disk Exceeds 1k Operations"), any(String.class), verify(service).create(eq("abc"), eq("Disk Exceeds 1k Operations"), any(String.class),
eq(expression), eq(AlarmExpression.of(expression)), any(List.class), any(List.class), eq(expression), eq(AlarmExpression.of(expression)), any(List.class), any(List.class),
any(List.class)); any(List.class));
} }
public void shouldErrorOnCreateWithInvalidDimensions() { public void shouldErrorOnCreateWithInvalidMetricName() {
String expression = "avg(hpcs.compute{instance_id=937, metric_name=disk_read_ops}) >= 90"; String expression = "avg(foo{service=hpcs.compute, instance_id=937}) >= 90";
ClientResponse response = createResponseFor(new CreateAlarmCommand( ClientResponse response = createResponseFor(new CreateAlarmCommand(
"Disk Exceeds 1k Operations", null, expression, alarmActions)); "Disk Exceeds 1k Operations", null, expression, alarmActions));
ErrorMessages.assertThat(response.getEntity(String.class)).matches("unprocessable_entity", 422, ErrorMessages.assertThat(response.getEntity(String.class)).matches("unprocessable_entity", 422,
"The required dimensions"); "foo is not a valid metric name for namespace hpcs.compute");
}
public void shouldErrorOnCreateWithInvalidDimensions() {
String expression = "avg(disk_read_ops{service=hpcs.compute, instance_id=937, foo=bar}) >= 90";
ClientResponse response = createResponseFor(new CreateAlarmCommand(
"Disk Exceeds 1k Operations", null, expression, alarmActions));
ErrorMessages.assertThat(response.getEntity(String.class)).matches("unprocessable_entity", 422,
"foo is not a valid dimension name for service hpcs.compute");
} }
public void shouldErrorOnCreateWithDuplicateDimensions() { public void shouldErrorOnCreateWithDuplicateDimensions() {
@ -104,15 +113,6 @@ public class AlarmResourceTest extends AbstractMonApiResourceTest {
"More than one value was given for dimension instance_id"); "More than one value was given for dimension instance_id");
} }
public void shouldErrorOnCreateWithEmptyDimensionsForHpCloudNamespace() {
String expression = "avg(hpcs.compute{metric_name=disk_read_ops}) >= 90";
ClientResponse response = createResponseFor(new CreateAlarmCommand(
"Disk Exceeds 1k Operations", null, expression, alarmActions));
ErrorMessages.assertThat(response.getEntity(String.class)).matches("unprocessable_entity", 422,
"The required dimensions [instance_id, instance_uuid, az] are not present");
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void shouldNotRequireDimensionsForCustomNamespace() { public void shouldNotRequireDimensionsForCustomNamespace() {
String expression = "avg(foo{metric_name=bar}) >= 90"; String expression = "avg(foo{metric_name=bar}) >= 90";
@ -132,15 +132,6 @@ public class AlarmResourceTest extends AbstractMonApiResourceTest {
"Unable to process the provided JSON", "Unexpected character (':'"); "Unable to process the provided JSON", "Unexpected character (':'");
} }
public void shouldErrorOnCreateWithInvalidMetricType() {
String expression = "avg(hpcs.compute{instance_id=937, az=2, instance_uuid=0ff588fc-d298-482f-bb11-4b52d56801a4, metric_name=blah}) >= 90";
ClientResponse response = createResponseFor(new CreateAlarmCommand(
"Disk Exceeds 1k Operations", null, expression, alarmActions));
ErrorMessages.assertThat(response.getEntity(String.class)).matches("unprocessable_entity", 422,
"blah is not a valid metric name for namespace hpcs.compute");
}
public void shouldErrorOnCreateWithInvalidOperator() { public void shouldErrorOnCreateWithInvalidOperator() {
String expression = "avg(hpcs.compute{instance_id=937, az=2, instance_uuid=0ff588fc-d298-482f-bb11-4b52d56801a4, metric_name=disk_read_ops}) & 90"; String expression = "avg(hpcs.compute{instance_id=937, az=2, instance_uuid=0ff588fc-d298-482f-bb11-4b52d56801a4, metric_name=disk_read_ops}) & 90";
ClientResponse response = createResponseFor(new CreateAlarmCommand( ClientResponse response = createResponseFor(new CreateAlarmCommand(
@ -155,9 +146,8 @@ public class AlarmResourceTest extends AbstractMonApiResourceTest {
ClientResponse response = createResponseFor(new CreateAlarmCommand( ClientResponse response = createResponseFor(new CreateAlarmCommand(
"Disk Exceeds 1k Operations", null, expression, null)); "Disk Exceeds 1k Operations", null, expression, null));
ErrorMessages.assertThat(response.getEntity(String.class)).matches("bad_request", 400, ErrorMessages.assertThat(response.getEntity(String.class)).matches("unprocessable_entity", 422,
"The request entity had the following errors:", "[alarmActions may not be empty (was null)]");
"[alarm.alarmActions may not be empty (was null)]");
} }
public void shouldErrorOnCreateWith0Period() { public void shouldErrorOnCreateWith0Period() {
@ -212,7 +202,6 @@ public class AlarmResourceTest extends AbstractMonApiResourceTest {
} }
public void shouldErrorOnCreateWithTooLongAlarmAction() { public void shouldErrorOnCreateWithTooLongAlarmAction() {
String expression = "avg(hpcs.compute{instance_id=937, az=2, instance_uuid=0ff588fc-d298-482f-bb11-4b52d56801a4, metric_name=disk_read_ops}) >= 90";
alarmActions = new ArrayList<String>(); alarmActions = new ArrayList<String>();
alarmActions.add("012345678901234567890123456789012345678901234567890"); alarmActions.add("012345678901234567890123456789012345678901234567890");
ClientResponse response = createResponseFor(new CreateAlarmCommand( ClientResponse response = createResponseFor(new CreateAlarmCommand(
@ -224,7 +213,7 @@ public class AlarmResourceTest extends AbstractMonApiResourceTest {
} }
public void shouldList() { public void shouldList() {
List<Alarm> alarms = client().resource("/v1.1/alarms") List<Alarm> alarms = client().resource("/v2.0/alarms")
.header("X-Tenant-Id", "abc") .header("X-Tenant-Id", "abc")
.get(new GenericType<List<Alarm>>() { .get(new GenericType<List<Alarm>>() {
}); });
@ -234,9 +223,9 @@ public class AlarmResourceTest extends AbstractMonApiResourceTest {
} }
public void shouldGet() { public void shouldGet() {
assertEquals(client().resource("/v1.1/alarms/123") assertEquals(client().resource("/v2.0/alarms/123")
.header("X-Tenant-Id", "abc") .header("X-Tenant-Id", "abc")
.get(Alarm.class), alarm); .get(AlarmDetail.class), alarm);
verify(repo).findById(eq("abc"), eq("123")); verify(repo).findById(eq("abc"), eq("123"));
} }
@ -244,7 +233,7 @@ public class AlarmResourceTest extends AbstractMonApiResourceTest {
doThrow(new EntityNotFoundException(null)).when(repo).findById(eq("abc"), eq("999")); doThrow(new EntityNotFoundException(null)).when(repo).findById(eq("abc"), eq("999"));
try { try {
client().resource("/v1.1/alarms/999").header("X-Tenant-Id", "abc").get(Alarm.class); client().resource("/v2.0/alarms/999").header("X-Tenant-Id", "abc").get(Alarm.class);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e.getMessage().contains("404")); assertTrue(e.getMessage().contains("404"));
@ -252,7 +241,7 @@ public class AlarmResourceTest extends AbstractMonApiResourceTest {
} }
public void shouldDelete() { public void shouldDelete() {
ClientResponse response = client().resource("/v1.1/alarms/123") ClientResponse response = client().resource("/v2.0/alarms/123")
.header("X-Tenant-Id", "abc") .header("X-Tenant-Id", "abc")
.delete(ClientResponse.class); .delete(ClientResponse.class);
assertEquals(response.getStatus(), 204); assertEquals(response.getStatus(), 204);
@ -263,7 +252,7 @@ public class AlarmResourceTest extends AbstractMonApiResourceTest {
doThrow(new EntityNotFoundException(null)).when(service).delete(eq("abc"), eq("999")); doThrow(new EntityNotFoundException(null)).when(service).delete(eq("abc"), eq("999"));
try { try {
client().resource("/v1.1/alarms/999").header("X-Tenant-Id", "abc").delete(); client().resource("/v2.0/alarms/999").header("X-Tenant-Id", "abc").delete();
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e.getMessage().contains("404")); assertTrue(e.getMessage().contains("404"));
@ -274,7 +263,7 @@ public class AlarmResourceTest extends AbstractMonApiResourceTest {
doThrow(new RuntimeException("")).when(repo).find(anyString()); doThrow(new RuntimeException("")).when(repo).find(anyString());
try { try {
client().resource("/v1.1/alarms").header("X-Tenant-Id", "abc").get(List.class); client().resource("/v2.0/alarms").header("X-Tenant-Id", "abc").get(List.class);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e.getMessage().contains("500")); assertTrue(e.getMessage().contains("500"));
@ -282,8 +271,8 @@ public class AlarmResourceTest extends AbstractMonApiResourceTest {
} }
public void shouldHydateLinksOnList() { public void shouldHydateLinksOnList() {
List<Link> expected = Arrays.asList(new Link("self", "/v1.1/alarms/123")); List<Link> expected = Arrays.asList(new Link("self", "/v2.0/alarms/123"));
List<Link> links = client().resource("/v1.1/alarms") List<Link> links = client().resource("/v2.0/alarms")
.header("X-Tenant-Id", "abc") .header("X-Tenant-Id", "abc")
.get(new GenericType<List<Alarm>>() { .get(new GenericType<List<Alarm>>() {
}) })
@ -293,15 +282,15 @@ public class AlarmResourceTest extends AbstractMonApiResourceTest {
} }
public void shouldHydateLinksOnGet() { public void shouldHydateLinksOnGet() {
List<Link> links = Arrays.asList(new Link("self", "/v1.1/alarms/123")); List<Link> links = Arrays.asList(new Link("self", "/v2.0/alarms/123"));
assertEquals(client().resource("/v1.1/alarms/123") assertEquals(client().resource("/v2.0/alarms/123")
.header("X-Tenant-Id", "abc") .header("X-Tenant-Id", "abc")
.get(Alarm.class) .get(AlarmDetail.class)
.getLinks(), links); .getLinks(), links);
} }
private ClientResponse createResponseFor(Object request) { private ClientResponse createResponseFor(Object request) {
return client().resource("/v1.1/alarms") return client().resource("/v2.0/alarms")
.header("X-Tenant-Id", "abc") .header("X-Tenant-Id", "abc")
.header("Content-Type", MediaType.APPLICATION_JSON) .header("Content-Type", MediaType.APPLICATION_JSON)
.post(ClientResponse.class, request); .post(ClientResponse.class, request);