Skip to content

Commit 8e8db5a

Browse files
author
salander85
committed
Avoid building resourceIdentifiers with invalid key values
1 parent 784c27e commit 8e8db5a

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

src/main/java/com/commercetools/sync/commons/utils/SyncUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.commercetools.api.models.common.Reference;
44
import com.commercetools.api.models.common.ResourceIdentifier;
5+
import com.commercetools.sync.services.impl.BaseTransformServiceImpl;
56
import java.util.ArrayList;
67
import java.util.List;
78
import java.util.Optional;
@@ -70,7 +71,8 @@ ResourceIdentifierT getResourceIdentifierWithKey(
7071
@Nullable final String key,
7172
final BiFunction<String, String, ResourceIdentifierT> toResourceIdentifier) {
7273

73-
if (!StringUtils.isEmpty(key)) {
74+
if (!StringUtils.isBlank(key)
75+
&& !BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER.equals(key)) {
7476
return toResourceIdentifier.apply(null, key);
7577
}
7678
return toResourceIdentifier.apply(id, null);

src/test/java/com/commercetools/sync/commons/utils/SyncUtilsTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.commercetools.sync.commons.utils;
22

3+
import static com.commercetools.sync.services.impl.BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER;
34
import static java.lang.String.format;
45
import static org.assertj.core.api.Assertions.assertThat;
56

@@ -161,4 +162,40 @@ void getResourceIdentifierWithKey_WithNullReference_ShouldReturnNull() {
161162
(id, key) -> CategoryResourceIdentifierBuilder.of().id(id).key(key).build());
162163
assertThat(resourceIdentifier).isNull();
163164
}
165+
166+
@Test
167+
void getResourceIdentifierWithKey_WithNotCachedReference_ShouldReturnResourceIdentifierWithId() {
168+
final String categoryId = UUID.randomUUID().toString();
169+
170+
final CategoryReference categoryReference =
171+
CategoryReferenceBuilder.of().id(categoryId).build();
172+
173+
final CategoryResourceIdentifier resourceIdentifier =
174+
SyncUtils.getResourceIdentifierWithKey(
175+
categoryReference,
176+
referenceIdToKeyCache,
177+
(id, key) -> CategoryResourceIdentifierBuilder.of().id(id).key(key).build());
178+
179+
assertThat(resourceIdentifier).isNotNull();
180+
assertThat(resourceIdentifier.getId()).isEqualTo(categoryId);
181+
}
182+
183+
@Test
184+
void
185+
getResourceIdentifierWithKey_WithCachedReferenceIsEmptyPlaceholder_ShouldReturnResourceIdentifierWithId() {
186+
final String categoryId = UUID.randomUUID().toString();
187+
referenceIdToKeyCache.add(categoryId, KEY_IS_NOT_SET_PLACE_HOLDER);
188+
189+
final CategoryReference categoryReference =
190+
CategoryReferenceBuilder.of().id(categoryId).build();
191+
192+
final CategoryResourceIdentifier resourceIdentifier =
193+
SyncUtils.getResourceIdentifierWithKey(
194+
categoryReference,
195+
referenceIdToKeyCache,
196+
(id, key) -> CategoryResourceIdentifierBuilder.of().id(id).key(key).build());
197+
198+
assertThat(resourceIdentifier).isNotNull();
199+
assertThat(resourceIdentifier.getId()).isEqualTo(categoryId);
200+
}
164201
}

src/test/java/com/commercetools/sync/products/utils/ProductTransformUtilsTest.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.commercetools.sync.products.utils;
22

3+
import static com.commercetools.sync.services.impl.BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER;
34
import static java.util.Arrays.asList;
45
import static java.util.Collections.singletonList;
56
import static org.assertj.core.api.Assertions.assertThat;
@@ -25,7 +26,6 @@
2526
import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache;
2627
import com.commercetools.sync.commons.utils.TestUtils;
2728
import com.commercetools.sync.products.ProductSyncMockUtils;
28-
import com.commercetools.sync.services.impl.BaseTransformServiceImpl;
2929
import com.fasterxml.jackson.core.JsonProcessingException;
3030
import com.fasterxml.jackson.databind.JsonNode;
3131
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -439,9 +439,9 @@ void transform_ProductReferences_ShouldReplaceReferencesIdsWithKeysAndMapToProdu
439439
.hasValueSatisfying(
440440
productDraft -> {
441441
assertThat(productDraft.getProductType().getKey())
442-
.isEqualTo(BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER);
442+
.isEqualTo(KEY_IS_NOT_SET_PLACE_HOLDER);
443443
assertThat(productDraft.getTaxCategory().getKey())
444-
.isEqualTo(BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER);
444+
.isEqualTo(KEY_IS_NOT_SET_PLACE_HOLDER);
445445
});
446446
}
447447

@@ -451,9 +451,8 @@ void transform_ProductReferences_ShouldReplaceReferencesIdsWithKeysAndMapToProdu
451451
throws Exception {
452452
// preparation
453453
final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class);
454-
referenceIdToKeyCache.add(
455-
"cda0dbf7-b42e-40bf-8453-241d5b587f93",
456-
BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER);
454+
referenceIdToKeyCache.add("cda0dbf7-b42e-40bf-8453-241d5b587f93", KEY_IS_NOT_SET_PLACE_HOLDER);
455+
referenceIdToKeyCache.add("ebbe95fb-2282-4f9a-8747-fbe440e02dc0", KEY_IS_NOT_SET_PLACE_HOLDER);
457456
final List<ProductProjection> productPage =
458457
Arrays.asList(
459458
ProductSyncMockUtils.createProductFromJson("product-with-unresolved-references.json"));
@@ -482,16 +481,19 @@ void transform_ProductReferences_ShouldReplaceReferencesIdsWithKeysAndMapToProdu
482481
.join();
483482

484483
// assertions
485-
486484
final Optional<ProductDraft> productKey1 =
487485
productsResolved.stream()
488486
.filter(productDraft -> "productKeyResolved".equals(productDraft.getKey()))
489487
.findFirst();
490488

491489
assertThat(productKey1)
492490
.hasValueSatisfying(
493-
productDraft ->
494-
assertThat(productDraft.getProductType().getKey()).isEqualTo("productTypeKey"));
491+
productDraft -> {
492+
assertThat(productDraft.getProductType().getKey()).isEqualTo("productTypeKey");
493+
assertThat(productDraft.getTaxCategory().getId())
494+
.isEqualTo("ebbe95fb-2282-4f9a-8747-fbe440e02dc0");
495+
assertThat(productDraft.getTaxCategory().getKey()).isNull();
496+
});
495497
}
496498

497499
@Test

0 commit comments

Comments
 (0)