@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); }
@Test public void withTotal() throws Exception { final PagedQueryResult<String> result = PagedQueryResult.of("hello").withTotal(500L); assertThat(result.getCount()).isEqualTo(1); assertThat(result.getOffset()).isEqualTo(0); assertThat(result.getResults()).isEqualTo(Collections.singletonList("hello")); assertThat(result.getTotal()).isEqualTo(500); }
@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); } }
@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(); }); }
@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 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 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())); }); }
public static void deleteProductsProductTypeAndProductDiscounts(final BlockingSphereClient client, final ProductType productType) { client.executeBlocking(ProductDiscountQuery.of().withLimit(500L)).getResults() .forEach(discount -> client.executeBlocking(ProductDiscountDeleteCommand.of(discount))); if (productType != null) { QueryPredicate<Product> ofProductType = ProductQueryModel.of().productType().is(productType); ProductQuery productsOfProductTypeQuery = ProductQuery.of().withPredicates(ofProductType).withLimit(500L); do { final List<Product> products = client.executeBlocking(productsOfProductTypeQuery).getResults(); final List<Product> unpublishedProducts = products.stream().map( product -> { if (product.getMasterData().isPublished()) { return client.executeBlocking(ProductUpdateCommand.of(product, Unpublish.of())); } else { return product; } } ).collect(toList()); final List<CompletionStage<Product>> stages = new LinkedList<>(); unpublishedProducts.forEach( product -> { final CompletionStage<Product> completionStage = client.execute(ProductDeleteCommand.of(product)); stages.add(completionStage); } ); stages.forEach(stage -> SphereClientUtils.blockingWait(stage, 30, TimeUnit.SECONDS)); deleteProductType(client, productType); } while (client.executeBlocking(productsOfProductTypeQuery).getCount() > 0); } }
@Test public void byKey() { withUpdateableShippingMethod(client(), shippingMethod -> { final String key = randomKey(); final ShippingMethodUpdateCommand cmd = ShippingMethodUpdateCommand.of(shippingMethod, SetKey.of(key)); final ShippingMethod updatedShippingMethod = client().executeBlocking(cmd); assertThat(updatedShippingMethod.getKey()).isEqualTo(key); client().executeBlocking(ShippingMethodDeleteCommand.ofKey(key, updatedShippingMethod.getVersion())); final ShippingMethodQuery shippingMethodQuery = ShippingMethodQuery.of().withPredicates(m -> m.key().is(key)); assertThat(client().executeBlocking(shippingMethodQuery).getCount()).isZero(); }); } }