@Override public boolean matches(final PagedQueryResult<T> value) { return value.getResults().size() == 1 && value.getResults().get(0).equals(expected); } };
private void consumePagedResult(@Nonnull PagedQueryResult<T> result, @Nonnull SphereClient client, @Nonnull Consumer<T> resultsConsumer) { result.getResults().forEach(resultsConsumer); queryNextPages(client, result.getTotal(), resultsConsumer) .forEach(stageStreamS -> stageStreamS.toCompletableFuture().join()); }
@Test public void singleValueResult() throws Exception { final PagedQueryResult<String> result = PagedQueryResult.of("hello"); assertThat(result.getCount()).isEqualTo(1); assertThat(result.getOffset()).isEqualTo(0); assertThat(result.getResults()).isEqualTo(Collections.singletonList("hello")); assertThat(result.getTotal()).isEqualTo(1); }
public ProductType fetchProductTypeByName() { final Optional<ProductType> productTypeOptional = client().executeBlocking(ProductTypeQuery.of().byName(PRODUCT_TYPE_NAME)).head(); final ProductType productType = productTypeOptional .orElseThrow(() -> new RuntimeException("product type " + PRODUCT_TYPE_NAME + " is not present.")); //end example parsing here return productType; }
@Override public PagedQueryResult<Message> deserialize(final HttpResponse httpResponse) { final PagedQueryResult<Message> pagedQueryResult = super.deserialize(httpResponse); final List<Message> results = pagedQueryResult.getResults().stream() .map((Message genericMessage) -> { final String type = genericMessage.getType(); return PagedQueryResult.of(pagedQueryResult.getOffset(), pagedQueryResult.getLimit(), pagedQueryResult.getTotal(), results);
@Test public void anonymousId() { final String anonymousId = randomKey(); final CartDraft cartDraft = CartDraft.of(EUR).withCountry(DE).withAnonymousId(anonymousId); final Cart cart = client().executeBlocking(CartCreateCommand.of(cartDraft)); final PagedQueryResult<Cart> result = client().executeBlocking(CartQuery.of().withPredicates(m -> m.anonymousId().is(anonymousId))); assertThat(result.getResults()).hasSize(1); assertThat(result.head().get().getId()).isEqualTo(cart.getId()); }
@Test public void selectAPriceByCurrencyInProductProjectionQuery() { final List<PriceDraft> prices = asList(PriceDraft.of(EURO_30), PriceDraft.of(USD_20)); withProductOfPrices(prices, product -> { final ProductProjectionQuery request = ProductProjectionQuery.ofStaged() .withPredicates(m -> m.id().is(product.getId()))//to limit the test scope .withPriceSelection(PriceSelection.of(EUR));//price selection config final PagedQueryResult<ProductProjection> result = client().executeBlocking(request); assertThat(result.getCount()).isEqualTo(1); final ProductVariant masterVariant = result.getResults().get(0).getMasterVariant(); assertThat(masterVariant.getPrice()).isNotNull().has(price(PriceDraft.of(EURO_30))); }); }
@Test public void overPaging() throws Exception { final long offset = 100000; final PagedQueryResult<Category> result = client().executeBlocking(CategoryQuery.of().withOffset(offset)); assertThat(result.getOffset()).isEqualTo(100000); assertThat(result.getCount()).isEqualTo(0); assertThat(result.getResults().size()).isEqualTo(0); } }
/** * @param <T> the type of the underlying model * @param offset The offset supplied by the client or the server default. * @param total The total number of results matching the request. * @param results The mocked result. * @return the paged query result. * @deprecated PagedQueryResult should remain as a read model, and be constructed only by deserialization. */ @Deprecated static <T> PagedQueryResultDsl<T> of(final Long offset, final Long total, final List<T> results) { return of(offset, -1L, total, results); }
assertThat(messageQueryResult.getTotal()).isGreaterThanOrEqualTo(1L); final PaymentTransactionAddedMessage paymentTransactionAddedMessage = messageQueryResult.head().get(); assertThat(paymentTransactionAddedMessage.getTransaction().getTimestamp()).isEqualTo(firstRefundTransaction.getTimestamp()); });
@Test public void changeIsDefault() throws Exception { //only one can be default one, so clean up if there is any final Query<ShippingMethod> query = ShippingMethodQuery.of().withPredicates(QueryPredicate.of("isDefault = true")); final Optional<ShippingMethod> defaultShippingMethodOption = client().executeBlocking(query).head(); defaultShippingMethodOption.ifPresent(sm -> client().executeBlocking(ShippingMethodUpdateCommand.of(sm, ChangeIsDefault.toFalse()))); withUpdateableShippingMethod(client(), shippingMethod -> { assertThat(shippingMethod.isDefault()).isFalse(); final ShippingMethodUpdateCommand cmd = ShippingMethodUpdateCommand.of(shippingMethod, ChangeIsDefault.toTrue()); final ShippingMethod updatedShippingMethod = client().executeBlocking(cmd); assertThat(updatedShippingMethod.isDefault()).isTrue(); final Long defaultShippingMethods = client().executeBlocking(ShippingMethodQuery.of().byIsDefault()).getCount(); assertThat(defaultShippingMethods).isEqualTo(1); return client().executeBlocking(ShippingMethodUpdateCommand.of(updatedShippingMethod, ChangeIsDefault.toFalse())); }); }
@Test public void withFetchTotalFalseRemovesTotalFromOutput() throws Exception { withCategory(client(), category -> { final CategoryQuery query = CategoryQuery.of().byId(category.getId()); final PagedQueryResult<Category> resultWithTotal = client().executeBlocking(query); assertThat(resultWithTotal.getTotal()) .as("total is by default present") .isNotNull().isEqualTo(1); final CategoryQuery queryWithoutTotal = query.withFetchTotal(false); final PagedQueryResult<Category> resultWithoutTotal = client().executeBlocking(queryWithoutTotal); assertThat(resultWithoutTotal.getTotal()) .as("total is not present") .isNull(); }); }
@Test public void getPagesCountForEmptyResults() throws Exception { final PagedQueryResult<Integer> queryResult = PagedQueryResult.of( 80L, PAGE_SIZE, 0L, listOfSize(0L)); assertThat(queryResult.getTotalPages()).isEqualTo(0); }
@Test public void byVersioned() { withUpdateableShippingMethod(client(), shippingMethod -> { assertThat(shippingMethod).isNotNull(); final String shippingMethodId = shippingMethod.getId(); client().executeBlocking(ShippingMethodDeleteCommand.of(shippingMethod)); final ShippingMethodQuery shippingMethodQuery = ShippingMethodQuery.of().withPredicates(m -> m.id().is(shippingMethodId)); assertThat(client().executeBlocking(shippingMethodQuery).getCount()).isZero(); }); }
public static void withPersistentDiscountCode(final BlockingSphereClient client, final Consumer<DiscountCode> consumer) { final String code = DiscountCodeFixtures.class.getSimpleName() + "-persistent-4"; final Optional<DiscountCode> fetchedDiscountCode = client.executeBlocking(DiscountCodeQuery.of() .withPredicates(DiscountCodeQueryModel.of().code().is(code))).head(); final DiscountCode discountCode = fetchedDiscountCode.orElseGet(() -> createDiscountCode(client, code)); consumer.accept(discountCode); }
@Override public PagedQueryResult<Message> deserialize(final HttpResponse httpResponse) { final PagedQueryResult<Message> pagedQueryResult = super.deserialize(httpResponse); final List<Message> results = pagedQueryResult.getResults().stream() .map((Message genericMessage) -> { final String type = genericMessage.getType(); return PagedQueryResult.of(pagedQueryResult.getOffset(), pagedQueryResult.getLimit(), pagedQueryResult.getTotal(), results);
@Test public void locale() { final CartDraft cartDraft = CartDraft.of(EUR).withLocale(Locale.GERMAN); final Cart cart = client().executeBlocking(CartCreateCommand.of(cartDraft)); final PagedQueryResult<Cart> result = client().executeBlocking(CartQuery.of() .plusPredicates(m -> m.is(cart)) .plusPredicates(m -> m.locale().is(Locale.GERMAN))); assertThat(result.getResults()).hasSize(1); assertThat(result.head().get().getId()).isEqualTo(cart.getId()); } }
@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))); }); }