### Question:
InstanceExchangeFilterFunctions { public static ExchangeFilterFunction convertLegacyEndpoint(LegacyEndpointConverter converter) { return (ClientRequest request, ExchangeFunction next) -> { Mono<ClientResponse> clientResponse = next.exchange(request); if (request.attribute(ATTRIBUTE_ENDPOINT).map(converter::canConvert).orElse(false)) { return clientResponse.flatMap(response -> { if (response.headers() .contentType() .map(t -> ACTUATOR_V1_MEDIATYPE.isCompatibleWith(t) || APPLICATION_JSON.isCompatibleWith(t)) .orElse(false)) { return convertClientResponse(converter::convert, ACTUATOR_V2_MEDIATYPE).apply(response); } return Mono.just(response); }); } return clientResponse; }; } private  InstanceExchangeFilterFunctions(); static ExchangeFilterFunction setInstance(Instance instance); static ExchangeFilterFunction setInstance(Mono<Instance> instance); static ExchangeFilterFunction addHeaders(HttpHeadersProvider httpHeadersProvider); static ExchangeFilterFunction toExchangeFilterFunction(InstanceExchangeFilterFunction delegate); static ExchangeFilterFunction rewriteEndpointUrl(); static ExchangeFilterFunction convertLegacyEndpoint(LegacyEndpointConverter converter); static ExchangeFilterFunction setDefaultAcceptHeader(); static ExchangeFilterFunction retry(int defaultRetries, Map<String, Integer> retriesPerEndpoint); static final String ATTRIBUTE_INSTANCE; static final String ATTRIBUTE_ENDPOINT; }### Answer:
@Test public void should_convert_v1_actuator() { ExchangeFilterFunction filter = InstanceExchangeFilterFunctions.convertLegacyEndpoint(new TestLegacyEndpointConverter()); ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("/test")) .attribute(ATTRIBUTE_ENDPOINT, "test") .build(); ClientResponse response = ClientResponse.create(HttpStatus.OK) .header(CONTENT_TYPE, ActuatorMediaType.V1_JSON) .body(Flux.just(ORIGINAL)) .build(); Mono<ClientResponse> convertedResponse = filter.filter(request, r -> Mono.just(response)); StepVerifier.create(convertedResponse) .assertNext(r -> StepVerifier.create(r.body(BodyExtractors.toDataBuffers())) .expectNext(CONVERTED) .verifyComplete()) .verifyComplete(); }
@Test public void should_convert_json() { ExchangeFilterFunction filter = InstanceExchangeFilterFunctions.convertLegacyEndpoint(new TestLegacyEndpointConverter()); ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("/test")) .attribute(ATTRIBUTE_ENDPOINT, "test") .build(); ClientResponse response = ClientResponse.create(HttpStatus.OK) .header(CONTENT_TYPE, APPLICATION_JSON_VALUE) .body(Flux.just(ORIGINAL)) .build(); Mono<ClientResponse> convertedResponse = filter.filter(request, r -> Mono.just(response)); StepVerifier.create(convertedResponse) .assertNext(r -> StepVerifier.create(r.body(BodyExtractors.toDataBuffers())) .expectNext(CONVERTED) .verifyComplete()) .verifyComplete(); }
@Test public void should_not_convert_v2_actuator() { ExchangeFilterFunction filter = InstanceExchangeFilterFunctions.convertLegacyEndpoint(new TestLegacyEndpointConverter()); ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("/test")) .attribute(ATTRIBUTE_ENDPOINT, "test") .build(); ClientResponse response = ClientResponse.create(HttpStatus.OK) .header(CONTENT_TYPE, ActuatorMediaType.V2_JSON) .body(Flux.just(ORIGINAL)) .build(); Mono<ClientResponse> convertedResponse = filter.filter(request, r -> Mono.just(response)); StepVerifier.create(convertedResponse) .assertNext(r -> StepVerifier.create(r.body(BodyExtractors.toDataBuffers())) .expectNext(ORIGINAL) .verifyComplete()) .verifyComplete(); }