@Override public DirectionlessQuerySort<T> category(final String categoryId) { return DirectionlessQuerySort.of(new QueryModelImpl<T>(this, categoryId){}); } }
@Override public DirectionlessQuerySort<T> sort() { return new DirectionlessQuerySort<>(this); } }
public QuerySort<T> desc() { return by(DESC); }
@Test public void generateSortExpressionDesc() throws Exception { assertThat(model.sort().desc().toSphereSort()).isEqualTo("id desc"); } }
@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"); }
@Test public void generateSortExpressionAsc() throws Exception { assertThat(model.sort().asc().toSphereSort()).isEqualTo("id asc"); }
@Test public void sortByOrderHint() { final List<String> hints = Arrays.asList("0.03539970150419025", "0.9124532896017283", "0.4677879627352788", "0.9306522049331096", "0.8442774484430258", "0.3002734797783949", "0.9053018615787292", "0.5897829295233494", "0.9736635784904704", "0.393321565584442", "0.605741386570728", "0.6644442788442779", "0.8541807248539874", "0.9945130750802729", "0.12979277595041627"); List<String> sortedHints = new ArrayList<>(hints); Collections.sort(sortedHints, Comparator.comparingDouble(Double::valueOf)); withCategories(client(), hints.stream().map(CategoryQueryIntegrationTest::categoryWithHint).collect(Collectors.toList()), categories -> { PagedQueryResult<Category> res = client().executeBlocking(CategoryQuery.of().plusSort(m -> m.orderHint().sort().asc()).withLimit(500)); List<String> queryResultHints = res.getResults().stream().filter(cat -> categories.contains(cat)).map(cat -> cat.getOrderHint()).collect(Collectors.toList()); assertThat(queryResultHints).containsExactlyElementsOf(sortedHints); }); }
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 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); }
@Override public DirectionlessQuerySort<T> category(final String categoryId) { return DirectionlessQuerySort.of(new QueryModelImpl<T>(this, categoryId){}); } }
@Override public DirectionlessQuerySort<T> sort() { return new DirectionlessQuerySort<>(this); }
public QuerySort<T> asc() { return by(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 sortBySyncedAt() throws Exception { assertThat(OrderQueryModel.of().syncInfo().syncedAt().sort().asc().toSphereSort()) .isEqualTo("syncInfo.syncedAt asc"); } }
@Override public DirectionlessQuerySort<T> category(final String categoryId) { return DirectionlessQuerySort.of(new QueryModelImpl<T>(this, categoryId){}); } }
@Override public DirectionlessQuerySort<T> sort() { return new DirectionlessQuerySort<>(this); }
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); } }
.withSort(m -> m.type().sort().asc()) .withExpansionPaths(m -> m.resource()) .forMessageTypes(messageHints);
@Override public DirectionlessQuerySort<T> sort() { return new DirectionlessQuerySort<>(this); }