Skip to content

Commit c340210

Browse files
authored
Merge pull request #1124 from commercetools/DEVX-275_fix-npe-inventory-sync
Fix NPE in Inventory entry reference resolution
2 parents 9629c5f + ef09c84 commit c340210

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

src/main/java/com/commercetools/sync/inventories/utils/InventoryReferenceResolutionUtils.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
import com.commercetools.api.models.inventory.InventoryEntry;
1212
import com.commercetools.api.models.inventory.InventoryEntryDraft;
1313
import com.commercetools.api.models.inventory.InventoryEntryDraftBuilder;
14+
import com.commercetools.api.models.type.CustomFieldsDraft;
1415
import com.commercetools.api.models.type.Type;
1516
import com.commercetools.api.models.type.TypeReference;
1617
import com.commercetools.api.models.type.TypeResourceIdentifier;
1718
import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache;
1819
import java.util.List;
1920
import javax.annotation.Nonnull;
21+
import javax.annotation.Nullable;
2022

2123
/**
2224
* Util class which provides utilities that can be used when syncing resources from a source
@@ -82,15 +84,40 @@ private static InventoryEntryDraft mapToInventoryEntryDraft(
8284
inventoryEntry.getSupplyChannel(),
8385
referenceIdToKeyCache,
8486
(id, key) -> ChannelResourceIdentifierBuilder.of().key(key).id(id).build());
85-
return InventoryEntryDraftBuilder.of()
86-
.sku(inventoryEntry.getSku())
87-
.quantityOnStock(inventoryEntry.getQuantityOnStock())
88-
.expectedDelivery(inventoryEntry.getExpectedDelivery())
89-
.restockableInDays(inventoryEntry.getRestockableInDays())
90-
.key(inventoryEntry.getKey())
91-
.custom(mapToCustomFieldsDraft(inventoryEntry, referenceIdToKeyCache))
92-
.supplyChannel(channelResourceIdentifier)
93-
.build();
87+
final CustomFieldsDraft customFieldsDraft =
88+
mapToCustomFieldsDraft(inventoryEntry, referenceIdToKeyCache);
89+
return getInventoryEntryDraft(inventoryEntry, customFieldsDraft, channelResourceIdentifier);
90+
}
91+
92+
/**
93+
* Creates a new {@link InventoryEntryDraft} from given {@link InventoryEntry}, already mapped
94+
* {@link CustomFieldsDraft} and channel as {@link ChannelResourceIdentifier}.
95+
*
96+
* @param inventoryEntry - a template inventoryEntry to build the draft from
97+
* @param mappedCustomFields - a customFieldsDraft or null
98+
* @param channel - a resource identifier representing the supply channel or null
99+
* @return a new {@link InventoryEntryDraft} with all fields copied from the {@param
100+
* inventoryEntry} and custom fields set {@param mappedCustomFields} and supply channel with
101+
* {@param channel} resource identifier - it will return empty InventoryEntryDraft if sku or
102+
* quantityOnStock are missing.
103+
*/
104+
private static InventoryEntryDraft getInventoryEntryDraft(
105+
@Nonnull final InventoryEntry inventoryEntry,
106+
@Nullable final CustomFieldsDraft mappedCustomFields,
107+
@Nullable final ChannelResourceIdentifier channel) {
108+
if (inventoryEntry.getSku() != null && inventoryEntry.getQuantityOnStock() != null) {
109+
return InventoryEntryDraftBuilder.of()
110+
.sku(inventoryEntry.getSku())
111+
.quantityOnStock(inventoryEntry.getQuantityOnStock())
112+
.expectedDelivery(inventoryEntry.getExpectedDelivery())
113+
.restockableInDays(inventoryEntry.getRestockableInDays())
114+
.key(inventoryEntry.getKey())
115+
.custom(mappedCustomFields)
116+
.supplyChannel(channel)
117+
.build();
118+
} else {
119+
return InventoryEntryDraft.of();
120+
}
94121
}
95122

96123
private InventoryReferenceResolutionUtils() {}

src/test/java/com/commercetools/sync/inventories/utils/InventoryReferenceResolutionUtilsTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,33 @@ void mapToInventoryEntryDrafts_WithNullReferences_ShouldNotReturnResourceIdentif
125125
assertThat(referenceReplacedDraft.getSupplyChannel()).isNull();
126126
}
127127
}
128+
129+
@Test
130+
void mapToInventoryEntryDrafts_WithMissingRequiredFields_ShouldNotFailAndReturnEmptyDraft() {
131+
// preparation
132+
final InventoryEntry mockInventory = mock(InventoryEntry.class);
133+
when(mockInventory.getQuantityOnStock()).thenReturn(null);
134+
135+
// asserts
136+
assertThat(
137+
InventoryReferenceResolutionUtils.mapToInventoryEntryDrafts(
138+
List.of(mockInventory), referenceIdToKeyCache)
139+
.get(0))
140+
.isEqualTo(InventoryEntryDraft.of());
141+
142+
when(mockInventory.getSku()).thenReturn("Any sku");
143+
assertThat(
144+
InventoryReferenceResolutionUtils.mapToInventoryEntryDrafts(
145+
List.of(mockInventory), referenceIdToKeyCache)
146+
.get(0))
147+
.isEqualTo(InventoryEntryDraft.of());
148+
149+
when(mockInventory.getSku()).thenReturn(null);
150+
when(mockInventory.getQuantityOnStock()).thenReturn(1L);
151+
assertThat(
152+
InventoryReferenceResolutionUtils.mapToInventoryEntryDrafts(
153+
List.of(mockInventory), referenceIdToKeyCache)
154+
.get(0))
155+
.isEqualTo(InventoryEntryDraft.of());
156+
}
128157
}

0 commit comments

Comments
 (0)