|
2 | 2 |
|
3 | 3 | import static org.hamcrest.CoreMatchers.allOf; |
4 | 4 | import static org.hamcrest.CoreMatchers.containsString; |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | +import static org.junit.Assert.assertThat; |
5 | 7 |
|
| 8 | +import javax.ws.rs.client.Entity; |
6 | 9 | import javax.ws.rs.core.Application; |
| 10 | +import javax.ws.rs.core.Form; |
| 11 | +import javax.ws.rs.core.MediaType; |
| 12 | +import javax.ws.rs.core.Response; |
7 | 13 |
|
8 | 14 | import org.glassfish.jersey.test.JerseyTest; |
9 | | -import org.junit.Assert; |
| 15 | +import org.glassfish.jersey.test.TestProperties; |
10 | 16 | import org.junit.Test; |
11 | 17 |
|
12 | 18 | import com.baeldung.jersey.server.config.ViewApplicationConfig; |
| 19 | +import com.baeldung.jersey.server.model.Fruit; |
| 20 | +import com.baeldung.jersey.server.providers.FruitExceptionMapper; |
13 | 21 |
|
14 | 22 | public class FruitResourceIntegrationTest extends JerseyTest { |
15 | 23 |
|
16 | 24 | @Override |
17 | 25 | protected Application configure() { |
18 | | - return new ViewApplicationConfig(); |
| 26 | + enable(TestProperties.LOG_TRAFFIC); |
| 27 | + enable(TestProperties.DUMP_ENTITY); |
| 28 | + |
| 29 | + ViewApplicationConfig config = new ViewApplicationConfig(); |
| 30 | + config.register(FruitExceptionMapper.class); |
| 31 | + return config; |
19 | 32 | } |
20 | 33 |
|
21 | 34 | @Test |
22 | | - public void testAllFruit() { |
| 35 | + public void givenGetAllFruit_whenCorrectRequest_thenAllTemplateInvoked() { |
23 | 36 | final String response = target("/fruit/all").request() |
24 | 37 | .get(String.class); |
25 | | - Assert.assertThat(response, allOf(containsString("banana"), containsString("apple"), containsString("kiwi"))); |
| 38 | + assertThat(response, allOf(containsString("banana"), containsString("apple"), containsString("kiwi"))); |
26 | 39 | } |
27 | 40 |
|
28 | 41 | @Test |
29 | | - public void testIndex() { |
| 42 | + public void givenGetFruit_whenCorrectRequest_thenIndexTemplateInvoked() { |
30 | 43 | final String response = target("/fruit").request() |
31 | 44 | .get(String.class); |
32 | | - Assert.assertThat(response, containsString("Welcome Fruit Index Page!")); |
| 45 | + assertThat(response, containsString("Welcome Fruit Index Page!")); |
33 | 46 | } |
34 | 47 |
|
35 | 48 | @Test |
36 | | - public void testErrorTemplate() { |
| 49 | + public void givenGetFruitByName_whenFruitUnknown_thenErrorTemplateInvoked() { |
37 | 50 | final String response = target("/fruit/orange").request() |
38 | 51 | .get(String.class); |
39 | | - Assert.assertThat(response, containsString("Error - Fruit not found: orange!")); |
| 52 | + assertThat(response, containsString("Error - Fruit not found: orange!")); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void givenCreateFruit_whenFormContainsNullParam_thenResponseCodeIsBadRequest() { |
| 57 | + Form form = new Form(); |
| 58 | + form.param("name", "apple"); |
| 59 | + form.param("colour", null); |
| 60 | + Response response = target("fruit/create").request(MediaType.APPLICATION_FORM_URLENCODED) |
| 61 | + .post(Entity.form(form)); |
| 62 | + |
| 63 | + assertEquals("Http Response should be 400 ", 400, response.getStatus()); |
| 64 | + assertThat(response.readEntity(String.class), containsString("Fruit colour must not be null")); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void givenUpdateFruit_whenFormContainsBadSerialParam_thenResponseCodeIsBadRequest() { |
| 69 | + Form form = new Form(); |
| 70 | + form.param("serial", "2345-2345"); |
| 71 | + |
| 72 | + Response response = target("fruit/update").request(MediaType.APPLICATION_FORM_URLENCODED) |
| 73 | + .put(Entity.form(form)); |
| 74 | + |
| 75 | + assertEquals("Http Response should be 400 ", 400, response.getStatus()); |
| 76 | + assertThat(response.readEntity(String.class), containsString("Fruit serial number is not valid")); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void givenCreateFruit_whenFruitIsInvalid_thenResponseCodeIsBadRequest() { |
| 81 | + Fruit fruit = new Fruit("Blueberry", "purple"); |
| 82 | + fruit.setWeight(1); |
| 83 | + |
| 84 | + Response response = target("fruit/create").request(MediaType.APPLICATION_JSON_TYPE) |
| 85 | + .post(Entity.entity(fruit, MediaType.APPLICATION_JSON_TYPE)); |
| 86 | + |
| 87 | + assertEquals("Http Response should be 400 ", 400, response.getStatus()); |
| 88 | + assertThat(response.readEntity(String.class), containsString("Fruit weight must be 10 or greater")); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void givenFruitExists_whenSearching_thenResponseContainsFruit() { |
| 93 | + Fruit fruit = new Fruit(); |
| 94 | + fruit.setName("strawberry"); |
| 95 | + fruit.setWeight(20); |
| 96 | + Response response = target("fruit/create").request(MediaType.APPLICATION_JSON_TYPE) |
| 97 | + .post(Entity.entity(fruit, MediaType.APPLICATION_JSON_TYPE)); |
| 98 | + |
| 99 | + assertEquals("Http Response should be 204 ", 204, response.getStatus()); |
| 100 | + |
| 101 | + final String json = target("fruit/search/strawberry").request() |
| 102 | + .get(String.class); |
| 103 | + assertThat(json, containsString("{\"name\":\"strawberry\",\"weight\":20}")); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void givenFruit_whenFruitIsInvalid_thenReponseContainsCustomExceptions() { |
| 108 | + final Response response = target("fruit/exception").request() |
| 109 | + .get(); |
| 110 | + |
| 111 | + assertEquals("Http Response should be 400 ", 400, response.getStatus()); |
| 112 | + String responseString = response.readEntity(String.class); |
| 113 | + assertThat(responseString, containsString("exception.<return value>.colour size must be between 5 and 200")); |
| 114 | + assertThat(responseString, containsString("exception.<return value>.name size must be between 5 and 200")); |
40 | 115 | } |
41 | 116 |
|
42 | 117 | } |
0 commit comments