private void check(final Function<CustomerQueryModel, QueryPredicate<Customer>> f, final boolean checkDistraction) { final CustomerQueryModel model = CustomerQueryModel.of(); final QueryPredicate<Customer> predicate = f.apply(model); final Query<Customer> query = CustomerQuery.of().withPredicates(predicate).withSort(model.createdAt().sort().desc()); final List<Customer> results = client().executeBlocking(query).getResults(); final List<String> ids = results.stream().map(x -> x.getId()).collect(toList()); assertThat(ids).contains(customer.getId()); if (checkDistraction) { assertThat(ids.contains(distraction.getId())).isFalse(); } }
@Test public void sortBySyncedAt() throws Exception { assertThat(OrderQueryModel.of().syncInfo().syncedAt().sort().asc().toSphereSort()) .isEqualTo("syncInfo.syncedAt asc"); } }
@Test public void queryPureJson() throws Exception { withCustomObject(client(), existingCustomObject -> { final CustomObjectQuery<JsonNode> clientRequest = CustomObjectQuery.ofJsonNode() .withSort(m -> m.createdAt().sort().desc()); final PagedQueryResult<CustomObject<JsonNode>> result = client().executeBlocking(clientRequest); assertThat(result.getResults().stream().filter(item -> item.hasSameIdAs(existingCustomObject)).count()) .isGreaterThanOrEqualTo(1); final String expected = existingCustomObject.getValue().getBar(); final CustomObject<JsonNode> loadedCustomObject = result.head().get(); final JsonNode jsonNode = loadedCustomObject.getValue(); final String actual = jsonNode.get("bar").asText("it is not present"); assertThat(actual).isEqualTo(expected); }); }
@Test public void plusSort() { final List<QuerySort<Category>> sort = CategoryQuery.of() .withSort(m -> m.externalId().sort().asc()) .plusSort(m -> m.createdAt().sort().desc()).sort(); assertThat(sort).hasSize(2); assertThat(sort).extractingResultOf("toSphereSort").containsExactly("externalId asc", "createdAt desc"); }
private CartQuery decorateQueryWithAdditionalInfo(final CartQuery query) { return query .plusPredicates(cart -> cart.cartState().is(CartState.ACTIVE)) .plusExpansionPaths(c -> c.shippingInfo().shippingMethod()) // TODO use run hook on cart query instead .plusExpansionPaths(c -> c.paymentInfo().payments()) .withSort(cart -> cart.lastModifiedAt().sort().desc()) .withLimit(1); } }
@Test public void queryByNotName() throws Exception { withCategory(client(), category1 -> withCategory(client(), category2 -> { final Query<Category> query = CategoryQuery.of(). withPredicates(m -> m.name().lang(Locale.ENGLISH).isNot(category1.getName().get(Locale.ENGLISH))) .withSort(m -> m.createdAt().sort().desc()); final boolean category1IsPresent = client().executeBlocking(query).getResults().stream().anyMatch(cat -> cat.getId().equals(category1.getId())); assertThat(category1IsPresent).isFalse(); }) ); }
@Test public void getReviewsForOneProduct() { final String productId = product.getId(); final ReviewQuery reviewQuery = ReviewQuery.of() .withPredicates(m -> m.target().id().is(productId)) .withSort(m -> m.createdAt().sort().desc()) .withLimit(REVIEWS_PER_PRODUCT); final List<Review> reviews = client().executeBlocking(reviewQuery).getResults(); assertThat(reviews).hasSize(REVIEWS_PER_PRODUCT); assertThat(reviews).extracting(r -> r.getTarget().getId()).containsOnlyElementsOf(singletonList(productId)); }
@Test public void getApprovedReviewsForOneProduct() { final String productId = product.getId(); final ReviewQuery reviewQuery = ReviewQuery.of() .withPredicates(review -> review.includedInStatistics().is(true).and(review.target().id().is(productId))) .withSort(m -> m.createdAt().sort().desc()) .withLimit(REVIEWS_PER_PRODUCT); final List<Review> reviews = client().executeBlocking(reviewQuery).getResults(); assertThat(reviews).hasSize(REVIEWS_PER_PRODUCT); assertThat(reviews).extracting(r -> r.getTarget().getId()).containsOnlyElementsOf(singletonList(productId)); assertThat(reviews).extracting(r -> r.isIncludedInStatistics()).containsOnlyElementsOf(singletonList(true)); }
@Test public void queryByNegatedPredicateName() throws Exception { withCategory(client(), category1 -> withCategory(client(), category2 -> { final Query<Category> query = CategoryQuery.of(). withPredicates(m -> { final QueryPredicate<Category> predicate = m.name().lang(Locale.ENGLISH).is(category1.getName().get(Locale.ENGLISH)).negate(); return predicate; }) .withSort(m -> m.createdAt().sort().desc()); final boolean category1IsPresent = client().executeBlocking(query).getResults().stream().anyMatch(cat -> cat.getId().equals(category1.getId())); assertThat(category1IsPresent).isFalse(); }) ); }
@Test public void queryByNegatedPredicateNameValidWithAnd() throws Exception { withCategory(client(), category1 -> withCategory(client(), category2 -> { final Query<Category> query = CategoryQuery.of(). withPredicates(m -> { final QueryPredicate<Category> predicate = m.name().lang(Locale.ENGLISH).is(category1.getName().get(Locale.ENGLISH)).negate() .and(m.id().is(category1.getId())); return predicate; }) .withSort(m -> m.createdAt().sort().desc()); final boolean category1IsPresent = client().executeBlocking(query).getResults().stream().anyMatch(cat -> cat.getId().equals(category1.getId())); assertThat(category1IsPresent).isFalse(); }) ); }
public static List<Review> query(final BlockingSphereClient client, final State initialState) { //this ID can be cached final String initialStateId = client.executeBlocking(StateQuery.of().byKey("to-approve")).head().get().getId(); final ReviewQuery reviewQuery = ReviewQuery.of() .withPredicates(m -> m.state().id().is(initialStateId)) .withSort(m -> m.createdAt().sort().asc());//oldest first final List<Review> reviewsToApprove = client.executeBlocking(reviewQuery).getResults(); assertThat(reviewsToApprove).extracting(review -> review.getTitle()).contains("review title"); return reviewsToApprove; } }
private PaymentQuery createPaymentsForCartQuery(Cart cart, String pspId, String methodId) { PaymentQuery queryAllRefPayments = PaymentQuery.of() .plusPredicates(p -> p.paymentMethodInfo().paymentInterface().is(pspId)) .plusPredicates(p -> p.paymentMethodInfo().method().is(methodId)) .plusSort(p -> p.createdAt().sort().desc()); if(null != cart.getPaymentInfo()) { for (Reference<Payment> ref : cart.getPaymentInfo().getPayments()) { queryAllRefPayments = queryAllRefPayments.plusPredicates(p -> p.id().is(ref.getId())); } } return queryAllRefPayments; } }
@Test public void byCustomerIdAndByCustomerEmail() throws Exception { withCustomerAndCart(client(), (customer, cart) -> { final CartQuery cartQuery = CartQuery.of() .withSort(m -> m.createdAt().sort().desc()) .withLimit(1L) .withPredicates( m -> m.customerId().is(customer.getId()) .and(m.customerEmail().is(customer.getEmail()))); final Cart loadedCart = client().executeBlocking(cartQuery).head().get(); assertThat(loadedCart.getCustomerId()).contains(customer.getId()); }); }
private PaymentQuery createPaymentsForCartQuery(Cart cart, String pspId, String methodId) { PaymentQuery queryAllRefPayments = PaymentQuery.of() .plusPredicates(p -> p.paymentMethodInfo().paymentInterface().is(pspId)) .plusPredicates(p -> p.paymentMethodInfo().method().is(methodId)) .plusSort(p -> p.createdAt().sort().desc()); if(null != cart.getPaymentInfo()) { for (Reference<Payment> ref : cart.getPaymentInfo().getPayments()) { queryAllRefPayments = queryAllRefPayments.plusPredicates(p -> p.id().is(ref.getId())); } } return queryAllRefPayments; } }
@Test public void queryForASpecificResource() throws Exception { withOrderAndReturnInfo(client(), ((order, returnInfo) -> { final Query<SimpleOrderMessage> query = MessageQuery.of() .withPredicates(m -> m.resource().id().is(order.getId())) .withSort(m -> m.createdAt().sort().desc()) .withExpansionPaths(m -> m.resource()) .forMessageType(SimpleOrderMessage.MESSAGE_HINT); assertEventually(() -> { final List<SimpleOrderMessage> results = client().executeBlocking(query).getResults(); final Optional<Order> orderOptional = Optional.ofNullable(results.get(0).getResource().getObj()); assertThat(orderOptional.map(o -> o.getCreatedAt())).contains(order.getCreatedAt()); }); return order; })); }
@Test public void queryByHasStagedChanges() throws Exception { withProduct(client(), product -> { final Product updated = client().executeBlocking(ProductUpdateCommand.of(product, ChangeName.of(randomSlug()))); final ProductProjectionQuery query = ProductProjectionQuery.of(STAGED) .withPredicates(m -> m.hasStagedChanges().is(true)) .withSort(m -> m.createdAt().sort().desc()); final PagedQueryResult<ProductProjection> pagedQueryResult = client().executeBlocking(query); assertThat(ids(pagedQueryResult)).contains(updated.getId()); }); }
@Test public void queryByName() throws Exception { with2products("queryByName", (p1, p2) ->{ final Query<ProductProjection> query1 = ProductProjectionQuery.of(STAGED) .withPredicates(m -> m.name().lang(ENGLISH).is(en(p1.getMasterData().getStaged().getName()))) .withSort(m -> m.createdAt().sort().desc()) .withLimit(1L); assertThat(ids(client().executeBlocking(query1))).containsOnly(p1.getId()); }); }
@Test public void queryTaxedPrice() throws Exception { withFilledCart(client(), cart -> { final CartQuery cartQuery = CartQuery.of() .withSort(m -> m.createdAt().sort().desc()) .withLimit(1L) .withPredicates(m -> m.taxedPrice().isPresent() .and(m.taxedPrice().totalNet().centAmount().is(centAmountOf(cart.getTaxedPrice().getTotalNet()))) .and(m.taxedPrice().totalGross().centAmount().is(centAmountOf(cart.getTaxedPrice().getTotalGross())) .and(m.id().is(cart.getId()))) ); final Cart loadedCart = client().executeBlocking(cartQuery).head().get(); assertThat(loadedCart.getId()).isEqualTo(cart.getId()); }); }
public void predicateTestCase(final QueryPredicate<Category> predicate, final Consumer<List<Category>> assertions) { withCategory(client(), CategoryDraftBuilder.of(en("x1"), en("x1")).description(null), c1 -> { withCategory(client(), CategoryDraftBuilder.of(en("x2").plus(Locale.CHINESE, "xx"), en("x2")).description(en("desc 2")), c2 -> { withCategory(client(), CategoryDraftBuilder.of(en("x10"), en("x10")), c10 -> { final Query<Category> query = CategoryQuery.of().withPredicates(predicate).withSort(CategoryQueryModel.of().createdAt().sort().desc()); final List<Category> results = client().executeBlocking(query).getResults(); assertions.accept(results); }); }); }); } }