StudentIntegrationTest.java 3.07 KB
package com.krunal.reactive.it;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.krunal.reactive.ReactiveProgrammingApplication;
import com.krunal.reactive.model.Student;
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.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ReactiveProgrammingApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class StudentIntegrationTest {

    private static final Student STUDENT = new Student(31L, "Praful", "12");

    private static final Mono<Student> MONO_STUDENT = Mono.just(STUDENT);

    private String URI_FOR_OPERATION = null;

    @LocalServerPort
    private int port;

    @Autowired
    private WebClient.Builder webClientBuilder;

    @Bean
    public WebClient.Builder getWebClientBuilder() {
        return WebClient.builder();
    }

    @Autowired
    private TestRestTemplate testRestTemplate;

    private HttpHeaders header = null;

    ResponseEntity<String> responseEntity = null;


    @Test
    public void saveStudent() throws JsonProcessingException {
        URI_FOR_OPERATION = "/student/save";
        webClientBuilder.build().post()
                .uri(fullUrlWithPort(URI_FOR_OPERATION))
                .contentType(MediaType.APPLICATION_JSON)
                .body(Mono.just(Student.class), Student.class)
                .retrieve()
                .bodyToMono(Student.class);
        //assertEquals(STUDENT, studentMono);

        /*String inputJson = converToJson(STUDENT);
        HttpEntity<Student> userEntity = new HttpEntity<Student>(STUDENT, header);
        responseEntity = testRestTemplate.exchange(fullUrlWithPort(URI_FOR_OPERATION)
                , HttpMethod.POST, userEntity, String.class);
        String responseToJson = responseEntity.getBody();
        assertEquals(inputJson, responseToJson);*/
        /*webTestClient.post().uri(URI_FOR_OPERATION)
                .body(Mono.just(STUDENT),Student.class)
                .exchange()
                .expectBody(Mono.class)
                .returnResult().getResponseBody();*/


    }

    private String fullUrlWithPort(String uri) {
        return "http://localhost:" + port + uri;
    }

    private String converToJson(Object object) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(object);
    }
}