withProductInCategory(client(), jeansCategory, jeansProduct -> { withProductInCategory(client(), tshirtCategory, tshirtProduct -> { final ProductProjectionQuery sphereRequest = ProductProjectionQuery.ofStaged().withPredicates(m -> m.categories().isIn(asList(mensClothingCategory))); final PagedQueryResult<ProductProjection> resultForParentCategory = .isEmpty(); final ProductProjectionQuery query = ProductProjectionQuery.ofStaged().withPredicates(m -> m.categories().isIn(asList(tshirtCategory, jeansCategory))); assertThat(query.predicates().get(0)) .isEqualTo(QueryPredicate.of(format("categories(id in (\"%s\", \"%s\"))", tshirtCategory.getId(), jeansCategory.getId())));
@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()); }) ) ) ); }
private static List<ProductProjection> queryForCategoryAndSort(final String categoryId) { final ProductProjectionQuery query = ProductProjectionQuery.ofStaged() .withPredicates(productModel -> productModel.categories().id().is(categoryId)) .withSort(productModel -> productModel.categoryOrderHints().category(categoryId).asc()); return client().executeBlocking(query).getResults(); }