-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an API compatibility check to Nessie clients (#6818)
- Loading branch information
Showing
23 changed files
with
530 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
api/client/src/main/java/org/projectnessie/client/http/NessieApiCompatibility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (C) 2023 Dremio | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.projectnessie.client.http; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
public class NessieApiCompatibility { | ||
|
||
private static final String MIN_API_VERSION = "minSupportedApiVersion"; | ||
private static final String MAX_API_VERSION = "maxSupportedApiVersion"; | ||
private static final String ACTUAL_API_VERSION = "actualApiVersion"; | ||
|
||
/** | ||
* Checks if the API version of the client is compatible with the server's. | ||
* | ||
* @param clientApiVersion the API version of the client | ||
* @param httpClient the underlying HTTP client. | ||
* @throws NessieApiCompatibilityException if the API version is not compatible. | ||
*/ | ||
public static void check(int clientApiVersion, HttpClient httpClient) | ||
throws NessieApiCompatibilityException { | ||
JsonNode config = httpClient.newRequest().path("config").get().readEntity(JsonNode.class); | ||
int minServerApiVersion = | ||
config.hasNonNull(MIN_API_VERSION) ? config.get(MIN_API_VERSION).asInt() : 1; | ||
int maxServerApiVersion = config.get(MAX_API_VERSION).asInt(); | ||
int actualServerApiVersion = | ||
config.hasNonNull(ACTUAL_API_VERSION) ? config.get(ACTUAL_API_VERSION).asInt() : 0; | ||
if (clientApiVersion < minServerApiVersion | ||
|| clientApiVersion > maxServerApiVersion | ||
|| (actualServerApiVersion > 0 && clientApiVersion != actualServerApiVersion)) { | ||
throw new NessieApiCompatibilityException( | ||
clientApiVersion, minServerApiVersion, maxServerApiVersion, actualServerApiVersion); | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
api/client/src/main/java/org/projectnessie/client/http/NessieApiCompatibilityException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (C) 2023 Dremio | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.projectnessie.client.http; | ||
|
||
public class NessieApiCompatibilityException extends RuntimeException { | ||
|
||
private final int clientApiVersion; | ||
private final int minServerApiVersion; | ||
private final int maxServerApiVersion; | ||
private final int actualServerApiVersion; | ||
|
||
public NessieApiCompatibilityException( | ||
int clientApiVersion, | ||
int minServerApiVersion, | ||
int maxServerApiVersion, | ||
int actualServerApiVersion) { | ||
super( | ||
formatMessage( | ||
clientApiVersion, minServerApiVersion, maxServerApiVersion, actualServerApiVersion)); | ||
this.clientApiVersion = clientApiVersion; | ||
this.minServerApiVersion = minServerApiVersion; | ||
this.maxServerApiVersion = maxServerApiVersion; | ||
this.actualServerApiVersion = actualServerApiVersion; | ||
} | ||
|
||
private static String formatMessage( | ||
int clientApiVersion, | ||
int minServerApiVersion, | ||
int maxServerApiVersion, | ||
int actualServerApiVersion) { | ||
if (clientApiVersion < minServerApiVersion) { | ||
return String.format( | ||
"API version %d is too old for server (minimum supported version is %d)", | ||
clientApiVersion, minServerApiVersion); | ||
} | ||
if (clientApiVersion > maxServerApiVersion) { | ||
return String.format( | ||
"API version %d is too new for server (maximum supported version is %d)", | ||
clientApiVersion, maxServerApiVersion); | ||
} | ||
return String.format( | ||
"API version mismatch, check URI prefix (expected: %d, actual: %d)", | ||
clientApiVersion, actualServerApiVersion); | ||
} | ||
|
||
/** The client's API version. */ | ||
public int getClientApiVersion() { | ||
return clientApiVersion; | ||
} | ||
|
||
/** The minimum API version supported by the server. */ | ||
public int getMinServerApiVersion() { | ||
return minServerApiVersion; | ||
} | ||
|
||
/** The maximum API version supported by the server. */ | ||
public int getMaxServerApiVersion() { | ||
return maxServerApiVersion; | ||
} | ||
|
||
/** | ||
* The actual API version used by the server, or zero if the server does not report its actual API | ||
* version. | ||
*/ | ||
public int getActualServerApiVersion() { | ||
return actualServerApiVersion; | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
api/client/src/test/java/org/projectnessie/client/http/TestNessieApiCompatibility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright (C) 2023 Dremio | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.projectnessie.client.http; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||
import static java.net.HttpURLConnection.HTTP_OK; | ||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.assertj.core.api.InstanceOfAssertFactories.type; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; | ||
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; | ||
import com.github.tomakehurst.wiremock.junit5.WireMockTest; | ||
import java.net.URI; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.projectnessie.client.rest.NessieHttpResponseFilter; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@WireMockTest | ||
class TestNessieApiCompatibility { | ||
|
||
enum Expectation { | ||
OK, | ||
TOO_OLD, | ||
TOO_NEW, | ||
MISMATCH; | ||
|
||
public String expectedErrorMessage() { | ||
switch (this) { | ||
case TOO_OLD: | ||
return "too old"; | ||
case TOO_NEW: | ||
return "too new"; | ||
case MISMATCH: | ||
return "mismatch"; | ||
default: | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource( | ||
value = { | ||
"1, 1, 1, 0, OK", | ||
"1, 1, 2, 1, OK", | ||
"1, 1, 2, 2, MISMATCH", // v2 endpoint mistakenly called with v1 client | ||
"1, 2, 2, 2, TOO_OLD", | ||
"2, 1, 1, 1, TOO_NEW", | ||
"2, 1, 2, 1, MISMATCH", // v1 endpoint mistakenly called with v2 client | ||
"2, 1, 2, 2, OK", | ||
"2, 2, 2, 2, OK", | ||
}) | ||
void checkApiCompatibility( | ||
int client, | ||
int serverMin, | ||
int serverMax, | ||
int serverActual, | ||
Expectation expectation, | ||
WireMockRuntimeInfo wireMock) { | ||
|
||
ObjectNode config = JsonNodeFactory.instance.objectNode(); | ||
config.set("minSupportedApiVersion", JsonNodeFactory.instance.numberNode(serverMin)); | ||
config.set("maxSupportedApiVersion", JsonNodeFactory.instance.numberNode(serverMax)); | ||
if (serverActual > 0) { | ||
config.set("actualApiVersion", JsonNodeFactory.instance.numberNode(serverActual)); | ||
} | ||
|
||
stubFor( | ||
get("/config") | ||
.willReturn( | ||
ResponseDefinitionBuilder.responseDefinition() | ||
.withStatus(HTTP_OK) | ||
.withBody(config.toString()) | ||
.withHeader("Content-Type", "application/json"))); | ||
|
||
try (HttpClient httpClient = | ||
HttpClient.builder() | ||
.setBaseUri(URI.create(wireMock.getHttpBaseUrl())) | ||
.setObjectMapper(new ObjectMapper()) | ||
.addResponseFilter(new NessieHttpResponseFilter()) | ||
.build()) { | ||
|
||
if (expectation == Expectation.OK) { | ||
|
||
assertThatCode(() -> NessieApiCompatibility.check(client, httpClient)) | ||
.doesNotThrowAnyException(); | ||
|
||
} else { | ||
|
||
assertThatThrownBy(() -> NessieApiCompatibility.check(client, httpClient)) | ||
.hasMessageContaining(expectation.expectedErrorMessage()) | ||
.asInstanceOf(type(NessieApiCompatibilityException.class)) | ||
.extracting( | ||
NessieApiCompatibilityException::getClientApiVersion, | ||
NessieApiCompatibilityException::getMinServerApiVersion, | ||
NessieApiCompatibilityException::getMaxServerApiVersion, | ||
NessieApiCompatibilityException::getActualServerApiVersion) | ||
.containsExactly(client, serverMin, serverMax, serverActual); | ||
} | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ient/src/test/java/org/projectnessie/client/http/TestNessieApiCompatibilityException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2023 Dremio | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.projectnessie.client.http; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class TestNessieApiCompatibilityException { | ||
|
||
@Test | ||
void testMessages() { | ||
NessieApiCompatibilityException e = new NessieApiCompatibilityException(1, 2, 3, 3); | ||
assertThat(e.getMessage()) | ||
.isEqualTo("API version 1 is too old for server (minimum supported version is 2)"); | ||
e = new NessieApiCompatibilityException(5, 3, 4, 4); | ||
assertThat(e.getMessage()) | ||
.isEqualTo("API version 5 is too new for server (maximum supported version is 4)"); | ||
e = new NessieApiCompatibilityException(3, 2, 4, 2); | ||
assertThat(e.getMessage()) | ||
.isEqualTo("API version mismatch, check URI prefix (expected: 3, actual: 2)"); | ||
} | ||
|
||
@Test | ||
void testGetters() { | ||
NessieApiCompatibilityException e = new NessieApiCompatibilityException(1, 2, 4, 3); | ||
assertThat(e.getClientApiVersion()).isEqualTo(1); | ||
assertThat(e.getMinServerApiVersion()).isEqualTo(2); | ||
assertThat(e.getMaxServerApiVersion()).isEqualTo(4); | ||
assertThat(e.getActualServerApiVersion()).isEqualTo(3); | ||
} | ||
} |
Oops, something went wrong.