diff --git a/commercetools/commercetools-sdk-java-api/src/integrationTest/java/commercetools/ConcurrentModificationTest.java b/commercetools/commercetools-sdk-java-api/src/integrationTest/java/commercetools/ConcurrentModificationTest.java index ddbe8027ca8..d81cff1386a 100644 --- a/commercetools/commercetools-sdk-java-api/src/integrationTest/java/commercetools/ConcurrentModificationTest.java +++ b/commercetools/commercetools-sdk-java-api/src/integrationTest/java/commercetools/ConcurrentModificationTest.java @@ -79,6 +79,39 @@ public void concurrentMod() { }); } + @Test + public void concurrentModWithoutRetry() { + ServiceRegion region = System.getenv("CTP_REGION") == null ? ServiceRegion.GCP_EUROPE_WEST1 + : ServiceRegion.valueOf(System.getenv("CTP_REGION")); + String authURL = System.getenv("CTP_AUTH_URL") == null ? region.getOAuthTokenUrl() + : System.getenv("CTP_AUTH_URL"); + String apiUrl = System.getenv("CTP_API_URL") == null ? region.getApiUrl() : System.getenv("CTP_API_URL"); + + final ProjectApiRoot projectApiRoot = ApiRootBuilder.of() + .defaultClient(ClientCredentials.of() + .withClientId(CommercetoolsTestUtils.getClientId()) + .withClientSecret(CommercetoolsTestUtils.getClientSecret()) + .build(), + authURL, apiUrl) + .withErrorMiddleware(ErrorMiddleware.ExceptionMode.UNWRAP_COMPLETION_EXCEPTION) + .build(CommercetoolsTestUtils.getProjectKey()); + + CartsFixtures.withUpdateableCart(cart -> { + + final Cart modCart = RetryHandler + .concurrentModification(projectApiRoot.carts() + .withId(cart.getId()) + .post(CartUpdateBuilder.of() + .version(cart.getVersion()) + .actions(CartSetCountryActionBuilder.of().country("DE").build()) + .build()), + CartUpdate::builder, CartUpdateBuilder::version) + .executeBlocking() + .getBody(); + return modCart; + }); + } + @Test public void concurrentModDelete() { ServiceRegion region = System.getenv("CTP_REGION") == null ? ServiceRegion.GCP_EUROPE_WEST1 @@ -112,6 +145,60 @@ public void concurrentModDelete() { }); } + @Test + public void concurrentModDeleteWithoutRetry() { + ServiceRegion region = System.getenv("CTP_REGION") == null ? ServiceRegion.GCP_EUROPE_WEST1 + : ServiceRegion.valueOf(System.getenv("CTP_REGION")); + String authURL = System.getenv("CTP_AUTH_URL") == null ? region.getOAuthTokenUrl() + : System.getenv("CTP_AUTH_URL"); + String apiUrl = System.getenv("CTP_API_URL") == null ? region.getApiUrl() : System.getenv("CTP_API_URL"); + + final ProjectApiRoot projectApiRoot = ApiRootBuilder.of() + .defaultClient(ClientCredentials.of() + .withClientId(CommercetoolsTestUtils.getClientId()) + .withClientSecret(CommercetoolsTestUtils.getClientSecret()) + .build(), + authURL, apiUrl) + .withErrorMiddleware(ErrorMiddleware.ExceptionMode.UNWRAP_COMPLETION_EXCEPTION) + .build(CommercetoolsTestUtils.getProjectKey()); + + ProductTypeFixtures.withUpdateableProductType(productType -> { + + final ProductType deletedProductType = RetryHandler + .concurrentModification(projectApiRoot.productTypes().delete(productType)) + .executeBlocking() + .getBody(); + return deletedProductType; + }); + } + + @Test + public void concurrentModMiddlewareSuccess() { + String projectKey = CommercetoolsTestUtils.getProjectKey(); + + ProjectApiRoot projectApiRoot = ApiRootBuilder.of() + .defaultClient(ClientCredentials.of() + .withClientId(CommercetoolsTestUtils.getClientId()) + .withClientSecret(CommercetoolsTestUtils.getClientSecret()) + .build(), + ServiceRegion.GCP_EUROPE_WEST1) + .addConcurrentModificationMiddleware(3) + .build(projectKey); + + CartsFixtures.withUpdateableCart(cart -> { + + final Cart modCart = projectApiRoot.carts() + .withId(cart.getId()) + .post(CartUpdateBuilder.of() + .version(cart.getVersion()) + .actions(CartSetCountryActionBuilder.of().country("DE").build()) + .build()) + .executeBlocking() + .getBody(); + return modCart; + }); + } + @Test public void concurrentModMiddleware() { String projectKey = CommercetoolsTestUtils.getProjectKey(); diff --git a/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/client/ConcurrentModificationDeleteRetryHandler.java b/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/client/ConcurrentModificationDeleteRetryHandler.java index 5d6ffc7dc3e..d6dea4ba6e3 100644 --- a/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/client/ConcurrentModificationDeleteRetryHandler.java +++ b/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/client/ConcurrentModificationDeleteRetryHandler.java @@ -10,6 +10,7 @@ import java.util.function.Function; import com.commercetools.api.client.error.ConcurrentModificationException; +import com.spotify.futures.CompletableFutures; import io.vrap.rmf.base.client.*; @@ -41,9 +42,7 @@ public CompletableFuture> execute() { return f; }; - return request.execute() - .handle((r, ex) -> (ex == null) ? this : fn.apply(ex)) - .thenCompose(x -> (CompletableFuture>) x); + return CompletableFutures.exceptionallyCompose(request.execute(), fn).toCompletableFuture(); } @Override diff --git a/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/client/ConcurrentModificationRetryHandler.java b/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/client/ConcurrentModificationRetryHandler.java index ac1994052c1..def0b120e1f 100644 --- a/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/client/ConcurrentModificationRetryHandler.java +++ b/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/client/ConcurrentModificationRetryHandler.java @@ -11,6 +11,7 @@ import com.commercetools.api.client.error.ConcurrentModificationException; import com.commercetools.api.models.ResourceUpdate; +import com.spotify.futures.CompletableFutures; import io.vrap.rmf.base.client.*; @@ -45,9 +46,7 @@ public CompletableFuture> execute() { return f; }; - return request.execute() - .handle((r, ex) -> (ex == null) ? this : fn.apply(ex)) - .thenCompose(x -> (CompletableFuture>) x); + return CompletableFutures.exceptionallyCompose(request.execute(), fn).toCompletableFuture(); } @Override