Commit 40bf4a1a authored by Administrator's avatar Administrator

Adds the HasHttpStatus matcher

This matchers makes the Response status easier to check in tests.
parent f739d617
...@@ -11,8 +11,12 @@ import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithPersis ...@@ -11,8 +11,12 @@ import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithPersis
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithoutPets; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithoutPets;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owner; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owner;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owners; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owners;
import static es.uvigo.esei.xcs.http.util.HasHttpStatus.hasHttpStatus;
import static javax.ws.rs.client.Entity.json; import static javax.ws.rs.client.Entity.json;
import static org.hamcrest.CoreMatchers.equalTo; import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.METHOD_NOT_ALLOWED;
import static javax.ws.rs.core.Response.Status.OK;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
...@@ -80,7 +84,7 @@ public class OwnerResourceRestTest { ...@@ -80,7 +84,7 @@ public class OwnerResourceRestTest {
) throws Exception { ) throws Exception {
final Response response = webTarget.request().get(); final Response response = webTarget.request().get();
assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); assertThat(response, hasHttpStatus(OK));
final Owner owner = response.readEntity(Owner.class); final Owner owner = response.readEntity(Owner.class);
final Owner expected = owner("pepe"); final Owner expected = owner("pepe");
...@@ -108,7 +112,7 @@ public class OwnerResourceRestTest { ...@@ -108,7 +112,7 @@ public class OwnerResourceRestTest {
) throws Exception { ) throws Exception {
final Response response = webTarget.request().get(); final Response response = webTarget.request().get();
assertThat(response.getStatusInfo(), is(equalTo(Response.Status.BAD_REQUEST))); assertThat(response, hasHttpStatus(BAD_REQUEST));
} }
@Test @InSequence(6) @Test @InSequence(6)
...@@ -131,7 +135,7 @@ public class OwnerResourceRestTest { ...@@ -131,7 +135,7 @@ public class OwnerResourceRestTest {
) throws Exception { ) throws Exception {
final Response response = webTarget.request().get(); final Response response = webTarget.request().get();
assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); assertThat(response, hasHttpStatus(OK));
final List<Owner> list = ListOwnerType.readEntity(response); final List<Owner> list = ListOwnerType.readEntity(response);
assertThat(list, containsOwnersInAnyOrder(owners())); assertThat(list, containsOwnersInAnyOrder(owners()));
...@@ -191,7 +195,7 @@ public class OwnerResourceRestTest { ...@@ -191,7 +195,7 @@ public class OwnerResourceRestTest {
private void testCreateOwner(WebTarget webTarget, Owner newOwner, Owner persistentOwner) { private void testCreateOwner(WebTarget webTarget, Owner newOwner, Owner persistentOwner) {
final Response response = webTarget.request().post(json(newOwner)); final Response response = webTarget.request().post(json(newOwner));
assertThat(response.getStatusInfo(), is(equalTo(Response.Status.CREATED))); assertThat(response, hasHttpStatus(CREATED));
final String location = response.getHeaderString("Location"); final String location = response.getHeaderString("Location");
...@@ -218,7 +222,7 @@ public class OwnerResourceRestTest { ...@@ -218,7 +222,7 @@ public class OwnerResourceRestTest {
final Response response = webTarget.request().put(json(owner)); final Response response = webTarget.request().put(json(owner));
assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); assertThat(response, hasHttpStatus(OK));
} }
@Test @InSequence(32) @Test @InSequence(32)
...@@ -241,7 +245,7 @@ public class OwnerResourceRestTest { ...@@ -241,7 +245,7 @@ public class OwnerResourceRestTest {
) throws Exception { ) throws Exception {
final Response response = webTarget.request().delete(); final Response response = webTarget.request().delete();
assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); assertThat(response, hasHttpStatus(OK));
} }
@Test @InSequence(42) @Test @InSequence(42)
...@@ -264,7 +268,7 @@ public class OwnerResourceRestTest { ...@@ -264,7 +268,7 @@ public class OwnerResourceRestTest {
) throws Exception { ) throws Exception {
final Response response = webTarget.request().delete(); final Response response = webTarget.request().delete();
assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); assertThat(response, hasHttpStatus(OK));
} }
@Test @InSequence(45) @Test @InSequence(45)
...@@ -287,7 +291,7 @@ public class OwnerResourceRestTest { ...@@ -287,7 +291,7 @@ public class OwnerResourceRestTest {
) throws Exception { ) throws Exception {
final Response response = webTarget.request().delete(); final Response response = webTarget.request().delete();
assertThat(response.getStatusInfo(), is(equalTo(Response.Status.METHOD_NOT_ALLOWED))); assertThat(response, hasHttpStatus(METHOD_NOT_ALLOWED));
} }
@Test @InSequence(48) @Test @InSequence(48)
......
...@@ -7,8 +7,11 @@ import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.anyOwner; ...@@ -7,8 +7,11 @@ import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.anyOwner;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithFreshPets; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithFreshPets;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithPersistentPets; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.newOwnerWithPersistentPets;
import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owners; import static es.uvigo.esei.xcs.domain.entities.OwnersDataset.owners;
import static es.uvigo.esei.xcs.http.util.HasHttpStatus.hasHttpStatus;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.Collections.emptyList; import static java.util.Collections.emptyList;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.OK;
import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall; import static org.easymock.EasyMock.expectLastCall;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
...@@ -66,7 +69,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { ...@@ -66,7 +69,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport {
final Response response = resource.get(owner.getLogin()); final Response response = resource.get(owner.getLogin());
assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); assertThat(response, hasHttpStatus(OK));
assertThat(response.getEntity(), is(instanceOf(Owner.class))); assertThat(response.getEntity(), is(instanceOf(Owner.class)));
assertThat((Owner) response.getEntity(), is(equalsToOwner(owner))); assertThat((Owner) response.getEntity(), is(equalsToOwner(owner)));
} }
...@@ -102,7 +105,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { ...@@ -102,7 +105,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport {
final Response response = resource.list(); final Response response = resource.list();
assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); assertThat(response, hasHttpStatus(OK));
assertThat(response.getEntity(), is(instanceOf(List.class))); assertThat(response.getEntity(), is(instanceOf(List.class)));
assertThat((List<Owner>) response.getEntity(), containsOwnersInAnyOrder(owners)); assertThat((List<Owner>) response.getEntity(), containsOwnersInAnyOrder(owners));
} }
...@@ -117,7 +120,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { ...@@ -117,7 +120,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport {
final Response response = resource.list(); final Response response = resource.list();
assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); assertThat(response, hasHttpStatus(OK));
assertThat(response.getEntity(), is(instanceOf(List.class))); assertThat(response.getEntity(), is(instanceOf(List.class)));
assertThat((List<Owner>) response.getEntity(), is(empty())); assertThat((List<Owner>) response.getEntity(), is(empty()));
} }
...@@ -143,7 +146,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { ...@@ -143,7 +146,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport {
final Response response = resource.create(newOwner); final Response response = resource.create(newOwner);
assertThat(response.getStatusInfo(), is(equalTo(Response.Status.CREATED))); assertThat(response, hasHttpStatus(CREATED));
assertThat(response.getHeaderString("Location"), is(equalTo(mockUri.toString()))); assertThat(response.getHeaderString("Location"), is(equalTo(mockUri.toString())));
} }
...@@ -166,7 +169,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { ...@@ -166,7 +169,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport {
final Response response = resource.update(owner); final Response response = resource.update(owner);
assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); assertThat(response, hasHttpStatus(OK));
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
...@@ -186,7 +189,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { ...@@ -186,7 +189,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport {
final Response response = resource.delete(login); final Response response = resource.delete(login);
assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); assertThat(response, hasHttpStatus(OK));
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
......
package es.uvigo.esei.xcs.http.util;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.StatusType;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.TypeSafeMatcher;
public class HasHttpStatus extends TypeSafeMatcher<Response> {
private StatusType status;
public HasHttpStatus(StatusType status) {
this.status = status;
}
public HasHttpStatus(int statusCode) {
this(Response.Status.fromStatusCode(statusCode));
}
@Override
public void describeTo(Description description) {
description.appendValue(this.status);
}
@Override
protected boolean matchesSafely(Response item) {
return this.status.getStatusCode() == item.getStatus();
}
@Factory
public static HasHttpStatus hasHttpStatus(int statusCode) {
return new HasHttpStatus(statusCode);
}
@Factory
public static HasHttpStatus hasHttpStatus(StatusType status) {
return new HasHttpStatus(status);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment