Skip to content

Commit b6d97dd

Browse files
authored
fix: Restoring state from previous SDK version works. (#1134)
1 parent 1067e89 commit b6d97dd

File tree

2 files changed

+117
-4
lines changed

2 files changed

+117
-4
lines changed

src/main/java/com/box/sdk/BoxAPIConnection.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,10 @@ public void restore(String state) {
752752
String userAgent = json.get("userAgent").asString();
753753
String tokenURL = getKeyValueOrDefault(json, "tokenURL", null);
754754
String revokeURL = getKeyValueOrDefault(json, "revokeURL", null);
755-
String baseURL = json.get("baseURL").asString();
756-
String baseUploadURL = json.get("baseUploadURL").asString();
755+
String baseURL = adoptBaseUrlWhenLoadingFromOldVersion(getKeyValueOrDefault(json, "baseURL", DEFAULT_BASE_URL));
756+
String baseUploadURL = adoptUploadBaseUrlWhenLoadingFromOldVersion(
757+
getKeyValueOrDefault(json, "baseUploadURL", DEFAULT_BASE_UPLOAD_URL)
758+
);
757759
String authorizationURL = getKeyValueOrDefault(json, "authorizationURL", DEFAULT_BASE_AUTHORIZATION_URL);
758760
boolean autoRefresh = json.get("autoRefresh").asBoolean();
759761

@@ -789,6 +791,26 @@ public void restore(String state) {
789791

790792
}
791793

794+
private String adoptBaseUrlWhenLoadingFromOldVersion(String url) {
795+
if (url == null) {
796+
return null;
797+
}
798+
String urlEndingWithSlash = fixBaseUrl(url);
799+
return urlEndingWithSlash.equals("https://api.box.com/2.0/")
800+
? DEFAULT_BASE_URL
801+
: urlEndingWithSlash;
802+
}
803+
804+
private String adoptUploadBaseUrlWhenLoadingFromOldVersion(String url) {
805+
if (url == null) {
806+
return null;
807+
}
808+
String urlEndingWithSlash = fixBaseUrl(url);
809+
return urlEndingWithSlash.equals("https://upload.box.com/api/2.0/")
810+
? DEFAULT_BASE_UPLOAD_URL
811+
: urlEndingWithSlash;
812+
}
813+
792814
protected String getKeyValueOrDefault(JsonObject json, String key, String defaultValue) {
793815
return Optional.ofNullable(json.get(key))
794816
.filter(js -> !js.isNull())

src/test/java/com/box/sdk/BoxAPIConnectionTest.java

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.eclipsesource.json.JsonObject;
2424
import com.github.tomakehurst.wiremock.client.WireMock;
2525
import com.github.tomakehurst.wiremock.junit.WireMockRule;
26-
import java.io.IOException;
2726
import java.net.MalformedURLException;
2827
import java.net.URI;
2928
import java.net.URISyntaxException;
@@ -499,7 +498,99 @@ public void allowsToSaveAndRestoreApplicationConnection() throws URISyntaxExcept
499498
}
500499

501500
@Test
502-
public void successfullyRestoresConnectionWithDeprecatedSettings() throws IOException {
501+
public void restoresProperUrlsFromOldVersions() {
502+
// given
503+
String accessToken = "access_token";
504+
String clientId = "some_client_id";
505+
String clientSecret = "some_client_secret";
506+
String refreshToken = "some_refresh_token";
507+
BoxAPIConnection api = new BoxAPIConnection(clientId, clientSecret, accessToken, refreshToken);
508+
api.setBaseURL("https://api.box.com/2.0/");
509+
api.setBaseUploadURL("https://upload.box.com/api/2.0/");
510+
String savedConnection = api.save();
511+
512+
// when
513+
BoxAPIConnection restoredApi = BoxAPIConnection.restore(clientId, clientSecret, savedConnection);
514+
515+
// then
516+
assertThat("https://api.box.com/2.0/", is(restoredApi.getBaseURL()));
517+
assertThat("https://upload.box.com/api/2.0/", is(restoredApi.getBaseUploadURL()));
518+
}
519+
520+
@Test
521+
public void restoresProperUrlsWhenNotSet() {
522+
// given
523+
String accessToken = "access_token";
524+
String clientId = "some_client_id";
525+
String clientSecret = "some_client_secret";
526+
String refreshToken = "some_refresh_token";
527+
BoxAPIConnection api = new BoxAPIConnection(clientId, clientSecret, accessToken, refreshToken);
528+
api.setBaseURL("https://api.box.com/2.0/");
529+
String savedConnection = api.save();
530+
531+
// when
532+
BoxAPIConnection restoredApi = BoxAPIConnection.restore(clientId, clientSecret, savedConnection);
533+
534+
// then
535+
assertThat("https://api.box.com/2.0/", is(restoredApi.getBaseURL()));
536+
assertThat("https://upload.box.com/api/2.0/", is(restoredApi.getBaseUploadURL()));
537+
}
538+
539+
@Test
540+
public void restoresProperUrlsWhenDeprecatedUrlsAreSet() {
541+
// given
542+
String clientId = "some_client_id";
543+
String clientSecret = "some_client_secret";
544+
545+
String savedConnection = "{"
546+
+ "\"accessToken\":\"access_token\","
547+
+ "\"refreshToken\":\"some_refresh_token\","
548+
+ "\"lastRefresh\":0,"
549+
+ "\"expires\":0,"
550+
+ "\"userAgent\":\"Box Java SDK v3.8.0 (Java 1.8.0_345)\","
551+
+ "\"tokenURL\":\"https://api.box.com/token\","
552+
+ "\"revokeURL\":\"https://api.box.com/revoke\","
553+
+ "\"baseURL\":\"https://api.box.com/\","
554+
+ "\"baseUploadURL\":\"https://upload.box.com/api/\","
555+
+ "\"authorizationURL\":\"https://account.box.com/api/\","
556+
+ "\"autoRefresh\":true,\"maxRetryAttempts\":5"
557+
+ "}";
558+
// when
559+
BoxAPIConnection restoredApi = BoxAPIConnection.restore(clientId, clientSecret, savedConnection);
560+
561+
// then
562+
assertThat("https://api.box.com/token", is(restoredApi.getTokenURL()));
563+
assertThat("https://api.box.com/revoke", is(restoredApi.getRevokeURL()));
564+
}
565+
566+
@Test
567+
public void restoresProperUrlsWhenSomeAreMissing() {
568+
// given
569+
String accessToken = "access_token";
570+
String clientId = "some_client_id";
571+
String clientSecret = "some_client_secret";
572+
String refreshToken = "some_refresh_token";
573+
574+
String savedConnection = "{"
575+
+ "\"accessToken\":\"access_token\","
576+
+ "\"refreshToken\":\"some_refresh_token\","
577+
+ "\"lastRefresh\":0,"
578+
+ "\"expires\":0,"
579+
+ "\"userAgent\":\"Box Java SDK v3.8.0 (Java 1.8.0_345)\","
580+
+ "\"baseURL\":\"https://api.box.com/\","
581+
+ "\"authorizationURL\":\"https://account.box.com/api/\","
582+
+ "\"autoRefresh\":true,\"maxRetryAttempts\":5"
583+
+ "}";
584+
// when
585+
BoxAPIConnection restoredApi = BoxAPIConnection.restore(clientId, clientSecret, savedConnection);
586+
587+
// then
588+
assertThat("https://api.box.com/2.0/", is(restoredApi.getBaseURL()));
589+
assertThat("https://upload.box.com/api/2.0/", is(restoredApi.getBaseUploadURL()));
590+
}
591+
592+
@Test
593+
public void successfullyRestoresConnectionWithDeprecatedSettings() {
503594
String restoreState = TestUtils.getFixture("BoxAPIConnection/State");
504595
String restoreStateDeprecated = TestUtils.getFixture("BoxAPIConnection/StateDeprecated");
505596

0 commit comments

Comments
 (0)