Skip to content

Commit

Permalink
fix: Restoring state from previous SDK version works. (#1134)
Browse files Browse the repository at this point in the history
  • Loading branch information
antusus authored Dec 19, 2022
1 parent 1067e89 commit b6d97dd
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
26 changes: 24 additions & 2 deletions src/main/java/com/box/sdk/BoxAPIConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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())
Expand Down
95 changes: 93 additions & 2 deletions src/test/java/com/box/sdk/BoxAPIConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");

Expand Down

0 comments on commit b6d97dd

Please sign in to comment.