/** * 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(); }
/** * 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(); }
/** * 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(); }
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"); }
@Test public void findMatchingVariantsOnSearch() throws Exception { final ProductProjection product = getProductProjectionFromSearch(); assertThat(product.findMatchingVariants()).extracting(v -> v.getId()).containsExactly(2, 4); }
@Test public void findMatchingVariantsOnQuery() throws Exception { final ProductProjection product = getProductProjectionFromQuery(); assertThat(product.findMatchingVariants()).extracting(v -> v.getId()).isEmpty(); }
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); }); }