diff --git a/spring-security-rest-full/src/main/webapp/WEB-INF/web.xml b/spring-security-rest-full/src/main/webapp/WEB-INF/web.xml index d88703596f..4232428520 100644 --- a/spring-security-rest-full/src/main/webapp/WEB-INF/web.xml +++ b/spring-security-rest-full/src/main/webapp/WEB-INF/web.xml @@ -1,52 +1,59 @@ - + http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> - Spring Security REST Application + Spring Security REST Application - - - contextClass - + + + contextClass + org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - contextConfigLocation - org.baeldung.spring - + + + contextConfigLocation + org.baeldung.spring + - - org.springframework.web.context.ContextLoaderListener - + + org.springframework.web.context.ContextLoaderListener + - - - api - org.springframework.web.servlet.DispatcherServlet - 1 - - - api - / - + + etagFilter + org.springframework.web.filter.ShallowEtagHeaderFilter + + + etagFilter + /* + - - - springSecurityFilterChain - org.springframework.web.filter.DelegatingFilterProxy - - - springSecurityFilterChain - /* - + + + api + org.springframework.web.servlet.DispatcherServlet + 1 + + + api + / + - - index.html - + + + springSecurityFilterChain + org.springframework.web.filter.DelegatingFilterProxy + + + springSecurityFilterChain + /* + + + + index.html + \ No newline at end of file diff --git a/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractBasicLiveTest.java b/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractBasicLiveTest.java new file mode 100644 index 0000000000..a8cd7f4f30 --- /dev/null +++ b/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractBasicLiveTest.java @@ -0,0 +1,158 @@ +package org.baeldung.common.web; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.apache.commons.lang3.RandomStringUtils.randomNumeric; +import static org.baeldung.web.util.HTTPLinkHeaderUtil.extractURIByRel; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.io.Serializable; +import java.util.List; + +import org.junit.Ignore; +import org.junit.Test; + +import com.google.common.net.HttpHeaders; +import com.jayway.restassured.response.Response; + +public abstract class AbstractBasicLiveTest extends AbstractLiveTest { + + public AbstractBasicLiveTest(final Class clazzToSet) { + super(clazzToSet); + } + + // tests + + @Test + public void givenResourceExists_whenRetrievingResource_thenEtagIsAlsoReturned() { + // Given + final String uriOfResource = createAsUri(); + + // When + final Response findOneResponse = givenAuth().header("Accept", "application/json").get(uriOfResource); + + // Then + assertNotNull(findOneResponse.getHeader(HttpHeaders.ETAG)); + } + + @Test + public void givenResourceWasRetrieved_whenRetrievingAgainWithEtag_thenNotModifiedReturned() { + // Given + final String uriOfResource = createAsUri(); + final Response findOneResponse = givenAuth().header("Accept", "application/json").get(uriOfResource); + final String etagValue = findOneResponse.getHeader(HttpHeaders.ETAG); + + // When + final Response secondFindOneResponse = givenAuth().header("Accept", "application/json").headers("If-None-Match", etagValue).get(uriOfResource); + + // Then + assertTrue(secondFindOneResponse.getStatusCode() == 304); + } + + @Test + @Ignore("No Update operation yet") + public void givenResourceWasRetrievedThenModified_whenRetrievingAgainWithEtag_thenResourceIsReturned() { + // Given + final String uriOfResource = createAsUri(); + final Response findOneResponse = givenAuth().header("Accept", "application/json").get(uriOfResource); + final String etagValue = findOneResponse.getHeader(HttpHeaders.ETAG); + + // existingResource.setName(randomAlphabetic(6)); + // getApi().update(existingResource.setName("randomString")); + + // When + final Response secondFindOneResponse = givenAuth().header("Accept", "application/json").headers("If-None-Match", etagValue).get(uriOfResource); + + // Then + assertTrue(secondFindOneResponse.getStatusCode() == 200); + } + + @Test + @Ignore("Not Yet Implemented By Spring - https://jira.springsource.org/browse/SPR-10164") + public void givenResourceExists_whenRetrievedWithIfMatchIncorrectEtag_then412IsReceived() { + // Given + final String uriOfResource = createAsUri(); + + // When + final Response findOneResponse = givenAuth().header("Accept", "application/json").headers("If-Match", randomAlphabetic(8)).get(uriOfResource); + + // Then + assertTrue(findOneResponse.getStatusCode() == 412); + } + + // find - one + + // find - all + + // find - all - paginated + + @Test + public void whenResourcesAreRetrievedPaged_then200IsReceived() { + final Response response = givenAuth().get(getURL() + "?page=0&size=10"); + + assertThat(response.getStatusCode(), is(200)); + } + + @Test + public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived() { + final String url = getURL() + "?page=" + randomNumeric(5) + "&size=10"; + final Response response = givenAuth().get(url); + + assertThat(response.getStatusCode(), is(404)); + } + + @Test + public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() { + create(); + + final Response response = givenAuth().get(getURL() + "?page=0&size=10"); + + assertFalse(response.body().as(List.class).isEmpty()); + } + + @Test + public void whenFirstPageOfResourcesAreRetrieved_thenSecondPageIsNext() { + final Response response = givenAuth().get(getURL() + "?page=0&size=2"); + + final String uriToNextPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "next"); + assertEquals(getURL() + "?page=1&size=2", uriToNextPage); + } + + @Test + public void whenFirstPageOfResourcesAreRetrieved_thenNoPreviousPage() { + final Response response = givenAuth().get(getURL() + "?page=0&size=2"); + + final String uriToPrevPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "prev"); + assertNull(uriToPrevPage); + } + + @Test + public void whenSecondPageOfResourcesAreRetrieved_thenFirstPageIsPrevious() { + create(); + create(); + + final Response response = givenAuth().get(getURL() + "?page=1&size=2"); + + final String uriToPrevPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "prev"); + assertEquals(getURL() + "?page=0&size=2", uriToPrevPage); + } + + @Test + public void whenLastPageOfResourcesIsRetrieved_thenNoNextPageIsDiscoverable() { + final Response first = givenAuth().get(getURL() + "?page=0&size=2"); + final String uriToLastPage = extractURIByRel(first.getHeader(HttpHeaders.LINK), "last"); + + final Response response = givenAuth().get(uriToLastPage); + + final String uriToNextPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "next"); + assertNull(uriToNextPage); + } + + // count + +} diff --git a/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractDiscoverabilityLiveTest.java b/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractDiscoverabilityLiveTest.java new file mode 100644 index 0000000000..4a7c19b6ce --- /dev/null +++ b/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractDiscoverabilityLiveTest.java @@ -0,0 +1,74 @@ +package org.baeldung.common.web; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.io.Serializable; + +import org.baeldung.persistence.model.Foo; +import org.baeldung.web.util.HTTPLinkHeaderUtil; +import org.hamcrest.core.AnyOf; +import org.junit.Test; +import org.springframework.http.MediaType; + +import com.google.common.net.HttpHeaders; +import com.jayway.restassured.response.Response; + +public abstract class AbstractDiscoverabilityLiveTest extends AbstractLiveTest { + + public AbstractDiscoverabilityLiveTest(final Class clazzToSet) { + super(clazzToSet); + } + + // tests + + // discoverability + + @Test + public void whenInvalidPOSTIsSentToValidURIOfResource_thenAllowHeaderListsTheAllowedActions() { + // Given + final String uriOfExistingResource = createAsUri(); + + // When + final Response res = givenAuth().post(uriOfExistingResource); + + // Then + final String allowHeader = res.getHeader(HttpHeaders.ALLOW); + assertThat(allowHeader, AnyOf. anyOf(containsString("GET"), containsString("PUT"), containsString("DELETE"))); + } + + @Test + public void whenResourceIsCreated_thenUriOfTheNewlyCreatedResourceIsDiscoverable() { + // When + final Foo newResource = new Foo(randomAlphabetic(6)); + final Response createResp = givenAuth().contentType(MediaType.APPLICATION_JSON_VALUE).body(newResource).post(getURL()); + final String uriOfNewResource = createResp.getHeader(HttpHeaders.LOCATION); + + // Then + final Response response = givenAuth().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE).get(uriOfNewResource); + + final Foo resourceFromServer = response.body().as(Foo.class); + assertThat(newResource, equalTo(resourceFromServer)); + } + + @Test + public void whenResourceIsRetrieved_thenUriToGetAllResourcesIsDiscoverable() { + // Given + final String uriOfExistingResource = createAsUri(); + + // When + final Response getResponse = givenAuth().get(uriOfExistingResource); + + // Then + final String uriToAllResources = HTTPLinkHeaderUtil.extractURIByRel(getResponse.getHeader("Link"), "collection"); + + final Response getAllResponse = givenAuth().get(uriToAllResources); + assertThat(getAllResponse.getStatusCode(), is(200)); + } + + // template method + +} diff --git a/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractLiveTest.java b/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractLiveTest.java index 86b934ae1e..862651266b 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractLiveTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractLiveTest.java @@ -1,20 +1,8 @@ package org.baeldung.common.web; -import static org.apache.commons.lang3.RandomStringUtils.randomNumeric; -import static org.baeldung.web.util.HTTPLinkHeaderUtil.extractURIByRel; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; - import java.io.Serializable; -import java.util.List; import org.baeldung.test.IMarshaller; -import org.hamcrest.core.AnyOf; -import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import com.google.common.base.Preconditions; @@ -37,90 +25,6 @@ public abstract class AbstractLiveTest { clazz = clazzToSet; } - // tests - @Test - public void whenInvalidPOSTIsSentToValidURIOfResource_thenAllowHeaderListsTheAllowedActions() { - // Given - final String uriOfExistingResource = createAsUri(); - - // When - final Response res = givenAuth().post(uriOfExistingResource); - - // Then - final String allowHeader = res.getHeader(HttpHeaders.ALLOW); - assertThat(allowHeader, AnyOf. anyOf(containsString("GET"), containsString("PUT"), containsString("DELETE"))); - } - - // find - one - - // find - all - - // find - all - paginated - - @Test - public void whenResourcesAreRetrievedPaged_then200IsReceived() { - final Response response = givenAuth().get(getFooURL() + "?page=0&size=10"); - - assertThat(response.getStatusCode(), is(200)); - } - - @Test - public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived() { - final String url = getFooURL() + "?page=" + randomNumeric(5) + "&size=10"; - final Response response = givenAuth().get(url); - - assertThat(response.getStatusCode(), is(404)); - } - - @Test - public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() { - create(); - - final Response response = givenAuth().get(getFooURL() + "?page=0&size=10"); - - assertFalse(response.body().as(List.class).isEmpty()); - } - - @Test - public void whenFirstPageOfResourcesAreRetrieved_thenSecondPageIsNext() { - final Response response = givenAuth().get(getFooURL() + "?page=0&size=2"); - - final String uriToNextPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "next"); - assertEquals(getFooURL() + "?page=1&size=2", uriToNextPage); - } - - @Test - public void whenFirstPageOfResourcesAreRetrieved_thenNoPreviousPage() { - final Response response = givenAuth().get(getFooURL() + "?page=0&size=2"); - - final String uriToPrevPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "prev"); - assertNull(uriToPrevPage); - } - - @Test - public void whenSecondPageOfResourcesAreRetrieved_thenFirstPageIsPrevious() { - create(); - create(); - - final Response response = givenAuth().get(getFooURL() + "?page=1&size=2"); - - final String uriToPrevPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "prev"); - assertEquals(getFooURL() + "?page=0&size=2", uriToPrevPage); - } - - @Test - public void whenLastPageOfResourcesIsRetrieved_thenNoNextPageIsDiscoverable() { - final Response first = givenAuth().get(getFooURL() + "?page=0&size=2"); - final String uriToLastPage = extractURIByRel(first.getHeader(HttpHeaders.LINK), "last"); - - final Response response = givenAuth().get(uriToLastPage); - - final String uriToNextPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "next"); - assertNull(uriToNextPage); - } - - // count - // template method public abstract void create(); @@ -145,12 +49,12 @@ public abstract class AbstractLiveTest { final RequestSpecification givenAuthenticated = givenAuth(); final String resourceAsString = marshaller.encode(resource); - return givenAuthenticated.contentType(marshaller.getMime()).body(resourceAsString).post(getFooURL()); + return givenAuthenticated.contentType(marshaller.getMime()).body(resourceAsString).post(getURL()); } // - private String getFooURL() { + protected String getURL() { return "http://localhost:8080/spring-security-rest-full/foos"; } diff --git a/spring-security-rest-full/src/test/java/org/baeldung/web/FooDiscoverabilityLiveTest.java b/spring-security-rest-full/src/test/java/org/baeldung/web/FooDiscoverabilityLiveTest.java new file mode 100644 index 0000000000..28dfcd372c --- /dev/null +++ b/spring-security-rest-full/src/test/java/org/baeldung/web/FooDiscoverabilityLiveTest.java @@ -0,0 +1,33 @@ +package org.baeldung.web; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; + +import org.baeldung.common.web.AbstractDiscoverabilityLiveTest; +import org.baeldung.persistence.model.Foo; +import org.baeldung.spring.ConfigTest; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { ConfigTest.class }, loader = AnnotationConfigContextLoader.class) +public class FooDiscoverabilityLiveTest extends AbstractDiscoverabilityLiveTest { + + public FooDiscoverabilityLiveTest() { + super(Foo.class); + } + + // API + + @Override + public final void create() { + create(new Foo(randomAlphabetic(6))); + } + + @Override + public final String createAsUri() { + return createAsUri(new Foo(randomAlphabetic(6))); + } + +} diff --git a/spring-security-rest-full/src/test/java/org/baeldung/web/FooLiveTest.java b/spring-security-rest-full/src/test/java/org/baeldung/web/FooLiveTest.java index 36b30d5e57..9024ca4f96 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/web/FooLiveTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/web/FooLiveTest.java @@ -2,7 +2,7 @@ package org.baeldung.web; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; -import org.baeldung.common.web.AbstractLiveTest; +import org.baeldung.common.web.AbstractBasicLiveTest; import org.baeldung.persistence.model.Foo; import org.baeldung.spring.ConfigTest; import org.junit.runner.RunWith; @@ -12,7 +12,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { ConfigTest.class }, loader = AnnotationConfigContextLoader.class) -public class FooLiveTest extends AbstractLiveTest { +public class FooLiveTest extends AbstractBasicLiveTest { public FooLiveTest() { super(Foo.class);