package com.juan.medina.microservices;

import com.juan.medina.microservices.model.HelloRequest;
import com.juan.medina.microservices.model.HelloResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.FluxExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RouterFunction;
import reactor.test.StepVerifier;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ReactiveMsExampleApplicationTests {

    private static final String DEFAULT_VALUE = "world";
    private static final String CUSTOM_VALUE = "reactive";
    private static final String HELLO_PATH = "/hello";
    private static final String NAME_ARG = "{name}";

    private WebTestClient client;

    @Autowired
    private RouterFunction<?> helloRouterFunction;

    @Before
    public void setup() {
        client = WebTestClient.bindToRouterFunction(helloRouterFunction).build();
    }

    @Test
    public void defaultHelloTest() {

        FluxExchangeResult<HelloResponse> result = client.get()
                .uri(HELLO_PATH)
                .accept(APPLICATION_JSON_UTF8)
                .exchange()
                .expectStatus().isOk()
                .expectHeader().contentType(APPLICATION_JSON_UTF8)
                .expectBody(HelloResponse.class)
                .returnResult();

        StepVerifier.create(result.getResponseBody())
                .consumeNextWith(it ->
                        assertThat(it.getHello(), is(DEFAULT_VALUE)
                        ))
                .thenCancel()
                .verify();
    }

    @Test
    public void getHelloTest() {

        FluxExchangeResult<HelloResponse> result = client.get()
                .uri(builder -> builder.path(HELLO_PATH)
                .path("/").path(NAME_ARG).build(CUSTOM_VALUE))
                .accept(APPLICATION_JSON_UTF8)
                .exchange()
                .expectStatus().isOk()
                .expectHeader().contentType(APPLICATION_JSON_UTF8)
                .expectBody(HelloResponse.class)
                .returnResult();

        StepVerifier.create(result.getResponseBody())
                .consumeNextWith(it ->
                        assertThat(it.getHello(), is(CUSTOM_VALUE)
                        ))
                .thenCancel()
                .verify();
    }

    @Test
    public void postHelloTest() {

        FluxExchangeResult<HelloResponse> result = client.post()
                .uri(HELLO_PATH)
                .accept(APPLICATION_JSON_UTF8)
                .exchange(BodyInserters.fromObject(new HelloRequest(CUSTOM_VALUE)))
                .expectStatus().isOk()
                .expectHeader().contentType(APPLICATION_JSON_UTF8)
                .expectBody(HelloResponse.class)
                .returnResult();

        StepVerifier.create(result.getResponseBody())
                .consumeNextWith(it ->
                        assertThat(it.getHello(), is(CUSTOM_VALUE)
                        ))
                .thenCancel()
                .verify();

    }

}

@@ -2038,34 +2038,38 @@
            .
-thenCancel
+expectComplete
 ()%0A         
@@ -2785,34 +2785,38 @@
            .
-thenCancel
+expectComplete
 ()%0A         
@@ -3511,18 +3511,22 @@
    .
-thenCancel
+expectComplete
 ()%0A 

change cancel into expectComplete