public static <T> List<T> blockingWaitForEach(final Stream<? extends CompletionStage<T>> stream, final long timeout, final TimeUnit unit) { return stream .map(stage -> blockingWait(stage, timeout, unit)) .collect(toList()); }
@Override public <T> T executeBlocking(final SphereRequest<T> sphereRequest, final long timeout, final TimeUnit unit) { final CompletionStage<T> completionStage = execute(sphereRequest); return blockingWait(completionStage, timeout, unit); } }
protected Optional<Zone> getZone(final Cart cart) { // Need to do this since zones are not expanded in shipping methods yet (but will be soon) // Rather this (even though it's expensive -one request per shipping method-) but it will mean less breaking changes in the future final ZoneFinderByCart zoneFinder = injector.getInstance(ZoneFinderByCart.class); return blockingWait(zoneFinder.findZone(cart), Duration.ofMinutes(1)); }
@Override public <T> T executeBlocking(final SphereRequest<T> sphereRequest, final long timeout, final TimeUnit unit) { final CompletionStage<T> completionStage = execute(sphereRequest); return blockingWait(completionStage, sphereRequest, timeout, unit); }
@Override public <T> T executeBlocking(final SphereRequest<T> sphereRequest, final long timeout, final TimeUnit unit) { final CompletionStage<T> completionStage = execute(sphereRequest); return blockingWait(completionStage, sphereRequest, timeout, unit); }
@Inject protected DefaultProjectContext(final Configuration configuration, @Named("global") final SphereClient client) { try { final SphereRequest<Project> request = ProjectGet.of(); final Project project = blockingWait(client.execute(request), Duration.ofMinutes(1)); this.locales = projectLocales(configuration, project); this.countryCodes = projectCountries(configuration, project); this.currencies = projectCurrencies(configuration, project); logger.debug("Provide ProjectContext: Languages {}, Countries {}, Currencies {}", locales, countryCodes, currencies); } catch (SphereTimeoutException e) { throw new RuntimeException("Could not fetch project information", e); } }
private MetaProductType getMetaProductType() { final List<ProductType> productTypes = blockingWait(queryAll(client, ProductTypeQuery.of()), 30, TimeUnit.SECONDS); return MetaProductType.of(productTypes); } }
@Test public void fetchAllAsJson_withMapper() throws Exception { final CompletionStage<List<JsonNode>> categoryPagesStage = QueryExecutionUtils .queryAll(client(), JsonNodeQuery.of("/categories"), category -> category, 500); final List<JsonNode> categoryNodes = SphereClientUtils .blockingWait(categoryPagesStage, Duration.ofMinutes(1)); assertThat(categoryNodes) .hasSize(15) .matches(cats -> cats.parallelStream() .anyMatch(cat -> cat.get("slug").get("en").asText().equals("boots-women"))); }
@Test public void fetchAllAsJson() throws Exception { final CompletionStage<List<JsonNode>> categoriesStage = QueryExecutionUtils.queryAll(client(), JsonNodeQuery.of("/categories"), 500); final List<JsonNode> categories = SphereClientUtils.blockingWait(categoriesStage, Duration.ofMinutes(1)); assertThat(categories) .hasSize(15) .matches(cats -> cats.parallelStream().anyMatch(cat -> cat.get("slug").get("en").asText().equals("boots-women"))); }
@Test public void collectAllExternalIds() throws Exception { final List<String> externalIds = new ArrayList<>(); final Consumer<Category> categoryConsumer = category -> externalIds.add(category.getExternalId()); final CompletionStage<Void> categoriesStage = QueryExecutionUtils .queryAll(client(), CategoryQuery.of(), categoryConsumer, 500); SphereClientUtils.blockingWait(categoriesStage, Duration.ofMinutes(1)); assertThat(externalIds).hasSize(15); IntStream.range(0, externalIds.size()).forEach(index -> assertThat(externalIds).contains(index + "")); }
@Test public void fetchAllExternalIdsWithNonUniformPageSizes() throws Exception { final CompletionStage<List<String>> categoriesStage = QueryExecutionUtils .queryAll(client(), CategoryQuery.of(), Category::getExternalId, 4); final List<String> externalIds = SphereClientUtils .blockingWait(categoriesStage, Duration.ofMinutes(1)); assertThat(externalIds).hasSize(15); IntStream.range(0, externalIds.size()).forEach(index -> assertThat(externalIds).contains(index + "")); }
@Test public void fetchAllExternalIdsWithUniformPageSizes() throws Exception { final CompletionStage<List<String>> categoriesStage = QueryExecutionUtils .queryAll(client(), CategoryQuery.of(), Category::getExternalId, 3); final List<String> externalIds = SphereClientUtils .blockingWait(categoriesStage, Duration.ofMinutes(1)); assertThat(externalIds).hasSize(15); IntStream.range(0, externalIds.size()).forEach(index -> assertThat(externalIds).contains(index + "")); }
public void fetchAccessToken() { final SphereAuthConfig config = getSphereClientConfig(); final CompletionStage<String> stringCompletionStage = TokensFacade.fetchAccessToken(config); final String accessToken = blockingWait(stringCompletionStage, 2, SECONDS); assertThat(accessToken).isNotEmpty(); }
@Test public void fetchAll() throws Exception { final CompletionStage<List<Category>> categoriesStage = QueryExecutionUtils.queryAll(client(), CategoryQuery.of(), 500); final List<Category> categories = SphereClientUtils.blockingWait(categoriesStage, Duration.ofMinutes(1)); assertThat(categories) .hasSize(15) .matches(cats -> cats.parallelStream().anyMatch(cat -> cat.getSlug().get(ENGLISH).equals("boots-women"))); }
@Test public void fetchAll_withMapper() throws Exception { final CompletionStage<List<Category>> categoriesStage = QueryExecutionUtils .queryAll(client(), CategoryQuery.of(), category -> category, 500); final List<Category> categories = SphereClientUtils .blockingWait(categoriesStage, Duration.ofMinutes(1)); assertThat(categories) .hasSize(15) .matches(cats -> cats.parallelStream().anyMatch(cat -> cat.getSlug().get(ENGLISH).equals("boots-women"))); }
private CategoryTree createCategoryTree() { //stuff from previous example final CompletionStage<List<Category>> categoriesStage = QueryExecutionUtils.queryAll(client(), CategoryQuery.of()); final List<Category> categories = SphereClientUtils.blockingWait(categoriesStage, Duration.ofMinutes(1)); return CategoryTree.of(categories); }
@Test public void fetchRoots() throws Exception { final CategoryQuery seedQuery = CategoryQuery.of().withPredicates(m -> m.parent().isNotPresent()); final CompletionStage<List<Category>> categoriesStage = QueryExecutionUtils.queryAll(client(), seedQuery); final List<Category> rootCategories = SphereClientUtils.blockingWait(categoriesStage, Duration.ofMinutes(1)); assertThat(rootCategories.stream().allMatch(cat -> cat.getParent() == null)) .overridingErrorMessage("fetched only root categories") .isTrue(); }
@Test public void fetchRoots_withMapper() throws Exception { final CategoryQuery seedQuery = CategoryQuery.of().withPredicates(m -> m.parent().isNotPresent()); final CompletionStage<List<Category>> rootCategoryStage = QueryExecutionUtils .queryAll(client(), seedQuery, category -> category); final List<Category> rootCategories = SphereClientUtils .blockingWait(rootCategoryStage, Duration.ofMinutes(1)); assertThat(rootCategories.stream().allMatch(cat -> cat.getParent() == null)) .overridingErrorMessage("fetched only root categories") .isTrue(); }
@Test public void retryOnGatewayProblems() { final SphereClient client = getSphereClient(getHttpClient()); final SphereClient retryClient = RetryBadGatewayExample.ofRetry(client); final Project result = blockingWait(retryClient.execute(ProjectGet.of()), 16, TimeUnit.SECONDS); Assertions.assertThat(result.getKey()).isEqualTo("foo"); }
@Test public void retryDelete() throws InterruptedException { final SphereClient client = getSphereClient(getDeleteHttpClient()); final SphereClient retryClient = RetryDeleteExample.ofRetry(client); final Category result = blockingWait(retryClient.execute(CategoryDeleteCommand.of(Versioned.of("some-id", 5L))), 800, TimeUnit.MILLISECONDS);//thread pool needs warm-up Assertions.assertThat(result.getVersion()).isEqualTo(7L); }