public static PriceDraftBuilder of(final Price template) { return of(template.getValue()) .country(template.getCountry()) .customerGroup(template.getCustomerGroup()) .channel(Optional.ofNullable(template.getChannel()).map(Referenceable::toResourceIdentifier).orElse(null)) .validFrom(template.getValidFrom()) .validUntil(template.getValidUntil()) .custom(copyCustom(template)); } }
/** * 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 PriceBuilder of(final Price template) { return new PriceBuilder(template.getChannel(), template.getCountry(), template.getCustom(), template.getCustomerGroup(), template.getDiscounted(), template.getId(), template.getTiers(), template.getValidFrom(), template.getValidUntil(), template.getValue()); } }
@JsonIgnore public static Price of(final BigDecimal amount, final CurrencyUnit currencyUnit) { return of(MoneyImpl.of(amount, currencyUnit)); } }
public static PriceBuilder of(final Price template) { return of(template.getValue()) .country(template.getCountry()) .customerGroup(template.getCustomerGroup()) .channel(template.getChannel()) .discounted(template.getDiscounted()); }
protected MoneyContext getMoneyContext(final LineItem lineItem) { return MoneyContext.of(lineItem.getPrice().getValue().getCurrency(), userContext.locale()); } }
public void setDiscountedPriceWithStaged(final Boolean staged) { final ProductDiscountPredicate predicate = ProductDiscountPredicate.of("1 = 1");//can be used for all products final ProductDiscountDraft productDiscountDraft = ProductDiscountDraft.of(randomSlug(), randomSlug(), predicate, ExternalProductDiscountValue.of(), randomSortOrder(), true); //don't forget that one product discount can be used for multiple products withProductDiscount(client(), productDiscountDraft, externalProductDiscount -> { withProductOfPrices(client(), singletonList(PriceDraft.of(EURO_40)), product -> { final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(product, Publish.of())); final Price originalPrice = publishedProduct.getMasterData().getStaged().getMasterVariant().getPrices().get(0); assertThat(originalPrice.getDiscounted()).isNull(); final String priceId = originalPrice.getId(); final SetDiscountedPrice action = SetDiscountedPrice.of(priceId, DiscountedPrice.of(EURO_5, externalProductDiscount.toReference()), staged); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(publishedProduct, action)); final Price stagedPrice = updatedProduct.getMasterData().getStaged().getMasterVariant().getPrices().get(0); assertThat(stagedPrice.getValue()).isEqualTo(EURO_40); assertThat(stagedPrice.getDiscounted().getValue()).isEqualTo(EURO_5); assertThat(stagedPrice.getDiscounted().getDiscount()).isEqualTo(externalProductDiscount.toReference()); final Price currentPrice = updatedProduct.getMasterData().getCurrent().getMasterVariant().getPrices().get(0); if (staged) { assertThat(stagedPrice).isNotEqualTo(currentPrice); } else { assertThat(stagedPrice).isEqualTo(currentPrice); } }); }); }
@Test public void checkProductVaiantAtributes() throws Exception { assertThat(productVariant.getSku().equals("1f92d491-2180-489c-bcef-3966cdc68c0b")); assertThat(productVariant.getPrices()) .as("comparing prices ") .isEqualTo(asList(Price.of(BigDecimal.valueOf(12.34), DefaultCurrencyUnits.EUR).withCountry(CountryCode.DE).withId("70a56289-bd20-4c47-8364-47b498ad993b"))); assertThat(productVariant.getImages().isEmpty()); assertThat(productVariant.getAssets().isEmpty()); assertThat(productVariant.getAttributes().isEmpty()); } }
@Test public void lineItemModel() throws Exception { withFilledCart(client(), cart -> { final LineItem lineItem = cart.getLineItems().get(0); final String englishName = lineItem.getName().get(Locale.ENGLISH); final Price price = lineItem.getPrice(); final Long centAmount = price.getValue().query(MonetaryQueries.convertMinorPart()); final ItemState itemState = lineItem.getState().stream().findFirst().get(); final CartQuery query = CartQuery.of() .withLimit(1L) .withPredicates(m -> m.lineItems().id().is(lineItem.getId()) .and(m.lineItems().quantity().is(lineItem.getQuantity())) .and(m.lineItems().productId().is(lineItem.getProductId())) .and(m.lineItems().name().locale(ENGLISH).is(englishName)) .and(m.lineItems().variant().sku().is(lineItem.getVariant().getSku())) .and(m.lineItems().price().discounted().isNotPresent()) .and(m.lineItems().price().id().is(price.getId())) .and(m.lineItems().price().value().centAmount().is(centAmount)) .and(m.lineItems().price().country().is(price.getCountry())) .and(m.lineItems().state().quantity().is(itemState.getQuantity())) .and(m.lineItems().state().state().is(itemState.getState())) ); final CartQuery cartQuery = query.plusPredicates(m -> m.id().is(cart.getId())); final Cart loadedCart = client().executeBlocking(cartQuery).head().get(); assertThat(loadedCart.getId()).isEqualTo(cart.getId()); }); }
@Test public void createProductWithCustomPrice() { withEmptyProductType(client(), randomKey(), productType -> withUpdateableType(client(), type -> { final String value = "foo"; final PriceDraft price = PriceDraft.of(EURO_1) .withCustom(CustomFieldsDraft.ofTypeIdAndObjects(type.getId(), singletonMap(STRING_FIELD_NAME, value))); final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().price(price).build(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, randomSlug(), randomSlug(), masterVariant).build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); final Price loadedPrice = product.getMasterData().getStaged().getMasterVariant().getPrices().get(0); assertThat(loadedPrice.getValue()).isEqualTo(EURO_1); assertThat(loadedPrice.getCustom().getFieldAsString(STRING_FIELD_NAME)).isEqualTo(value); client().executeBlocking(ProductDeleteCommand.of(product)); return type; }) ); }
@Test public void createProductWithTiers() { withEmptyProductType(client(), randomKey(), productType -> withUpdateableType(client(), type -> { final List<PriceTier> tiers = Arrays.asList(PriceTierBuilder.of(10, EURO_5).build()); final PriceDraft priceWithTiers = PriceDraftBuilder.of(EURO_1) .tiers(tiers) .build(); final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of() .price(priceWithTiers) .build(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, randomSlug(), randomSlug(), masterVariant).build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); final Price loadedPrice = product.getMasterData().getStaged().getMasterVariant().getPrices().get(0); assertThat(loadedPrice.getValue()).isEqualTo(EURO_1); assertThat(loadedPrice.getTiers()).containsExactlyElementsOf(tiers); client().executeBlocking(ProductDeleteCommand.of(product)); return type; }) ); }
@Test public void expandedDiscountedPrice() throws Exception { final Product product = SphereJsonUtils.readObjectFromResource("product-with-expanded-discounted-price.json", Product.typeReference()); final Price price = product.getMasterData().getStaged().getMasterVariant().getPrices().get(0); final DiscountedPrice discountedPrice = price.getDiscounted(); final ProductDiscount productDiscount = discountedPrice.getDiscount().getObj(); assertThat(productDiscount.getName().get(Locale.ENGLISH)).isEqualTo("demo product discount"); assertThat(productDiscount.getValue()).isInstanceOf(AbsoluteProductDiscountValue.class); }
@Test public void canExpandChannelOfPrices() throws Exception { ChannelFixtures.withChannelOfRole(client(), ChannelRole.INVENTORY_SUPPLY, channel -> { withUpdateablePricedProduct(client(), PriceDraft.of(MoneyImpl.of(new BigDecimal("12.34"), EUR)).withCountry(DE).withChannel(channel), product -> { final ExpansionPath<Product> expansionPath = ProductExpansionModel.of().masterData().staged().masterVariant().prices().channel().expansionPaths().get(0); final Query<Product> query = query(product).withExpansionPaths(expansionPath); final List<Price> prices = client().executeBlocking(query).head().get().getMasterData().getStaged().getMasterVariant().getPrices(); assertThat(prices .stream() .anyMatch(price -> Optional.ofNullable(price.getChannel()).map(channelRef -> channelRef.getObj() != null).orElse(false))) .isTrue(); return product; }); }); }
@Test public void canExpandCustomerGroupOfPrices() throws Exception { withCustomerGroup(client(), customerGroup -> { withUpdateablePricedProduct(client(), PriceDraft.of(MoneyImpl.of(new BigDecimal("12.34"), EUR)).withCountry(DE).withCustomerGroup(customerGroup), product -> { final ExpansionPath<Product> expansionPath = ProductExpansionModel.of().masterData().staged().masterVariant().prices().customerGroup().expansionPaths().get(0); final Query<Product> query = query(product).withExpansionPaths(expansionPath); final List<Price> prices = client().executeBlocking(query).head().get().getMasterData().getStaged().getMasterVariant().getPrices(); assertThat(prices .stream() .anyMatch(price -> Optional.ofNullable(price.getCustomerGroup()).map(customerGroupReference -> customerGroupReference.getObj() != null).orElse(false))) .isTrue(); return product; }); } ); }
@Test public void setProductPriceCustomTypeAndsetProductPriceCustomField() { withUpdateableType(client(), type -> { withUpdateablePricedProduct(client(), product -> { final String priceId = getFirstPrice(product).getId(); final UpdateAction<Product> updateAction = SetProductPriceCustomType. ofTypeIdAndObjects(type.getId(), STRING_FIELD_NAME, "a value", priceId); final ProductUpdateCommand productUpdateCommand = ProductUpdateCommand.of(product, updateAction); final Product updatedProduct = client().executeBlocking(productUpdateCommand); final Price price = getFirstPrice(updatedProduct); assertThat(price.getCustom().getFieldAsString(STRING_FIELD_NAME)) .isEqualTo("a value"); final Product updated2 = client().executeBlocking(ProductUpdateCommand.of(updatedProduct, SetProductPriceCustomField.ofObject(STRING_FIELD_NAME, "a new value", priceId))); assertThat(getFirstPrice(updated2).getCustom().getFieldAsString(STRING_FIELD_NAME)) .isEqualTo("a new value"); return updated2; }); return type; }); }
private static CustomFieldsDraft customFieldsDraftOrNull(final Price template) { return Optional.ofNullable(template.getCustom()) .map(CustomFieldsDraft::ofCustomFields) .orElse(null); } }
/** * Action to remove a price * @param price the price to remove including an ID * @return action */ public static RemovePrice of(final Price price) { return of(Optional.ofNullable(price.getId()).orElseThrow(() -> new IllegalArgumentException("Expected price with ID."))); }
@Test public void equalsIgnoreId() { final String jsonAsString = "{\n" + " \"id\": \"foo-id\",\n" + " \"value\": {\n" + " \"currencyCode\": \"EUR\",\n" + " \"centAmount\": 2800\n" + " }\n" + " }"; final Price price1 = SphereJsonUtils.readObject(jsonAsString, Price.class); final Price price2 = SphereJsonUtils.readObject(jsonAsString.replace("foo-id", "bar-id"), Price.class); assertThat(price1.getId()).isEqualTo("foo-id"); assertThat(price2.getId()).isEqualTo("bar-id"); assertThat(price1).isNotEqualTo(price2); assertThat(price1.equalsIgnoreId(price2)).isTrue(); } }
@Override public Price build() { return new Price(value, country, customerGroup, channel, discounted); } }
public Price withCountry(final CountryCode country) { return withCountry(Optional.of(country)); }
public static PriceBuilder of(final Price template) { return of(template.getValue()) .country(template.getCountry()) .customerGroup(template.getCustomerGroup()) .channel(template.getChannel()) .discounted(template.getDiscounted()); }