From 5ab7703880bc43e881af475f525bf8ff53df616f Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Mon, 22 Jan 2024 10:07:42 +0100 Subject: [PATCH 1/2] fix RetryHandler class cast exception --- .../ConcurrentModificationTest.java | 66 +++++++++++++++++++ ...currentModificationDeleteRetryHandler.java | 5 +- .../ConcurrentModificationRetryHandler.java | 5 +- 3 files changed, 70 insertions(+), 6 deletions(-) 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..78d0caef6e6 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,39 @@ 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 ApiHttpResponse response = projectApiRoot.productTypes() + // .update(productType, + // builder -> builder.plus( + // actionBuilder -> actionBuilder.changeDescriptionBuilder().description("new description"))) + // .executeBlocking(); + + final ProductType deletedProductType = RetryHandler + .concurrentModification(projectApiRoot.productTypes().delete(productType)) + .executeBlocking() + .getBody(); + return deletedProductType; + }); + } + @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 From fa84a6e7d2c4264c1790b244db932ab5b6caaf43 Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Mon, 22 Jan 2024 10:10:59 +0100 Subject: [PATCH 2/2] add test for ConcurrentModification middleware --- .../ConcurrentModificationTest.java | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) 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 78d0caef6e6..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 @@ -164,12 +164,6 @@ public void concurrentModDeleteWithoutRetry() { ProductTypeFixtures.withUpdateableProductType(productType -> { - // final ApiHttpResponse response = projectApiRoot.productTypes() - // .update(productType, - // builder -> builder.plus( - // actionBuilder -> actionBuilder.changeDescriptionBuilder().description("new description"))) - // .executeBlocking(); - final ProductType deletedProductType = RetryHandler .concurrentModification(projectApiRoot.productTypes().delete(productType)) .executeBlocking() @@ -178,6 +172,33 @@ public void concurrentModDeleteWithoutRetry() { }); } + @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();