i have this url which is working:
which returns only the database missing elements of the input. i have a third party not modifiable service method like this:
<code>public Mono<List<String>> getInvalidList(final Flux<String> input)
</code>
<code>public Mono<List<String>> getInvalidList(final Flux<String> input)
</code>
public Mono<List<String>> getInvalidList(final Flux<String> input)
and to make this api work i’ve wrote this handler method:
<code>public Mono<ServerResponse> getInvalidKeyItems(final ServerRequest req) {
final EntryMessage message = log.traceEntry("getInvalidKeyItems[request={}]", req);
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
.body(service.getInvalidList(req.bodyToFlux(Integer.class).map(Object::toString)), List.class)
.doOnSuccess(result -> log.traceExit(message, result));
}
</code>
<code>public Mono<ServerResponse> getInvalidKeyItems(final ServerRequest req) {
final EntryMessage message = log.traceEntry("getInvalidKeyItems[request={}]", req);
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
.body(service.getInvalidList(req.bodyToFlux(Integer.class).map(Object::toString)), List.class)
.doOnSuccess(result -> log.traceExit(message, result));
}
</code>
public Mono<ServerResponse> getInvalidKeyItems(final ServerRequest req) {
final EntryMessage message = log.traceEntry("getInvalidKeyItems[request={}]", req);
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
.body(service.getInvalidList(req.bodyToFlux(Integer.class).map(Object::toString)), List.class)
.doOnSuccess(result -> log.traceExit(message, result));
}
and this router method:
<code> @Bean
public RouterFunction<ServerResponse> getInvalidKeyItems(final KeyItemHandler handler) {
return route(POST("/somePath").and(POST_AND_ACCEPT_JSON),
handler::getInvalidKeyItems);
}
</code>
<code> @Bean
public RouterFunction<ServerResponse> getInvalidKeyItems(final KeyItemHandler handler) {
return route(POST("/somePath").and(POST_AND_ACCEPT_JSON),
handler::getInvalidKeyItems);
}
</code>
@Bean
public RouterFunction<ServerResponse> getInvalidKeyItems(final KeyItemHandler handler) {
return route(POST("/somePath").and(POST_AND_ACCEPT_JSON),
handler::getInvalidKeyItems);
}
which POST_AND_ACCEPT_JSON
is a custom short variable for
<code>RequestPredicates.method(HttpMethod.POST).and(RequestPredicates.accept(new MediaType[]{MediaType.APPLICATION_JSON}));
</code>
<code>RequestPredicates.method(HttpMethod.POST).and(RequestPredicates.accept(new MediaType[]{MediaType.APPLICATION_JSON}));
</code>
RequestPredicates.method(HttpMethod.POST).and(RequestPredicates.accept(new MediaType[]{MediaType.APPLICATION_JSON}));
again, all of this is already working. what is not working is my router unit test:
<code>@SpringBootTest
@ExtendWith({SpringExtension.class})
@AutoConfigureWebTestClient
class KeyItemRouterTest {
@Autowired
WebTestClient client;
@MockBean
KeyItemService service;
@Test
@WithMockUser
void validateKeyItemTest() {
Mockito.when(service.getInvalidList(Flux.fromIterable(List.of("1", "2", "3"))))
.thenReturn(Mono.just(List.of("1")));
client.post().uri("/somePath").contentType(MediaType.APPLICATION_JSON)
.body(Mono.just(List.of("1", "2", "3")), List.class)
.exchange()
.expectStatus().isOk()
.expectBody(List.class)
.value(list -> Assertions.assertIterableEquals(List.of("2", "3"), list));
}
</code>
<code>@SpringBootTest
@ExtendWith({SpringExtension.class})
@AutoConfigureWebTestClient
class KeyItemRouterTest {
@Autowired
WebTestClient client;
@MockBean
KeyItemService service;
@Test
@WithMockUser
void validateKeyItemTest() {
Mockito.when(service.getInvalidList(Flux.fromIterable(List.of("1", "2", "3"))))
.thenReturn(Mono.just(List.of("1")));
client.post().uri("/somePath").contentType(MediaType.APPLICATION_JSON)
.body(Mono.just(List.of("1", "2", "3")), List.class)
.exchange()
.expectStatus().isOk()
.expectBody(List.class)
.value(list -> Assertions.assertIterableEquals(List.of("2", "3"), list));
}
</code>
@SpringBootTest
@ExtendWith({SpringExtension.class})
@AutoConfigureWebTestClient
class KeyItemRouterTest {
@Autowired
WebTestClient client;
@MockBean
KeyItemService service;
@Test
@WithMockUser
void validateKeyItemTest() {
Mockito.when(service.getInvalidList(Flux.fromIterable(List.of("1", "2", "3"))))
.thenReturn(Mono.just(List.of("1")));
client.post().uri("/somePath").contentType(MediaType.APPLICATION_JSON)
.body(Mono.just(List.of("1", "2", "3")), List.class)
.exchange()
.expectStatus().isOk()
.expectBody(List.class)
.value(list -> Assertions.assertIterableEquals(List.of("2", "3"), list));
}
which gets me this error:
<code>{"timestamp":1714607064355,"path":"/somePath","status":"BAD_REQUEST","error":"'producer' must not be null","message":"'producer' must not be null","requestId":"46a88fc7","exception":"java.lang.IllegalArgumentException"}
java.lang.AssertionError: Status expected:<200 OK> but was:<400 BAD_REQUEST>
Expected :200 OK
Actual :400 BAD_REQUEST
</code>
<code>{"timestamp":1714607064355,"path":"/somePath","status":"BAD_REQUEST","error":"'producer' must not be null","message":"'producer' must not be null","requestId":"46a88fc7","exception":"java.lang.IllegalArgumentException"}
java.lang.AssertionError: Status expected:<200 OK> but was:<400 BAD_REQUEST>
Expected :200 OK
Actual :400 BAD_REQUEST
</code>
{"timestamp":1714607064355,"path":"/somePath","status":"BAD_REQUEST","error":"'producer' must not be null","message":"'producer' must not be null","requestId":"46a88fc7","exception":"java.lang.IllegalArgumentException"}
java.lang.AssertionError: Status expected:<200 OK> but was:<400 BAD_REQUEST>
Expected :200 OK
Actual :400 BAD_REQUEST
i’ve tried changing
<code>.body(Mono.just(List.of("1", "2", "3")), List.class)
</code>
<code>.body(Mono.just(List.of("1", "2", "3")), List.class)
</code>
.body(Mono.just(List.of("1", "2", "3")), List.class)
to
<code>.bodyValue(List.of("1", "2", "3"))`
</code>
<code>.bodyValue(List.of("1", "2", "3"))`
</code>
.bodyValue(List.of("1", "2", "3"))`
to
<code>.body(BodyInserters.fromPublisher(Mono.just(List.of("1","2","3")), List.class))
</code>
<code>.body(BodyInserters.fromPublisher(Mono.just(List.of("1","2","3")), List.class))
</code>
.body(BodyInserters.fromPublisher(Mono.just(List.of("1","2","3")), List.class))
but all of them resulted in the same error. how should i add a list of strings in my body request when unit testing?