StudentIntegrationTest.java
3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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);
}
}