|
| 1 | +package pragmatech.digital.workshops.lab5.experiment; |
| 2 | + |
| 3 | +import java.net.URI; |
| 4 | +import java.time.LocalDate; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.AfterEach; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Nested; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest; |
| 12 | +import org.springframework.boot.webtestclient.autoconfigure.AutoConfigureWebTestClient; |
| 13 | +import org.springframework.context.annotation.Import; |
| 14 | +import org.springframework.http.MediaType; |
| 15 | +import org.springframework.test.context.ContextConfiguration; |
| 16 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 17 | +import pragmatech.digital.workshops.lab5.LocalDevTestcontainerConfig; |
| 18 | +import pragmatech.digital.workshops.lab5.config.OpenLibraryApiStub; |
| 19 | +import pragmatech.digital.workshops.lab5.config.WireMockContextInitializer; |
| 20 | +import pragmatech.digital.workshops.lab5.entity.Book; |
| 21 | +import pragmatech.digital.workshops.lab5.repository.BookRepository; |
| 22 | + |
| 23 | +/** |
| 24 | + * Integration tests for {@link pragmatech.digital.workshops.lab5.controller.BookController} |
| 25 | + * using a real embedded HTTP server (RANDOM_PORT). |
| 26 | + * |
| 27 | + * <p>Key observations about this setup: |
| 28 | + * <ul> |
| 29 | + * <li>A real Tomcat server starts on a random port — WebTestClient sends actual TCP requests</li> |
| 30 | + * <li>The test thread and server thread are DIFFERENT — {@code @Transactional} on this class |
| 31 | + * would NOT roll back server-side changes (different transaction boundary)</li> |
| 32 | + * <li>Data saved directly via {@code bookRepository} in {@code @BeforeEach} IS visible to |
| 33 | + * the server because it is auto-committed before the HTTP request is sent</li> |
| 34 | + * <li>Manual cleanup via {@code @AfterEach} is required to prevent data leaking between tests</li> |
| 35 | + * </ul> |
| 36 | + */ |
| 37 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 38 | +@Import(LocalDevTestcontainerConfig.class) |
| 39 | +@ContextConfiguration(initializers = WireMockContextInitializer.class) |
| 40 | +@AutoConfigureWebTestClient |
| 41 | +class BookControllerIT { |
| 42 | + |
| 43 | + private static final String VALID_ISBN = "978-0134757599"; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private WebTestClient webTestClient; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + private BookRepository bookRepository; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private OpenLibraryApiStub openLibraryApiStub; |
| 53 | + |
| 54 | + @AfterEach |
| 55 | + void cleanUp() { |
| 56 | + // The server thread commits data to the shared database. |
| 57 | + // We cannot rely on @Transactional rollback here — manual cleanup is required. |
| 58 | + bookRepository.deleteAll(); |
| 59 | + } |
| 60 | + |
| 61 | + @Nested |
| 62 | + class GetAllBooks { |
| 63 | + |
| 64 | + @BeforeEach |
| 65 | + void setUp() { |
| 66 | + // Data saved here is auto-committed BEFORE the HTTP request is sent. |
| 67 | + // The server thread CAN see this data because it is already in the DB. |
| 68 | + bookRepository.save(new Book("111-1234567890", "Clean Code", "Robert C. Martin", LocalDate.of(2008, 8, 1))); |
| 69 | + bookRepository.save(new Book("222-1234567890", "Effective Java", "Joshua Bloch", LocalDate.of(2018, 1, 6))); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void shouldReturnAllBooksWhenGettingAllBooks() { |
| 74 | + webTestClient.get() |
| 75 | + .uri("/api/books") |
| 76 | + .accept(MediaType.APPLICATION_JSON) |
| 77 | + .exchange() |
| 78 | + .expectStatus().isOk() |
| 79 | + .expectBody() |
| 80 | + .jsonPath("$").isArray() |
| 81 | + .jsonPath("$.length()").isEqualTo(2); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void shouldAllowUnauthenticatedAccessToBookListWhenGettingAllBooks() { |
| 86 | + webTestClient.get() |
| 87 | + .uri("/api/books") |
| 88 | + .exchange() |
| 89 | + .expectStatus().isOk(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Nested |
| 94 | + class GetBookById { |
| 95 | + |
| 96 | + @Test |
| 97 | + void shouldReturnBookWhenBookExistsAndUserIsAuthenticated() { |
| 98 | + Book savedBook = bookRepository.save( |
| 99 | + new Book("333-1234567890", "Domain-Driven Design", "Eric Evans", LocalDate.of(2003, 8, 30))); |
| 100 | + |
| 101 | + webTestClient.get() |
| 102 | + .uri("/api/books/{id}", savedBook.getId()) |
| 103 | + .accept(MediaType.APPLICATION_JSON) |
| 104 | + .headers(headers -> headers.setBasicAuth("user", "user")) |
| 105 | + .exchange() |
| 106 | + .expectStatus().isOk() |
| 107 | + .expectBody() |
| 108 | + .jsonPath("$.isbn").isEqualTo("333-1234567890") |
| 109 | + .jsonPath("$.title").isEqualTo("Domain-Driven Design") |
| 110 | + .jsonPath("$.status").isEqualTo("AVAILABLE"); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void shouldReturn401WhenGettingBookByIdWithoutAuthentication() { |
| 115 | + // GET /api/books/{id} requires ROLE_USER — no credentials → 401 |
| 116 | + webTestClient.get() |
| 117 | + .uri("/api/books/1") |
| 118 | + .exchange() |
| 119 | + .expectStatus().isUnauthorized(); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void shouldReturn404WhenBookDoesNotExist() { |
| 124 | + webTestClient.get() |
| 125 | + .uri("/api/books/99999") |
| 126 | + .headers(headers -> headers.setBasicAuth("user", "user")) |
| 127 | + .exchange() |
| 128 | + .expectStatus().isNotFound(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Nested |
| 133 | + class CreateBook { |
| 134 | + |
| 135 | + @Test |
| 136 | + void shouldCreateBookAndReturnLocationWhenRequestIsValid() { |
| 137 | + openLibraryApiStub.stubForSuccessfulBookResponse(VALID_ISBN); |
| 138 | + |
| 139 | + String requestBody = """ |
| 140 | + { |
| 141 | + "isbn": "%s", |
| 142 | + "title": "Effective Java", |
| 143 | + "author": "Joshua Bloch", |
| 144 | + "publishedDate": "2018-01-06" |
| 145 | + } |
| 146 | + """.formatted(VALID_ISBN); |
| 147 | + |
| 148 | + URI locationUri = webTestClient.post() |
| 149 | + .uri("/api/books") |
| 150 | + .contentType(MediaType.APPLICATION_JSON) |
| 151 | + .headers(headers -> headers.setBasicAuth("user", "user")) |
| 152 | + .bodyValue(requestBody) |
| 153 | + .exchange() |
| 154 | + .expectStatus().isCreated() |
| 155 | + .expectHeader().exists("Location") |
| 156 | + .returnResult(Void.class) |
| 157 | + .getResponseHeaders() |
| 158 | + .getLocation(); |
| 159 | + |
| 160 | + // The book was committed by the server thread — we can now fetch it |
| 161 | + webTestClient.get() |
| 162 | + .uri(locationUri.getPath()) |
| 163 | + .headers(headers -> headers.setBasicAuth("user", "user")) |
| 164 | + .exchange() |
| 165 | + .expectStatus().isOk() |
| 166 | + .expectBody() |
| 167 | + .jsonPath("$.isbn").isEqualTo(VALID_ISBN) |
| 168 | + .jsonPath("$.title").isEqualTo("Effective Java") |
| 169 | + .jsonPath("$.status").isEqualTo("AVAILABLE"); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + void shouldReturn400WhenIsbnFormatIsInvalid() { |
| 174 | + String requestBody = """ |
| 175 | + { |
| 176 | + "isbn": "not-a-valid-isbn", |
| 177 | + "title": "Some Book", |
| 178 | + "author": "Some Author", |
| 179 | + "publishedDate": "2020-01-01" |
| 180 | + } |
| 181 | + """; |
| 182 | + |
| 183 | + webTestClient.post() |
| 184 | + .uri("/api/books") |
| 185 | + .contentType(MediaType.APPLICATION_JSON) |
| 186 | + .headers(headers -> headers.setBasicAuth("user", "user")) |
| 187 | + .bodyValue(requestBody) |
| 188 | + .exchange() |
| 189 | + .expectStatus().isBadRequest(); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + void shouldReturn401WhenCreatingBookWithoutAuthentication() { |
| 194 | + String requestBody = """ |
| 195 | + { |
| 196 | + "isbn": "%s", |
| 197 | + "title": "Effective Java", |
| 198 | + "author": "Joshua Bloch", |
| 199 | + "publishedDate": "2018-01-06" |
| 200 | + } |
| 201 | + """.formatted(VALID_ISBN); |
| 202 | + |
| 203 | + webTestClient.post() |
| 204 | + .uri("/api/books") |
| 205 | + .contentType(MediaType.APPLICATION_JSON) |
| 206 | + .bodyValue(requestBody) |
| 207 | + .exchange() |
| 208 | + .expectStatus().isUnauthorized(); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + @Nested |
| 213 | + class DeleteBook { |
| 214 | + |
| 215 | + @Test |
| 216 | + void shouldDeleteBookWhenUserIsAdmin() { |
| 217 | + Book savedBook = bookRepository.save( |
| 218 | + new Book("444-1234567890", "Refactoring", "Martin Fowler", LocalDate.of(2018, 11, 20))); |
| 219 | + |
| 220 | + webTestClient.delete() |
| 221 | + .uri("/api/books/{id}", savedBook.getId()) |
| 222 | + .headers(headers -> headers.setBasicAuth("admin", "admin")) |
| 223 | + .exchange() |
| 224 | + .expectStatus().isNoContent(); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + void shouldReturn403WhenDeletingBookWithoutAdminRole() { |
| 229 | + Book savedBook = bookRepository.save( |
| 230 | + new Book("555-1234567890", "The Pragmatic Programmer", "David Thomas", LocalDate.of(2019, 9, 13))); |
| 231 | + |
| 232 | + webTestClient.delete() |
| 233 | + .uri("/api/books/{id}", savedBook.getId()) |
| 234 | + .headers(headers -> headers.setBasicAuth("user", "user")) |
| 235 | + .exchange() |
| 236 | + .expectStatus().isForbidden(); |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments