diff --git a/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceRestTest.java b/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceRestTest.java index 45db6db750ea36b75aff8d77dfea95a8d763b98a..48c478c774798006bf6fa9948d5f0c006d2b3e14 100644 --- a/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceRestTest.java +++ b/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceRestTest.java @@ -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.owner; 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 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.junit.Assert.assertThat; @@ -80,7 +84,7 @@ public class OwnerResourceRestTest { ) throws Exception { 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 expected = owner("pepe"); @@ -107,8 +111,8 @@ public class OwnerResourceRestTest { @ArquillianResteasyResource(BASE_PATH + NON_EXISTENT_LOGIN) ResteasyWebTarget webTarget ) throws Exception { final Response response = webTarget.request().get(); - - assertThat(response.getStatusInfo(), is(equalTo(Response.Status.BAD_REQUEST))); + + assertThat(response, hasHttpStatus(BAD_REQUEST)); } @Test @InSequence(6) @@ -131,7 +135,7 @@ public class OwnerResourceRestTest { ) throws Exception { final Response response = webTarget.request().get(); - assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); + assertThat(response, hasHttpStatus(OK)); final List list = ListOwnerType.readEntity(response); assertThat(list, containsOwnersInAnyOrder(owners())); @@ -190,8 +194,8 @@ public class OwnerResourceRestTest { private void testCreateOwner(WebTarget webTarget, Owner newOwner, Owner persistentOwner) { 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"); @@ -217,8 +221,8 @@ public class OwnerResourceRestTest { owner.changePassword("newpassword"); final Response response = webTarget.request().put(json(owner)); - - assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); + + assertThat(response, hasHttpStatus(OK)); } @Test @InSequence(32) @@ -240,8 +244,8 @@ public class OwnerResourceRestTest { @ArquillianResteasyResource(BASE_PATH + OWNER_WITHOUT_PETS_LOGIN) ResteasyWebTarget webTarget ) throws Exception { final Response response = webTarget.request().delete(); - - assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); + + assertThat(response, hasHttpStatus(OK)); } @Test @InSequence(42) @@ -263,8 +267,8 @@ public class OwnerResourceRestTest { @ArquillianResteasyResource(BASE_PATH + OWNER_WITH_PETS_LOGIN) ResteasyWebTarget webTarget ) throws Exception { final Response response = webTarget.request().delete(); - - assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); + + assertThat(response, hasHttpStatus(OK)); } @Test @InSequence(45) @@ -286,8 +290,8 @@ public class OwnerResourceRestTest { @ArquillianResteasyResource(BASE_PATH) ResteasyWebTarget webTarget ) throws Exception { 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) diff --git a/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceUnitTest.java b/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceUnitTest.java index a635711892829804677d4bdcfa6c75bf6322a1b4..1e61febd7fa5c92795b496e379523eaf291c7c09 100644 --- a/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceUnitTest.java +++ b/rest/src/test/java/es/uvigo/esei/xcs/rest/OwnerResourceUnitTest.java @@ -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.newOwnerWithPersistentPets; 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.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.expectLastCall; import static org.hamcrest.CoreMatchers.equalTo; @@ -66,7 +69,7 @@ public class OwnerResourceUnitTest extends EasyMockSupport { 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((Owner) response.getEntity(), is(equalsToOwner(owner))); } @@ -101,8 +104,8 @@ public class OwnerResourceUnitTest extends EasyMockSupport { replayAll(); 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((List) response.getEntity(), containsOwnersInAnyOrder(owners)); } @@ -116,8 +119,8 @@ public class OwnerResourceUnitTest extends EasyMockSupport { replayAll(); 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((List) response.getEntity(), is(empty())); } @@ -142,8 +145,8 @@ public class OwnerResourceUnitTest extends EasyMockSupport { replayAll(); 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()))); } @@ -165,8 +168,8 @@ public class OwnerResourceUnitTest extends EasyMockSupport { replayAll(); final Response response = resource.update(owner); - - assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); + + assertThat(response, hasHttpStatus(OK)); } @Test(expected = IllegalArgumentException.class) @@ -185,8 +188,8 @@ public class OwnerResourceUnitTest extends EasyMockSupport { replayAll(); final Response response = resource.delete(login); - - assertThat(response.getStatusInfo(), is(equalTo(Response.Status.OK))); + + assertThat(response, hasHttpStatus(OK)); } @Test(expected = IllegalArgumentException.class) diff --git a/tests/src/main/java/es/uvigo/esei/xcs/http/util/HasHttpStatus.java b/tests/src/main/java/es/uvigo/esei/xcs/http/util/HasHttpStatus.java new file mode 100644 index 0000000000000000000000000000000000000000..dcd75e954cc58a2ef76282a51fa30a132c86c770 --- /dev/null +++ b/tests/src/main/java/es/uvigo/esei/xcs/http/util/HasHttpStatus.java @@ -0,0 +1,40 @@ +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 { + 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); + } +}