|
| 1 | +package org.baeldung.web; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import org.baeldung.persistence.model.MyUser; |
| 6 | +import org.baeldung.spring.Application; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.springframework.boot.test.SpringApplicationConfiguration; |
| 11 | +import org.springframework.http.MediaType; |
| 12 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 13 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 14 | + |
| 15 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 16 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 17 | +import com.jayway.restassured.RestAssured; |
| 18 | +import com.jayway.restassured.response.Response; |
| 19 | +import com.jayway.restassured.specification.RequestSpecification; |
| 20 | + |
| 21 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 22 | +@SpringApplicationConfiguration(classes = Application.class) |
| 23 | +@WebAppConfiguration |
| 24 | +public class MyUserLiveTest { |
| 25 | + |
| 26 | + private ObjectMapper mapper = new ObjectMapper(); |
| 27 | + private MyUser userJohn = new MyUser("john", "doe", "john@test.com", 11); |
| 28 | + private MyUser userTom = new MyUser("tom", "doe", "tom@test.com", 20); |
| 29 | + |
| 30 | + private static boolean setupDataCreated = false; |
| 31 | + |
| 32 | + @Before |
| 33 | + public void setupData() throws JsonProcessingException { |
| 34 | + if (!setupDataCreated) { |
| 35 | + withRequestBody(givenAuth(), userJohn).post("http://localhost:8080/myusers"); |
| 36 | + withRequestBody(givenAuth(), userTom).post("http://localhost:8080/myusers"); |
| 37 | + setupDataCreated = true; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void whenGettingListOfUsers_thenCorrect() { |
| 43 | + final Response response = givenAuth().get("http://localhost:8080/api/myusers"); |
| 44 | + final MyUser[] result = response.as(MyUser[].class); |
| 45 | + assertEquals(result.length, 2); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void givenFirstName_whenGettingListOfUsers_thenCorrect() { |
| 50 | + final Response response = givenAuth().get("http://localhost:8080/api/myusers?firstName=john"); |
| 51 | + final MyUser[] result = response.as(MyUser[].class); |
| 52 | + assertEquals(result.length, 1); |
| 53 | + assertEquals(result[0].getEmail(), userJohn.getEmail()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void givenPartialLastName_whenGettingListOfUsers_thenCorrect() { |
| 58 | + final Response response = givenAuth().get("http://localhost:8080/api/myusers?lastName=do"); |
| 59 | + final MyUser[] result = response.as(MyUser[].class); |
| 60 | + assertEquals(result.length, 2); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void givenEmail_whenGettingListOfUsers_thenIgnored() { |
| 65 | + final Response response = givenAuth().get("http://localhost:8080/api/myusers?email=john"); |
| 66 | + final MyUser[] result = response.as(MyUser[].class); |
| 67 | + assertEquals(result.length, 2); |
| 68 | + } |
| 69 | + |
| 70 | + private RequestSpecification givenAuth() { |
| 71 | + return RestAssured.given().auth().preemptive().basic("user1", "user1Pass"); |
| 72 | + } |
| 73 | + |
| 74 | + private RequestSpecification withRequestBody(final RequestSpecification req, final Object obj) throws JsonProcessingException { |
| 75 | + return req.contentType(MediaType.APPLICATION_JSON_VALUE).body(mapper.writeValueAsString(obj)); |
| 76 | + } |
| 77 | +} |
0 commit comments