|
| 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 | +} |
0 commit comments