@Test public void useIdPredicateInsteadOfOffset() throws Exception { final ProductQuery seedQuery = ProductQuery.of() //the original predicate, which queries products for a certain product type //the idea works also for no predicate to get all products .withPredicates(m -> m.productType().is(productType)) //important, we sort by id, otherwise id > $lastId would not make sense .withSort(m -> m.id().sort().asc()) .withLimit(PAGE_SIZE) .withFetchTotal(false);//saves also resources and time final CompletionStage<List<Product>> resultStage = findNext(seedQuery, seedQuery, new LinkedList<>()); final List<Product> actualProducts = resultStage.toCompletableFuture().join(); assertThat(actualProducts).hasSize(createdProducts.size()); //!!! the underlying database has a different algorithm to sort by ID, it is a UUID, which differs from String sorting final List<Product> javaSortedActual = actualProducts.stream().sorted(BY_ID_COMPARATOR).collect(toList()); assertThat(javaSortedActual).isEqualTo(createdProducts); }
private CompletionStage<List<Product>> findNext(final ProductQuery seedQuery, final ProductQuery query, final List<Product> products) { final CompletionStage<PagedQueryResult<Product>> pageResult = sphereClient().execute(query); return pageResult.thenCompose(page -> { final List<Product> results = page.getResults(); products.addAll(results); final boolean isLastQueryPage = results.size() < PAGE_SIZE; if (isLastQueryPage) { return CompletableFuture.completedFuture(products); } else { final String lastId = getIdForNextQuery(page); return findNext(seedQuery, seedQuery .plusPredicates(m -> m.id().isGreaterThan(lastId)), products); } }); }
private ProductQuery query(final Product product) { return ProductQuery.of().withPredicates(m -> m.id().is(product.getId())); } }
private void checkIsFoundByPublishedFlag(final Product product, final boolean value) { final Optional<Product> productFromQuery = client().executeBlocking(ProductQuery.of() .withPredicates(m -> { return m.masterData().isPublished().is(value); }) .plusPredicates(m -> m.id().is(product.getId()))).head(); assertThat(productFromQuery.get().getId()).isEqualTo(product.getId()); }
@Test public void queryProductsWithAnyDiscount() throws Exception { withUpdateableProductDiscount(client(), (ProductDiscount productDiscount, Product product) -> { final ProductQuery query = ProductQuery.of() .withPredicates(m -> m.id().is(product.getId()) .and(m.masterData().staged().masterVariant().prices().discounted().isPresent())); final Duration maxWaitTime = Duration.ofMinutes(2); final Duration waitBeforeRetry = Duration.ofMillis(500); assertEventually(maxWaitTime, waitBeforeRetry, () -> { final Optional<Product> loadedProduct = client().executeBlocking(query).head(); assertThat(loadedProduct.isPresent()).isTrue(); assertThat(loadedProduct.get().getId()).isEqualTo(product.getId()); }); return productDiscount; }); }
@Test public void expandVariants() { CustomerGroupFixtures.withB2cCustomerGroup(client(), customerGroup -> ProductFixtures.withProductType(client(), randomString(), productType -> withProduct(client(), new VariantsCottonTShirtProductDraftSupplier(productType, randomString(), customerGroup), product -> { final PagedQueryResult<Product> result = client().executeBlocking(ProductQuery.of() .withPredicates(m -> m.id().is(product.getId())) .withExpansionPaths(m -> m.masterData().staged().variants().prices().customerGroup()) .withLimit(1L)); final Price priceWithCustomerGroup = result.head().get().getMasterData().getStaged().getVariants().get(0).getPrices().stream() .filter(price -> Objects.equals(price.getCustomerGroup(), customerGroup.toReference())) .findFirst().get(); assertThat(priceWithCustomerGroup.getCustomerGroup().getObj()).isNotNull().isEqualTo(customerGroup); }) ) ); }
@Test public void selectAPriceByCurrencyInProductQuery() { final List<PriceDraft> prices = asList(PriceDraft.of(EURO_30), PriceDraft.of(USD_20)); withProductOfPrices(prices, product -> { final ProductQuery request = ProductQuery.of() .withPredicates(m -> m.id().is(product.getId()))//to limit the test scope .withPriceSelection(PriceSelection.of(EUR));//price selection config final PagedQueryResult<Product> result = client().executeBlocking(request); assertThat(result.getCount()).isEqualTo(1); final ProductVariant masterVariant = result.getResults().get(0).getMasterData().getStaged().getMasterVariant(); assertThat(masterVariant.getPrice()).isNotNull().has(price(PriceDraft.of(EURO_30))); }); }
@Test public void transitionState() { withStateByBuilder(client(), builder -> builder.type(PRODUCT_STATE), state -> { withUpdateableProduct(client(), product -> { assertThat(product.getState()).isNull(); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, asList(TransitionState.of(state)))); assertThat(updatedProduct.getState()).isEqualTo(state.toReference()); assertEventually(() -> { final PagedQueryResult<ProductStateTransitionMessage> messageQueryResult = client().executeBlocking(MessageQuery.of() .withPredicates(m -> m.resource().is(product)) .forMessageType(ProductStateTransitionMessage.MESSAGE_HINT)); assertThat(messageQueryResult.getResults()).isNotEmpty(); final ProductStateTransitionMessage message = messageQueryResult.head().get(); assertThat(message.getState()).isEqualTo(state.toReference()); }); //check query model final ProductQuery query = ProductQuery.of() .withPredicates(m -> m.id().is(product.getId()).and(m.state().is(state))); final Product productByState = client().executeBlocking(query) .head().get(); assertThat(productByState).isEqualTo(updatedProduct); return updatedProduct; }); }); }