Skip to content

Commit

Permalink
Merge pull request #1118 from commercetools/DEVX-277_Fix-npe-in-state…
Browse files Browse the repository at this point in the history
…-reference-resolution-utils

Fix npe in state reference resolution utils
  • Loading branch information
salander85 authored Nov 30, 2023
2 parents 7e5f98c + 68a4953 commit 71bf9a6
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,7 @@ private static CategoryDraft mapToCategoryDraft(
getResourceIdentifierWithKey(
category.getParent(),
referenceIdToKeyCache,
(id, key) -> {
final CategoryResourceIdentifierBuilder builder =
CategoryResourceIdentifierBuilder.of();
if (id == null) {
return builder.key(key).build();
} else {
return builder.id(id).build();
}
});
(id, key) -> CategoryResourceIdentifierBuilder.of().key(key).id(id).build());
return CategoryDraftBuilder.of()
.key(category.getKey())
.slug(category.getSlug())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,11 @@ public static CustomFieldsDraft mapToCustomFieldsDraft(
SyncUtils.getResourceIdentifierWithKey(
typeReference,
referenceIdToKeyCache,
(id, key) -> {
final TypeResourceIdentifierBuilder builder =
TypeResourceIdentifierBuilder.of();
if (id == null) {

return builder.key(key).build();
} else {
return builder.id(id).build();
}
}))
(id, key) ->
TypeResourceIdentifierBuilder.of()
.key(key)
.id(id)
.build()))
.orElse(null))
.build())
.orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ResourceIdentifierT getResourceIdentifierWithKey(
@Nullable final String key,
final BiFunction<String, String, ResourceIdentifierT> toResourceIdentifier) {

if (!StringUtils.isEmpty(key)) {
if (!StringUtils.isBlank(key)) {
return toResourceIdentifier.apply(null, key);
}
return toResourceIdentifier.apply(id, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,7 @@ private static InventoryEntryDraft mapToInventoryEntryDraft(
getResourceIdentifierWithKey(
inventoryEntry.getSupplyChannel(),
referenceIdToKeyCache,
(id, key) -> {
final ChannelResourceIdentifierBuilder builder =
ChannelResourceIdentifierBuilder.of();
if (id == null) {
return builder.key(key).build();
} else {
return builder.id(id).build();
}
});
(id, key) -> ChannelResourceIdentifierBuilder.of().key(key).id(id).build());
return InventoryEntryDraftBuilder.of()
.sku(inventoryEntry.getSku())
.quantityOnStock(inventoryEntry.getQuantityOnStock())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,7 @@ private static ChannelResourceIdentifier createChannelResourceIdentifier(
getResourceIdentifierWithKey(
channelReference,
referenceIdToKeyCache,
(id, key) -> {
final ChannelResourceIdentifierBuilder builder =
ChannelResourceIdentifierBuilder.of();
if (id == null) {
return builder.key(key).build();
} else {
return builder.id(id).build();
}
});
(id, key) -> ChannelResourceIdentifierBuilder.of().key(key).id(id).build());
} else if (channelReference != null) {
channelResourceIdentifier =
ChannelResourceIdentifierBuilder.of().id(channelReference.getId()).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,41 +149,19 @@ public static List<ProductDraft> mapToProductDrafts(
getResourceIdentifierWithKey(
product.getProductType(),
referenceIdToKeyCache,
(id, key) -> {
final ProductTypeResourceIdentifierBuilder builder =
ProductTypeResourceIdentifierBuilder.of();
if (id == null) {
return builder.key(key).build();
} else {
return builder.id(id).build();
}
});
(id, key) ->
ProductTypeResourceIdentifierBuilder.of().id(id).key(key).build());
final TaxCategoryResourceIdentifier taxCategoryResourceIdentifier =
getResourceIdentifierWithKey(
product.getTaxCategory(),
referenceIdToKeyCache,
(id, key) -> {
final TaxCategoryResourceIdentifierBuilder builder =
TaxCategoryResourceIdentifierBuilder.of();
if (id == null) {
return builder.key(key).build();
} else {
return builder.id(id).build();
}
});
(id, key) ->
TaxCategoryResourceIdentifierBuilder.of().key(key).id(id).build());
final StateResourceIdentifier stateResourceIdentifier =
getResourceIdentifierWithKey(
product.getState(),
referenceIdToKeyCache,
(id, key) -> {
final StateResourceIdentifierBuilder builder =
StateResourceIdentifierBuilder.of();
if (id == null) {
return builder.key(key).build();
} else {
return builder.id(id).build();
}
});
(id, key) -> StateResourceIdentifierBuilder.of().key(key).id(id).build());
return productDraftBuilder
.masterVariant(masterVariantDraftWithKeys)
.variants(variantDraftsWithKeys)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,37 @@ public static List<StateDraft> mapToStateDrafts(
state -> {
final List<StateResourceIdentifier> newTransitions =
replaceTransitionIdsWithKeys(state, referenceIdToKeyCache);
return StateDraftBuilder.of()
.key(state.getKey())
.type(state.getType())
.name(state.getName())
.description(state.getDescription())
.initial(state.getInitial())
.roles(state.getRoles())
.transitions(newTransitions)
.build();
return getStateDraft(state, newTransitions);
})
.collect(Collectors.toList());
}

/**
* Creates a new {@link StateDraft} from given {@link State} and transitions as {@link
* java.util.List}&lt; {@link StateResourceIdentifier}&gt;
*
* @param state - template state to build the draft from
* @param newTransitions - transformed list of state resource identifiers
* @return a new {@link StateDraft} with all fields copied from the {@param state} and transitions
* set {@param newTransitions} - it will return empty StateDraft if key or type are missing.
*/
private static StateDraft getStateDraft(
State state, List<StateResourceIdentifier> newTransitions) {
if (state.getKey() != null && state.getType() != null) {
return StateDraftBuilder.of()
.key(state.getKey())
.type(state.getType())
.name(state.getName())
.description(state.getDescription())
.initial(state.getInitial())
.roles(state.getRoles())
.transitions(newTransitions)
.build();
} else {
return StateDraft.of();
}
}

@SuppressWarnings("PMD.ReturnEmptyCollectionRatherThanNull")
private static List<StateResourceIdentifier> replaceTransitionIdsWithKeys(
@Nonnull final State state, @Nonnull final ReferenceIdToKeyCache referenceIdToKeyCache) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.commercetools.sync.commons.utils;

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

Expand Down Expand Up @@ -161,4 +162,40 @@ void getResourceIdentifierWithKey_WithNullReference_ShouldReturnNull() {
(id, key) -> CategoryResourceIdentifierBuilder.of().id(id).key(key).build());
assertThat(resourceIdentifier).isNull();
}

@Test
void getResourceIdentifierWithKey_WithNotCachedReference_ShouldReturnResourceIdentifierWithId() {
final String categoryId = UUID.randomUUID().toString();

final CategoryReference categoryReference =
CategoryReferenceBuilder.of().id(categoryId).build();

final CategoryResourceIdentifier resourceIdentifier =
SyncUtils.getResourceIdentifierWithKey(
categoryReference,
referenceIdToKeyCache,
(id, key) -> CategoryResourceIdentifierBuilder.of().id(id).key(key).build());

assertThat(resourceIdentifier).isNotNull();
assertThat(resourceIdentifier.getId()).isEqualTo(categoryId);
}

@Test
void
getResourceIdentifierWithKey_WithCachedReferenceIsEmptyPlaceholder_ShouldReturnResourceIdentifierWithKeyPlaceholder() {
final String categoryId = UUID.randomUUID().toString();
referenceIdToKeyCache.add(categoryId, KEY_IS_NOT_SET_PLACE_HOLDER);

final CategoryReference categoryReference =
CategoryReferenceBuilder.of().id(categoryId).build();

final CategoryResourceIdentifier resourceIdentifier =
SyncUtils.getResourceIdentifierWithKey(
categoryReference,
referenceIdToKeyCache,
(id, key) -> CategoryResourceIdentifierBuilder.of().id(id).key(key).build());

assertThat(resourceIdentifier).isNotNull();
assertThat(resourceIdentifier.getKey()).isEqualTo(KEY_IS_NOT_SET_PLACE_HOLDER);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.commercetools.sync.products.utils;

import static com.commercetools.sync.services.impl.BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -25,7 +26,6 @@
import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache;
import com.commercetools.sync.commons.utils.TestUtils;
import com.commercetools.sync.products.ProductSyncMockUtils;
import com.commercetools.sync.services.impl.BaseTransformServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -439,9 +439,9 @@ void transform_ProductReferences_ShouldReplaceReferencesIdsWithKeysAndMapToProdu
.hasValueSatisfying(
productDraft -> {
assertThat(productDraft.getProductType().getKey())
.isEqualTo(BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER);
.isEqualTo(KEY_IS_NOT_SET_PLACE_HOLDER);
assertThat(productDraft.getTaxCategory().getKey())
.isEqualTo(BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER);
.isEqualTo(KEY_IS_NOT_SET_PLACE_HOLDER);
});
}

Expand All @@ -451,9 +451,8 @@ void transform_ProductReferences_ShouldReplaceReferencesIdsWithKeysAndMapToProdu
throws Exception {
// preparation
final ProjectApiRoot sourceClient = mock(ProjectApiRoot.class);
referenceIdToKeyCache.add(
"cda0dbf7-b42e-40bf-8453-241d5b587f93",
BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER);
referenceIdToKeyCache.add("cda0dbf7-b42e-40bf-8453-241d5b587f93", KEY_IS_NOT_SET_PLACE_HOLDER);
referenceIdToKeyCache.add("ste95fb-2282-4f9a-8747-fbe440e02dcs0", KEY_IS_NOT_SET_PLACE_HOLDER);
final List<ProductProjection> productPage =
Arrays.asList(
ProductSyncMockUtils.createProductFromJson("product-with-unresolved-references.json"));
Expand Down Expand Up @@ -482,16 +481,17 @@ void transform_ProductReferences_ShouldReplaceReferencesIdsWithKeysAndMapToProdu
.join();

// assertions

final Optional<ProductDraft> productKey1 =
productsResolved.stream()
.filter(productDraft -> "productKeyResolved".equals(productDraft.getKey()))
.findFirst();

assertThat(productKey1)
.hasValueSatisfying(
productDraft ->
assertThat(productDraft.getProductType().getKey()).isEqualTo("productTypeKey"));
productDraft -> {
assertThat(productDraft.getProductType().getKey()).isEqualTo("productTypeKey");
assertThat(productDraft.getState().getKey()).isEqualTo(KEY_IS_NOT_SET_PLACE_HOLDER);
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,35 @@ void mapToStateDrafts_WithNullReferences_ShouldNotFail() {
assertThat(referenceReplacedDrafts.get(0).getTransitions()).isEqualTo(null);
}

@Test
void mapToStateDrafts_WithMissingRequiredFields_ShouldNotFailAndReturnEmptyDraft() {
// preparation
final State mockState = mock(State.class);
when(mockState.getTransitions()).thenReturn(null);

// asserts
assertThat(
StateReferenceResolutionUtils.mapToStateDrafts(
List.of(mockState), referenceIdToKeyCache)
.get(0))
.isEqualTo(StateDraft.of());

when(mockState.getKey()).thenReturn("Any key");
assertThat(
StateReferenceResolutionUtils.mapToStateDrafts(
List.of(mockState), referenceIdToKeyCache)
.get(0))
.isEqualTo(StateDraft.of());

when(mockState.getKey()).thenReturn(null);
when(mockState.getType()).thenReturn(StateTypeEnum.LINE_ITEM_STATE);
assertThat(
StateReferenceResolutionUtils.mapToStateDrafts(
List.of(mockState), referenceIdToKeyCache)
.get(0))
.isEqualTo(StateDraft.of());
}

@Nonnull
private static State getStateMock(@Nonnull final String key) {
final State state = mock(State.class);
Expand Down

0 comments on commit 71bf9a6

Please sign in to comment.