Skip to content

Commit

Permalink
Fix query predicate in syncer and adjust CLIRunnerIT
Browse files Browse the repository at this point in the history
  • Loading branch information
salander85 committed Nov 2, 2023
1 parent e2eff94 commit 3eb884f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
Expand All @@ -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(
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -1100,6 +1122,8 @@ private static void assertAllResourcesAreSyncedToTarget(
assertShoppingListsAreSyncedCorrectly(targetClient);
}

}

public static void assertAllSyncersLoggingEvents(final int numberOfResources) {

assertThat(cliRunnerTestLogger.getAllLoggingEvents())
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/commercetools/project/sync/Syncer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -144,7 +142,7 @@ public CompletionStage<Void> sync(@Nullable final String runnerName, final boole
syncStage =
customObjectService
.getCurrentCtpTimestamp(runnerName, syncModuleName)
.thenCompose(
.thenAccept(
currentCtpTimestamp ->
syncResourcesSinceLastSync(
sourceProjectKey, syncModuleName, runnerName, currentCtpTimestamp));
Expand Down Expand Up @@ -207,11 +205,11 @@ private CompletionStage<PagedQueryT> 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
Expand Down

0 comments on commit 3eb884f

Please sign in to comment.