Skip to content

Commit b3dccb4

Browse files
Merge pull request #220 from commercetools/218-fix-categoryOrderHint-resolution
218 fix category order hint resolution
2 parents 47c3a28 + 4159bd5 commit b3dccb4

20 files changed

Lines changed: 1332 additions & 835 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Build Status](https://travis-ci.org/commercetools/commercetools-sync-java.svg?branch=master)](https://travis-ci.org/commercetools/commercetools-sync-java)
44
[![codecov](https://codecov.io/gh/commercetools/commercetools-sync-java/branch/master/graph/badge.svg)](https://codecov.io/gh/commercetools/commercetools-sync-java)
55
[![Download](https://api.bintray.com/packages/commercetools/maven/commercetools-sync-java/images/download.svg) ](https://bintray.com/commercetools/maven/commercetools-sync-java/_latestVersion)
6-
[![Javadoc](http://javadoc-badge.appspot.com/com.commercetools/commercetools-sync-java.svg?label=Javadoc)](https://commercetools.github.io/commercetools-sync-java/v/v1.0.0-M7/)
6+
[![Javadoc](http://javadoc-badge.appspot.com/com.commercetools/commercetools-sync-java.svg?label=Javadoc)](https://commercetools.github.io/commercetools-sync-java/v/v1.0.0-M8/)
77
[![Known Vulnerabilities](https://snyk.io/test/github/commercetools/commercetools-sync-java/4b2e26113d591bda158217c5dc1cf80a88665646/badge.svg)](https://snyk.io/test/github/commercetools/commercetools-sync-java/4b2e26113d591bda158217c5dc1cf80a88665646)
88

99
Java API which exposes utilities for building update actions and automatic syncing of CTP data from external sources
@@ -21,7 +21,7 @@ Java API which exposes utilities for building update actions and automatic synci
2121
- [Ivy](#ivy)
2222
- [Roadmap](#roadmap)
2323
- [Release Notes](/docs/RELEASE_NOTES.md)
24-
- [Javadoc](https://commercetools.github.io/commercetools-sync-java/v/v1.0.0-M7/)
24+
- [Javadoc](https://commercetools.github.io/commercetools-sync-java/v/v1.0.0-M8/)
2525

2626
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
2727
## Usage
@@ -56,20 +56,20 @@ most popular ones:
5656
<dependency>
5757
<groupId>com.commercetools</groupId>
5858
<artifactId>commercetools-sync-java</artifactId>
59-
<version>v1.0.0-M7</version>
59+
<version>v1.0.0-M8</version>
6060
</dependency>
6161
````
6262
#### Gradle
6363
````groovy
64-
implementation 'com.commercetools:commercetools-sync-java:v1.0.0-M7'
64+
implementation 'com.commercetools:commercetools-sync-java:v1.0.0-M8'
6565
````
6666
#### SBT
6767
````
68-
libraryDependencies += "com.commercetools" % "commercetools-sync-java" % "v1.0.0-M7"
68+
libraryDependencies += "com.commercetools" % "commercetools-sync-java" % "v1.0.0-M8"
6969
````
7070
#### Ivy
7171
````xml
72-
<dependency org="com.commercetools" name="commercetools-sync-java" rev="v1.0.0-M7"/>
72+
<dependency org="com.commercetools" name="commercetools-sync-java" rev="v1.0.0-M8"/>
7373
````
7474

7575

docs/RELEASE_NOTES.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
3434

3535

36+
- [v1.0.0-M8 - Dec 29, 2017](#v100-m8----dec-29-2017)
3637
- [v1.0.0-M7 - Dec 15, 2017](#v100-m7----dec-15-2017)
3738
- [v1.0.0-M6 - Dec 5, 2017](#v100-m6----dec-5-2017)
3839
- [v1.0.0-M5 - Nov 16, 2017](#v100-m5----nov-16-2017)
@@ -45,29 +46,28 @@
4546
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
4647

4748
<!--
48-
### v1.0.0-M9 - Jan 10, 2018
49+
### v1.0.0-M9 - Jan 15, 2018
4950
[Commits](https://github.com/commercetools/commercetools-sync-java/compare/v1.0.0-M8...v1.0.0-M9) |
5051
[Javadoc](https://commercetools.github.io/commercetools-sync-java/v/v1.0.0-M9/) |
5152
[Jar](https://bintray.com/commercetools/maven/commercetools-sync-java/v1.0.0-M9)
5253
-->
5354

54-
<!--
55-
### v1.0.0-M8 - Dec 28, 2017
55+
### v1.0.0-M8 - Dec 29, 2017
5656
[Commits](https://github.com/commercetools/commercetools-sync-java/compare/v1.0.0-M7...v1.0.0-M8) |
5757
[Javadoc](https://commercetools.github.io/commercetools-sync-java/v/v1.0.0-M8/) |
5858
[Jar](https://bintray.com/commercetools/maven/commercetools-sync-java/v1.0.0-M8)
5959

60-
61-
**Enhancements** (1)
62-
- **Build Tools** - Bumped Gradle to version 4.4. [#205](https://github.com/commercetools/commercetools-sync-java/issues/205)
63-
6460
**New Features** (1)
65-
- **Category Sync** - Exposed new method in CategorySyncStatistics#getNumberOfCategoriesWithMissingParents which gets the
61+
- **Category Sync** - Exposed new method `CategorySyncStatistics#getNumberOfCategoriesWithMissingParents` which gets the
6662
total number of categories with missing parents from the statistics instance. [#186](https://github.com/commercetools/commercetools-sync-java/issues/186)
6763

68-
**Changes** (1)
64+
**Changes** (2)
6965
- **Product Sync** - Changed product sync statistics report message wording. [#186](https://github.com/commercetools/commercetools-sync-java/issues/186)
70-
-->
66+
- **Product Sync** - Exposed new methods `ProductReferenceResolver#resolveStateReference`, `ProductReferenceResolver#resolveTaxCategoryReference`, `ProductReferenceResolver#resolveCategoryReferences` and `ProductReferenceResolver#resolveProductTypeReference`.
67+
[#218](https://github.com/commercetools/commercetools-sync-java/issues/218)
68+
69+
**Enhancements** (1)
70+
- **Build Tools** - Bumped Gradle to version 4.4. [#205](https://github.com/commercetools/commercetools-sync-java/issues/205)
7171

7272

7373
### v1.0.0-M7 - Dec 15, 2017

src/integration-test/java/com/commercetools/sync/integration/commons/utils/CategoryITUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public static Map<String, JsonNode> getCustomFieldsJsons() {
199199
* @param categoryDrafts the drafts to build the categories from.
200200
*/
201201
public static List<Category> createCategories(@Nonnull final SphereClient ctpClient,
202-
@Nonnull final List<CategoryDraft> categoryDrafts) {
202+
@Nonnull final List<CategoryDraft> categoryDrafts) {
203203
final List<CompletableFuture<Category>> futures = new ArrayList<>();
204204
for (CategoryDraft categoryDraft : categoryDrafts) {
205205
final CategoryCreateCommand categoryCreateCommand = CategoryCreateCommand.of(categoryDraft);

src/integration-test/java/com/commercetools/sync/integration/ctpprojectsource/products/ProductReferenceResolverIT.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.ArrayList;
2424
import java.util.List;
2525
import java.util.Locale;
26-
import java.util.concurrent.CompletionException;
2726

2827
import static com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat;
2928
import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY;
@@ -172,8 +171,7 @@ public void sync_withNewProductWithNoProductTypeKey_ShouldFailCreatingTheProduct
172171
+ " Reason: Reference 'id' field value is blank (null/empty).",
173172
productDraft.getKey(), ReferenceResolutionException.class.getCanonicalName(), productDraft.getKey()));
174173
assertThat(errorCallBackExceptions).hasSize(1);
175-
assertThat(errorCallBackExceptions.get(0)).isExactlyInstanceOf(CompletionException.class);
176-
assertThat(errorCallBackExceptions.get(0).getCause()).isExactlyInstanceOf(ReferenceResolutionException.class);
174+
assertThat(errorCallBackExceptions.get(0)).isExactlyInstanceOf(ReferenceResolutionException.class);
177175
assertThat(warningCallBackMessages).isEmpty();
178176
}
179177
}

src/integration-test/java/com/commercetools/sync/integration/ctpprojectsource/products/templates/beforeupdatecallback/KeepOtherVariantsSyncIT.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static java.lang.String.format;
3636
import static java.util.Collections.singletonList;
3737
import static org.assertj.core.api.Assertions.assertThat;
38+
import static com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat;
3839

3940
public class KeepOtherVariantsSyncIT {
4041

@@ -114,10 +115,8 @@ public void sync_withRemovedVariants_shouldNotRemoveVariants() {
114115
final ProductSync productSync = new ProductSync(syncOptions);
115116

116117
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(singletonList(productDraft)));
117-
assertThat(syncStatistics.getProcessed()).isEqualTo(1);
118-
assertThat(syncStatistics.getCreated()).isEqualTo(0);
119-
assertThat(syncStatistics.getUpdated()).isEqualTo(1);
120-
assertThat(syncStatistics.getFailed()).isEqualTo(0);
118+
119+
assertThat(syncStatistics).hasValues(1, 0, 1, 0);
121120
assertThat(errorCallBackExceptions).isEmpty();
122121
assertThat(errorCallBackMessages).isEmpty();
123122
assertThat(warningCallBackMessages).isEmpty();
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.commercetools.sync.integration.externalsource.products;
2+
3+
import com.commercetools.sync.commons.exceptions.ReferenceResolutionException;
4+
import com.commercetools.sync.products.ProductSync;
5+
import com.commercetools.sync.products.ProductSyncOptions;
6+
import com.commercetools.sync.products.ProductSyncOptionsBuilder;
7+
import com.commercetools.sync.products.helpers.ProductSyncStatistics;
8+
import io.sphere.sdk.categories.Category;
9+
import io.sphere.sdk.models.Reference;
10+
import io.sphere.sdk.products.ProductDraft;
11+
import io.sphere.sdk.producttypes.ProductType;
12+
import org.junit.AfterClass;
13+
import org.junit.Before;
14+
import org.junit.BeforeClass;
15+
import org.junit.Test;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
import java.util.Locale;
20+
import java.util.function.BiConsumer;
21+
import java.util.function.Consumer;
22+
23+
import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY;
24+
import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME;
25+
import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories;
26+
import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType;
27+
import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts;
28+
import static com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts;
29+
import static com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData;
30+
import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType;
31+
import static com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT;
32+
import static com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH;
33+
import static com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH;
34+
import static com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft;
35+
import static com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints;
36+
import static com.commercetools.tests.utils.CompletionStageUtil.executeBlocking;
37+
import static io.sphere.sdk.producttypes.ProductType.referenceOfId;
38+
import static java.util.Collections.singletonList;
39+
import static org.assertj.core.api.Assertions.assertThat;
40+
import static com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat;
41+
42+
public class ProductReferenceResolverIT {
43+
44+
private static ProductType productType;
45+
private static List<Category> categories;
46+
private List<String> errorCallBackMessages;
47+
private List<String> warningCallBackMessages;
48+
private List<Throwable> errorCallBackExceptions;
49+
50+
@BeforeClass
51+
public static void setup() {
52+
deleteProductSyncTestData(CTP_TARGET_CLIENT);
53+
createCategoriesCustomType(OLD_CATEGORY_CUSTOM_TYPE_KEY, Locale.ENGLISH,
54+
OLD_CATEGORY_CUSTOM_TYPE_NAME, CTP_TARGET_CLIENT);
55+
categories = createCategories(CTP_TARGET_CLIENT, getCategoryDrafts(null, 2));
56+
productType = createProductType(PRODUCT_TYPE_RESOURCE_PATH, CTP_TARGET_CLIENT);
57+
}
58+
59+
@Before
60+
public void setupPerTest() {
61+
clearSyncTestCollections();
62+
deleteAllProducts(CTP_TARGET_CLIENT);
63+
}
64+
65+
private void clearSyncTestCollections() {
66+
errorCallBackMessages = new ArrayList<>();
67+
errorCallBackExceptions = new ArrayList<>();
68+
warningCallBackMessages = new ArrayList<>();
69+
}
70+
71+
private ProductSyncOptions getProductSyncOptions() {
72+
final BiConsumer<String, Throwable> errorCallBack = (errorMessage, exception) -> {
73+
errorCallBackMessages.add(errorMessage);
74+
errorCallBackExceptions.add(exception);
75+
};
76+
77+
final Consumer<String> warningCallBack = warningMessage -> warningCallBackMessages.add(warningMessage);
78+
79+
return ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT)
80+
.errorCallback(errorCallBack)
81+
.warningCallback(warningCallBack)
82+
.build();
83+
}
84+
85+
@AfterClass
86+
public static void tearDown() {
87+
deleteProductSyncTestData(CTP_TARGET_CLIENT);
88+
}
89+
90+
@Test
91+
public void sync_withNewProductWithInvalidCategoryReferences_ShouldFailCreatingTheProduct() {
92+
// Replace the id with key for one category reference but not the other.
93+
final List<Reference<Category>> invalidCategoryReferences = new ArrayList<>();
94+
invalidCategoryReferences.add(Category.referenceOfId(categories.get(0).getId()));
95+
invalidCategoryReferences.add(Category.referenceOfId(categories.get(1).getKey()));
96+
97+
// Create a product with the invalid category references. (i.e. not ready for reference resolution).
98+
final ProductDraft productDraft =
99+
createProductDraft(PRODUCT_KEY_1_RESOURCE_PATH, referenceOfId(productType.getKey()), null,
100+
null, invalidCategoryReferences, createRandomCategoryOrderHints(invalidCategoryReferences));
101+
102+
final ProductSync productSync = new ProductSync(getProductSyncOptions());
103+
final ProductSyncStatistics syncStatistics =
104+
executeBlocking(productSync.sync(singletonList(productDraft)));
105+
106+
assertThat(syncStatistics).hasValues(1, 0, 0, 1);
107+
assertThat(errorCallBackExceptions).hasSize(1);
108+
final Throwable exception = errorCallBackExceptions.get(0);
109+
assertThat(exception).isExactlyInstanceOf(ReferenceResolutionException.class)
110+
.hasMessageContaining("Failed to resolve reference 'category' on ProductDraft with "
111+
+ "key:'productKey1'")
112+
.hasMessageContaining("Reason: Found a UUID in the id field. Expecting a key without a "
113+
+ "UUID value.");
114+
115+
assertThat(errorCallBackMessages).hasSize(1);
116+
assertThat(errorCallBackMessages.get(0))
117+
.contains("Failed to resolve references on ProductDraft with key:'productKey1'");
118+
assertThat(warningCallBackMessages).isEmpty();
119+
}
120+
}

src/main/java/com/commercetools/sync/categories/CategorySync.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ protected CompletionStage<CategorySyncStatistics> processBatch(@Nonnull final Li
163163
.thenAccept(fetchedCategories ->
164164
processFetchedCategories(fetchedCategories,
165165
referencesResolvedDrafts, keyToIdCache))
166-
.thenAccept(result ->
166+
.thenAccept(ignoredResult ->
167167
updateCategoriesSequentially(categoryDraftsToUpdate))
168-
.thenCompose(result ->
168+
.thenCompose(ignoredResult ->
169169
updateCategoriesInParallel(categoryDraftsToUpdate))
170-
.thenApply((result) -> {
170+
.thenApply((ignoredResult) -> {
171171
statistics.incrementProcessed(numberOfNewDraftsToProcess);
172172
return statistics;
173173
});

src/main/java/com/commercetools/sync/products/ProductSync.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private void processFetchedProducts(@Nonnull final Set<Product> matchingProducts
157157
}
158158
final String errorMessage = format(FAILED_TO_RESOLVE_REFERENCES,
159159
productDraft.getKey(), actualException);
160-
handleError(errorMessage, referenceResolutionException);
160+
handleError(errorMessage, actualException);
161161
return null;
162162
}).toCompletableFuture().join();
163163
} else {

0 commit comments

Comments
 (0)