static Reference<Product> reference(final Product product) { return Reference.of(referenceTypeId(), product.getId(), product); }
@Override public boolean isPublished() { return product.getMasterData().isPublished(); } }
@Override public Reference<ProductType> getProductType() { return product.getProductType(); }
withTaxedProduct(client(), product3 -> { final LineItemDraft lineItemDraft1 = LineItemDraft.of(product1, 1, 15); final LineItemDraft lineItemDraftOfVariantIdentifier = LineItemDraftBuilder.ofVariantIdentifier(product2.getMasterData().getStaged().getMasterVariant().getIdentifier(), 25L).build(); String sku = product3.getMasterData().getStaged().getMasterVariant().getSku(); final LineItemDraft lineItemDraftOfSku = LineItemDraftBuilder.ofSkuVariantIdentifier(BySkuVariantIdentifier.of(sku), 35L).build(); final List<LineItemDraft> lineItems = asList(lineItemDraft1, lineItemDraftOfVariantIdentifier, lineItemDraftOfSku); final List<CustomLineItemDraft> customLineItems = singletonList(CustomLineItemDraft.of(randomSlug(), "foo-bar", EURO_5, product1.getTaxCategory(), 1L, null)); s.assertThat(cart.getLineItems()).hasSize(3); final LineItem lineItem1 = cart.getLineItems().get(0); s.assertThat(lineItem1.getProductId()).isEqualTo(product1.getId()); s.assertThat(lineItem1.getQuantity()).isEqualTo(15); s.assertThat(lineItem1.getProductType()).isEqualTo(product1.getProductType()); s.assertThat(lineItem1.getProductType().getObj()).isNotNull(); s.assertThat(lineItem2.getProductId()).isEqualTo(product2.getId()); s.assertThat(lineItem2.getVariant().getId()).isEqualTo(product2.getMasterData().getStaged().getMasterVariant().getId()); s.assertThat(lineItem3.getProductId()).isEqualTo(product3.getId()); s.assertThat(lineItem3.getVariant().getId()).isEqualTo(product3.getMasterData().getStaged().getMasterVariant().getId()); s.assertThat(lineItem3.getVariant().getSku()).isEqualTo(product3.getMasterData().getStaged().getMasterVariant().getSku());
@Test public void lightweigthProductsBySku() { withProduct(client(), product1 -> { withProduct(client(), product2 -> { final List<String> skus = asList(product1, product2).stream() .map(product -> product.getMasterData().getStaged().getMasterVariant().getSku()) .collect(Collectors.toList()); final Set<LightweightProduct> actual = new HashSet<>(client().executeBlocking(LightweightProduct.requestOfSkus(skus))); final Set<LightweightProduct> expected = asList(product1, product2).stream() .map(product -> { final String sku = product.getMasterData().getStaged().getMasterVariant().getSku(); return new LightweightProduct(product.getId(), product.getVersion(), singletonList(sku)); }) .collect(Collectors.toSet()); assertThat(actual).isEqualTo(expected); }); }); } }
@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)); }); }); }); }); }
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 sortByAttributeAscending() throws Exception { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .withSort(productModel -> productModel.allVariants().attribute().ofNumber(ATTR_NAME_SIZE).asc()); testResultIds(search, resultIds -> assertThat(resultIds).containsExactly(product2.getId(), product1.getId(), product3.getId())); }
@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; }); }
@Override public Optional<Reference<TaxCategory>> getTaxCategory() { return product.getTaxCategory(); }
@Test public void createProductWithExternalImage() throws Exception { withStateByBuilder(client(), stateBuilder -> stateBuilder.initial(true).type(PRODUCT_STATE), initialProductState -> { final ProductType productType = ProductTypeFixtures.defaultProductType(client()); final Image image = Image.ofWidthAndHeight("http://www.commercetools.com/assets/img/ct_logo_farbe.gif", 460, 102, "commercetools logo"); final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of() .images(image) .build(); final TaxCategory taxCategory = defaultTaxCategory(client()); final SearchKeywords searchKeywords = SearchKeywords.of(Locale.ENGLISH, asList(SearchKeyword.of("foo bar baz", CustomSuggestTokenizer.of(asList("foo, baz"))))); final ProductDraft productDraft = ProductDraftBuilder .of(productType, en("product with external image"), randomSlug(), masterVariant) .taxCategory(taxCategory) .searchKeywords(searchKeywords) .state(initialProductState) .build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); final Image loadedImage = product.getMasterData().getStaged().getMasterVariant().getImages().get(0); assertThat(loadedImage).isEqualTo(image); assertThat(product.getTaxCategory()).isEqualTo(taxCategory.toReference()); assertThat(product.getMasterData().getStaged().getSearchKeywords()).isEqualTo(searchKeywords); assertThat(product.getState()).isEqualTo(initialProductState.toReference()); //clean up test client().executeBlocking(ProductDeleteCommand.of(product)); }); }
@Test public void variantIdentifierIsAvailable() throws Exception { withProduct(client(), product -> { final ByIdVariantIdentifier identifier = product.getMasterData().getStaged().getMasterVariant().getIdentifier(); assertThat(identifier).isEqualTo(ByIdVariantIdentifier.of(product.getId(), 1)); }); }
type -> { referenceResolver.addResourceByKey("sample-product", product); referenceResolver.addResourceByKey("standard-tax", product.getTaxCategory()); final CartDraft cartDraft = draftFromJsonResource("drafts-tests/cart.json", CartDraft.class, referenceResolver); assertThat(cart.getInventoryMode()).isEqualTo(InventoryMode.TRACK_ONLY); final LineItem lineItem = cart.getLineItems().get(0); assertThat(lineItem.getProductId()).isEqualTo(product.getId()); assertThat(lineItem.getVariant().getId()).isEqualTo(1); assertThat(lineItem.getQuantity()).isEqualTo(2); assertThat(customLineItem.getMoney()).isEqualTo(EURO_20); assertThat(customLineItem.getSlug()).isEqualTo("foo"); assertThat(customLineItem.getTaxCategory()).isEqualTo(product.getTaxCategory()); final Address expectedAddress = Address.of(DE).withLastName("Osgood").withFax("0300000000"); assertThat(cart.getShippingAddress()).isEqualTo(expectedAddress);
@Test public void queryByProductType() throws Exception { with2products("queryByProductType", (p1, p2) ->{ final Query<ProductProjection> query = ProductProjectionQuery.of(STAGED) .byProductType(p1.getProductType()) .withExpansionPaths(m -> m.productType()); final PagedQueryResult<ProductProjection> queryResult = client().executeBlocking(query); assertThat(queryResult.head().get().getProductType()).is(expanded()); assertThat(ids(queryResult)).containsOnly(p1.getId()); }); }
@Override default Reference<Product> toReference() { return reference(this); }
@Test public void productType() throws Exception { final Consumer<Product> user = product -> { final Query<Product> query = ProductQuery.of(). bySlug(ProductProjectionType.CURRENT, Locale.ENGLISH, englishSlugOf(product.getMasterData().getStaged())). withExpansionPaths(ProductExpansionModel.of().productType()). toQuery(); final PagedQueryResult<Product> queryResult = client().executeBlocking(query); final Reference<ProductType> productTypeReference = queryResult.head().get().getProductType(); assertThat(productTypeReference).is(expanded()); }; withProduct(client(), "productTypeReferenceExpansion", user); }
@Test public void taxCategory() throws Exception { TaxCategoryFixtures.withTransientTaxCategory(client(), taxCategory -> withProduct(client(), product -> { final Product productWithTaxCategory = client().executeBlocking(ProductUpdateCommand.of(product, SetTaxCategory.of(taxCategory))); assertThat(productWithTaxCategory.getTaxCategory()).isNotNull(); final Query<Product> query = ProductQuery.of(). bySlug(ProductProjectionType.CURRENT, Locale.ENGLISH, englishSlugOf(product.getMasterData().getStaged())). withExpansionPaths(ProductExpansionModel.of().taxCategory()). toQuery(); final PagedQueryResult<Product> queryResult = client().executeBlocking(query); final Reference<TaxCategory> productTypeReference = firstOf(queryResult).getTaxCategory(); assertThat(productTypeReference).is(expanded()); }) ); } }
@Override public Long getVersion() { return product.getVersion(); }
@Test public void message() { ProductTypeFixtures.withEmptyProductType(client(), productType -> { final String key = randomKey(); final Image image = createExternalImage(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, randomSlug(), randomSlug(), ProductVariantDraftBuilder.of().images(image).build()).key(key).build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); final Product deletedProduct = client().executeBlocking(ProductDeleteCommand.ofKey(key, product.getVersion())); Query<ProductDeletedMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(deletedProduct)) .withSort(m -> m.createdAt().sort().desc()) .withExpansionPaths(m -> m.resource()) .withLimit(1L) .forMessageType(ProductDeletedMessage.MESSAGE_HINT); assertEventually(() -> { final PagedQueryResult<ProductDeletedMessage> queryResult = client().executeBlocking(messageQuery); assertThat(queryResult.head()).isPresent(); final ProductDeletedMessage message = queryResult.head().get(); assertThat(message.getResource().getId()).isEqualTo(product.getId()); assertThat(message.getRemovedImageUrls()).containsExactly(image.getUrl()); }); }); } }
@Test public void queryByReviewRating() { withProduct(client(), product -> { withReview(client(), b -> b.target(product).rating(1), review1 -> { withReview(client(), b -> b.target(product).rating(3), review2 -> { assertEventually(() -> { final ProductQuery query = ProductQuery.of() .withPredicates(m -> m.reviewRatingStatistics().averageRating().is(2.0)) .plusPredicates(m -> m.reviewRatingStatistics().count().is(2)) .plusPredicates(m -> m.is(product)); final List<Product> results = client().executeBlocking(query).getResults(); assertThat(results).hasSize(1); final Product loadedProduct = results.get(0); assertThat(loadedProduct.getId()).isEqualTo(product.getId()); assertThat(loadedProduct.getReviewRatingStatistics().getCount()).isEqualTo(2); }); }); }); }); }