Commit 39e00913 authored by Syed Javed Ali's avatar Syed Javed Ali

Added FluxAndMonoController.java and FluxAndMonoControllerTest.java classes...

Added FluxAndMonoController.java and FluxAndMonoControllerTest.java classes for testing get end points
parent 917326c5
......@@ -21,7 +21,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.oracle.database.jdbc:ojdbc8'
//runtimeOnly 'com.oracle.database.jdbc:ojdbc8'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
......
package com.nisum.app.controller;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.time.Duration;
@RestController
public class FluxAndMonoController {
//This will return all data at a time not event by event
// (internally following events but not visible on postman)
/*@GetMapping("/flux")
public Flux<Integer> getFlux(){
return Flux.just(1,2,3,4)
.log();
}*/
//In this case every event is emitted after one sec
// but in postman it gives all data after 4 sec becoz it assume it is JSON response
@GetMapping("/flux")
public Flux<Integer> getFlux(){
return Flux.just(1,2,3,4)
.delayElements(Duration.ofSeconds(1))
.log();
}
//we can get events after 1 sec
@GetMapping(value = "/fluxstream"
,produces = MediaType.APPLICATION_NDJSON_VALUE)
public Flux<Integer> getFluxStream(){
return Flux.just(1,2,3,4)
.delayElements(Duration.ofSeconds(1))
.log();
}
}
package com.nisum.app.controller;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.EntityExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(SpringExtension.class)
@WebFluxTest
public class FluxAndMonoControllerTest {
@Autowired
WebTestClient webTestClient; //alternative to TestRestTemplate
//in imperative programming
//to check all events completed
@Test
public void flux_approach1(){
Flux<Integer> integerFlux=webTestClient.get().uri("/flux")
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.expectStatus().isOk()
.returnResult(Integer.class)
.getResponseBody();
StepVerifier.create(integerFlux)
.expectSubscription()
.expectNext(1)
.expectNext(2)
.expectNext(3)
.expectNext(4)
.verifyComplete();
}
//to check number of elements emitted from flux
@Test
public void flux_approach2(){
webTestClient.get().uri("/flux")
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
.expectBodyList(Integer.class)
.hasSize(4);
}
//getting result as value
@Test
public void flux_approach3(){
List<Integer> expectedIntegerList= Arrays.asList(1,2,3,4);
EntityExchangeResult<List<Integer>> entityExchangeResult= webTestClient.get().uri("/flux")
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.expectStatus().isOk()
.expectBodyList(Integer.class)
.returnResult();
assertEquals(expectedIntegerList,
entityExchangeResult.getResponseBody());
}
@Test
public void flux_approach4(){
List<Integer> expectedIntegerList= Arrays.asList(1,2,3,4);
webTestClient.get().uri("/flux")
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.expectStatus().isOk()
.expectBodyList(Integer.class)
.consumeWith(response->{
assertEquals
(expectedIntegerList, response.getResponseBody());
});
}
}
package com.nisum.app.fluxandmono;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
public class FluxAndMonoUnitTest {
......@@ -71,7 +71,7 @@ public class FluxAndMonoUnitTest {
}*/
// we can check by passing all value as one in expectNext()
@Test
/*@Test
public void testFluxElementsCount_WithError(){
Flux<String> stringFlux=Flux.just("Spring","SpringBoot","Reactive Spring")
......@@ -83,5 +83,25 @@ public class FluxAndMonoUnitTest {
.expectErrorMessage("Error Occured")
.verify();
}*/
// 1. success case, to verify data emit by mono
/*@Test
public void monoTest(){
Mono<String> stringMono= Mono.just("Spring");
StepVerifier.create(stringMono.log())
.expectNext("Spring")
.verifyComplete();
}*/
// 1. Error case, to verify data emit by mono
@Test
public void monoTest_Error(){
//Mono emits only one data so creating error directly
StepVerifier.create(Mono.error(new RuntimeException("Error occured")).log())
.expectError()
.verify();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment