Skip to content

Commit

Permalink
Merge pull request #2189 from commercetools/project_suspended_test
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude authored Nov 12, 2021
2 parents 9c77fad + 97ccba0 commit 44af181
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ private List<RetryRule> createRules() {
final boolean unauthorized = latestError instanceof UnauthorizedException;
return unknownHost || unauthorized;
};
final Predicate<RetryContext> isSuspended = r -> {
final Throwable latestError = r.getLatestError();
return latestError instanceof SuspendedProjectException;
};
final RetryRule fatalRetryRule = RetryRule.of(isFatal, RetryAction.ofShutdownServiceAndSendLatestException());
final RetryRule retryScheduledRetryRule = RetryRule.of(RetryPredicate.ofAlwaysTrue(), RetryAction.ofScheduledRetry(2, retryContext -> Duration.ofMillis(retryContext.getAttempt() * retryContext.getAttempt() * 50)));
return asList(fatalRetryRule, retryScheduledRetryRule);
final RetryRule suspendedRetryRule = RetryRule.of(isSuspended, RetryAction.ofExponentialBackoff(100, 1000, 60000));
return asList(suspendedRetryRule, fatalRetryRule, retryScheduledRetryRule);
}

private CompletionStage<Tokens> supervisedTokenSupplier(final Supplier<CompletionStage<Tokens>> supplier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ private Tokens parseResponse(final HttpResponse httpResponse, final HttpRequest
exception = new InvalidClientCredentialsException(config);
}
if (error.equals("invalid_scope")) {
exception = new InvalidScopeException(exception);
final String description = jsonNode.get("error_description").asText();
if (description.endsWith("suspended")) {
exception = new SuspendedProjectException(exception);
} else {
exception = new InvalidScopeException(exception);
}
}
} catch (final JsonException e) {
exception = new UnauthorizedException(httpResponse.toString(), e,httpResponse.getStatusCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.sphere.sdk.http.HttpResponse;
import io.sphere.sdk.models.Base;
import org.junit.Test;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
Expand All @@ -31,6 +32,21 @@ public void selectNextRetryTime() {
assertThat(AuthActor.selectNextRetryTime(-100L)).as("at least a second").isEqualTo(1L);
}

@Test
public void testSuspendedClient() throws Exception {
final SuspendableDoubleHttpClient httpClient = getSuspendableHttpClient();

try(final SphereAccessTokenSupplier supplier =
AutoRefreshSphereAccessTokenSupplierImpl.createAndBeginRefreshInBackground(SphereAuthConfig.of("project-key", "client-id", "clientSecret"), httpClient, true)) {
Thread.sleep(2000);
httpClient.suspended = true;
Thread.sleep(120000);
LoggerFactory.getLogger(AutoRefreshSphereAccessTokenSupplierImplTest.class).debug("Project unsuspended");
httpClient.suspended = false;
Thread.sleep(10000);
}
}

private TestDoubleHttpClient getHttpClient() {
return new TestDoubleHttpClient() {
@Override
Expand All @@ -40,6 +56,31 @@ protected HttpResponse executeSync(final HttpRequest httpRequest, final int requ
};
}

private SuspendableDoubleHttpClient getSuspendableHttpClient() {
return new SuspendableDoubleHttpClient() {
@Override
protected HttpResponse executeSync(final HttpRequest httpRequest, final int requestId) {
if (suspended) {
return HttpResponse.of(400, "{\n" +
" \"statusCode\" : 400,\n" +
" \"message\" : \"Project 'test-project-suspension-1' is suspended\",\n" +
" \"errors\" : [ {\n" +
" \"code\" : \"invalid_scope\",\n" +
" \"message\" : \"Project 'test-project-suspension-1' is suspended\"\n" +
" } ],\n" +
" \"error\" : \"invalid_scope\",\n" +
" \"error_description\" : \"Project 'test-project-suspension-1' is suspended\"\n" +
" }");
}
return HttpResponse.of(200, "{\"access_token\": \"vkFuQ6oTwj8_Ye4eiRSsqMeqLYNeQRJi\", \"expires_in\": 1}");
}
};
}

public static abstract class SuspendableDoubleHttpClient extends TestDoubleHttpClient {
public boolean suspended = false;
}

public static abstract class TestDoubleHttpClient extends Base implements HttpClient {
private final AtomicInteger timesCalled = new AtomicInteger(0);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.sphere.sdk.client;

/**
* when trying to make a call to commercetools with suspended project
*
*/
public class SuspendedProjectException extends UnauthorizedException{
private static final long serialVersionUID = 0L;

public SuspendedProjectException(final Throwable cause) {
super("Project suspended",cause,400);
}
}

0 comments on commit 44af181

Please sign in to comment.