/** * 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())); } }
@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()); }); } }
.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()))); }); });
@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()); }) ) ) ); }