Getting a warning when use objectmapper in flux inappropriate blocking method call in java reactor - spring-webflux

i am new to reactor, i tried to create a flux from Iterable. then i want to convert my object into string by using object mapper. then the ide warns a message like this in this part of the code new ObjectMapper().writeValueAsString(event). the message Inappropriate blocking method call. there is no compile error. could you suggest a solution.
Flux.fromIterable(Arrays.asList(new Event(), new Event()))
.flatMap(event -> {
try {
return Mono.just(new ObjectMapper().writeValueAsString(event));
} catch (JsonProcessingException e) {
return Mono.error(e);
}
})
.subscribe(jsonStrin -> {
System.out.println("jsonStrin = " + jsonStrin);
});

I will give you an answer, but I don't pretty sure this is what you want. it seems like block the thread. so then you can't get the exact benefits of reactive if you block the thread. that's why the IDE warns you. you can create the mono with monoSink. like below.
AtomicReference<ObjectMapper> objectMapper = new AtomicReference<>(new ObjectMapper());
Flux.fromIterable(Arrays.asList(new Event(), new Event()))
.flatMap(event -> {
return Mono.create(monoSink -> {
try {
monoSink.success(objectMapper .writeValueAsString(event));
} catch (JsonProcessingException e) {
monoSink.error(e);
}
});
})
.cast(String.class) // this cast will help you to axact data type that you want to continue the pipeline
.subscribe(jsonString -> {
System.out.println("jsonString = " + jsonString);
});
please try out this method and check that error will be gone.
it doesn't matter if objectMapper is be a normal java object as you did. (if you don't change). it is not necessary for your case.

You need to do it like this:
Flux.fromIterable(Arrays.asList(new Event(), new Event()))
.flatMap(event -> {
try {
return Mono.just(new ObjectMapper().writeValueAsString(event));
} catch (JsonProcessingException e) {
return Mono.error(e);
}
})
.subscribe(jsonStrin -> {
System.out.println("jsonStrin = " + jsonStrin);
});

Related

How to extract value from MonoNext (and convert to byte[])

Map<String,Mono<byte[]>> map = new HashMap<>();
List<User> userList = new ArrayList<>();
map.entrySet().stream().forEach(entry -> {
if (entry.getValue() == null) {
log.info("Data not found for key {} ", entry.getKey());
} else {
entry.getValue().log().map(value -> {
try {
return User.parseFrom(value);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
return null;
}).log().subscribe(p -> userList.add(p));
}
here entry.getValue() => MonoNext
parseFrom(accepts byte[])
I am new to reactive programming world, How to resolve this MonoNext to values it actually have, tried using flatMap instead but that also didnot work
Any suggestion appreciated !! Thanks in advance !!
MonoNext (an internal Reactor implementation of Mono) emits the value asynchronously, which means that it might not have yet the value when evaluated in your code. The only way to retrieve the value is to subscribe to it (either manually or as part of a Reactor pipeline using flatMap and others) and wait until the Mono emits its item.
Here is what your code would look like if placed in a Reactor pipeline using flatMap:
Map<String, Mono<byte[]>> map = new HashMap<>();
List<User> userList = Flux.fromIterable(map.entrySet())
.filter(entry -> entry.getValue() != null)
.doOnDiscard(Map.Entry.class, entry -> log.info("Data not found for key {} ", entry.getKey()))
.flatMap(entry -> entry.getValue()
.log()
.map(User::parseFrom)
.onErrorResume(error -> Mono.fromRunnable(error::printStackTrace)))
.collectList()
.block();
Note that the block operator will wait until all items are retrieved. If you want to stay asynchronous, you can remove the block and return a Mono<List<User>>, or also remove the collectList to return a Flux<User>.

mockk every {}.throws() Exception fails test

I need to verify that a certain call is not made, when a previous method call throws an Exception.
// GIVEN
every { relaxedMock.eats() }.throws(NotHungryException())
// WHEN
sut.live()
// THEN
verify (exactly = 0) { relaxedMock2.sleeps() }
Problem with this code, it fails because of the Exception thrown and not because of the failed verification.
I understand that your WHEN block will always throw an exception.
In that case you have multiple options from my point of view:
Simple plain Kotlin. Wrap the WHEN block with a try-catch block, e.g. like this:
// GIVEN
every { relaxedMock.eats() }.throws(NotHungryException())
// WHEN
var exceptionThrown: Boolean = false
try {
sut.live()
} catch(exception: NotHungryException) {
// Maybe put some assertions on the exception here.
exceptionThrown = true
}
assertTrue(exceptionThrown)
// THEN
verify (exactly = 0) { relaxedMock2.sleeps() }
For a bit nicer code, you can use JUnit5 API's Assertions. assertThrows will expect an exception being thrown by a specific piece of code. It will fail the test, if no exception is thrown. Also it will return the thrown exception, for you to inspect it.
import org.junit.jupiter.api.Assertions
// GIVEN
every { relaxedMock.eats() }.throws(NotHungryException())
// WHEN
val exception = Assertions.assertThrows(NotHungryException::class.java) { sut.live() }
// THEN
verify (exactly = 0) { relaxedMock2.sleeps() }
If you're using Kotest you can use the shouldThrow assertion. Which also allows you to retrieve the thrown exception and validate its type.
import io.kotest.assertions.throwables.shouldThrow
// GIVEN
every { relaxedMock.eats() }.throws(NotHungryException())
// WHEN
val exception = shouldThrow<NotHungryException> { sut.live() }
// THEN
verify (exactly = 0) { relaxedMock2.sleeps() }
I had similar issue and found that my method is not surrounded by try catch. This mean the method will always throw exception.
Test
The unit test to verify the result when the following method is called while stubbing it with predefine Exception
#Test
fun returnSearchError() {
every { searchService.search(query) }.throws(BadSearchException())
val result = searchRepository.search(query)
assertEquals(SearchStates.SearchError, result)
}
Faulty code
fun search(query: String): SearchStates {
val result = searchService.search(query) // No try catch for the thrown exception
return try {
SearchStates.MatchingResult(result)
} catch (badSearchException: BadSearchException) {
SearchStates.SearchError
}
}
Refactored it to
fun search(query: String): SearchStates {
return try {
val result = searchService.search(query)
SearchStates.MatchingResult(result)
} catch (badSearchException: BadSearchException) {
SearchStates.SearchError
}
}

webflux Mono<T> onErrorReturn not called

this is my HandlerFunction
public Mono<ServerResponse> getTime(ServerRequest serverRequest) {
return time(serverRequest).onErrorReturn("some errors has happened !").flatMap(s -> {
// this didn't called
return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN).syncBody(s);
});
}
time(ServerRequest serverRequest) method is
private Mono<String> time(ServerRequest request) {
String format = DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now());
return Mono.just("time is:" + format + "," + request.queryParam("name").get());
}
when i don't using param "name",it will throw one NoSuchElementException;
But, the Mono onErrorReturn not working!
why or what do i wrong?
The onError... operators are meant to deal with error signals happening in the pipeline.
In your case, the NoSuchElementException is thrown outside of the reactive pipeline, before anything can subscribe to the returned Mono.
I think you might get the behavior you're looking for by deferring the execution like this:
private Mono<String> time(ServerRequest request) {
return Mono.defer(() -> {
String format = DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now());
Mono.just("time is:" + format + "," + request.queryParam("name").get());
});
}

Catch exception in Ktor-locations if non valid route parameter

I'm new in kotlin world. So I have some problem. I'm using ktor framework and try to use ktor-locations (https://ktor.io/servers/features/locations.html#route-classes)
And as example
#Location("/show/{id}")
data class Show(val id: Int)
routing {
get<Show> { show ->
call.respondText(show.id)
}
}
Everything is good, when I try to get /show/1
But if route will be /show/test there is NumberFormatException, cause DefaultConversionService try to convert id to Int and can't do it.
So my question is, how can I catch this exception and return Json with some error data. For example, if not using locations I can do smt like this
routing {
get("/{id}") {
val id = call.parameters["id"]!!.toIntOrNull()
call.respond(when (id) {
null -> JsonResponse.failure(HttpStatusCode.BadRequest.value, "wrong id parameter")
else -> JsonResponse.success(id)
})
}
}
Thx for help!
You can do a simple try-catch in order to catch the parsing exception which is thrown when a string can not be converted to an integer.
routing {
get("/{id}") {
val id = try {
call.parameters["id"]?.toInt()
} catch (e : NumberFormatException) {
null
}
call.respond(when (id) {
null -> HttpStatusCode.BadRequest
else -> "The value of the id is $id"
})
}
}
Other way of handling exception is to use StatusPages module:
install(StatusPages) {
// catch NumberFormatException and send back HTTP code 400
exception<NumberFormatException> { cause ->
call.respond(HttpStatusCode.BadRequest)
}
}
This should work with using Location feature. Please note that Location is experimental above ktor version 1.0.

Testing In A Try Catch With Moq Compared To Rhino Mocks

I've just been working on some tests using Moq but ran into trouble trying to test a method I wanted to call twice through a try catch block. The principle is that the first call throws an exception, then in the catch I correct the problem and call the method again.
I managed to do it with Rhino Mocks as below but being new to both frameworks I wondered if anyone could tell me if the same can be achieved using Moq.
// C.U.T
public class Mockee
{
bool theCatLives = true;
public Mockee() { }
public virtual void SetFalse()
{
theCatLives = false;
}
}
[Test]
public void TestTryCatch(){
var mr = new MockRepository();
var mock = mr.StrictMock<Mockee>();
mr.Record();
Expect.Call(mock.SetFalse).Throw(new Exception());
Expect.Call(mock.SetFalse);
mr.ReplayAll();
try
{
mock.SetFalse();
}
catch
{
mock.SetFalse();
}
mock.VerifyAllExpectations();
}
This isn't particularly easy to do with Moq, as it has no concept of ordered expectations. You can, however, use the Callback method and throw exceptions from there, like this:
var actions = new Queue<Action>(new Action[]
{
() => { throw new Exception(); },
() => { }
});
var mock = new Mock<Mockee>();
mock.Setup(m => m.SetFalse()).Callback(() => actions.Dequeue()()).Verifiable();
try
{
mock.Object.SetFalse();
}
catch
{
mock.Object.SetFalse();
}
mock.Verify();
However, one caveat is that this version only checks whether the SetFalse method was called at all.
If you want to verify that it was called twice, you can change the last statement to this:
mock.Verify(m => m.SetFalse(), Times.Exactly(2));
However, this slightly violates the DRY principle because you would be stating the same Setup twice, but you could get around that by first declaring and defining a variable of type Expression<Action<Mockee>> and use it for both the Setup and the Verify methods...