@SuppressWarnings("unchecked") @Test public void isInSubtree() { withCategoryAndParentCategory(client(), (Category cat, Category parent) -> { withProduct(client(), builder -> builder.categoriesAsObjectList(singletonList(cat)), product -> { final String parentId = parent.getId(); assertThat(product.getMasterData().getStaged().getCategories()) .contains(cat.toReference()) .doesNotContain(parent.toReference()); assertEventually(() -> { //search for products directly in parent category final ProductProjectionSearch requestForDirectCategories = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.categories().id().is(parentId)); assertThat(client().executeBlocking(requestForDirectCategories).getResults()).isEmpty(); //search for products in parent category or its descendants final ProductProjectionSearch requestForTree = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.categories().id().isInSubtree(parentId)); final List<ProductProjection> results = client().executeBlocking(requestForTree).getResults(); assertThat(results).hasSize(1); }); }); }); } }
@Test public void isInSubtree() { final List<String> categoryIds1 = getCategoryIds("B-1", "C-2-2"); final List<String> categoryIds2 = getCategoryIds("B-1-2", "C-1-3"); final List<String> categoryIds3 = getCategoryIds("B-3", "C"); withProductInCategories(client(), categoryIds1, (Product product1) -> { withProductInCategories(client(), categoryIds2, (Product product2) -> { withProductInCategories(client(), categoryIds3, (Product product3) -> { assertEventually(() -> { final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> { final String categoryId = getCategoryIds("B-1").get(0); return m.categories().id().isInSubtree(categoryId); }) .plusQueryFilters(m -> m.id().isIn(asList(product1.getId(), product2.getId(), product3.getId()))); assertThat(client().executeBlocking(request)).has(onlyProducts(product1, product2)); }); }); }); }); }