diff --git a/src/integration-test/java/com/commercetools/project/sync/CliRunnerIT.java b/src/integration-test/java/com/commercetools/project/sync/CliRunnerIT.java index 95800826..722580b1 100644 --- a/src/integration-test/java/com/commercetools/project/sync/CliRunnerIT.java +++ b/src/integration-test/java/com/commercetools/project/sync/CliRunnerIT.java @@ -92,13 +92,17 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.vrap.rmf.base.client.ApiHttpResponse; import io.vrap.rmf.base.client.utils.json.JsonUtils; + +import java.time.Duration; import java.time.ZonedDateTime; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; import javax.annotation.Nonnull; import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import uk.org.lidalia.slf4jext.Level; @@ -359,12 +363,14 @@ static void tearDownSuite() { @Test void run_WithSyncAsArgumentWithAllArg_ShouldExecuteAllSyncers() { // test + assertEventually(Duration.ofSeconds(5), Duration.ofSeconds(5), () ->{ CliRunner.of().run(new String[] {"-s", "all"}, createITSyncerFactory()); + // assertions + assertAllSyncersLoggingEvents(1); + assertAllResourcesAreSyncedToTarget(CTP_TARGET_CLIENT); + } + ); - // assertions - assertAllSyncersLoggingEvents(1); - - assertAllResourcesAreSyncedToTarget(CTP_TARGET_CLIENT); } @Test @@ -552,6 +558,7 @@ void run_WithUpdatedCustomer_ShouldSyncCustomersAndStoreLastSyncTimestampsAsCust // test CliRunner.of().run(new String[] {"-s", "customers"}, syncerFactory); + // assertions assertUpdatedCustomersAreSyncedCorrectly(CTP_SOURCE_CLIENT, CTP_TARGET_CLIENT); assertUpdatedCustomObjectTimestampAfterSync( @@ -579,7 +586,7 @@ private ZonedDateTime getCustomObjectLastModifiedTime( final CustomObjectPagedQueryResponse lastSyncResult = fetchLastSyncCustomObject(targetClient, syncModuleName, runnerName); - return lastSyncResult.getResults().get(0).getLastModifiedAt(); + return lastSyncResult.getResults().isEmpty() ? null : lastSyncResult.getResults().get(0).getLastModifiedAt(); } private void updateCustomerSourceObject(@Nonnull final ProjectApiRoot sourceProjectClient) { @@ -588,10 +595,8 @@ private void updateCustomerSourceObject(@Nonnull final ProjectApiRoot sourceProj .customers() .withKey(RESOURCE_KEY) .get() - .execute() - .thenApply(ApiHttpResponse::getBody) - .toCompletableFuture() - .join(); + .executeBlocking() + .getBody(); sourceProjectClient .customers() @@ -603,9 +608,7 @@ private void updateCustomerSourceObject(@Nonnull final ProjectApiRoot sourceProj CustomerSetFirstNameActionBuilder.of().firstName("testFirstName").build(), CustomerSetLastNameActionBuilder.of().lastName("testLastName").build()) .version(customer.getVersion())) - .execute() - .toCompletableFuture() - .join(); + .executeBlocking(); } private void assertUpdatedCustomersAreSyncedCorrectly( @@ -661,6 +664,25 @@ private void assertUpdatedCustomersAreSyncedCorrectly( } private void prepareDataForShoppingListSync(ProjectApiRoot sourceClient) { + QueryUtils.queryAll( + sourceClient.shoppingLists().get(), + shoppingLists -> { + CompletableFuture.allOf( + shoppingLists.stream() + .map( + shoppingList -> + sourceClient + .shoppingLists() + .delete(shoppingList) + .execute() + .thenApply(ApiHttpResponse::getBody)) + .map(CompletionStage::toCompletableFuture) + .toArray(CompletableFuture[]::new)) + .join(); + }) + .toCompletableFuture() + .join(); + final ShoppingListDraft shoppingListDraft = ShoppingListDraftBuilder.of() .name(ofEnglish("shoppingList-name")) @@ -1100,6 +1122,8 @@ private static void assertAllResourcesAreSyncedToTarget( assertShoppingListsAreSyncedCorrectly(targetClient); } + } + public static void assertAllSyncersLoggingEvents(final int numberOfResources) { assertThat(cliRunnerTestLogger.getAllLoggingEvents()) diff --git a/src/main/java/com/commercetools/project/sync/Syncer.java b/src/main/java/com/commercetools/project/sync/Syncer.java index 68385170..1bcd17a7 100644 --- a/src/main/java/com/commercetools/project/sync/Syncer.java +++ b/src/main/java/com/commercetools/project/sync/Syncer.java @@ -33,8 +33,6 @@ import net.logstash.logback.marker.Markers; import org.slf4j.Logger; -// This class compiles but not tested yet -// TODO: Test class and adjust logic if needed /** * Base class of the syncer that handles syncing a resource from a source CTP project to a target * CTP project. @@ -144,7 +142,7 @@ public CompletionStage sync(@Nullable final String runnerName, final boole syncStage = customObjectService .getCurrentCtpTimestamp(runnerName, syncModuleName) - .thenCompose( + .thenAccept( currentCtpTimestamp -> syncResourcesSinceLastSync( sourceProjectKey, syncModuleName, runnerName, currentCtpTimestamp)); @@ -207,11 +205,11 @@ private CompletionStage getQueryOfResourcesSinceLastSync( private PagedQueryT getQueryWithTimeBoundedPredicate( @Nonnull final ZonedDateTime lowerBound, @Nonnull final ZonedDateTime upperBound) { + final String queryPredicate = format( + "lastModifiedAt >= \"%s\" AND lastModifiedAt <= \"%s\"", lowerBound, upperBound); return (PagedQueryT) getQuery() - .withWhere("lastModifiedAt >= \":lower\" AND lastModifiedAt <= \":upper\"") - .withPredicateVar("lower", lowerBound) - .withPredicateVar("upper", upperBound); + .withWhere(queryPredicate); } @Nonnull