/** * Creates a new object initialized with the fields of the template parameter. * * @param template the template * @return a new object initialized from the template */ public static ProductVariantDraftBuilder of(final ProductVariant template) { return new ProductVariantDraftBuilder(copyAssets(template.getAssets()), copyAttributes(template.getAttributes()), template.getImages(), template.getKey(), copyPrices(template.getPrices()), template.getSku()); } }
static Optional<ProductVariant> getVariant(final Integer variantId, final ProductDataLike product) { final Optional<ProductVariant> result; final ProductVariant masterVariant = product.getMasterVariant(); if (Objects.equals(variantId, masterVariant.getId())) { result = Optional.of(masterVariant); } else { result = product.getVariants().stream().filter(v -> Objects.equals(v.getId(), variantId)).findFirst(); } return result; }
/** * Finds all variants that match the search criteria used in the search request, if any. * @return the list containing all matching variants */ default List<ProductVariant> findMatchingVariants() { return getAllVariants().stream() .filter(v -> Optional.ofNullable(v.isMatchingVariant()).orElse(false)) .collect(Collectors.toList()); }
protected Optional<PriceLike> resolvePrice(final ProductVariant variant) { PriceLike price = firstNonNull(variant.getPrice(), variant.getScopedPrice()); return Optional.ofNullable(price); }
final ProductVariant variant = productWithVariant.getMasterData().getStaged().getVariants().get(0); assertThat(productWithVariant.getMasterData().hasStagedChanges()).isEqualTo(staged); assertThat(variant.getId()).isEqualTo(2); assertThat(variant.findAttribute(moneyAttribute).get()).isEqualTo(EURO_10); assertThat(variant.findAttribute(colorAttribute).get()).isEqualTo(color); assertThat(variant.findAttribute(sizeAttribute).get()).isEqualTo(Sizes.M); assertThat(variant.getSku()).isEqualTo(sku); assertThat(variant.getKey()).isEqualTo(key); assertThat(variant.getImages()).containsExactly(image);
final LineItem lineItem = order.getLineItems().get(0); assertThat(lineItem.getProductId()).isEqualTo(product.getId()); assertThat(lineItem.getVariant().getId()).isEqualTo(variantId); final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant(); assertThat(lineItem.getVariant().getAttributes()).isEqualTo(masterVariant.getAttributes()); assertThat(lineItem.getVariant().getImages()).isEqualTo(masterVariant.getImages()); assertThat(lineItem.getVariant().getPrices()).isNotEmpty(); assertThat(lineItem.getVariant().getSku()).contains(masterVariant.getSku()); assertThat(lineItem.getQuantity()).isEqualTo(2); assertThat(lineItem.getName()).isEqualTo(name);
public void addExternalImageBySkuWithStaged(final Boolean staged) { withUpdateableProduct(client(), (Product product) -> { final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant(); assertThat(masterVariant.getImages()).hasSize(0); assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final Image image = createExternalImage(); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, AddExternalImage.ofSku(masterVariant.getSku(), image, staged))); assertThat(updatedProduct.getMasterData().getStaged().getMasterVariant().getImages()).isEqualTo(asList(image)); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
public void removeAssetBySkuWithStaged(final Boolean staged) { withProductHavingAssets(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant(); final String assetId = masterVariant.getAssets().get(0).getId(); final String sku = masterVariant.getSku(); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, RemoveAsset.ofSku(sku, assetId, staged))); final List<Asset> assets = updatedProduct.getMasterData().getStaged().getMasterVariant().getAssets(); assertThat(assets).allMatch(asset -> !asset.getId().equals(assetId)); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
public void changeAssetNameByVariantIdAndAssetKeyWithStaged(@Nonnull final Boolean staged) { withProductHavingAssets(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final LocalizedString newName = LocalizedString.ofEnglish("new name"); final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant(); final String assetKey = masterVariant.getAssets().get(0).getKey(); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, ChangeAssetName.ofAssetKeyAndVariantId(masterVariant.getId(), assetKey, newName, staged)); final Product updatedProduct = client().executeBlocking(cmd); final Asset updatedAsset = updatedProduct.getMasterData().getStaged().getMasterVariant().getAssets().get(0); assertThat(updatedAsset.getName()).isEqualTo(newName); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
/** * Finds a variant by SKU. * @param sku the sku for the variant * @return Optional of the found variant */ default Optional<ProductVariant> findVariantBySku(final String sku) { Objects.requireNonNull(sku); return getAllVariants().stream().filter(v -> sku.equals(v.getSku())).findFirst(); }
public default Optional<Attribute> getAttribute(final String attributeName) { requireNonNull(attributeName); return getAttributes().stream().filter(attr -> attr.getName().equals(attributeName)).findAny(); }
final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant(); final ProductVariant productVariant = lineItem.getVariant(); assertThat(productVariant.getAttributes()).isEqualTo(attributesOfOrder).isNotEqualTo(masterVariant.getAttributes()); assertThat(productVariant.getImages()).isEqualTo(images).isNotEqualTo(masterVariant.getImages()); assertEqualPrices(productVariant.getPrices(), prices);
final LineItem lineItem = order.getLineItems().get(0); assertThat(lineItem.getProductId()).isEqualTo(productId); assertThat(lineItem.getVariant().getId()).isEqualTo(1); assertThat(lineItem.getVariant().getPrices().get(0).getValue()).isEqualTo(EURO_20); assertThat(lineItem.getVariant().getAttribute("foo").getValue(AttributeAccess.ofString())).isEqualTo("bar"); assertThat(lineItem.getTaxRate().getName()).isEqualTo("default-tax"); assertThat(lineItem.getTaxRate().isIncludedInPrice()).isTrue();
public void setProductVariantKeyByVariantIdWithStaged(final Boolean staged) { final String key = randomKey(); withProduct(client(), (Product product) -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final Integer variantId = product.getMasterData().getStaged().getMasterVariant().getId(); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, SetProductVariantKey.ofKeyAndVariantId(key, variantId, staged)); final Product updatedProduct = client().executeBlocking(cmd); assertThat(updatedProduct.getMasterData().getStaged().getMasterVariant().getKey()).isEqualTo(key); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); }); }
protected Optional<String> createImage(final ProductVariant variant) { return variant.getImages().stream().findFirst().map(Image::getUrl); }
public void setProductVariantKeyBySkuWithStaged(final Boolean staged) { final String key = randomKey(); withProduct(client(), (Product product) -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final String sku = product.getMasterData().getStaged().getMasterVariant().getSku(); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, SetProductVariantKey.ofKeyAndSku(key, sku, staged)); final Product updatedProduct = client().executeBlocking(cmd); assertThat(updatedProduct.getMasterData().getStaged().getMasterVariant().getKey()).isEqualTo(key); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); }); }
public void setAttributeWithObjectsWithStaged(final Boolean staged) { withProduct(client(), (Product product) -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final String sku = product.getMasterData().getStaged().getMasterVariant().getSku(); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, asList( SetAttribute.ofSku(sku, "size", "M", staged), SetAttribute.ofVariantId(MASTER_VARIANT_ID, "color", "red", staged) ))); final ProductVariant masterVariant = updatedProduct.getMasterData().getStaged().getMasterVariant(); assertThat(masterVariant.getAttribute("size").getValueAsEnumValue()).isEqualTo(EnumValue.of("M", "M")); assertThat(masterVariant.getAttribute("color").getValueAsLocalizedEnumValue().getKey()).isEqualTo("red"); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); }); }
@Test public void executeValidQuery(){ withUpdateableProductDiscount(client(),((productDiscount, product) -> { final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant(); assertThat(masterVariant.getPrices()).isNotEmpty(); final Price masterVariantPrice = masterVariant.getPrices().get(0); final ProductDiscount queryedProductDiscount = client().executeBlocking(MatchingProductDiscountGet.of(product.getId(), masterVariant.getId(),true , masterVariantPrice)); assertThat(productDiscount).isEqualTo(queryedProductDiscount); return productDiscount; })); }
assertThat(productProjectionOptional).isPresent(); final ProductProjection productProjection = productProjectionOptional.get(); assertThat(productProjection.getMasterVariant().isMatchingVariant()).isFalse(); final ProductVariant variantWithAttribute2 = productProjection.getVariants().get(0); assertThat(variantWithAttribute2.isMatchingVariant()).isFalse(); final ProductVariant positiveVariant = productProjection.getVariants().get(1); assertThat(positiveVariant.getAttribute(attributeName).getValueAsString()).isEqualTo("3"); assertThat(positiveVariant.isMatchingVariant()).isTrue(); }); assertEventually(() -> { final Optional<ProductProjection> productProjectionOptional = client().executeBlocking(withoutFlagSearchRequest).head(); assertThat(productProjectionOptional).isPresent(); assertThat(productProjectionOptional.get().getMasterVariant().isMatchingVariant()).isNull(); assertThat(productProjectionOptional.get().getVariants().get(0).isMatchingVariant()).isNull(); final ProductVariant positiveVariant = productProjectionOptional.get().getVariants().get(1); assertThat(positiveVariant.getAttribute(attributeName).getValueAsString()).isEqualTo("3"); assertThat(positiveVariant.isMatchingVariant()).isNull(); });
private Price searchWith(final Product product, final PriceSelection selection) { final ProductProjectionSearch searchRequest = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.id().is(product.getId()))//to limit the test scope .withPriceSelection(selection);//price selection config final PagedSearchResult<ProductProjection> searchResult = client().executeBlocking(searchRequest); assertThat(searchResult.getCount()).isEqualTo(1); return searchResult.head().get().getMasterVariant().getPrice(); }