static SphereApiConfig of(final String projectKey) { return of(projectKey, API_URL); }
static SphereApiConfig of(final String projectKey) { return of(projectKey, API_URL); }
@Override public <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest) { final HttpRequestIntent httpRequest = sphereRequest.httpRequestIntent(); final HttpResponse httpResponse = function.apply(httpRequest); try { final T t = SphereClientImpl.parse(sphereRequest, objectMapper, SphereApiConfig.of("fake-project-key-for-testing", "https://createHttpTestDouble.tld"), httpResponse); return CompletableFutureUtils.successful(t); } catch (final Exception e) { return CompletableFutureUtils.failed(e); } }
@Override public <T> CompletableFuture<T> execute(final SphereRequest<T> sphereRequest) { final HttpRequestIntent httpRequest = sphereRequest.httpRequestIntent(); final HttpResponse httpResponse = function.apply(httpRequest); try { final T t = SphereClientImpl.parse(httpResponse, sphereRequest, objectMapper, SphereApiConfig.of("createHttpTestDouble", "https://createHttpTestDouble.tld")); return CompletableFutureUtils.successful(t); } catch (final Exception e) { return CompletableFutureUtils.failed(e); } }
@Test public void validateApiUrl() throws Exception { assertThatThrownBy(() -> SphereApiConfig.of("foo", null)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("apiUrl"); assertThatThrownBy(() -> SphereApiConfig.of("foo", "")) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("apiUrl"); } }
private SphereClient getSphereClient(final HttpClient httpClient) { return SphereClient.of(SphereApiConfig.of("projectKey"), httpClient, SphereAccessTokenSupplier.ofConstantToken("accessToken")); }
private SphereClient clientWithResponse(final HttpResponse response) { return SphereClient.of(SphereApiConfig.of("test"), new HttpClient() { @Override public CompletionStage<HttpResponse> execute(final HttpRequest httpRequest) { return CompletableFutureUtils.successful(response); } @Override public void close() { } }, SphereAccessTokenSupplier.ofConstantToken("foo")); } }
private SphereClient getSphereClient(final HttpClient httpClient) { return SphereClient.of(SphereApiConfig.of("projectKey"), httpClient, SphereAccessTokenSupplier.ofConstantToken("accessToken")); }
@Test public void apiRequestWithWrongToken() throws Throwable { client(); final SphereApiConfig config = SphereApiConfig.of(getSphereClientConfig().getProjectKey(), getSphereClientConfig().getApiUrl()); final SphereClient client = SphereClient.of(config, newHttpClient(),SphereAccessTokenSupplier.ofConstantToken("invalid-token")); expectExceptionAndClose(client, InvalidTokenException.class, client.execute(CategoryQuery.of())); }
@Test public void requestsArePossible() throws Exception { withCategory(client(), category -> { final CategoryQuery categoryQuery = CategoryQuery.of(); final SphereApiConfig apiConfig = SphereApiConfig.of(getSphereClientConfig().getProjectKey(), getSphereClientConfig().getApiUrl()); final SphereAuthConfig authConfig = SphereAuthConfig.of(getSphereClientConfig().getProjectKey(), getSphereClientConfig().getClientId(), getSphereClientConfig().getClientSecret(), getSphereClientConfig().getAuthUrl()); final SphereAccessTokenSupplier fixedTokenSupplier = SphereAccessTokenSupplier.ofOneTimeFetchingToken(authConfig, newHttpClient(), true); final SphereClient oneTokenClient = SphereClient.of(apiConfig, newHttpClient(), fixedTokenSupplier); final Long actual = oneTokenClient.execute(categoryQuery).toCompletableFuture().join().getTotal(); assertThat(actual).isGreaterThanOrEqualTo(1); oneTokenClient.close(); }); } }