Refactoring in ApiVersionInserter
Refine naming of static factory methods, and update them to be shortcuts for instance creation. See gh-34919
This commit is contained in:
parent
f4f0e52003
commit
5b19f6249e
|
@ -41,19 +41,19 @@ public class ApiVersionTests {
|
|||
|
||||
@Test
|
||||
void header() {
|
||||
Map<String, String> result = performRequest(ApiVersionInserter.fromHeader("X-API-Version").build());
|
||||
Map<String, String> result = performRequest(ApiVersionInserter.useHeader("X-API-Version"));
|
||||
assertThat(result.get(HEADER_NAME)).isEqualTo("1.2");
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryParam() {
|
||||
Map<String, String> result = performRequest(ApiVersionInserter.fromQueryParam("api-version").build());
|
||||
Map<String, String> result = performRequest(ApiVersionInserter.useQueryParam("api-version"));
|
||||
assertThat(result.get("query")).isEqualTo("api-version=1.2");
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathSegment() {
|
||||
Map<String, String> result = performRequest(ApiVersionInserter.fromPathSegment(0).build());
|
||||
Map<String, String> result = performRequest(ApiVersionInserter.usePathSegment(0));
|
||||
assertThat(result.get("path")).isEqualTo("/1.2/path");
|
||||
}
|
||||
|
||||
|
|
|
@ -53,27 +53,34 @@ public interface ApiVersionInserter {
|
|||
|
||||
|
||||
/**
|
||||
* Create a builder for an inserter that sets a header.
|
||||
* Create an inserter that sets a header.
|
||||
* @param header the name of a header to hold the version
|
||||
*/
|
||||
static Builder fromHeader(@Nullable String header) {
|
||||
return new DefaultApiVersionInserterBuilder(header, null, null);
|
||||
static ApiVersionInserter useHeader(@Nullable String header) {
|
||||
return new DefaultApiVersionInserterBuilder(header, null, null).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder for an inserter that sets a query parameter.
|
||||
* Create an inserter that sets a query parameter.
|
||||
* @param queryParam the name of a query parameter to hold the version
|
||||
*/
|
||||
static Builder fromQueryParam(@Nullable String queryParam) {
|
||||
return new DefaultApiVersionInserterBuilder(null, queryParam, null);
|
||||
static ApiVersionInserter useQueryParam(@Nullable String queryParam) {
|
||||
return new DefaultApiVersionInserterBuilder(null, queryParam, null).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder for an inserter that inserts a path segment.
|
||||
* Create an inserter that inserts a path segment.
|
||||
* @param pathSegmentIndex the index of the path segment to hold the version
|
||||
*/
|
||||
static Builder fromPathSegment(@Nullable Integer pathSegmentIndex) {
|
||||
return new DefaultApiVersionInserterBuilder(null, null, pathSegmentIndex);
|
||||
static ApiVersionInserter usePathSegment(@Nullable Integer pathSegmentIndex) {
|
||||
return new DefaultApiVersionInserterBuilder(null, null, pathSegmentIndex).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder for an {@link ApiVersionInserter}.
|
||||
*/
|
||||
static Builder builder() {
|
||||
return new DefaultApiVersionInserterBuilder(null, null, null);
|
||||
}
|
||||
|
||||
|
||||
|
@ -86,19 +93,19 @@ public interface ApiVersionInserter {
|
|||
* Configure the inserter to set a header.
|
||||
* @param header the name of the header to hold the version
|
||||
*/
|
||||
Builder fromHeader(@Nullable String header);
|
||||
Builder useHeader(@Nullable String header);
|
||||
|
||||
/**
|
||||
* Configure the inserter to set a query parameter.
|
||||
* @param queryParam the name of the query parameter to hold the version
|
||||
*/
|
||||
Builder fromQueryParam(@Nullable String queryParam);
|
||||
Builder useQueryParam(@Nullable String queryParam);
|
||||
|
||||
/**
|
||||
* Configure the inserter to insert a path segment.
|
||||
* @param pathSegmentIndex the index of the path segment to hold the version
|
||||
*/
|
||||
Builder fromPathSegment(@Nullable Integer pathSegmentIndex);
|
||||
Builder usePathSegment(@Nullable Integer pathSegmentIndex);
|
||||
|
||||
/**
|
||||
* Format the version Object into a String using the given {@link ApiVersionFormatter}.
|
||||
|
|
|
@ -23,9 +23,9 @@ import org.jspecify.annotations.Nullable;
|
|||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 7.0
|
||||
* @see ApiVersionInserter#fromHeader(String)
|
||||
* @see ApiVersionInserter#fromQueryParam(String)
|
||||
* @see ApiVersionInserter#fromPathSegment(Integer)
|
||||
* @see ApiVersionInserter#useHeader(String)
|
||||
* @see ApiVersionInserter#useQueryParam(String)
|
||||
* @see ApiVersionInserter#usePathSegment(Integer)
|
||||
*/
|
||||
final class DefaultApiVersionInserterBuilder implements ApiVersionInserter.Builder {
|
||||
|
||||
|
@ -50,7 +50,7 @@ final class DefaultApiVersionInserterBuilder implements ApiVersionInserter.Build
|
|||
* Configure the inserter to set a header.
|
||||
* @param header the name of the header to hold the version
|
||||
*/
|
||||
public ApiVersionInserter.Builder fromHeader(@Nullable String header) {
|
||||
public ApiVersionInserter.Builder useHeader(@Nullable String header) {
|
||||
this.header = header;
|
||||
return this;
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ final class DefaultApiVersionInserterBuilder implements ApiVersionInserter.Build
|
|||
* Configure the inserter to set a query parameter.
|
||||
* @param queryParam the name of the query parameter to hold the version
|
||||
*/
|
||||
public ApiVersionInserter.Builder fromQueryParam(@Nullable String queryParam) {
|
||||
public ApiVersionInserter.Builder useQueryParam(@Nullable String queryParam) {
|
||||
this.queryParam = queryParam;
|
||||
return this;
|
||||
}
|
||||
|
@ -68,7 +68,7 @@ final class DefaultApiVersionInserterBuilder implements ApiVersionInserter.Build
|
|||
* Configure the inserter to insert a path segment.
|
||||
* @param pathSegmentIndex the index of the path segment to hold the version
|
||||
*/
|
||||
public ApiVersionInserter.Builder fromPathSegment(@Nullable Integer pathSegmentIndex) {
|
||||
public ApiVersionInserter.Builder usePathSegment(@Nullable Integer pathSegmentIndex) {
|
||||
this.pathSegmentIndex = pathSegmentIndex;
|
||||
return this;
|
||||
}
|
||||
|
|
|
@ -59,50 +59,47 @@ public class RestClientVersionTests {
|
|||
|
||||
@Test
|
||||
void header() {
|
||||
performRequest(ApiVersionInserter.fromHeader("X-API-Version"));
|
||||
performRequest(ApiVersionInserter.useHeader("X-API-Version"));
|
||||
expectRequest(request -> assertThat(request.getHeader("X-API-Version")).isEqualTo("1.2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryParam() {
|
||||
performRequest(ApiVersionInserter.fromQueryParam("api-version"));
|
||||
performRequest(ApiVersionInserter.useQueryParam("api-version"));
|
||||
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/path?api-version=1.2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathSegmentIndexLessThanSize() {
|
||||
performRequest(ApiVersionInserter.fromPathSegment(0).withVersionFormatter(v -> "v" + v));
|
||||
performRequest(ApiVersionInserter.builder().usePathSegment(0).withVersionFormatter(v -> "v" + v).build());
|
||||
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/v1.2/path"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathSegmentIndexEqualToSize() {
|
||||
performRequest(ApiVersionInserter.fromPathSegment(1).withVersionFormatter(v -> "v" + v));
|
||||
performRequest(ApiVersionInserter.builder().usePathSegment(1).withVersionFormatter(v -> "v" + v).build());
|
||||
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/path/v1.2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathSegmentIndexGreaterThanSize() {
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> performRequest(ApiVersionInserter.fromPathSegment(2)))
|
||||
.isThrownBy(() -> performRequest(ApiVersionInserter.usePathSegment(2)))
|
||||
.withMessage("Cannot insert version into '/path' at path segment index 2");
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultVersion() {
|
||||
ApiVersionInserter inserter = ApiVersionInserter.fromHeader("X-API-Version").build();
|
||||
ApiVersionInserter inserter = ApiVersionInserter.useHeader("X-API-Version");
|
||||
RestClient restClient = restClientBuilder.defaultApiVersion(1.2).apiVersionInserter(inserter).build();
|
||||
restClient.get().uri("/path").retrieve().body(String.class);
|
||||
|
||||
expectRequest(request -> assertThat(request.getHeader("X-API-Version")).isEqualTo("1.2"));
|
||||
}
|
||||
|
||||
private void performRequest(ApiVersionInserter.Builder builder) {
|
||||
ApiVersionInserter versionInserter = builder.build();
|
||||
RestClient restClient = restClientBuilder.apiVersionInserter(versionInserter).build();
|
||||
|
||||
restClient.get()
|
||||
.uri("/path")
|
||||
private void performRequest(ApiVersionInserter versionInserter) {
|
||||
restClientBuilder.apiVersionInserter(versionInserter).build()
|
||||
.get().uri("/path")
|
||||
.apiVersion(1.2)
|
||||
.retrieve()
|
||||
.body(String.class);
|
||||
|
|
|
@ -274,7 +274,7 @@ class RestClientAdapterTests {
|
|||
void apiVersion() throws Exception {
|
||||
RestClient restClient = RestClient.builder()
|
||||
.baseUrl(anotherServer.url("/").toString())
|
||||
.apiVersionInserter(ApiVersionInserter.fromHeader("X-API-Version").build())
|
||||
.apiVersionInserter(ApiVersionInserter.useHeader("X-API-Version"))
|
||||
.build();
|
||||
|
||||
RestClientAdapter adapter = RestClientAdapter.create(restClient);
|
||||
|
|
|
@ -58,46 +58,45 @@ public class WebClientVersionTests {
|
|||
|
||||
@Test
|
||||
void header() {
|
||||
performRequest(ApiVersionInserter.fromHeader("X-API-Version"));
|
||||
performRequest(ApiVersionInserter.useHeader("X-API-Version"));
|
||||
expectRequest(request -> assertThat(request.getHeader("X-API-Version")).isEqualTo("1.2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryParam() {
|
||||
performRequest(ApiVersionInserter.fromQueryParam("api-version"));
|
||||
performRequest(ApiVersionInserter.useQueryParam("api-version"));
|
||||
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/path?api-version=1.2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathSegmentIndexLessThanSize() {
|
||||
performRequest(ApiVersionInserter.fromPathSegment(0).withVersionFormatter(v -> "v" + v));
|
||||
performRequest(ApiVersionInserter.builder().usePathSegment(0).withVersionFormatter(v -> "v" + v).build());
|
||||
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/v1.2/path"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathSegmentIndexEqualToSize() {
|
||||
performRequest(ApiVersionInserter.fromPathSegment(1).withVersionFormatter(v -> "v" + v));
|
||||
performRequest(ApiVersionInserter.builder().usePathSegment(1).withVersionFormatter(v -> "v" + v).build());
|
||||
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/path/v1.2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathSegmentIndexGreaterThanSize() {
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> performRequest(ApiVersionInserter.fromPathSegment(2)))
|
||||
.isThrownBy(() -> performRequest(ApiVersionInserter.usePathSegment(2)))
|
||||
.withMessage("Cannot insert version into '/path' at path segment index 2");
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultVersion() {
|
||||
ApiVersionInserter inserter = ApiVersionInserter.fromHeader("X-API-Version").build();
|
||||
ApiVersionInserter inserter = ApiVersionInserter.useHeader("X-API-Version");
|
||||
WebClient webClient = webClientBuilder.defaultApiVersion(1.2).apiVersionInserter(inserter).build();
|
||||
webClient.get().uri("/path").retrieve().bodyToMono(String.class).block();
|
||||
|
||||
expectRequest(request -> assertThat(request.getHeader("X-API-Version")).isEqualTo("1.2"));
|
||||
}
|
||||
|
||||
private void performRequest(ApiVersionInserter.Builder builder) {
|
||||
ApiVersionInserter versionInserter = builder.build();
|
||||
private void performRequest(ApiVersionInserter versionInserter) {
|
||||
WebClient webClient = webClientBuilder.apiVersionInserter(versionInserter).build();
|
||||
webClient.get().uri("/path").apiVersion(1.2).retrieve().bodyToMono(String.class).block();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue