diff --git a/src/main/java/com/box/sdk/BoxAPIConnection.java b/src/main/java/com/box/sdk/BoxAPIConnection.java index 64db1b7ea..bba170e65 100644 --- a/src/main/java/com/box/sdk/BoxAPIConnection.java +++ b/src/main/java/com/box/sdk/BoxAPIConnection.java @@ -752,8 +752,10 @@ public void restore(String state) { String userAgent = json.get("userAgent").asString(); String tokenURL = getKeyValueOrDefault(json, "tokenURL", null); String revokeURL = getKeyValueOrDefault(json, "revokeURL", null); - String baseURL = json.get("baseURL").asString(); - String baseUploadURL = json.get("baseUploadURL").asString(); + String baseURL = adoptBaseUrlWhenLoadingFromOldVersion(getKeyValueOrDefault(json, "baseURL", DEFAULT_BASE_URL)); + String baseUploadURL = adoptUploadBaseUrlWhenLoadingFromOldVersion( + getKeyValueOrDefault(json, "baseUploadURL", DEFAULT_BASE_UPLOAD_URL) + ); String authorizationURL = getKeyValueOrDefault(json, "authorizationURL", DEFAULT_BASE_AUTHORIZATION_URL); boolean autoRefresh = json.get("autoRefresh").asBoolean(); @@ -789,6 +791,26 @@ public void restore(String state) { } + private String adoptBaseUrlWhenLoadingFromOldVersion(String url) { + if (url == null) { + return null; + } + String urlEndingWithSlash = fixBaseUrl(url); + return urlEndingWithSlash.equals("https://api.box.com/2.0/") + ? DEFAULT_BASE_URL + : urlEndingWithSlash; + } + + private String adoptUploadBaseUrlWhenLoadingFromOldVersion(String url) { + if (url == null) { + return null; + } + String urlEndingWithSlash = fixBaseUrl(url); + return urlEndingWithSlash.equals("https://upload.box.com/api/2.0/") + ? DEFAULT_BASE_UPLOAD_URL + : urlEndingWithSlash; + } + protected String getKeyValueOrDefault(JsonObject json, String key, String defaultValue) { return Optional.ofNullable(json.get(key)) .filter(js -> !js.isNull()) diff --git a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java index bfd1c8e8c..16650f490 100644 --- a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java +++ b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java @@ -23,7 +23,6 @@ import com.eclipsesource.json.JsonObject; import com.github.tomakehurst.wiremock.client.WireMock; import com.github.tomakehurst.wiremock.junit.WireMockRule; -import java.io.IOException; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; @@ -499,7 +498,99 @@ public void allowsToSaveAndRestoreApplicationConnection() throws URISyntaxExcept } @Test - public void successfullyRestoresConnectionWithDeprecatedSettings() throws IOException { + public void restoresProperUrlsFromOldVersions() { + // given + String accessToken = "access_token"; + String clientId = "some_client_id"; + String clientSecret = "some_client_secret"; + String refreshToken = "some_refresh_token"; + BoxAPIConnection api = new BoxAPIConnection(clientId, clientSecret, accessToken, refreshToken); + api.setBaseURL("https://api.box.com/2.0/"); + api.setBaseUploadURL("https://upload.box.com/api/2.0/"); + String savedConnection = api.save(); + + // when + BoxAPIConnection restoredApi = BoxAPIConnection.restore(clientId, clientSecret, savedConnection); + + // then + assertThat("https://api.box.com/2.0/", is(restoredApi.getBaseURL())); + assertThat("https://upload.box.com/api/2.0/", is(restoredApi.getBaseUploadURL())); + } + + @Test + public void restoresProperUrlsWhenNotSet() { + // given + String accessToken = "access_token"; + String clientId = "some_client_id"; + String clientSecret = "some_client_secret"; + String refreshToken = "some_refresh_token"; + BoxAPIConnection api = new BoxAPIConnection(clientId, clientSecret, accessToken, refreshToken); + api.setBaseURL("https://api.box.com/2.0/"); + String savedConnection = api.save(); + + // when + BoxAPIConnection restoredApi = BoxAPIConnection.restore(clientId, clientSecret, savedConnection); + + // then + assertThat("https://api.box.com/2.0/", is(restoredApi.getBaseURL())); + assertThat("https://upload.box.com/api/2.0/", is(restoredApi.getBaseUploadURL())); + } + + @Test + public void restoresProperUrlsWhenDeprecatedUrlsAreSet() { + // given + String clientId = "some_client_id"; + String clientSecret = "some_client_secret"; + + String savedConnection = "{" + + "\"accessToken\":\"access_token\"," + + "\"refreshToken\":\"some_refresh_token\"," + + "\"lastRefresh\":0," + + "\"expires\":0," + + "\"userAgent\":\"Box Java SDK v3.8.0 (Java 1.8.0_345)\"," + + "\"tokenURL\":\"https://api.box.com/token\"," + + "\"revokeURL\":\"https://api.box.com/revoke\"," + + "\"baseURL\":\"https://api.box.com/\"," + + "\"baseUploadURL\":\"https://upload.box.com/api/\"," + + "\"authorizationURL\":\"https://account.box.com/api/\"," + + "\"autoRefresh\":true,\"maxRetryAttempts\":5" + + "}"; + // when + BoxAPIConnection restoredApi = BoxAPIConnection.restore(clientId, clientSecret, savedConnection); + + // then + assertThat("https://api.box.com/token", is(restoredApi.getTokenURL())); + assertThat("https://api.box.com/revoke", is(restoredApi.getRevokeURL())); + } + + @Test + public void restoresProperUrlsWhenSomeAreMissing() { + // given + String accessToken = "access_token"; + String clientId = "some_client_id"; + String clientSecret = "some_client_secret"; + String refreshToken = "some_refresh_token"; + + String savedConnection = "{" + + "\"accessToken\":\"access_token\"," + + "\"refreshToken\":\"some_refresh_token\"," + + "\"lastRefresh\":0," + + "\"expires\":0," + + "\"userAgent\":\"Box Java SDK v3.8.0 (Java 1.8.0_345)\"," + + "\"baseURL\":\"https://api.box.com/\"," + + "\"authorizationURL\":\"https://account.box.com/api/\"," + + "\"autoRefresh\":true,\"maxRetryAttempts\":5" + + "}"; + // when + BoxAPIConnection restoredApi = BoxAPIConnection.restore(clientId, clientSecret, savedConnection); + + // then + assertThat("https://api.box.com/2.0/", is(restoredApi.getBaseURL())); + assertThat("https://upload.box.com/api/2.0/", is(restoredApi.getBaseUploadURL())); + } + + @Test + public void successfullyRestoresConnectionWithDeprecatedSettings() { String restoreState = TestUtils.getFixture("BoxAPIConnection/State"); String restoreStateDeprecated = TestUtils.getFixture("BoxAPIConnection/StateDeprecated");