Skip to content

Commit

Permalink
Make syncer implementation compilable (#485)
Browse files Browse the repository at this point in the history
* Migrate abstract syncer, utils and tests (only compilable)

* Adjust javadoc

* Adjust signature of syncer subclasses and fix code

* Formatting

* Remove unused constant
  • Loading branch information
salander85 authored Oct 12, 2023
1 parent 7b3fd5c commit 060af6f
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 239 deletions.
25 changes: 10 additions & 15 deletions src/main/java/com/commercetools/project/sync/Syncer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.commercetools.api.models.PagedQueryResourceRequest;
import com.commercetools.api.models.ResourcePagedQueryResponse;
import com.commercetools.api.models.ResourceUpdateAction;
import com.commercetools.api.models.WithKey;
import com.commercetools.api.models.category.Category;
import com.commercetools.api.models.category.CategoryDraft;
import com.commercetools.api.models.common.BaseResource;
Expand All @@ -33,7 +32,6 @@
import javax.annotation.Nullable;
import net.logstash.logback.marker.Markers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// This class compiles but not tested yet
// TODO: Test class and adjust logic if needed
Expand All @@ -43,8 +41,8 @@
*
* @param <ResourceT> The type of the resource to update (e.g. {@link ProductProjection}, {@link
* Category}, etc..)
* @param <ResourceUpdateActionT> The update actions to update the resource with (e.g.
* {@link com.commercetools.api.models.product.ProductUpdateAction})
* @param <ResourceUpdateActionT> The update actions to update the resource with (e.g. {@link
* com.commercetools.api.models.product.ProductUpdateAction})
* @param <ResourceDraftT> The type of the resource draft (e.g. {@link ProductDraft}, {@link
* CategoryDraft}, etc..)
* @param <SyncStatisticsT> The type of the sync statistics resulting from the sync process (e.g.
Expand All @@ -61,7 +59,7 @@
* com.commercetools.sync.categories.CategorySync}, etc..)
*/
public abstract class Syncer<
ResourceT extends BaseResource & DomainResource<ResourceT> & WithKey,
ResourceT extends BaseResource & DomainResource<ResourceT>,
ResourceUpdateActionT extends ResourceUpdateAction<ResourceUpdateActionT>,
ResourceDraftT,
SyncStatisticsT extends BaseSyncStatistics,
Expand All @@ -71,8 +69,6 @@ public abstract class Syncer<
BaseSyncT extends
BaseSync<ResourceT, ResourceDraftT, ResourceUpdateActionT, SyncStatisticsT, SyncOptionsT>> {

private static final Logger LOGGER = LoggerFactory.getLogger(Syncer.class);

/* Using default Caffeine cache implementation from sync-java library for caching reference
* IdToKey values.
*/
Expand Down Expand Up @@ -112,11 +108,10 @@ public Syncer(
}

/**
* Fetches the sourceClient's project resources of type {@code ResourceT} with all needed references
* expanded and treats each page as a batch to the sync process. Then executes the sync process
* on every page sequentially. It then returns a completion stage
* containing a {@link Void} result after the execution of the sync process and logging the
* result.
* Fetches the sourceClient's project resources of type {@code ResourceT} with all needed
* references expanded and treats each page as a batch to the sync process. Then executes the sync
* process on every page sequentially. It then returns a completion stage containing a {@link
* Void} result after the execution of the sync process and logging the result.
*
* <p>Note: If {@param isFullSync} is {@code false}, i.e. a delta sync is required, the method
* checks if there was a last sync time stamp persisted as a custom object in the target project
Expand Down Expand Up @@ -266,9 +261,9 @@ private SyncStatisticsT syncPage(@Nonnull final List<ResourceT> page) {
}

/**
* Given a {@link List} representing a page of resources of type {@link ResourceT}, this method creates
* a list of drafts of type {@link ResourceDraftT} where reference ids of the references are
* replaced with keys and are ready for reference resolution by the sync process.
* Given a {@link List} representing a page of resources of type {@link ResourceT}, this method
* creates a list of drafts of type {@link ResourceDraftT} where reference ids of the references
* are replaced with keys and are ready for reference resolution by the sync process.
*
* @return a {@link CompletionStage} containing a list of drafts of type {@link ResourceDraftT}
* after being transformed from type {@link ResourceT}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import static com.commercetools.project.sync.util.SyncUtils.logWarningCallback;
import static com.commercetools.sync.cartdiscounts.utils.CartDiscountTransformUtils.toCartDiscountDrafts;

import com.commercetools.api.client.ByProjectKeyCartDiscountsGet;
import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.models.cart_discount.CartDiscount;
import com.commercetools.api.models.cart_discount.CartDiscountDraft;
import com.commercetools.api.models.cart_discount.CartDiscountPagedQueryResponse;
import com.commercetools.api.models.cart_discount.CartDiscountUpdateAction;
import com.commercetools.project.sync.Syncer;
import com.commercetools.project.sync.service.CustomObjectService;
import com.commercetools.project.sync.service.impl.CustomObjectServiceImpl;
Expand All @@ -14,11 +20,6 @@
import com.commercetools.sync.commons.exceptions.SyncException;
import com.commercetools.sync.commons.utils.QuadConsumer;
import com.commercetools.sync.commons.utils.TriConsumer;
import io.sphere.sdk.cartdiscounts.CartDiscount;
import io.sphere.sdk.cartdiscounts.CartDiscountDraft;
import io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery;
import io.sphere.sdk.client.SphereClient;
import io.sphere.sdk.commands.UpdateAction;
import java.time.Clock;
import java.util.List;
import java.util.Optional;
Expand All @@ -27,40 +28,42 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// This class compiles but not tested yet
// TODO: Test class and adjust logic if needed
public final class CartDiscountSyncer
extends Syncer<
CartDiscount,
CartDiscount,
CartDiscountUpdateAction,
CartDiscountDraft,
CartDiscount,
CartDiscountSyncStatistics,
CartDiscountSyncOptions,
CartDiscountQuery,
ByProjectKeyCartDiscountsGet,
CartDiscountPagedQueryResponse,
CartDiscountSync> {

private static final Logger LOGGER = LoggerFactory.getLogger(CartDiscountSyncer.class);

/** Instantiates a {@link Syncer} instance. */
private CartDiscountSyncer(
@Nonnull final CartDiscountSync cartDiscountSync,
@Nonnull final SphereClient sourceClient,
@Nonnull final SphereClient targetClient,
@Nonnull final ProjectApiRoot sourceClient,
@Nonnull final ProjectApiRoot targetClient,
@Nonnull final CustomObjectService customObjectService,
@Nonnull final Clock clock) {
super(cartDiscountSync, sourceClient, targetClient, customObjectService, clock);
}

@Nonnull
public static CartDiscountSyncer of(
@Nonnull final SphereClient sourceClient,
@Nonnull final SphereClient targetClient,
@Nonnull final ProjectApiRoot sourceClient,
@Nonnull final ProjectApiRoot targetClient,
@Nonnull final Clock clock) {

final QuadConsumer<
SyncException,
Optional<CartDiscountDraft>,
Optional<CartDiscount>,
List<UpdateAction<CartDiscount>>>
List<CartDiscountUpdateAction>>
logErrorCallback =
(exception, newResourceDraft, oldResource, updateActions) ->
logErrorCallback(LOGGER, "cart discount", exception, oldResource, updateActions);
Expand Down Expand Up @@ -91,8 +94,8 @@ protected CompletionStage<List<CartDiscountDraft>> transform(

@Nonnull
@Override
protected CartDiscountQuery getQuery() {
return CartDiscountQuery.of();
protected ByProjectKeyCartDiscountsGet getQuery() {
return getSourceClient().cartDiscounts().get();
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import static com.commercetools.project.sync.util.SyncUtils.logWarningCallback;
import static com.commercetools.sync.categories.utils.CategoryTransformUtils.toCategoryDrafts;

import com.commercetools.api.client.ByProjectKeyCategoriesGet;
import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.models.category.Category;
import com.commercetools.api.models.category.CategoryDraft;
import com.commercetools.api.models.category.CategoryPagedQueryResponse;
import com.commercetools.api.models.category.CategoryUpdateAction;
import com.commercetools.project.sync.Syncer;
import com.commercetools.project.sync.service.CustomObjectService;
import com.commercetools.project.sync.service.impl.CustomObjectServiceImpl;
Expand All @@ -14,11 +20,6 @@
import com.commercetools.sync.commons.exceptions.SyncException;
import com.commercetools.sync.commons.utils.QuadConsumer;
import com.commercetools.sync.commons.utils.TriConsumer;
import io.sphere.sdk.categories.Category;
import io.sphere.sdk.categories.CategoryDraft;
import io.sphere.sdk.categories.queries.CategoryQuery;
import io.sphere.sdk.client.SphereClient;
import io.sphere.sdk.commands.UpdateAction;
import java.time.Clock;
import java.util.List;
import java.util.Optional;
Expand All @@ -27,39 +28,38 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// This class compiles but not tested yet
// TODO: Test class and adjust logic if needed
public final class CategorySyncer
extends Syncer<
Category,
Category,
CategoryUpdateAction,
CategoryDraft,
Category,
CategorySyncStatistics,
CategorySyncOptions,
CategoryQuery,
ByProjectKeyCategoriesGet,
CategoryPagedQueryResponse,
CategorySync> {

private static final Logger LOGGER = LoggerFactory.getLogger(CategorySyncer.class);

/** Instantiates a {@link Syncer} instance. */
private CategorySyncer(
@Nonnull final CategorySync categorySync,
@Nonnull final SphereClient sourceClient,
@Nonnull final SphereClient targetClient,
@Nonnull final ProjectApiRoot sourceClient,
@Nonnull final ProjectApiRoot targetClient,
@Nonnull final CustomObjectService customObjectService,
@Nonnull final Clock clock) {
super(categorySync, sourceClient, targetClient, customObjectService, clock);
}

@Nonnull
public static CategorySyncer of(
@Nonnull final SphereClient sourceClient,
@Nonnull final SphereClient targetClient,
@Nonnull final ProjectApiRoot sourceClient,
@Nonnull final ProjectApiRoot targetClient,
@Nonnull final Clock clock) {
final QuadConsumer<
SyncException,
Optional<CategoryDraft>,
Optional<Category>,
List<UpdateAction<Category>>>
SyncException, Optional<CategoryDraft>, Optional<Category>, List<CategoryUpdateAction>>
logErrorCallback =
(exception, newResourceDraft, oldResource, updateActions) ->
logErrorCallback(LOGGER, "category", exception, oldResource, updateActions);
Expand Down Expand Up @@ -88,8 +88,8 @@ protected CompletionStage<List<CategoryDraft>> transform(@Nonnull final List<Cat

@Nonnull
@Override
protected CategoryQuery getQuery() {
return CategoryQuery.of();
protected ByProjectKeyCategoriesGet getQuery() {
return getSourceClient().categories().get();
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import static com.commercetools.project.sync.util.SyncUtils.logWarningCallback;
import static com.commercetools.sync.customers.utils.CustomerTransformUtils.toCustomerDrafts;

import com.commercetools.api.client.ByProjectKeyCustomersGet;
import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.models.customer.Customer;
import com.commercetools.api.models.customer.CustomerDraft;
import com.commercetools.api.models.customer.CustomerPagedQueryResponse;
import com.commercetools.api.models.customer.CustomerUpdateAction;
import com.commercetools.project.sync.Syncer;
import com.commercetools.project.sync.service.CustomObjectService;
import com.commercetools.project.sync.service.impl.CustomObjectServiceImpl;
Expand All @@ -14,11 +20,6 @@
import com.commercetools.sync.customers.CustomerSyncOptions;
import com.commercetools.sync.customers.CustomerSyncOptionsBuilder;
import com.commercetools.sync.customers.helpers.CustomerSyncStatistics;
import io.sphere.sdk.client.SphereClient;
import io.sphere.sdk.commands.UpdateAction;
import io.sphere.sdk.customers.Customer;
import io.sphere.sdk.customers.CustomerDraft;
import io.sphere.sdk.customers.queries.CustomerQuery;
import java.time.Clock;
import java.util.List;
import java.util.Optional;
Expand All @@ -27,37 +28,36 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// This class compiles but not tested yet
// TODO: Test class and adjust logic if needed
public final class CustomerSyncer
extends Syncer<
Customer,
Customer,
CustomerUpdateAction,
CustomerDraft,
Customer,
CustomerSyncStatistics,
CustomerSyncOptions,
CustomerQuery,
ByProjectKeyCustomersGet,
CustomerPagedQueryResponse,
CustomerSync> {

private static final Logger LOGGER = LoggerFactory.getLogger(CustomerSyncer.class);

private CustomerSyncer(
@Nonnull final CustomerSync customerSync,
@Nonnull final SphereClient sourceClient,
@Nonnull final SphereClient targetClient,
@Nonnull final ProjectApiRoot sourceClient,
@Nonnull final ProjectApiRoot targetClient,
@Nonnull final CustomObjectService customObjectService,
@Nonnull final Clock clock) {
super(customerSync, sourceClient, targetClient, customObjectService, clock);
}

public static CustomerSyncer of(
@Nonnull final SphereClient sourceClient,
@Nonnull final SphereClient targetClient,
@Nonnull final ProjectApiRoot sourceClient,
@Nonnull final ProjectApiRoot targetClient,
@Nonnull final Clock clock) {
final QuadConsumer<
SyncException,
Optional<CustomerDraft>,
Optional<Customer>,
List<UpdateAction<Customer>>>
SyncException, Optional<CustomerDraft>, Optional<Customer>, List<CustomerUpdateAction>>
logErrorCallback =
(exception, newResourceDraft, oldResource, updateActions) ->
logErrorCallback(LOGGER, "customer", exception, oldResource, updateActions);
Expand Down Expand Up @@ -86,8 +86,8 @@ protected CompletionStage<List<CustomerDraft>> transform(@Nonnull final List<Cus

@Nonnull
@Override
protected CustomerQuery getQuery() {
return CustomerQuery.of();
protected ByProjectKeyCustomersGet getQuery() {
return getSourceClient().customers().get();
}

@Nonnull
Expand Down
Loading

0 comments on commit 060af6f

Please sign in to comment.