@Override public <T> CompletableFuture<T> execute(final SphereRequest<T> sphereRequest) { return delegate.execute(sphereRequest); } }
@Override public <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest) { return delegate.execute(sphereRequest); } }
@Override public CompletionStage<Payment> findPayment(String id) { PaymentByIdGet q = PaymentByIdGet.of(id); return sphereClient.execute(q); }
@Override public CompletionStage<Payment> findPayment(String id) { PaymentByIdGet q = PaymentByIdGet.of(id); return sphereClient.execute(q); }
protected CompletionStage<Cart> setPaymentToCart(final Cart cart, final PaymentMethodInfo selectedPaymentMethod) { final List<PaymentMethodInfo> selectedPaymentMethods = singletonList(selectedPaymentMethod); return withPaymentsToRemove(cart, selectedPaymentMethods, paymentsToRemove -> withPaymentsToAdd(cart, selectedPaymentMethods, paymentsToAdd -> { final Stream<RemovePayment> removePaymentStream = paymentsToRemove.stream().map(RemovePayment::of); final Stream<AddPayment> addPaymentStream = paymentsToAdd.stream().map(AddPayment::of); final List<UpdateAction<Cart>> updateActions = Stream.concat(removePaymentStream, addPaymentStream).collect(toList()); return sphere().execute(CartUpdateCommand.of(cart, updateActions)); }) ); }
protected CompletionStage<List<ShippingMethod>> getShippingMethods() { return injector().getInstance(CartInSession.class).findCartId() .map(cartId -> sphere().execute(ShippingMethodsByCartGet.of(cartId))) .orElseGet(() -> completedFuture(emptyList())); } }
@Override public CompletionStage<Optional<Payment>> findPayment(Cart cart, String pspId, String methodId) { PaymentQuery queryAllRefPayments = createPaymentsForCartQuery(cart, pspId, methodId); return sphereClient.execute(queryAllRefPayments) .thenApply(page -> page.getTotal() > 0 ? Optional.of(page.getResults().get(0)) : Optional.empty()); }
private CompletionStage<Optional<Zone>> fetchZoneByCountry(final Location location) { final ZoneQuery request = ZoneQuery.of().byLocation(location); return sphereClient.execute(request) .thenApplyAsync(PagedResult::head); }
@Override public CompletionStage<Optional<Payment>> findPaymentWithoutTransaction(Cart cart, String pspId, String methodId) { if(null == cart.getPaymentInfo() || null == cart.getPaymentInfo().getPayments() || cart.getPaymentInfo().getPayments().isEmpty()) { return CompletableFuture.supplyAsync(() -> Optional.empty()); } PaymentQuery queryAllRefPayments = createPaymentsForCartQuery(cart, pspId, methodId); queryAllRefPayments = queryAllRefPayments .plusPredicates(p -> p.transactions().isEmpty()); return sphereClient.execute(queryAllRefPayments) .thenApply(page -> page.getTotal() > 0 ? Optional.of(page.getResults().get(0)) : Optional.empty()); }
@Test public void underTheLimitNewRequestsAreProcessed() throws Exception { final SphereClient client = QueueSphereClientDecorator.of(FAKE_SPHERE_CLIENT, MAX_PARALLEL_REQUESTS); for (int i = 0; i < MAX_PARALLEL_REQUESTS - 1; i++) { client.execute(SPHERE_REQUEST_FINISH_ON_COMMAND); } final String s = execute(client, SPHERE_REQUEST_SUCCESS); assertThat(s).isEqualTo(DUMMY_RESULT); }
@Override public boolean matches(final SphereClient client) { final Throwable throwable = catchThrowable(() -> blockingWait(client.execute(ProjectGet.of()), Duration.ofMillis(100))); return throwable != null && throwable instanceof IllegalStateException; } }.describedAs("closed");
@Test public void noSearchHintNoteOnNormalException() throws Exception { final SphereClient client = TestDoubleSphereClientFactory.createHttpTestDouble(intent -> HttpResponse.of(500)); assertThatThrownBy(() -> client.execute(CategoryQuery.of()).toCompletableFuture().join()) .hasCauseInstanceOf(SphereException.class) .matches(e -> ((SphereException) e.getCause()).getAdditionalNotes().stream().allMatch(s -> !s.contains("reindex"))); }
private CompletionStage<Order> createOrder(final Cart cart) { final String orderNumber = generateOrderNumber(); final OrderFromCartDraft orderDraft = OrderFromCartDraft.of(cart, orderNumber, orderInitialPaymentState(cart)); return sphere().execute(OrderFromCartCreateCommand.of(orderDraft)) .thenApplyAsync(order -> { injector().getInstance(OrderInSession.class).store(order); injector().getInstance(CartInSession.class).remove(); return order; }, defaultContext()); } }
@Test public void throwsTimeoutExceptionOnTimeout() throws Exception { final SphereClient sphereClient = new NotAnsweringSphereClient(); final SphereClient decoratedClient = TimeoutSphereClientDecorator.of(sphereClient, DELAY, TimeUnit.MILLISECONDS); assertThatThrownBy(() -> decoratedClient.execute(DummySphereRequest.of()).toCompletableFuture().join()) .hasCauseInstanceOf(SphereTimeoutException.class); }
public CompletionStage<Result> showHealth() throws IOException { final ProductProjectionSearch productRequest = ProductProjectionSearch.ofCurrent().withLimit(1); return sphere.execute(productRequest) .thenApplyAsync(this::renderGoodHealthStatus, HttpExecution.defaultContext()) .exceptionally(this::renderBadHealthStatus) .thenApply(result -> result.as(Http.MimeTypes.JSON)); }
public void createWithClient() { final ProductDraft productTemplate = new SimpleCottonTShirtProductDraftSupplier(productType, "demo product").get(); final ProductCreateCommand command = ProductCreateCommand.of(productTemplate); final CompletionStage<Product> result = client.execute(command); } }
@Test public void itWorks() throws Exception { //we cannot check this in TeamCity, this test is rather reserved for Travis ci if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { final SphereClientConfig config = getSphereClientConfig(); final HttpClient httpClient = ApacheHttpClientAdapter.of(HttpAsyncClients.createDefault()); final SphereAccessTokenSupplier supplier = SphereAccessTokenSupplier.ofOneTimeFetchingToken(config, httpClient, false); final SphereClient client = SphereClient.of(config, httpClient, supplier); final Project project = client.execute(ProjectGet.of()).toCompletableFuture().join(); assertThat(project.getKey()).isEqualTo(getSphereClientConfig().getProjectKey()); } }
protected CompletionStage<Optional<Order>> findOrderById(final String orderId) { final OrderByIdGet baseRequest = OrderByIdGet.of(orderId).plusExpansionPaths(m -> m.paymentInfo().payments()); final OrderByIdGet orderByIdGet = OrderByIdGetHook.runHook(hooks(), baseRequest); return sphere().execute(orderByIdGet) .thenApplyAsync(nullableOrder -> { if (nullableOrder != null) { OrderLoadedHook.runHook(hooks(), nullableOrder); } return Optional.ofNullable(nullableOrder); }, defaultContext()); }
@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); }
protected CompletionStage<Cart> executeCartUpdateCommandWithHooks(final CartUpdateCommand cmd) { final CartUpdateCommand command = CartUpdateCommandHook.runHook(hooks(), cmd); final CompletionStage<Cart> cartAfterOriginalCommandStage = sphere().execute(command); return cartAfterOriginalCommandStage .thenComposeAsync(cart -> CartUpdatedActionHook.runHook(hooks(), cart, cmd), defaultContext()) .thenApplyAsync(cart -> { CartUpdatedHook.runHook(hooks(), cart); return cart; }, defaultContext()); }