|
| 1 | +package com.baeldung.hoverfly; |
| 2 | + |
| 3 | +import static io.specto.hoverfly.junit.core.SimulationSource.dsl; |
| 4 | +import static io.specto.hoverfly.junit.dsl.HoverflyDsl.service; |
| 5 | +import static io.specto.hoverfly.junit.dsl.HttpBodyConverter.jsonWithSingleQuotes; |
| 6 | +import static io.specto.hoverfly.junit.dsl.ResponseCreators.success; |
| 7 | +import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.any; |
| 8 | +import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.equalsTo; |
| 9 | +import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.equalsToJson; |
| 10 | +import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.equalsToXml; |
| 11 | +import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.matches; |
| 12 | +import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.startsWith; |
| 13 | +import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.matchesJsonPath; |
| 14 | +import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.matchesXPath; |
| 15 | + |
| 16 | +import static org.junit.Assert.assertEquals; |
| 17 | +import static org.junit.Assert.assertTrue; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.net.URISyntaxException; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | + |
| 23 | +import org.apache.commons.lang3.time.StopWatch; |
| 24 | +import org.junit.ClassRule; |
| 25 | +import org.junit.Test; |
| 26 | +import org.springframework.http.HttpStatus; |
| 27 | +import org.springframework.http.RequestEntity; |
| 28 | +import org.springframework.http.ResponseEntity; |
| 29 | +import org.springframework.web.client.RestTemplate; |
| 30 | + |
| 31 | +import io.specto.hoverfly.junit.core.SimulationSource; |
| 32 | +import io.specto.hoverfly.junit.rule.HoverflyRule; |
| 33 | + |
| 34 | +public class HoverflyApiTest { |
| 35 | + |
| 36 | + private static final SimulationSource source = dsl( |
| 37 | + service("http://www.baeldung.com") |
| 38 | + .get("/api/courses/1") |
| 39 | + .willReturn(success().body( |
| 40 | + jsonWithSingleQuotes("{'id':'1','name':'HCI'}"))) |
| 41 | + |
| 42 | + .post("/api/courses") |
| 43 | + .willReturn(success()) |
| 44 | + |
| 45 | + .andDelay(3, TimeUnit.SECONDS) |
| 46 | + .forMethod("POST"), |
| 47 | + |
| 48 | + service(matches("www.*dung.com")) |
| 49 | + .get(startsWith("/api/student")) |
| 50 | + .queryParam("page", any()) |
| 51 | + .willReturn(success()) |
| 52 | + |
| 53 | + .post(equalsTo("/api/student")) |
| 54 | + .body(equalsToJson(jsonWithSingleQuotes("{'id':'1','name':'Joe'}"))) |
| 55 | + .willReturn(success()) |
| 56 | + |
| 57 | + .put("/api/student/1") |
| 58 | + .body(matchesJsonPath("$.name")) |
| 59 | + .willReturn(success()) |
| 60 | + |
| 61 | + .post("/api/student") |
| 62 | + .body(equalsToXml("<student><id>2</id><name>John</name></student>")) |
| 63 | + .willReturn(success()) |
| 64 | + |
| 65 | + .put("/api/student/2") |
| 66 | + .body(matchesXPath("/student/name")) |
| 67 | + .willReturn(success())); |
| 68 | + |
| 69 | + @ClassRule |
| 70 | + public static final HoverflyRule rule = HoverflyRule.inSimulationMode(source); |
| 71 | + private final RestTemplate restTemplate = new RestTemplate(); |
| 72 | + |
| 73 | + @Test |
| 74 | + public void givenGetCourseById_whenRequestSimulated_thenAPICalledSuccessfully() throws URISyntaxException { |
| 75 | + final ResponseEntity<String> courseResponse = restTemplate.getForEntity( |
| 76 | + "http://www.baeldung.com/api/courses/1", String.class); |
| 77 | + |
| 78 | + assertEquals(HttpStatus.OK, courseResponse.getStatusCode()); |
| 79 | + assertEquals("{\"id\":\"1\",\"name\":\"HCI\"}", courseResponse.getBody()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void givenPostCourse_whenDelayInRequest_thenResponseIsDelayed() throws URISyntaxException { |
| 84 | + StopWatch stopWatch = new StopWatch(); |
| 85 | + stopWatch.start(); |
| 86 | + final ResponseEntity<Void> postResponse = restTemplate.postForEntity( |
| 87 | + "http://www.baeldung.com/api/courses", null, Void.class); |
| 88 | + stopWatch.stop(); |
| 89 | + long postTime = stopWatch.getTime(); |
| 90 | + |
| 91 | + assertEquals(HttpStatus.OK, postResponse.getStatusCode()); |
| 92 | + assertTrue(3L <= TimeUnit.MILLISECONDS.toSeconds(postTime)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void givenGetStudent_whenRequestMatcher_thenAPICalledSuccessfully() throws URISyntaxException { |
| 97 | + final ResponseEntity<Void> courseResponse = restTemplate.getForEntity( |
| 98 | + "http://www.baeldung.com/api/student?page=3", Void.class); |
| 99 | + |
| 100 | + assertEquals(HttpStatus.OK, courseResponse.getStatusCode()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void givenPostStudent_whenBodyRequestMatcherJson_thenResponseContainsEqualJson() throws URISyntaxException { |
| 105 | + final ResponseEntity<Void> postResponse = restTemplate.postForEntity( |
| 106 | + "http://www.baeldung.com/api/student", "{\"id\":\"1\",\"name\":\"Joe\"}", Void.class); |
| 107 | + |
| 108 | + assertEquals(HttpStatus.OK, postResponse.getStatusCode()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void givenPutStudent_whenJsonPathMatcher_thenRequestJsonContainsElementInPath() throws URISyntaxException { |
| 113 | + RequestEntity<String> putRequest = RequestEntity |
| 114 | + .put(new URI("http://www.baeldung.com/api/student/1")) |
| 115 | + .body("{\"id\":\"1\",\"name\":\"Trevor\"}"); |
| 116 | + |
| 117 | + ResponseEntity<String> putResponse = restTemplate.exchange(putRequest, String.class); |
| 118 | + assertEquals(HttpStatus.OK, putResponse.getStatusCode()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void givenPostStudent_whenBodyRequestMatcherXml_thenResponseContainsEqualXml() throws URISyntaxException { |
| 123 | + final ResponseEntity<Void> postResponse = restTemplate.postForEntity( |
| 124 | + "http://www.baeldung.com/api/student", "<student><id>2</id><name>John</name></student>", Void.class); |
| 125 | + |
| 126 | + assertEquals(HttpStatus.OK, postResponse.getStatusCode()); |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + @Test |
| 131 | + public void givenPutStudent_whenXPathMatcher_thenRequestXmlContainsElementInXPath() throws URISyntaxException { |
| 132 | + RequestEntity<String> putRequest = RequestEntity |
| 133 | + .put(new URI("http://www.baeldung.com/api/student/2")) |
| 134 | + .body("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" |
| 135 | + + "<student><id>2</id><name>Monica</name></student>"); |
| 136 | + |
| 137 | + ResponseEntity<String> putResponse = restTemplate.exchange(putRequest, String.class); |
| 138 | + assertEquals(HttpStatus.OK, putResponse.getStatusCode()); |
| 139 | + } |
| 140 | +} |
0 commit comments