What the below code does is that it gets items for specific(using client1
) customer and delete them by making another endpoint, client2
. The first endpoint provides pagination.
Now, I am having issue with testing it. As you can see, I am using wiremock but it does not seem working properly as the last endpoints never called. I have provided the response body for the first stub, was expecting the next endpoints to be triggered. I tried to debugging by putting break point to see if they are called, but the code never reach the section where the two endpoint are triggered. I am not sure the way I did is correct. Is this the correct approach to test using wiremock?
public void deleteItems(String id) {
client1.get()
.uri(urlBuilder -> urlBuilder
.path(CLIENT_ONE_ENDPOINT)
.queryParam("email", id)
.build())
.retrieve()
.bodyToMono(Items.class)
.expand(items -> {
while (!items.isOnLastPage()) {
int nextPage = items.getCurrentPage() + 1;
return client1.get()
.uri(builder -> builder
.path(CLIENT_ONE_ENDPOINT)
.queryParam("email", id)
.queryParam("pageNumber", nextPage)
.build())
.retrieve()
.bodyToMono(Items.class);
}
return Flux.empty();
})
.flatMapIterable(Items::getResults)
.map(or -> {
return client2.delete()
.uri(CLIENT_TWO_ENDPOINT + or.getId())
.retrieve()
.bodyToMono(ResponseEntity.class)
.subscribe();
})
.subscribe();
}
wiremock test
@Test
void itemsAreRemoved() {
String email = "example@example.com";
String itemId = "001";
WireMock.configureFor(PORT);
wireMockServer.stubFor(get(urlPathEqualTo(CLIENT_ONE_ENDPOINT))
.withQueryParam("email", equalTo(email))
.withHeader("Content-Type", equalTo("application/json"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody(loadResponse(itemId))
.withStatus(200)));
wireMockServer.stubFor(get(urlPathEqualTo(CLIENT_ONE_ENDPOINT))
.withQueryParam("email", equalTo(email))
.withQueryParam("pageNumber", equalTo("1"))
.withHeader("Content-Type", equalTo("application/json"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withStatus(200)));
wireMockServer.stubFor(delete(urlPathEqualTo(CLIENT_TWO_ENDPOINT + itemId)));
service.deleteItems(email);
verify(getRequestedFor(urlPathEqualTo(CLIENT_ONE_ENDPOINT))
.withQueryParam("email", equalTo(email)));
verify(getRequestedFor(urlPathEqualTo(CLIENT_ONE_ENDPOINT))
.withQueryParam("email", equalTo(email))
.withQueryParam("pageNumber", equalTo("1")));
verify(exactly(1), deleteRequestedFor(urlPathEqualTo(CLIENT_TWO_ENDPOINT + itemId)));
}