Skip to content

Commit c2c3b5e

Browse files
author
eugenp
committed
etag work
1 parent 71122ad commit c2c3b5e

File tree

6 files changed

+320
-144
lines changed

6 files changed

+320
-144
lines changed
Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,59 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
4-
xsi:schemaLocation="
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
3+
xsi:schemaLocation="
54
http://java.sun.com/xml/ns/javaee
6-
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
7-
id="WebApp_ID" version="3.0">
5+
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
86

9-
<display-name>Spring Security REST Application</display-name>
7+
<display-name>Spring Security REST Application</display-name>
108

11-
<!-- Spring root -->
12-
<context-param>
13-
<param-name>contextClass</param-name>
14-
<param-value>
9+
<!-- Spring root -->
10+
<context-param>
11+
<param-name>contextClass</param-name>
12+
<param-value>
1513
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
1614
</param-value>
17-
</context-param>
18-
<context-param>
19-
<param-name>contextConfigLocation</param-name>
20-
<param-value>org.baeldung.spring</param-value>
21-
</context-param>
22-
23-
<listener>
24-
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
25-
</listener>
26-
27-
<!-- Spring child -->
28-
<servlet>
29-
<servlet-name>api</servlet-name>
30-
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
31-
<load-on-startup>1</load-on-startup>
32-
</servlet>
33-
<servlet-mapping>
34-
<servlet-name>api</servlet-name>
35-
<url-pattern>/</url-pattern>
36-
</servlet-mapping>
37-
38-
<!-- Spring Security -->
39-
<filter>
40-
<filter-name>springSecurityFilterChain</filter-name>
41-
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
42-
</filter>
43-
<filter-mapping>
44-
<filter-name>springSecurityFilterChain</filter-name>
45-
<url-pattern>/*</url-pattern>
46-
</filter-mapping>
47-
48-
<welcome-file-list>
49-
<welcome-file>index.html</welcome-file>
50-
</welcome-file-list>
15+
</context-param>
16+
<context-param>
17+
<param-name>contextConfigLocation</param-name>
18+
<param-value>org.baeldung.spring</param-value>
19+
</context-param>
20+
21+
<listener>
22+
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
23+
</listener>
24+
25+
<filter>
26+
<filter-name>etagFilter</filter-name>
27+
<filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class>
28+
</filter>
29+
<filter-mapping>
30+
<filter-name>etagFilter</filter-name>
31+
<url-pattern>/*</url-pattern>
32+
</filter-mapping>
33+
34+
<!-- Spring child -->
35+
<servlet>
36+
<servlet-name>api</servlet-name>
37+
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
38+
<load-on-startup>1</load-on-startup>
39+
</servlet>
40+
<servlet-mapping>
41+
<servlet-name>api</servlet-name>
42+
<url-pattern>/</url-pattern>
43+
</servlet-mapping>
44+
45+
<!-- Spring Security -->
46+
<filter>
47+
<filter-name>springSecurityFilterChain</filter-name>
48+
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
49+
</filter>
50+
<filter-mapping>
51+
<filter-name>springSecurityFilterChain</filter-name>
52+
<url-pattern>/*</url-pattern>
53+
</filter-mapping>
54+
55+
<welcome-file-list>
56+
<welcome-file>index.html</welcome-file>
57+
</welcome-file-list>
5158

5259
</web-app>
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package org.baeldung.common.web;
2+
3+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4+
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
5+
import static org.baeldung.web.util.HTTPLinkHeaderUtil.extractURIByRel;
6+
import static org.hamcrest.Matchers.is;
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertFalse;
9+
import static org.junit.Assert.assertNotNull;
10+
import static org.junit.Assert.assertNull;
11+
import static org.junit.Assert.assertThat;
12+
import static org.junit.Assert.assertTrue;
13+
14+
import java.io.Serializable;
15+
import java.util.List;
16+
17+
import org.junit.Ignore;
18+
import org.junit.Test;
19+
20+
import com.google.common.net.HttpHeaders;
21+
import com.jayway.restassured.response.Response;
22+
23+
public abstract class AbstractBasicLiveTest<T extends Serializable> extends AbstractLiveTest<T> {
24+
25+
public AbstractBasicLiveTest(final Class<T> clazzToSet) {
26+
super(clazzToSet);
27+
}
28+
29+
// tests
30+
31+
@Test
32+
public void givenResourceExists_whenRetrievingResource_thenEtagIsAlsoReturned() {
33+
// Given
34+
final String uriOfResource = createAsUri();
35+
36+
// When
37+
final Response findOneResponse = givenAuth().header("Accept", "application/json").get(uriOfResource);
38+
39+
// Then
40+
assertNotNull(findOneResponse.getHeader(HttpHeaders.ETAG));
41+
}
42+
43+
@Test
44+
public void givenResourceWasRetrieved_whenRetrievingAgainWithEtag_thenNotModifiedReturned() {
45+
// Given
46+
final String uriOfResource = createAsUri();
47+
final Response findOneResponse = givenAuth().header("Accept", "application/json").get(uriOfResource);
48+
final String etagValue = findOneResponse.getHeader(HttpHeaders.ETAG);
49+
50+
// When
51+
final Response secondFindOneResponse = givenAuth().header("Accept", "application/json").headers("If-None-Match", etagValue).get(uriOfResource);
52+
53+
// Then
54+
assertTrue(secondFindOneResponse.getStatusCode() == 304);
55+
}
56+
57+
@Test
58+
@Ignore("No Update operation yet")
59+
public void givenResourceWasRetrievedThenModified_whenRetrievingAgainWithEtag_thenResourceIsReturned() {
60+
// Given
61+
final String uriOfResource = createAsUri();
62+
final Response findOneResponse = givenAuth().header("Accept", "application/json").get(uriOfResource);
63+
final String etagValue = findOneResponse.getHeader(HttpHeaders.ETAG);
64+
65+
// existingResource.setName(randomAlphabetic(6));
66+
// getApi().update(existingResource.setName("randomString"));
67+
68+
// When
69+
final Response secondFindOneResponse = givenAuth().header("Accept", "application/json").headers("If-None-Match", etagValue).get(uriOfResource);
70+
71+
// Then
72+
assertTrue(secondFindOneResponse.getStatusCode() == 200);
73+
}
74+
75+
@Test
76+
@Ignore("Not Yet Implemented By Spring - https://jira.springsource.org/browse/SPR-10164")
77+
public void givenResourceExists_whenRetrievedWithIfMatchIncorrectEtag_then412IsReceived() {
78+
// Given
79+
final String uriOfResource = createAsUri();
80+
81+
// When
82+
final Response findOneResponse = givenAuth().header("Accept", "application/json").headers("If-Match", randomAlphabetic(8)).get(uriOfResource);
83+
84+
// Then
85+
assertTrue(findOneResponse.getStatusCode() == 412);
86+
}
87+
88+
// find - one
89+
90+
// find - all
91+
92+
// find - all - paginated
93+
94+
@Test
95+
public void whenResourcesAreRetrievedPaged_then200IsReceived() {
96+
final Response response = givenAuth().get(getURL() + "?page=0&size=10");
97+
98+
assertThat(response.getStatusCode(), is(200));
99+
}
100+
101+
@Test
102+
public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived() {
103+
final String url = getURL() + "?page=" + randomNumeric(5) + "&size=10";
104+
final Response response = givenAuth().get(url);
105+
106+
assertThat(response.getStatusCode(), is(404));
107+
}
108+
109+
@Test
110+
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
111+
create();
112+
113+
final Response response = givenAuth().get(getURL() + "?page=0&size=10");
114+
115+
assertFalse(response.body().as(List.class).isEmpty());
116+
}
117+
118+
@Test
119+
public void whenFirstPageOfResourcesAreRetrieved_thenSecondPageIsNext() {
120+
final Response response = givenAuth().get(getURL() + "?page=0&size=2");
121+
122+
final String uriToNextPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "next");
123+
assertEquals(getURL() + "?page=1&size=2", uriToNextPage);
124+
}
125+
126+
@Test
127+
public void whenFirstPageOfResourcesAreRetrieved_thenNoPreviousPage() {
128+
final Response response = givenAuth().get(getURL() + "?page=0&size=2");
129+
130+
final String uriToPrevPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "prev");
131+
assertNull(uriToPrevPage);
132+
}
133+
134+
@Test
135+
public void whenSecondPageOfResourcesAreRetrieved_thenFirstPageIsPrevious() {
136+
create();
137+
create();
138+
139+
final Response response = givenAuth().get(getURL() + "?page=1&size=2");
140+
141+
final String uriToPrevPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "prev");
142+
assertEquals(getURL() + "?page=0&size=2", uriToPrevPage);
143+
}
144+
145+
@Test
146+
public void whenLastPageOfResourcesIsRetrieved_thenNoNextPageIsDiscoverable() {
147+
final Response first = givenAuth().get(getURL() + "?page=0&size=2");
148+
final String uriToLastPage = extractURIByRel(first.getHeader(HttpHeaders.LINK), "last");
149+
150+
final Response response = givenAuth().get(uriToLastPage);
151+
152+
final String uriToNextPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "next");
153+
assertNull(uriToNextPage);
154+
}
155+
156+
// count
157+
158+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.baeldung.common.web;
2+
3+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4+
import static org.hamcrest.Matchers.containsString;
5+
import static org.hamcrest.Matchers.equalTo;
6+
import static org.hamcrest.Matchers.is;
7+
import static org.junit.Assert.assertThat;
8+
9+
import java.io.Serializable;
10+
11+
import org.baeldung.persistence.model.Foo;
12+
import org.baeldung.web.util.HTTPLinkHeaderUtil;
13+
import org.hamcrest.core.AnyOf;
14+
import org.junit.Test;
15+
import org.springframework.http.MediaType;
16+
17+
import com.google.common.net.HttpHeaders;
18+
import com.jayway.restassured.response.Response;
19+
20+
public abstract class AbstractDiscoverabilityLiveTest<T extends Serializable> extends AbstractLiveTest<T> {
21+
22+
public AbstractDiscoverabilityLiveTest(final Class<T> clazzToSet) {
23+
super(clazzToSet);
24+
}
25+
26+
// tests
27+
28+
// discoverability
29+
30+
@Test
31+
public void whenInvalidPOSTIsSentToValidURIOfResource_thenAllowHeaderListsTheAllowedActions() {
32+
// Given
33+
final String uriOfExistingResource = createAsUri();
34+
35+
// When
36+
final Response res = givenAuth().post(uriOfExistingResource);
37+
38+
// Then
39+
final String allowHeader = res.getHeader(HttpHeaders.ALLOW);
40+
assertThat(allowHeader, AnyOf.<String> anyOf(containsString("GET"), containsString("PUT"), containsString("DELETE")));
41+
}
42+
43+
@Test
44+
public void whenResourceIsCreated_thenUriOfTheNewlyCreatedResourceIsDiscoverable() {
45+
// When
46+
final Foo newResource = new Foo(randomAlphabetic(6));
47+
final Response createResp = givenAuth().contentType(MediaType.APPLICATION_JSON_VALUE).body(newResource).post(getURL());
48+
final String uriOfNewResource = createResp.getHeader(HttpHeaders.LOCATION);
49+
50+
// Then
51+
final Response response = givenAuth().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE).get(uriOfNewResource);
52+
53+
final Foo resourceFromServer = response.body().as(Foo.class);
54+
assertThat(newResource, equalTo(resourceFromServer));
55+
}
56+
57+
@Test
58+
public void whenResourceIsRetrieved_thenUriToGetAllResourcesIsDiscoverable() {
59+
// Given
60+
final String uriOfExistingResource = createAsUri();
61+
62+
// When
63+
final Response getResponse = givenAuth().get(uriOfExistingResource);
64+
65+
// Then
66+
final String uriToAllResources = HTTPLinkHeaderUtil.extractURIByRel(getResponse.getHeader("Link"), "collection");
67+
68+
final Response getAllResponse = givenAuth().get(uriToAllResources);
69+
assertThat(getAllResponse.getStatusCode(), is(200));
70+
}
71+
72+
// template method
73+
74+
}

0 commit comments

Comments
 (0)