ProductToProductProjectionWrapper(final Product product, final ProductProjectionType productProjectionType) { this.product = product; this.productData = product.getMasterData().get(productProjectionType); }
@Nullable default ProductData get(final ProductProjectionType productProjectionType) { return productProjectionType == CURRENT ? getCurrent() : getStaged(); } }
@Override public boolean hasStagedChanges() { return product.getMasterData().hasStagedChanges(); }
public void changeSlugWithStaged(final Boolean staged) { withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final LocalizedString newSlug = LocalizedString.ofEnglish("new-slug-" + RANDOM.nextInt()); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, ChangeSlug.of(newSlug, staged))); assertThat(updatedProduct.getMasterData().getStaged().getSlug()).isEqualTo(newSlug); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
@Test public void readAttributeGetValueAs() throws Exception { final ProductVariant masterVariant = createProduct().getMasterData().getStaged().getMasterVariant(); final String attributeValue = masterVariant.findAttribute(SIZE_ATTR_NAME) .map((Attribute a) -> { final EnumValue enumValue = a.getValueAsEnumValue(); return enumValue.getLabel(); }) .orElse("not found"); assertThat(attributeValue).isEqualTo("S"); }
public void setSkuWithStaged(final Boolean staged) { final String oldSku = randomKey(); withProductOfSku(oldSku, (Product product) -> { assertThat(product.getMasterData().getStaged().getMasterVariant().getSku()).isEqualTo(oldSku); assertThat(product.getMasterData().getCurrent().getMasterVariant().getSku()).isEqualTo(oldSku); assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final String newSku = randomKey(); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, SetSku.of(MASTER_VARIANT_ID, newSku, staged)); final Product updatedProduct = client().executeBlocking(cmd); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
@Override public boolean isPublished() { return product.getMasterData().isPublished(); } }
@Test public void message() { ProductFixtures.withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().isPublished()).isFalse(); final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(product, Publish.of())); Query<ProductPublishedMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(product)) .withSort(m -> m.createdAt().sort().desc()) .withExpansionPaths(m -> m.resource()) .withLimit(1L) .forMessageType(ProductPublishedMessage.MESSAGE_HINT); assertEventually(() -> { final PagedQueryResult<ProductPublishedMessage> queryResult = client().executeBlocking(messageQuery); assertThat(queryResult.head()).isPresent(); final ProductPublishedMessage message = queryResult.head().get(); assertThat(message.getResource().getId()).isEqualTo(product.getId()); assertThat(message.getProductProjection().getMasterVariant()).isEqualTo(publishedProduct.getMasterData().getCurrent().getMasterVariant()); assertThat(message.getResource().getObj().getMasterData().getCurrent().getSlug()).isEqualTo(message.getProductProjection().getSlug()); assertThat(message.getRemovedImageUrls().size()).isEqualTo(0); }); return publishedProduct; }); } }
@Test public void recalculateAndUpdateProductData() throws Exception { withEmptyCartAndProduct(client(), (emptyCart, product) -> { //create cart with line item final AddLineItem action = AddLineItem.of(product.getId(), MASTER_VARIANT_ID, 1L); final Cart cartWithLineItem = client().executeBlocking(CartUpdateCommand.of(emptyCart, action)); final NamedAttributeAccess<LocalizedEnumValue> colorAttribute = Colors.ATTRIBUTE; final LocalizedEnumValue oldColor = cartWithLineItem.getLineItems().get(0).getVariant().findAttribute(colorAttribute).get(); //update the product final LocalizedEnumValue newValueForColor = Colors.RED; final SetAttribute localizedEnumUpdate = SetAttribute.of(MASTER_VARIANT_ID, colorAttribute, newValueForColor); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, asList(localizedEnumUpdate, Publish.of()))); assertThat(updatedProduct.getMasterData().getCurrent().getMasterVariant().findAttribute(colorAttribute)).contains(newValueForColor); //check the line item in the cart, the product data will not be updated final LineItem lineItemOfTheChangedProduct = client().executeBlocking(CartByIdGet.of(cartWithLineItem)).getLineItems().get(0); assertThat(lineItemOfTheChangedProduct.getVariant().findAttribute(colorAttribute)) .as("the new product attribute value is not automatically propagated to the line item in the cart") .contains(oldColor); //use recalculate with updateProductData flag, the product data will be updated final Cart recalculatedCart = client().executeBlocking(CartUpdateCommand.of(cartWithLineItem, Recalculate.of().withUpdateProductData(Boolean.TRUE))); assertThat(recalculatedCart.getLineItems().get(0).getVariant().findAttribute(colorAttribute)) .contains(newValueForColor); }); }
public void setDescriptionWithStaged(final Boolean staged) { withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final LocalizedString newDescription = LocalizedString.ofEnglish("new description " + RANDOM.nextInt()); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, SetDescription.of(newDescription, staged)); final Product updatedProduct = client().executeBlocking(cmd); assertThat(updatedProduct.getMasterData().getStaged().getDescription()).isEqualTo(newDescription); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
public static void withProductOfRestockableInDaysAndChannel(final BlockingSphereClient client, final int restockableInDays, @Nullable final Referenceable<Channel> channelReferenceable, final Consumer<Product> productConsumer) { final Reference<Channel> channelReference = Optional.ofNullable(channelReferenceable).map(Referenceable::toReference).orElse(null); ProductFixtures.withProduct(client, product -> { final String sku = product.getMasterData().getStaged().getMasterVariant().getSku(); final InventoryEntry inventoryEntry = client.executeBlocking(InventoryEntryCreateCommand.of(InventoryEntryDraft.of(sku, 5, null, restockableInDays, channelReference))); productConsumer.accept(product); client.executeBlocking(InventoryEntryDeleteCommand.of(inventoryEntry)); }); }
@Test public void setSku() throws Exception { final String oldSku = randomKey(); withProductOfSku(oldSku, (Product product) -> { assertThat(product.getMasterData().getStaged().getMasterVariant().getSku()).isEqualTo(oldSku); assertThat(product.getMasterData().getCurrent().getMasterVariant().getSku()).isEqualTo(oldSku); assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final String newSku = randomKey(); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, SetSku.of(MASTER_VARIANT_ID, newSku)); final Product updatedProduct = client().executeBlocking(cmd); assertThat(updatedProduct.getMasterData().getStaged().getMasterVariant().getSku()) .as("update action updates SKU in staged") .isEqualTo(newSku); assertThat(updatedProduct.getMasterData().getCurrent().getMasterVariant().getSku()) .as("update action updates NOT directly in current") .isEqualTo(oldSku) .isNotEqualTo(newSku); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isTrue(); return updatedProduct; }); }
@Override public Boolean isPublished() { return product.getMasterData().isPublished(); }
@Test public void createPublishedProduct() { withEmptyProductType(client(), randomKey(), productType -> { final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().build(); final LocalizedString name = randomSlug(); final LocalizedString slug = randomSlug(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, name, slug, masterVariant) .publish(true) .build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); assertThat(product.getMasterData().isPublished()).isTrue(); assertThat(product.getMasterData().getCurrent().getSlug()).isEqualTo(slug); assertEventually(() -> { final ProductProjectionSearch search = ProductProjectionSearch.ofCurrent() .withQueryFilters(m -> m.id().is(product.getId())); final PagedSearchResult<ProductProjection> searchResult = client().executeBlocking(search); assertThat(searchResult.getResults()).hasSize(1); }); unpublishAndDelete(product); }); }
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; }); }
@Nullable default ProductData get(final ProductProjectionType productProjectionType) { return productProjectionType == CURRENT ? getCurrent() : getStaged(); } }
@Test public void readAttributeWithoutProductTypeWithNamedAccessWithWrongType() throws Exception { final ProductVariant masterVariant = createProduct().getMasterData().getStaged().getMasterVariant(); assertThatThrownBy(() -> masterVariant.findAttribute(SIZE_ATTR_NAME, AttributeAccess.ofBoolean())) .isInstanceOf(JsonException.class); }
ProductToProductProjectionWrapper(final Product product, final ProductProjectionType productProjectionType) { this.product = product; this.productData = product.getMasterData().get(productProjectionType).get(); }
@Override public boolean isPublished() { return product.getMasterData().isPublished(); }
@Override public Boolean hasStagedChanges() { return product.getMasterData().hasStagedChanges(); }