/** * This is a test if no threads are blocking the the termination of the application. Needs to be executed on the ci server. * In case it has a leak, it just hangs. * This is necessary, since a unit test cannot cover this termination problem. * @param args unused command line parameters */ public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException { final SphereClient client = SphereClientFactory.of() .createClient(SphereClientConfig.ofEnvironmentVariables("JVM_SDK_IT")); final CompletionStage<PagedQueryResult<ProductProjection>> stage = client.execute(ProductProjectionQuery.ofStaged()); stage.toCompletableFuture().get(10, TimeUnit.SECONDS); client.close(); } }
private static QueryResult doQuery() { List<ProductProjection> sortedFromSearchForCategory1 = searchForCategoryAndSort(category1Id); List<ProductProjection> sortedFromQueryForCategory1 = queryForCategoryAndSort(category1Id); List<ProductProjection> sortedFromSearchForCategory2 = searchForCategoryAndSort(category2Id); List<ProductProjection> sortedFromQueryForCategory2 = queryForCategoryAndSort(category2Id); final List<String> ids = products.stream().map(p -> p.getId()).collect(toList()); final ProductProjectionQuery productProjectionQuery = ProductProjectionQuery.ofStaged() .withPredicates(productModel -> productModel.id().isIn(ids)); List<ProductProjection> productProjections = client().executeBlocking(productProjectionQuery).getResults(); return new QueryResult(productProjections, sortedFromSearchForCategory1, sortedFromSearchForCategory2, sortedFromQueryForCategory1, sortedFromQueryForCategory2); }
final ProductProjectionQuery query = ProductProjectionQuery.ofStaged() .withPredicates(m -> m.id().is(product.getId())) .plusExpansionPaths(m -> m.masterVariant().attributes().valueSet())
@Test public void execution2() throws Exception { withProduct(client(), product -> { final String slug = product.getMasterData().getStaged().getSlug().get(Locale.ENGLISH); final ProductProjectionQuery normalSphereRequest = ProductProjectionQuery.ofStaged() .withPredicates(m -> m.slug().locale(Locale.ENGLISH).is(slug)) .plusExpansionPaths(m -> m.productType()); final JsonNodeSphereRequest jsonNodeSphereRequest = JsonNodeSphereRequest.of(normalSphereRequest); assertThat(normalSphereRequest.httpRequestIntent()) .as("a JsonNodeSphereRequest creates the same request to the platform, but differs in the response") .isEqualTo(jsonNodeSphereRequest.httpRequestIntent()); final PagedQueryResult<ProductProjection> productProjectionPagedSearchResult = client().executeBlocking(normalSphereRequest); final JsonNode jsonNode = client().executeBlocking(jsonNodeSphereRequest);//all will be returned as JSON assertThat(jsonNode.get("results").get(0).get("productType").get("obj").get("description").asText()) .as("the expansion paths are honored") .isEqualTo("a 'T' shaped cloth"); }); } }
@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))); }); }
withProductInCategory(client(), jeansCategory, jeansProduct -> { withProductInCategory(client(), tshirtCategory, tshirtProduct -> { final ProductProjectionQuery sphereRequest = ProductProjectionQuery.ofStaged().withPredicates(m -> m.categories().isIn(asList(mensClothingCategory))); final PagedQueryResult<ProductProjection> resultForParentCategory = .isEmpty(); final ProductProjectionQuery query = ProductProjectionQuery.ofStaged().withPredicates(m -> m.categories().isIn(asList(tshirtCategory, jeansCategory))); assertThat(query.predicates().get(0)) .isEqualTo(QueryPredicate.of(format("categories(id in (\"%s\", \"%s\"))", tshirtCategory.getId(), jeansCategory.getId())));
@Test public void showProductAttributeTableWithDefaultFormatter() throws Exception { final List<String> attrNamesToShow = asList(COLOR_ATTR_NAME, SIZE_ATTR_NAME, MATCHING_PRODUCTS_ATTR_NAME, LAUNDRY_SYMBOLS_ATTR_NAME, RRP_ATTR_NAME, AVAILABLE_SINCE_ATTR_NAME); final Product product = createProduct(); final ProductProjectionQuery query = ProductProjectionQuery.ofStaged() .withPredicates(m -> m.id().is(product.getId())) .plusExpansionPaths(m -> m.masterVariant().attributes().valueSet()) .plusExpansionPaths(m -> m.productType()); final ProductProjection productProjection = client().executeBlocking(query).head().get(); final List<ProductType> productTypes = Collections.singletonList(productProjection.getProductType().getObj()); final List<Locale> locales = Collections.singletonList(ENGLISH); final DefaultProductAttributeFormatter formatter = DefaultProductAttributeFormatterDemo.createFormatter(productTypes, locales); DefaultProductAttributeFormatterDemo.example(attrNamesToShow, productProjection, formatter); }
@Test public void expandProductReferencesInProductAttributes() throws Exception { withProductWithProductReference(client(), (product, referencedProduct) -> { final Query<ProductProjection> query = ProductProjectionQuery.ofStaged() .withPredicates(m -> m.id().is(product.getId())) .withExpansionPaths(m -> m.masterVariant().attributes().value()) .toQuery(); final ProductProjection productProjection = client().executeBlocking(query).head().get(); final NamedAttributeAccess<Reference<Product>> namedAttributeAccess = AttributeAccess.ofProductReference().ofName("productreference"); final Reference<Product> productReference = productProjection.getMasterVariant().findAttribute(namedAttributeAccess).get(); final Product expandedReferencedProduct = productReference.getObj(); assertThat(expandedReferencedProduct.getId()).isEqualTo(referencedProduct.getId()); }); }
@Test public void queryByReviewRating() { withProduct(client(), product -> { withReview(client(), b -> b.target(product).rating(1), review1 -> { withReview(client(), b -> b.target(product).rating(3), review2 -> { assertEventually(Duration.ofSeconds(60), Duration.ofMillis(200), () -> { final ProductProjectionQuery query = ProductProjectionQuery.ofStaged() .withPredicates(m -> m.reviewRatingStatistics().averageRating().is(2.0)) .plusPredicates(m -> m.reviewRatingStatistics().count().is(2)) .plusPredicates(m -> m.reviewRatingStatistics().lowestRating().is(1)) .plusPredicates(m -> m.reviewRatingStatistics().highestRating().is(3)) .plusPredicates(m -> m.is(product)); final List<ProductProjection> results = client().executeBlocking(query).getResults(); assertThat(results).hasSize(1); assertThat(results.get(0).getId()).isEqualTo(product.getId()); }); }); }); }); }
private static List<ProductProjection> queryForCategoryAndSort(final String categoryId) { final ProductProjectionQuery query = ProductProjectionQuery.ofStaged() .withPredicates(productModel -> productModel.categories().id().is(categoryId)) .withSort(productModel -> productModel.categoryOrderHints().category(categoryId).asc()); return client().executeBlocking(query).getResults(); }