Mockito Test for Elastic Java api client - java

I have tried to mock the response for the below method, but it's not working. Not able to mock the search.hits().hits() method.
Any idea how to do that?
SearchResponse<Product> search = client.search(s -> s
.index("products")
.query(q -> q
.term(t -> t
.field("name")
.value(v -> v.stringValue("bicycle"))
)),
Product.class);
for (Hit<Product> hit: search.hits().hits()) {
processProduct(hit.source());
}
Reference for code- https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html#_your_first_request

Related

Inner list from DTO is null while testing with Spring WebFlux

I have the following unit test (JUnit 5):
FluxExchangeResult<CalendarDTO> calendarEntityResult = client.get()
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
.returnResult(CalendarDTO.class);
assertNotNull(calendarEntityResult);
final Flux<CalendarDTO> responseBody = calendarEntityResult.getResponseBody();
responseBody.flatMap(calendarDTO -> {
assertNotNull(calendarDTO);
final List<AppointmentDTO> calendarEvents = calendarDTO.getCalendarEvents();
assertNotNull(calendarEvents);
assertFalse(calendarEvents.isEmpty());
return Flux.just(calendarEvents);
}).map(calendarEvents ->
calendarEvents.get(0)
).doOnNext(appointmentDTO ->
assertEquals(appointmentDTO, validAppointmentDTO())
).subscribe();
/*StepVerifier.create(responseBody)
.assertNext(calendarDTO -> {
assertNotNull(calendarDTO);
final List<AppointmentDTO> calendarEvents = calendarDTO.getCalendarEvents();
assertNotNull(calendarEvents);
assertFalse(calendarEvents.isEmpty());
final AppointmentDTO appointmentDTO = calendarEvents.get(0);
assertNotNull(appointmentDTO);
assertEquals(validAppointmentDTO(), appointmentDTO);
})
.expectComplete()
.verify();*/
For some reason, the assertNotNull(calendarEvents); is failing. The method itself when running it with Postman is fine. What has me puzzled is that on debug time, the calendarEntityResult has calendarEvents!
> GET /appointments
> WebTestClient-Request-Id: [1]
No content
< 200 OK OK
< Content-Type: [application/json;charset=UTF-8]
< Content-Length: [377]
{"data":{"calendarEvents":[{"id":null,"startTime":"2020-01-16T13:19:37.510-06:00","endTime":"2020-01-16T14:19:37.511-06:00","timeZoneStart":"America/Regina","timeZoneEnd":"America/Regina","summary":"unit test summary","description":"unit test description","organizerName":"Developer","organizerEmail":"developer#dev.com","status":null,"alarm":15}]},"notifications":null}
The commented code gives the same result. To be clear, the DTO itself is not null; the problem is the calendarEvents array. It's possible I'm doing something wrong since I'm new to reactive programming in general, so code improvements are most welcome. Am I extracting the data in a wrong manner?
You should be using the stepverifier when asserting any type of flux. Will make your life easier.
final Flux<String> responseBody = testClient.get()
.exchange()
.expectStatus()
.isOk()
.returnResult(String.class)
.getResponseBody();
StepVerifier.create(responseBody)
.assertNext(s -> assertEquals(s, "Foo"))
.assertNext(s -> assertEquals(s, "Bar"));
Turns out the root DTO was already wrapped into yet another DTO. It probably caused getResponseBody() to misinterpret the contents of the list and defaulted those to null. Leaving response here for the curious:
final Flux<AppointmentCalendarResponse> responseBody = client.get()
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
.returnResult(AppointmentCalendarResponse.class)
.getResponseBody();
StepVerifier.create(responseBody)
.assertNext(data -> {
CalendarDTO calendarDTO = data.getData();
final List<AppointmentDTO> calendarEvents = calendarDTO.getCalendarEvents();
assertNotNull(calendarEvents);
})
.expectComplete()
.verify();
You're using the same DB?
Because generaly the DB test and DB Dev is different.

How reuse flux mono value?

Consider a code:
WebClient webClient = ... ;
public Mono<MyWrapper> someFunction () {
Mono<MyDto> mono = webClient.get()
.uri("myUrl")
.retrieve()
.bodyToMono(MyDto.class);
Mono<FirstDto> first = mono.map(dto -> {...});
Mono<SecondDto> second = mono.map(dto -> {...}); //<- connection closed error here
return Mono.zip(first, second).map(zip -> {
return new MyWrapper(first, second);
});
}
Second map operation leads to connection closed error. I suppose that flux tried to send new request. (Does it or not?)
Second is there a way to map mono twice: to one type and another one without sending new request?
Have you tried using compose?
Mono<MyWrapper> mono = webClient.get()
.uri("myUrl")
.retrieve()
.bodyToMono(MyDto.class)
.compose(dto -> dto
.zip(dto.map(dto -> {...}), dto.map(dto -> {...})))
.map(MyWrapper::new);
API:
https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#compose-java.util.function.Function-

Java Vavr : Log Exception on Failure

I want to log exceptions while using VAVR (formerly javaslang). Below is the sample code snippet.
//will get 500 as response from this url
String sampleurl = "http://someurl.com";
List<String> myList = List.of(sampleurl);
LOGGER.info("In {} with urls {}",getClass(),myList);
return Observable.from(myList).flatMap(url ->
Observable.create(subscriber -> {
Try<String> httpEntity = HttpUtil.retrieveData(url).flatMap(httpResponse -> Try.of( () -> EntityUtils.toString(httpResponse.getEntity())));
httpEntity
.andThen(subscriber::onNext)
.andThen(subscriber::onCompleted)
.onFailure(subscriber::onError);
}));
I am trying to log exception in the onFailure() block but nothing gets logged. Please advise me on this.
Regards,
Jai
In both cases, success and failure, Vavr works as expected. Here are simple tests:
// prints nothing
Try.success("ok")
.andThen(() -> {})
.andThen(() -> {})
.onFailure(System.out::println);
// prints "java.lang.Error: ok"
Try.failure(new Error("ok"))
.andThen(() -> {})
.andThen(() -> {})
.onFailure(System.out::println);
I see two possible answers why the failure is not logged in your example:
the logger configuration does not fit your needs
the observables are not processed and Try is never called
Disclamer: I'm the creator of Vavr

Spring webflux non-blocking response

I have a request method that use 'username' and 'password' body parameters to find real user from database, validate it by using request password and if everything is fine - return generated token as String.
public Mono<ServerResponse> handleLogin(ServerRequest request) {
User body = request.bodyToMono(User.class).block();
return userRepository.findById(body.getUsername())
.filter(user -> passwordEncoder.matches(body.getPassword(), user.getPassword()))
.flatMap(user -> ServerResponse.ok().body(Mono.just(tokens.store(user)), String.class))
.switchIfEmpty(ServerResponse.badRequest().build());
}
This method works fine, but i'm trying to make it as non-blocking and i'm not sure how to achieve it. Any help appreciated.
Updated
For now i changed my method content to
return request.bodyToMono(User.class)
.flatMap(body -> userRepository.findById(body.getUsername()).flatMap(user -> Mono.just(new Object[]{body.getPassword(), user})))
.filter(obj -> {
User user = (User) obj[1];
return user.isActive() && passwordEncoder.matches((CharSequence) obj[0], user.getPassword());
})
.flatMap(obj -> {
User user = (User) obj[1];
return ServerResponse.ok().body(Mono.just(tokens.store(user)), String.class);
})
.switchIfEmpty(ServerResponse.badRequest().build());
This is non-blocking, but looks like not so elegant solution. Can it be improved/simplified somehow?
Remember
1) Everything is stream.
2) Keep your flow composed
3) No blocking operation at all.
Solution
So, keep your flow composed without blocking operation
public Mono<ServerResponse> handleLogin(ServerRequest request) {
return request.bodyToMono(User.class)
.flatMap(body -> userRepository.findById(body.getUsername())
.filter(user -> passwordEncoder.matches(body.getPassword(), user.getPassword()))
.flatMap(user -> ServerResponse.ok().body(Mono.just(tokens.store(user)), String.class))
.switchIfEmpty(ServerResponse.badRequest().build());
}
Regarding the last update
Regarding the last update, from my point of view, the code might be optimized in a next way:
return request.bodyToMono(User.class)
.flatMap(body -> userRepository.findById(body.getUsername())
.filter(user -> user.isActive())
.filter(user -> passwordEncoder.matches(body.getPassword(), user.getPassword()))
)
.flatMap(user -> ServerResponse.ok().syncBody(tokens.store(user)))
.switchIfEmpty(ServerResponse.badRequest().build());

How can i load two level routing in High Server Http Akka using Java?

I'm trying to work with akka Http with Java.
I would like to load a route that look like this :
hello/world
On my HttpApp, I tried:
return route(
path("hello/world", () ->
get(() ->
complete("<h1>Say hello to akka-http</h1>")
)
);
Try the following, as per the docs.
import static akka.http.javadsl.server.PathMatchers.*;
return route(
path(segment("hello").slash(segment("world")), () ->
get(() ->
complete("<h1>Say hello to akka-http</h1>")
)
);

Categories

Resources