Commit 622395b4 authored by Syed Javed Ali's avatar Syed Javed Ali

Added test cases for Mono

parent 39e00913
...@@ -4,6 +4,7 @@ import org.springframework.http.MediaType; ...@@ -4,6 +4,7 @@ import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration; import java.time.Duration;
...@@ -12,31 +13,46 @@ public class FluxAndMonoController { ...@@ -12,31 +13,46 @@ public class FluxAndMonoController {
//This will return all data at a time not event by event //This will return all data at a time not event by event
// (internally following events but not visible on postman) // (internally following events but not visible on postman)
/*@GetMapping("/flux") @GetMapping("/flux")
public Flux<Integer> getFlux(){ public Flux<Integer> getFlux(){
return Flux.just(1,2,3,4) return Flux.just(1,2,3,4)
.log(); .log();
}*/ }
//In this case every event is emitted after one sec //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 // but in postman it gives all data after 4 sec becoz it assume it is JSON response
@GetMapping("/flux") /*@GetMapping("/flux")
public Flux<Integer> getFlux(){ public Flux<Integer> getFlux(){
return Flux.just(1,2,3,4) return Flux.just(1,2,3,4)
.delayElements(Duration.ofSeconds(1)) .delayElements(Duration.ofSeconds(1))
.log(); .log();
} }*/
//we can get events after 1 sec //we can get events after 1 sec
@GetMapping(value = "/fluxstream" /*@GetMapping(value = "/fluxstream"
,produces = MediaType.APPLICATION_NDJSON_VALUE) ,produces = MediaType.APPLICATION_NDJSON_VALUE)
public Flux<Integer> getFluxStream(){ public Flux<Integer> getFluxStream(){
return Flux.just(1,2,3,4) return Flux.just(1,2,3,4)
.delayElements(Duration.ofSeconds(1)) .delayElements(Duration.ofSeconds(1))
.log(); .log();
}*/
@GetMapping(value = "/fluxstream"
,produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<Long> getFluxStream(){
return Flux.interval(Duration.ofSeconds(1))
.log();
}
@GetMapping("/mono")
public Mono<Integer> getMono(){
return Mono.just(1)
.log();
} }
} }
...@@ -88,4 +88,38 @@ public class FluxAndMonoControllerTest { ...@@ -88,4 +88,38 @@ public class FluxAndMonoControllerTest {
(expectedIntegerList, response.getResponseBody()); (expectedIntegerList, response.getResponseBody());
}); });
} }
//for checking infinite elements emitted from flux
@Test
public void fluxStream(){
Flux<Long> longFlux=webTestClient.get().uri("/fluxstream")
.accept(MediaType.APPLICATION_STREAM_JSON)
.exchange()
.expectStatus().isOk()
.returnResult(Long.class)
.getResponseBody();
StepVerifier.create(longFlux)
.expectNext(0l)
.expectNext(1l)
.expectNext(2l)
.thenCancel()
.verify();
}
//to check for mono
@Test
public void mono(){
Integer expectedValue=new Integer(1);
webTestClient.get().uri("/mono")
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.expectStatus().isOk()
.expectBody(Integer.class)
.consumeWith(response->{
assertEquals(expectedValue,response.getResponseBody());
});
}
} }
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