|
| 1 | +package com.retailsvc.http; |
| 2 | + |
| 3 | +import static java.net.HttpURLConnection.HTTP_ACCEPTED; |
| 4 | +import static java.net.HttpURLConnection.HTTP_CREATED; |
| 5 | +import static java.net.HttpURLConnection.HTTP_NOT_FOUND; |
| 6 | +import static java.net.HttpURLConnection.HTTP_NOT_IMPLEMENTED; |
| 7 | +import static org.assertj.core.api.Assertions.assertThat; |
| 8 | + |
| 9 | +import java.util.Map; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +class ResponseTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + void acceptedNoBody() { |
| 16 | + Response r = Response.accepted(); |
| 17 | + |
| 18 | + assertThat(r.status()).isEqualTo(HTTP_ACCEPTED); |
| 19 | + assertThat(r.body()).isNull(); |
| 20 | + assertThat(r.headers()).isEmpty(); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + void acceptedWithBody() { |
| 25 | + Map<String, String> job = Map.of("id", "job-42"); |
| 26 | + Response r = Response.accepted(job); |
| 27 | + |
| 28 | + assertThat(r.status()).isEqualTo(HTTP_ACCEPTED); |
| 29 | + assertThat(r.body()).isEqualTo(job); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void createdWithBody() { |
| 34 | + Map<String, String> resource = Map.of("id", "x-1"); |
| 35 | + Response r = Response.created(resource); |
| 36 | + |
| 37 | + assertThat(r.status()).isEqualTo(HTTP_CREATED); |
| 38 | + assertThat(r.body()).isEqualTo(resource); |
| 39 | + assertThat(r.headers()).isEmpty(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void createdWithLocation() { |
| 44 | + Response r = Response.created(Map.of("id", "x-1"), "/things/x-1"); |
| 45 | + |
| 46 | + assertThat(r.status()).isEqualTo(HTTP_CREATED); |
| 47 | + assertThat(r.headers()).containsEntry("Location", "/things/x-1"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void notFoundNoBody() { |
| 52 | + Response r = Response.notFound(); |
| 53 | + |
| 54 | + assertThat(r.status()).isEqualTo(HTTP_NOT_FOUND); |
| 55 | + assertThat(r.body()).isNull(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void notFoundWithBody() { |
| 60 | + Map<String, String> problem = Map.of("title", "Missing"); |
| 61 | + Response r = Response.notFound(problem); |
| 62 | + |
| 63 | + assertThat(r.status()).isEqualTo(HTTP_NOT_FOUND); |
| 64 | + assertThat(r.body()).isEqualTo(problem); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void notImplementedNoBody() { |
| 69 | + Response r = Response.notImplemented(); |
| 70 | + |
| 71 | + assertThat(r.status()).isEqualTo(HTTP_NOT_IMPLEMENTED); |
| 72 | + assertThat(r.body()).isNull(); |
| 73 | + } |
| 74 | +} |
0 commit comments