@Override default Reference<Product> toReference() { return Product.reference(getId()); }
/** * 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(); }
/** * Finds the first matching variant according to the search criteria used in the search request, if any. * @return the first product variant that matches the search criteria, or empty if none does */ default Optional<ProductVariant> findFirstMatchingVariant() { return findMatchingVariants().stream().findFirst(); }
default Versioned<Product> toProductVersioned() { return Versioned.of(getId(), getVersion()); } }
default Optional<ProductVariant> findVariant(final ByIdVariantIdentifier identifier){ return getId().equals(identifier.getProductId()) ? Optional.ofNullable(getVariant(identifier.getVariantId())) : Optional.empty(); }
/** * Gets products from the same categories as the given product, excluding the product itself, up to {@code numProducts}. * Most expensive products first. * @param product the product to get suggestions for * @param numProducts the number of products the returned set should contain * @return the products related to this product */ @Override public CompletionStage<Set<ProductProjection>> relatedToProduct(final ProductProjection product, final int numProducts) { final Set<String> categoryIds = product.getCategories().stream() .map(Reference::getId) .collect(toSet()); if (categoryIds.isEmpty()) { return CompletableFuture.completedFuture(emptySet()); } else { return productsFromCategoryIds(categoryIds, numProducts + 1) .thenApply(products -> products.stream() .filter(p -> !p.getId().equals(product.getId())) .limit(numProducts) .collect(toSet())); } }
private Condition<ProductProjection> productWithMatchingVariantsHavingMaxSize() { final Predicate<ProductProjection> matchingVariantsHaveMaxSize = product -> { final long productMaximumSize = product.getAllVariants().stream() .mapToLong(variant -> variant.findAttribute(SIZE_ATTRIBUTE_ACCESS).orElse(0L)) .max() .getAsLong(); return product.findMatchingVariants().stream() .mapToLong(variant -> variant.findAttribute(SIZE_ATTRIBUTE_ACCESS).get()) .allMatch(size -> size == productMaximumSize); }; return new Condition<>(matchingVariantsHaveMaxSize, "all matching variants have maximum size"); }
final List<EnumValue> allVariantsSizeValues = productProjection.getAllVariants() .stream() .map(variant -> variant.getAttribute(sizeAttributeName).getValueAsEnumValue()) final Integer variantIdOfSizeX = productProjection.getAllVariants().get(2).getId(); final ChangeMasterVariant updateAction = ChangeMasterVariant.ofVariantId(variantIdOfSizeX); final ProductVariant newMasterVariant = productProjectionWithNewMasterVariant.getMasterVariant(); assertThat(newMasterVariant) .as("third variant is now the master variant") .isEqualTo(productProjection.getAllVariants().get(2)); assertThat(newMasterVariant.getAttribute(sizeAttributeName).getValueAsEnumValue()) .isEqualTo(Sizes.X); final List<EnumValue> reorderedVariantsSizeValues = productProjectionWithNewMasterVariant.getAllVariants() .stream() .map(variant -> variant.getAttribute(sizeAttributeName).getValueAsEnumValue())
private void testBlueFilter(final Function<List<FilterExpression<ProductProjection>>, ProductProjectionSearch> createSearch) { final ProductProjectionSearch search = createSearch.apply(PRODUCT_MODEL.allVariants().attribute().ofString(ATTR_NAME_COLOR).is("blue")); testResult(search, result -> { final List<ProductProjection> results = result.getResults(); final Predicate<ProductVariant> isBlue = variant -> variant.findAttribute(COLOR_ATTRIBUTE_ACCESS) .map(color -> color.equals("blue")) .orElse(false); final Condition<ProductVariant> blueVariant = new Condition<>(isBlue, "variant is blue"); assertThat(results) .flatExtracting(ProductProjection::getAllVariants) .areAtLeastOne(notMatching(blueVariant)) .areAtLeastOne(blueVariant); final List<ProductVariant> allMatchingVariants = results.stream() .flatMap(r -> r.findMatchingVariants().stream()) .collect(toList()); assertThat(allMatchingVariants).are(blueVariant); assertThat(results) .extracting(r -> r.findFirstMatchingVariant().get()) .containsOnlyElementsOf(allMatchingVariants); }); }
.overridingErrorMessage("if a product is in a category, you can directy query for it") .matches(elements -> elements.stream() .anyMatch(product -> product.getCategories().contains(tshirtCategory.toReference()))); }); });
public static Comparator<ProductProjection> comparingCategoryOrderHints(final String categoryId) { final Function<ProductProjection, String> orderHintExtractor = p -> Optional.ofNullable(p.getCategoryOrderHints()) .map(categoryOrderHints -> categoryOrderHints.get(categoryId)) .orElse(null); return comparing(orderHintExtractor, Comparator.nullsLast(Comparator.<String>naturalOrder())); } }
protected void fillName(final ProductProjection product, final ProductVariantBean bean) { bean.setName(createName(product.getName())); }
@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; }); } }
default Optional<Call> productDetailPageCall(final Locale locale, final ProductProjection product, final ProductVariant productVariant) { return product.getSlug().find(locale) .map(slug -> productDetailPageCall(locale.toLanguageTag(), slug, productVariant.getSku())); }
default Optional<ProductVariant> findVariant(final VariantIdentifier identifier){ return getId().equals(identifier.getProductId()) ? Optional.ofNullable(getVariant(identifier.getVariantId())) : Optional.empty(); }
@Test public void execution() throws Exception { withProduct(client(), "getProductProjectionById", product -> { final ProductProjectionType projectionType = STAGED; final String productId = product.getId(); final ProductProjectionByIdGet sphereRequest = ProductProjectionByIdGet.of(productId, projectionType); final ProductProjection productProjection = client().executeBlocking(sphereRequest); final String fetchedProjectionId = productProjection.getId(); assertThat(fetchedProjectionId).isEqualTo(productId); assertThat(productProjection.getCategories()).isEqualTo(product.getMasterData().get(projectionType).getCategories()); }); } }
default Versioned<Product> toProductVersioned() { return Versioned.of(getId(), getVersion()); }
@Test public void changeMasterVariantWithSku() { withUpdateableProductProjectionOfMultipleVariants(client(), (ProductProjection productProjection) -> { final int originalMasterVariantId = productProjection.getMasterVariant().getId(); final ProductVariant variantSupposedToBeMaster = productProjection.getAllVariants().get(2); final String sku = variantSupposedToBeMaster.getSku(); final ChangeMasterVariant updateAction = ChangeMasterVariant.ofSku(sku); client().executeBlocking(ProductUpdateCommand.of(productProjection, updateAction)); final ProductProjection productProjectionWithNewMasterVariant = client().executeBlocking(ProductProjectionByIdGet.of(productProjection, STAGED)); assertThat(productProjectionWithNewMasterVariant.getMasterVariant().getSku()).isEqualTo(sku); assertThat(productProjectionWithNewMasterVariant.getMasterVariant().getId()) .as("variant IDs don't change in reordering") .isNotEqualTo(originalMasterVariantId) .isEqualTo(variantSupposedToBeMaster.getId()); return productProjectionWithNewMasterVariant; }); }
@Test public void queryByCategory() throws Exception { withCategory(client(), cat3 -> withCategory(client(), cat1 -> withCategory(client(), cat2 -> with2products("queryByCategory", (p1, p2) -> { final Category cat1WithParent = client().executeBlocking(CategoryUpdateCommand.of(cat1, asList(ChangeParent.of(cat3)))); final Product productWithCat1 = client().executeBlocking(ProductUpdateCommand.of(p1, AddToCategory.of(cat1WithParent))); final Query<ProductProjection> query = ProductProjectionQuery.of(STAGED) .withPredicates(m -> m.categories().isIn(asList(cat1, cat2))) .withExpansionPaths(m -> m.categories().parent()); final PagedQueryResult<ProductProjection> queryResult = client().executeBlocking(query); assertThat(ids(queryResult)).containsOnly(productWithCat1.getId()); final Reference<Category> cat1Loaded = queryResult.head().get().getCategories().stream().findAny().get(); assertThat(cat1Loaded).as("cat of product is expanded").is(expanded()); final Reference<Category> parent = cat1Loaded.getObj().getParent(); assertThat(parent).as("parent of cat is expanded").is(expanded()); }) ) ) ); }
public static Comparator<ProductProjection> comparingCategoryOrderHints(final String categoryId) { final Function<ProductProjection, String> orderHintExtractor = p -> Optional.ofNullable(p.getCategoryOrderHints()) .map(categoryOrderHints -> categoryOrderHints.get(categoryId)) .orElse(null); return comparing(orderHintExtractor, Comparator.nullsLast(Comparator.<String>naturalOrder())); } }