11package com .commercetools .sync .products .helpers ;
22
3+ import static java .lang .String .format ;
34import static java .util .Arrays .asList ;
45import static java .util .Collections .singletonList ;
56import static org .apache .commons .lang3 .StringUtils .EMPTY ;
@@ -241,8 +242,7 @@ void validateAndCollectReferencedKeys_WithEmptyDraft_ShouldHaveEmptyResult() {
241242
242243 assertThat (errorCallBackMessages ).hasSize (1 );
243244 assertThat (errorCallBackMessages .get (0 ))
244- .isEqualTo (
245- String .format (ProductBatchValidator .PRODUCT_DRAFT_KEY_NOT_SET , productDraft .getName ()));
245+ .isEqualTo (format (ProductBatchValidator .PRODUCT_DRAFT_KEY_NOT_SET , productDraft .getName ()));
246246 assertThat (validDrafts ).isEmpty ();
247247 }
248248
@@ -255,13 +255,12 @@ void validateAndCollectReferencedKeys_WithEmptyDraft_ShouldHaveEmptyResult() {
255255
256256 assertThat (errorCallBackMessages ).hasSize (1 );
257257 assertThat (errorCallBackMessages .get (0 ))
258- .isEqualTo (
259- String .format (ProductBatchValidator .PRODUCT_DRAFT_KEY_NOT_SET , productDraft .getName ()));
258+ .isEqualTo (format (ProductBatchValidator .PRODUCT_DRAFT_KEY_NOT_SET , productDraft .getName ()));
260259 assertThat (validDrafts ).isEmpty ();
261260 }
262261
263262 @ Test
264- void validateAndCollectReferencedKeys_WithNullVariant_ShouldHaveValidationErrors () {
263+ void validateAndCollectReferencedKeys_WithNullVariant_ShouldBeValid () {
265264 final String productDraftKey = "key" ;
266265 final int variantPosition = 0 ;
267266
@@ -270,14 +269,8 @@ void validateAndCollectReferencedKeys_WithNullVariant_ShouldHaveValidationErrors
270269
271270 final Set <ProductDraft > validDrafts = getValidDrafts (Collections .singletonList (productDraft ));
272271
273- assertThat (validDrafts ).isEmpty ();
274- assertThat (errorCallBackMessages ).hasSize (1 );
275- assertThat (errorCallBackMessages .get (0 ))
276- .isEqualTo (
277- String .format (
278- ProductBatchValidator .PRODUCT_VARIANT_DRAFT_IS_NULL ,
279- variantPosition ,
280- productDraftKey ));
272+ assertThat (validDrafts ).hasSize (1 );
273+ assertThat (new ArrayList <>(validDrafts ).get (0 )).isEqualTo (productDraft );
281274 }
282275
283276 @ Test
@@ -296,11 +289,11 @@ void validateAndCollectReferencedKeys_WithVariantButNoKeyAndSku_ShouldHaveValida
296289 assertThat (errorCallBackMessages ).hasSize (2 );
297290 assertThat (errorCallBackMessages )
298291 .containsExactlyInAnyOrder (
299- String . format (
292+ format (
300293 ProductBatchValidator .PRODUCT_VARIANT_DRAFT_SKU_NOT_SET ,
301294 variantPosition ,
302295 productDraftKey ),
303- String . format (
296+ format (
304297 ProductBatchValidator .PRODUCT_VARIANT_DRAFT_KEY_NOT_SET ,
305298 variantPosition ,
306299 productDraftKey ));
@@ -324,7 +317,7 @@ void validateAndCollectReferencedKeys_WithNoVariantKey_ShouldHaveKeyValidationEr
324317 assertThat (errorCallBackMessages ).hasSize (1 );
325318 assertThat (errorCallBackMessages .get (0 ))
326319 .isEqualTo (
327- String . format (
320+ format (
328321 ProductBatchValidator .PRODUCT_VARIANT_DRAFT_KEY_NOT_SET ,
329322 variantPosition ,
330323 productDraftKey ));
@@ -347,7 +340,7 @@ void validateAndCollectReferencedKeys_WithNoVariantSku_ShouldHaveSkuValidationEr
347340 assertThat (errorCallBackMessages ).hasSize (1 );
348341 assertThat (errorCallBackMessages .get (0 ))
349342 .isEqualTo (
350- String . format (
343+ format (
351344 ProductBatchValidator .PRODUCT_VARIANT_DRAFT_SKU_NOT_SET ,
352345 variantPosition ,
353346 productDraftKey ));
@@ -431,21 +424,44 @@ void validateAndCollectReferencedKeys_WithDrafts_ShouldValidateCorrectly() {
431424 assertThat (errorCallBackMessages )
432425 .containsExactlyInAnyOrder (
433426 ProductBatchValidator .PRODUCT_DRAFT_IS_NULL ,
434- String .format (ProductBatchValidator .PRODUCT_DRAFT_KEY_NOT_SET , "null" ),
435- String .format (
436- ProductBatchValidator .PRODUCT_VARIANT_DRAFT_IS_NULL ,
437- 0 ,
427+ format (ProductBatchValidator .PRODUCT_DRAFT_KEY_NOT_SET , "null" ),
428+ format (
429+ ProductBatchValidator .PRODUCT_MASTER_VARIANT_DRAFT_IS_NULL ,
438430 inValidProductDraft1 .getKey ()),
439- String . format (
431+ format (
440432 ProductBatchValidator .PRODUCT_VARIANT_DRAFT_SKU_NOT_SET ,
441433 0 ,
442434 inValidProductDraft2 .getKey ()),
443- String . format (
435+ format (
444436 ProductBatchValidator .PRODUCT_VARIANT_DRAFT_SKU_NOT_SET ,
445437 1 ,
446438 inValidProductDraft2 .getKey ()));
447439 }
448440
441+ @ Test
442+ void
443+ validateAndCollectReferencedKeys_WithNullMasterVariantsAndOneVariant_shouldHaveValidationErrors () {
444+ final ProductDraft productDraft = mock (ProductDraft .class );
445+ when (productDraft .getKey ()).thenReturn ("key" );
446+
447+ final ProductVariantDraft validVariantDraft =
448+ ProductVariantDraftBuilder .of ().key ("variantKey" ).sku ("variantSku" ).build ();
449+
450+ when (productDraft .getVariants ()).thenReturn (singletonList (validVariantDraft ));
451+
452+ final ProductBatchValidator productBatchValidator =
453+ new ProductBatchValidator (syncOptions , syncStatistics );
454+ final ImmutablePair <Set <ProductDraft >, ProductBatchValidator .ReferencedKeys > pair =
455+ productBatchValidator .validateAndCollectReferencedKeys (singletonList (productDraft ));
456+
457+ assertThat (pair .getLeft ()).isEmpty ();
458+ assertThat (this .errorCallBackMessages ).hasSize (1 );
459+ assertThat (this .errorCallBackMessages .get (0 ))
460+ .isEqualTo (
461+ format (
462+ ProductBatchValidator .PRODUCT_MASTER_VARIANT_DRAFT_IS_NULL , productDraft .getKey ()));
463+ }
464+
449465 @ Test
450466 void
451467 validateAndCollectReferencedKeys_WithCustomerGroupRefsInPrices_ShouldCollectReferencesCorrectly () {
0 commit comments