|
| 1 | +package com.baeldung.rest.wiremock.scenario; |
| 2 | + |
| 3 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 4 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 5 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 6 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 7 | +import static org.junit.Assert.assertEquals; |
| 8 | + |
| 9 | +import java.io.BufferedReader; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStreamReader; |
| 12 | + |
| 13 | +import org.apache.http.HttpResponse; |
| 14 | +import org.apache.http.client.ClientProtocolException; |
| 15 | +import org.apache.http.client.methods.HttpGet; |
| 16 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 17 | +import org.apache.http.impl.client.HttpClients; |
| 18 | +import org.junit.Rule; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 22 | +import com.github.tomakehurst.wiremock.stubbing.Scenario; |
| 23 | + |
| 24 | +public class WireMockScenarioExampleIntegrationTest { |
| 25 | + |
| 26 | + private static final String THIRD_STATE = "third"; |
| 27 | + private static final String SECOND_STATE = "second"; |
| 28 | + private static final String TIP_01 = "finally block is not called when System.exit() is called in the try block"; |
| 29 | + private static final String TIP_02 = "keep your code clean"; |
| 30 | + private static final String TIP_03 = "use composition rather than inheritance"; |
| 31 | + private static final String TEXT_PLAIN = "text/plain"; |
| 32 | + |
| 33 | + static int port = 9999; |
| 34 | + |
| 35 | + @Rule |
| 36 | + public WireMockRule wireMockRule = new WireMockRule(port); |
| 37 | + |
| 38 | + @Test |
| 39 | + public void changeStateOnEachCallTest() throws IOException { |
| 40 | + createWireMockStub(Scenario.STARTED, SECOND_STATE, TIP_01); |
| 41 | + createWireMockStub(SECOND_STATE, THIRD_STATE, TIP_02); |
| 42 | + createWireMockStub(THIRD_STATE, Scenario.STARTED, TIP_03); |
| 43 | + |
| 44 | + assertEquals(TIP_01, nextTip()); |
| 45 | + assertEquals(TIP_02, nextTip()); |
| 46 | + assertEquals(TIP_03, nextTip()); |
| 47 | + assertEquals(TIP_01, nextTip()); |
| 48 | + } |
| 49 | + |
| 50 | + private void createWireMockStub(String currentState, String nextState, String responseBody) { |
| 51 | + stubFor(get(urlEqualTo("/java-tip")) |
| 52 | + .inScenario("java tips") |
| 53 | + .whenScenarioStateIs(currentState) |
| 54 | + .willReturn(aResponse() |
| 55 | + .withStatus(200) |
| 56 | + .withHeader("Content-Type", TEXT_PLAIN) |
| 57 | + .withBody(responseBody)) |
| 58 | + .willSetStateTo(nextState) |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + private String nextTip() throws ClientProtocolException, IOException { |
| 63 | + CloseableHttpClient httpClient = HttpClients.createDefault(); |
| 64 | + HttpGet request = new HttpGet(String.format("http://localhost:%s/java-tip", port)); |
| 65 | + HttpResponse httpResponse = httpClient.execute(request); |
| 66 | + return firstLineOfResponse(httpResponse); |
| 67 | + } |
| 68 | + |
| 69 | + private static String firstLineOfResponse(HttpResponse httpResponse) throws IOException { |
| 70 | + try (BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()))) { |
| 71 | + return reader.readLine(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments