@Override public LocalizedStrings getName() { return productData.getName(); }
@Override public LocalizedString getName() { return productData.getName(); }
@Override public LocalizedStrings getName() { return productData.getName(); }
@Override public LocalizedString getName() { return productData.getName(); }
@Override public LocalizedString getName() { return productData.getName(); }
protected String extractName(final Product instance) { return instance.getMasterData().getStaged().getName().find(ENGLISH).orElse("NOTPRESENTNAME"); }
public static void withFilledCart(final BlockingSphereClient client, final Consumer<Cart> f) { withTaxedProduct(client, product -> { final Cart cart = createCartWithShippingAddress(client); assertThat(cart.getLineItems()).hasSize(0); final long quantity = 3; final String productId = product.getId(); final AddLineItem action = AddLineItem.of(productId, 1, quantity); final Cart updatedCart = client.executeBlocking(CartUpdateCommand.of(cart, action)); assertThat(updatedCart.getLineItems()).hasSize(1); final LineItem lineItem = updatedCart.getLineItems().get(0); assertThat(lineItem.getName()).isEqualTo(product.getMasterData().getStaged().getName()); assertThat(lineItem.getQuantity()).isEqualTo(quantity); f.accept(updatedCart); }); }
@Test public void transformProductIntoProductProjection() throws Exception { final Product product = getProduct(); final ProductProjection staged = product.toProjection(ProductProjectionType.STAGED); assertThat(staged).overridingErrorMessage("staged is always present").isNotNull(); assertThat(staged.getName()).isEqualTo(product.getMasterData().getStaged().getName()); final ProductProjection current = product.toProjection(ProductProjectionType.CURRENT); assertThat(current).overridingErrorMessage("current can be empty").isNull(); }
public void changeNameWithStaged(final Boolean staged) { withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final LocalizedString newName = LocalizedString.ofEnglish("newName " + RANDOM.nextInt()); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, ChangeName.of(newName, staged))); assertThat(updatedProduct.getMasterData().getStaged().getName()).isEqualTo(newName); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
public static void withFilledCartWithTaxMode(final BlockingSphereClient client, final TaxMode taxMode, final Consumer<Cart> f) { withTaxedProduct(client, product -> { final Cart cart = client.executeBlocking(CartUpdateCommand.of(createCartWithShippingAddress(client), ChangeTaxMode.of(taxMode))); assertThat(cart.getLineItems()).hasSize(0); final long quantity = 3; final String productId = product.getId(); final AddLineItem action = AddLineItem.of(productId, 1, quantity); final Cart updatedCart = client.executeBlocking(CartUpdateCommand.of(cart, action)); assertThat(updatedCart.getLineItems()).hasSize(1); final LineItem lineItem = updatedCart.getLineItems().get(0); assertThat(lineItem.getName()).isEqualTo(product.getMasterData().getStaged().getName()); assertThat(lineItem.getQuantity()).isEqualTo(quantity); f.accept(updatedCart); }); }
@Test public void changeName() throws Exception { withUpdateableProduct(client(), product -> { final LocalizedString newName = LocalizedString.ofEnglish("newName " + RANDOM.nextInt()); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, ChangeName.of(newName))); assertThat(updatedProduct.getMasterData().getStaged().getName()).isEqualTo(newName); return updatedProduct; }); }
@Test public void productUpdateByKey() throws Exception { final String key = randomKey(); withUpdateableProduct(client(), builder -> builder.key(key), product -> { final LocalizedString newName = LocalizedString.ofEnglish("newName " + RANDOM.nextInt()); final ProductUpdateCommand cmd = ProductUpdateCommand.ofKey(key, product.getVersion(), ChangeName.of(newName)); final Product updatedProduct = client().executeBlocking(cmd); assertThat(updatedProduct.getMasterData().getStaged().getName()).isEqualTo(newName); assertThat(updatedProduct.getKey()).isEqualTo(key); return updatedProduct; }); }
@Test public void addLineItem() throws Exception { withEmptyCartAndProduct(client(), (cart, product) -> { assertThat(cart.getLineItems()).isEmpty(); final long quantity = 3; final String productId = product.getId(); final AddLineItem action = AddLineItem.of(productId, MASTER_VARIANT_ID, quantity); final Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cart, action)); assertThat(updatedCart.getLineItems()).hasSize(1); final LineItem lineItem = updatedCart.getLineItems().get(0); assertThat(lineItem.getName()).isEqualTo(product.getMasterData().getStaged().getName()); assertThat(lineItem.getQuantity()).isEqualTo(quantity); assertThat(lineItem.getProductSlug()).isEqualTo(product.getMasterData().getStaged().getSlug()); assertThat(lineItem.getVariant().getIdentifier()).isEqualTo(ByIdVariantIdentifier.of(lineItem.getProductId(), lineItem.getVariant().getId())); assertThat(lineItem.getDiscountedPricePerQuantity()).isNotNull().isEmpty(); }); }
@Test public void attributeShouldKeepExpansions() throws Exception { final AttributeAccess<Reference<Product>> access = AttributeAccess.ofProductReference(); final NamedAttributeAccess<Reference<Product>> namedAccess = access.ofName("foo"); assertThat(EXPANDED_PRODUCT_REFERENCE.getObj()).overridingErrorMessage("product reference is expanded").isNotNull(); final Attribute attribute = Attribute.of(namedAccess, EXPANDED_PRODUCT_REFERENCE); assertThat(attribute.getValue(access).getObj()).isNotNull(); final String jsonFilledRef = SphereJsonUtils.toJsonString(EXPANDED_PRODUCT_REFERENCE); final String jsonEmptyRef = SphereJsonUtils.toJsonString(EXPANDED_PRODUCT_REFERENCE.filled(null)); assertThat(jsonFilledRef) .overridingErrorMessage("references are not expanded if serialized") .doesNotContain(EXPANDED_PRODUCT_REFERENCE.getObj().getMasterData().getStaged().getName().get(Locale.ENGLISH)) .isEqualTo(jsonEmptyRef); }
@Test public void updateCommandPlusUpdateActions() { withUpdateableProduct(client(), (Product product) -> { assertThat(product.getMasterData().getStaged().getMasterVariant().getImages()).hasSize(0); final Image image = createExternalImage(); final AddExternalImage updateAction1 = AddExternalImage.of(image, MASTER_VARIANT_ID); final ProductUpdateCommand command = ProductUpdateCommand.of(product, updateAction1); assertThat(command.getUpdateActions()).hasSize(1); assertThat(command.getUpdateActions().get(0)).isEqualTo(updateAction1); final LocalizedString localizedName = en("New Name"); final ChangeName updateAction2 = ChangeName.of(localizedName); final ProductUpdateCommand updatedCommand = command.plusUpdateActions(asList(updateAction2)); assertThat(updatedCommand.getUpdateActions()).hasSize(2); assertThat(updatedCommand.getUpdateActions().get(1)).isEqualTo(updateAction2); final Product updatedProduct = client().executeBlocking(updatedCommand); assertThat(updatedProduct.getMasterData().getStaged().getMasterVariant().getImages()).isEqualTo(asList(image)); assertThat(updatedProduct.getMasterData().getStaged().getName()).isEqualTo(localizedName); return updatedProduct; }); }
public static void withLineItemAndCustomLineItemFilledCart(final BlockingSphereClient client, final UnaryOperator<Cart> op) { withTaxedProduct(client, product -> { final Cart cart = createCartWithShippingAddress(client); assertThat(cart.getLineItems()).hasSize(0); final long quantity = 3; final String productId = product.getId(); final AddLineItem addLineItemAction = AddLineItem.of(productId, 1, quantity); assertThat(cart.getCustomLineItems()).hasSize(0); final MonetaryAmount money = MoneyImpl.of("23.50", EUR); final String slug = "thing-slug"; final LocalizedString name = en("thing"); final CustomLineItemDraft item = CustomLineItemDraft.of(name, slug, money, product.getTaxCategory(), 5L, null); final AddCustomLineItem addCustomLineItemAction = AddCustomLineItem.of(item); final Cart updatedCart = client.executeBlocking(CartUpdateCommand.of(cart, asList(addLineItemAction, addCustomLineItemAction))); assertThat(updatedCart.getLineItems()).hasSize(1); final LineItem lineItem = updatedCart.getLineItems().get(0); assertThat(lineItem.getName()).isEqualTo(product.getMasterData().getStaged().getName()); assertThat(lineItem.getQuantity()).isEqualTo(quantity); final Cart cartToDelete = op.apply(updatedCart); client.executeBlocking(CartDeleteCommand.of(cartToDelete)); }); }
@Test public void addLineItemOfDraftOfSku() throws Exception { withEmptyCartAndProduct(client(), (cart, product) -> { assertThat(cart.getLineItems()).isEmpty(); final long quantity = 3; final Channel inventorySupplyChannel = ChannelFixtures.persistentChannelOfRole(client(), ChannelRole.INVENTORY_SUPPLY); final Channel distributionChannel = ChannelFixtures.persistentChannelOfRole(client(), ChannelRole.PRODUCT_DISTRIBUTION); final String sku = product.getMasterData().getStaged().getMasterVariant().getSku(); final LineItemDraft lineItemDraft = io.sphere.sdk.carts.LineItemDraftBuilder.ofSku(sku, quantity) .distributionChannel(distributionChannel) .supplyChannel(inventorySupplyChannel) .build(); final AddLineItem action = AddLineItem.of(lineItemDraft); final Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cart, action)); assertThat(updatedCart.getLineItems()).hasSize(1); final LineItem lineItem = updatedCart.getLineItems().get(0); assertThat(lineItem.getName()).isEqualTo(product.getMasterData().getStaged().getName()); assertThat(lineItem.getQuantity()).isEqualTo(quantity); assertThat(lineItem.getProductSlug()).isEqualTo(product.getMasterData().getStaged().getSlug()); assertThat(lineItem.getVariant().getIdentifier()).isEqualTo(ByIdVariantIdentifier.of(lineItem.getProductId(), lineItem.getVariant().getId())); assertThat(lineItem.getSupplyChannel().toReference()).isEqualTo(inventorySupplyChannel.toReference()); assertThat(lineItem.getDistributionChannel().toReference()).isEqualTo(distributionChannel.toReference()); assertThat(lineItem.getDiscountedPricePerQuantity()).isNotNull().isEmpty(); }); }
@Test public void differentiateBetweenCurrentAndStaged() throws Exception { withUpdateableProduct(client(), product -> { final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(product, Publish.of())); final Product mixedDataProduct = client().executeBlocking(ProductUpdateCommand.of(publishedProduct, ChangeName.of(randomSlug()))); final LocalizedString nameInCurrent = mixedDataProduct.getMasterData().getCurrent().getName(); final LocalizedString nameInStaged = mixedDataProduct.getMasterData().getStaged().getName(); final ProductProjectionQuery stagedQuery = ProductProjectionQuery.of(STAGED).withPredicates(m -> m.id().is(product.getId())); assertThat(client().executeBlocking(stagedQuery).head().get().getName()).isEqualTo(nameInStaged); final ProductProjectionQuery currentQuery = ProductProjectionQuery.of(CURRENT).withPredicates(m -> m.id().is(product.getId())); assertThat(client().executeBlocking(currentQuery).head().get().getName()).isEqualTo(nameInCurrent); return mixedDataProduct; }); }
@Test public void createProductByJsonDraft() throws Exception { final JsonNodeReferenceResolver referenceResolver = new JsonNodeReferenceResolver(); withCategory(client(), category -> { withProductType(client(), randomKey(), productType -> { withTaxCategory(client(), taxCategory -> { withStateByBuilder(client(), stateBuilder -> stateBuilder.initial(true).type(StateType.PRODUCT_STATE), state -> { referenceResolver.addResourceByKey("t-shirt-category", category); referenceResolver.addResourceByKey("t-shirt-product-type", productType); referenceResolver.addResourceByKey("standard-tax", taxCategory); referenceResolver.addResourceByKey("initial-product-state", state); final ProductDraft productDraft = draftFromJsonResource("drafts-tests/product.json", ProductDraft.class, referenceResolver); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); assertThat(product.getProductType()).isEqualTo(productType.toReference()); assertThat(product.getTaxCategory()).isEqualTo(taxCategory.toReference()); assertThat(product.getState()).isEqualTo(state.toReference()); final ProductData productData = product.getMasterData().getStaged(); assertThat(productData.getName()).isEqualTo(LocalizedString.ofEnglish("red shirt")); assertThat(productData.getCategories()).extracting("id").contains(category.getId()); client().executeBlocking(ProductDeleteCommand.of(product)); }); }); }); }); }
@Test public void queryByName() throws Exception { with2products("queryByName", (p1, p2) ->{ final Query<ProductProjection> query1 = ProductProjectionQuery.of(STAGED) .withPredicates(m -> m.name().lang(ENGLISH).is(en(p1.getMasterData().getStaged().getName()))) .withSort(m -> m.createdAt().sort().desc()) .withLimit(1L); assertThat(ids(client().executeBlocking(query1))).containsOnly(p1.getId()); }); }