Skip to content

Commit

Permalink
Add class type to sync statistics (#1105)
Browse files Browse the repository at this point in the history
  • Loading branch information
lojzatran authored Nov 6, 2023
1 parent d0c2fac commit 8f922fb
Show file tree
Hide file tree
Showing 24 changed files with 168 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import com.commercetools.sync.commons.helpers.BaseSyncStatistics;

public final class CartDiscountSyncStatistics extends BaseSyncStatistics {
public final class CartDiscountSyncStatistics
extends BaseSyncStatistics<CartDiscountSyncStatistics> {

/**
* Builds a summary of the cart discount sync statistics instance that looks like the following
Expand All @@ -17,4 +18,9 @@ public final class CartDiscountSyncStatistics extends BaseSyncStatistics {
public String getReportMessage() {
return getDefaultReportMessageForResource("cart discounts");
}

@Override
protected CartDiscountSyncStatistics getThis() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class CategorySyncStatistics extends BaseSyncStatistics {
public class CategorySyncStatistics extends BaseSyncStatistics<CategorySyncStatistics> {

/**
* The following {@link java.util.Map} ({@code categoryKeysWithMissingParents}) represents
Expand Down Expand Up @@ -52,6 +52,11 @@ public String getReportMessage() {
getNumberOfCategoriesWithMissingParents());
}

@Override
protected CategorySyncStatistics getThis() {
return this;
}

/**
* Returns the total number of categories with missing parents.
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/commercetools/sync/commons/BaseSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public abstract class BaseSync<
ResourceT extends BaseResource,
ResourceDraftT,
ResourceUpdateActionT extends ResourceUpdateAction<ResourceUpdateActionT>,
SyncStatisticsT extends BaseSyncStatistics,
SyncOptionsT extends BaseSyncOptions> {
SyncStatisticsT extends BaseSyncStatistics<SyncStatisticsT>,
SyncOptionsT extends BaseSyncOptions<ResourceT, ResourceDraftT, ResourceUpdateActionT>> {
protected final SyncStatisticsT statistics;
protected final SyncOptionsT syncOptions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nonnull;

public abstract class BaseSyncStatistics {
public abstract class BaseSyncStatistics<SyncStatisticsT extends BaseSyncStatistics> {
private AtomicInteger updated;
private AtomicInteger created;
private AtomicInteger failed;
Expand Down Expand Up @@ -280,6 +280,29 @@ public long getLatestBatchProcessingTimeInMillis() {
*/
public abstract String getReportMessage();

/**
* Gets the name of the implementation class extending this abstract class. This name is then
* serialized into JSON and stored in the custom object of the last sync statistics. Later we
* could use this name to deserialize the custom object into the correct implementation class.
*
* @return name of the implementation class extending this abstract class.
*/
public String getSyncStatisticsClassName() {
return getThis().getClass().getName();
}

/**
* Returns {@code this} instance of {@code T}, which extends {@link
* com.commercetools.sync.commons.helpers.BaseSyncStatistics}. The purpose of this method is to
* make sure that {@code this} is an instance of a class which extends {@link
* com.commercetools.sync.commons.helpers.BaseSyncStatistics} in order to be used in the generic
* methods of the class. Otherwise, without this method, the methods above would need to cast
* {@code this to T} which could lead to a runtime error of the class was extended in a wrong way.
*
* @return an instance of the class that overrides this method.
*/
protected abstract SyncStatisticsT getThis();

/**
* Builds a proper summary message of the statistics report of a given {@code resourceString} in
* following format:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.commercetools.sync.commons.helpers.BaseSyncStatistics;

public class CustomerSyncStatistics extends BaseSyncStatistics {
public class CustomerSyncStatistics extends BaseSyncStatistics<CustomerSyncStatistics> {

/**
* Builds a summary of the customer sync statistics instance that looks like the following
Expand All @@ -17,4 +17,9 @@ public class CustomerSyncStatistics extends BaseSyncStatistics {
public String getReportMessage() {
return getDefaultReportMessageForResource("customers");
}

@Override
protected CustomerSyncStatistics getThis() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.commercetools.sync.commons.helpers.BaseSyncStatistics;

public class CustomObjectSyncStatistics extends BaseSyncStatistics {
public class CustomObjectSyncStatistics extends BaseSyncStatistics<CustomObjectSyncStatistics> {
/**
* Builds a summary of the custom object sync statistics instance that looks like the following
* example:
Expand All @@ -16,4 +16,9 @@ public class CustomObjectSyncStatistics extends BaseSyncStatistics {
public String getReportMessage() {
return getDefaultReportMessageForResource("custom objects");
}

@Override
protected CustomObjectSyncStatistics getThis() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.commercetools.sync.commons.helpers.BaseSyncStatistics;

public class InventorySyncStatistics extends BaseSyncStatistics {
public class InventorySyncStatistics extends BaseSyncStatistics<InventorySyncStatistics> {

public InventorySyncStatistics() {
super();
Expand All @@ -22,4 +22,9 @@ public InventorySyncStatistics() {
public String getReportMessage() {
return getDefaultReportMessageForResource("inventory entries");
}

@Override
protected InventorySyncStatistics getThis() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class ProductSyncStatistics extends BaseSyncStatistics {
public class ProductSyncStatistics extends BaseSyncStatistics<ProductSyncStatistics> {

/**
* The following {@link Map} ({@code productKeysWithMissingParents}) represents products with
Expand Down Expand Up @@ -47,6 +47,11 @@ public String getReportMessage() {
getNumberOfProductsWithMissingParents());
}

@Override
protected ProductSyncStatistics getThis() {
return this;
}

/**
* Returns the total number of products with missing parents.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;

public class ProductTypeSyncStatistics extends BaseSyncStatistics {
public class ProductTypeSyncStatistics extends BaseSyncStatistics<ProductTypeSyncStatistics> {

/**
* The following {@link java.util.concurrent.ConcurrentHashMap} ({@code
Expand Down Expand Up @@ -82,6 +82,11 @@ public String getReportMessage() {
getNumberOfProductTypesWithMissingNestedProductTypes());
}

@Override
protected ProductTypeSyncStatistics getThis() {
return this;
}

/**
* Returns the total number of product types with at least one NestedType or a Set of NestedType
* attribute definition(s) referencing a missing productType.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import com.commercetools.sync.commons.helpers.BaseSyncStatistics;

public final class ShoppingListSyncStatistics extends BaseSyncStatistics {
public final class ShoppingListSyncStatistics
extends BaseSyncStatistics<ShoppingListSyncStatistics> {

/**
* Builds a summary of the shopping list sync statistics instance that looks like the following
Expand All @@ -17,4 +18,9 @@ public final class ShoppingListSyncStatistics extends BaseSyncStatistics {
public String getReportMessage() {
return getDefaultReportMessageForResource("shopping lists");
}

@Override
protected ShoppingListSyncStatistics getThis() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class StateSyncStatistics extends BaseSyncStatistics {
public class StateSyncStatistics extends BaseSyncStatistics<StateSyncStatistics> {

/**
* The following {@link java.util.Map} ({@code stateKeysWithMissingParents}) represents products
Expand Down Expand Up @@ -48,6 +48,11 @@ public String getReportMessage() {
getNumberOfStatesWithMissingParents());
}

@Override
protected StateSyncStatistics getThis() {
return this;
}

/**
* Returns the total number of states with missing parents.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Tax category sync statistics. Keeps track of processed, created, updated and failed states
* through whole sync process.
*/
public final class TaxCategorySyncStatistics extends BaseSyncStatistics {
public final class TaxCategorySyncStatistics extends BaseSyncStatistics<TaxCategorySyncStatistics> {

/**
* Builds a summary of the tax category sync statistics instance that looks like the following
Expand All @@ -21,4 +21,9 @@ public final class TaxCategorySyncStatistics extends BaseSyncStatistics {
public String getReportMessage() {
return getDefaultReportMessageForResource("tax categories");
}

@Override
protected TaxCategorySyncStatistics getThis() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.commercetools.sync.commons.helpers.BaseSyncStatistics;

public class TypeSyncStatistics extends BaseSyncStatistics {
public class TypeSyncStatistics extends BaseSyncStatistics<TypeSyncStatistics> {
/**
* Builds a summary of the type sync statistics instance that looks like the following example:
*
Expand All @@ -14,4 +14,9 @@ public class TypeSyncStatistics extends BaseSyncStatistics {
public String getReportMessage() {
return getDefaultReportMessageForResource("types");
}

@Override
protected TypeSyncStatistics getThis() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ void getReportMessage_WithIncrementedStats_ShouldGetCorrectMessage() {
"Summary: 6 cart discounts were processed in total "
+ "(1 created, 3 updated and 2 failed to sync).");
}

@Test
void getSyncStatisticsClassName_ShouldReturnCorrectClassName() {
assertThat(cartDiscountSyncStatistics.getSyncStatisticsClassName())
.isEqualTo("com.commercetools.sync.cartdiscounts.helpers.CartDiscountSyncStatistics");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,10 @@ void removeChildCategoryKeyFromMissingParentsMap_WithExistingKey_ShouldRemovePar
// assert
assertThat(categorySyncStatistics.getNumberOfCategoriesWithMissingParents()).isEqualTo(0);
}

@Test
void getSyncStatisticsClassName_ShouldReturnCorrectClassName() {
assertThat(categorySyncStatistics.getSyncStatisticsClassName())
.isEqualTo("com.commercetools.sync.categories.helpers.CategorySyncStatistics");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ void getReportMessage_WithIncrementedStats_ShouldGetCorrectMessage() {
"Summary: 6 customers were processed in total "
+ "(1 created, 3 updated and 2 failed to sync).");
}

@Test
void getSyncStatisticsClassName_ShouldReturnCorrectClassName() {
assertThat(customerSyncStatistics.getSyncStatisticsClassName())
.isEqualTo("com.commercetools.sync.customers.helpers.CustomerSyncStatistics");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ void getReportMessage_WithIncrementedStats_ShouldGetCorrectMessage() {
"Summary: 6 custom objects were processed in total "
+ "(1 created, 3 updated and 2 failed to sync).");
}

@Test
void getSyncStatisticsClassName_ShouldReturnCorrectClassName() {
assertThat(customObjectSyncStatistics.getSyncStatisticsClassName())
.isEqualTo("com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ void getReportMessage_WithIncrementedStats_ShouldGetCorrectMessage() {
"Summary: 3 inventory entries were processed in total "
+ "(1 created, 1 updated and 1 failed to sync).");
}

@Test
void getSyncStatisticsClassName_ShouldReturnCorrectClassName() {
assertThat(inventorySyncStatistics.getSyncStatisticsClassName())
.isEqualTo("com.commercetools.sync.inventories.helpers.InventorySyncStatistics");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,10 @@ void removeAndGetReferencingKeys_WithExistingKey_ShouldGetAndRemoveAllKeys() {
assertThat(result).containsExactly("b", "c");
assertThat(productSyncStatistics.getNumberOfProductsWithMissingParents()).isEqualTo(1);
}

@Test
void getSyncStatisticsClassName_ShouldReturnCorrectClassName() {
assertThat(productSyncStatistics.getSyncStatisticsClassName())
.isEqualTo("com.commercetools.sync.products.helpers.ProductSyncStatistics");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -722,4 +722,18 @@ void removeReferencingProductTypeKey_WithOnlyOccurrenceForMissingRef_ShouldRemov
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents().get("missing2"))
.containsOnlyKeys("referencing-product-type-4");
}

@Test
void getSyncStatisticsClassName_ShouldReturnCorrectClassName() {
// preparation
final ProductTypeSyncStatistics productTypeSyncStatistics =
new ProductTypeSyncStatistics(new ConcurrentHashMap<>());

// test
final String className = productTypeSyncStatistics.getSyncStatisticsClassName();

// assertion
assertThat(className)
.isEqualTo("com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ void getReportMessage_WithIncrementedStats_ShouldGetCorrectMessage() {
"Summary: 6 shopping lists were processed in total "
+ "(1 created, 3 updated and 2 failed to sync).");
}

@Test
void getSyncStatisticsClassName_ShouldReturnCorrectClassName() {
assertThat(shoppingListSyncStatistics.getSyncStatisticsClassName())
.isEqualTo("com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,10 @@ void removeAndGetReferencingKeys_WithExistingKey_ShouldGetAndRemoveAllKeys() {
assertThat(result).containsExactly("b", "c");
assertThat(stateSyncStatistics.getNumberOfStatesWithMissingParents()).isEqualTo(1);
}

@Test
void getSyncStatisticsClassName_ShouldReturnCorrectClassName() {
assertThat(stateSyncStatistics.getSyncStatisticsClassName())
.isEqualTo("com.commercetools.sync.states.helpers.StateSyncStatistics");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ void getReportMessage_WithRandomStats_ShouldGetCorrectMessage() {
+ "total (%s created, %s updated and %s failed to sync).",
processed, created, updated, failed);
}

@Test
void getSyncStatisticsClassName_ShouldReturnCorrectClassName() {
assertThat(new TaxCategorySyncStatistics().getSyncStatisticsClassName())
.isEqualTo("com.commercetools.sync.taxcategories.helpers.TaxCategorySyncStatistics");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ void getReportMessage_WithIncrementedStats_ShouldGetCorrectMessage() {
"Summary: 6 types were processed in total "
+ "(1 created, 3 updated and 2 failed to sync).");
}

@Test
void getSyncStatisticsClassName_ShouldReturnCorrectClassName() {
assertThat(typeSyncStatistics.getSyncStatisticsClassName())
.isEqualTo("com.commercetools.sync.types.helpers.TypeSyncStatistics");
}
}

0 comments on commit 8f922fb

Please sign in to comment.