public static SphereClientConfig of(final String projectKey, final String clientId, final String clientSecret) { return of(projectKey, clientId, clientSecret, AUTH_URL, API_URL); }
public SphereClientConfig withAuthUrl(final String authUrl) { return new SphereClientConfig(getProjectKey(), getClientId(), getClientSecret(), authUrl, getApiUrl(), getScopes(), getCorrelationIdGenerator()); }
public SphereClientConfig withApiUrl(final String apiUrl) { return new SphereClientConfig(getProjectKey(), getClientId(), getClientSecret(), getAuthUrl(), apiUrl, getScopes(), getCorrelationIdGenerator()); }
public static SphereClientConfigBuilder ofClientConfig(final SphereClientConfig config) { return ofKeyIdSecret(config.getProjectKey(), config.getClientId(), config.getClientSecret()) .authUrl(config.getAuthUrl()) .apiUrl(config.getApiUrl()) .scopeStrings(config.getScopes()) .correlationIdGenerator(config.getCorrelationIdGenerator()); }
@Test public void withApiUrl() throws Exception { final SphereClientConfig initial = SphereClientConfig.of("a", "b", "b"); assertThat(initial.getApiUrl()).isEqualTo("https://api.sphere.io"); final SphereClientConfig updated = initial.withApiUrl("another"); assertThat(updated.getApiUrl()).isEqualTo("another"); }
@Test public void withAuthUrl() throws Exception { final SphereClientConfig initial = SphereClientConfig.of("a", "b", "b"); assertThat(initial.getAuthUrl()).isEqualTo("https://auth.sphere.io"); final SphereClientConfig updated = initial.withAuthUrl("another"); assertThat(updated.getAuthUrl()).isEqualTo("another"); }
@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(); }); } }
@Test public void misconfigurationDomainInAuth() throws Exception { if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { try(final HttpClient httpClient = newHttpClient()) { final String authUrl = "http://sdfjifdsjifdsjfdsjdfsjidsfjidfs.de"; final String apiUrl = getSphereClientConfig().getApiUrl(); final SphereClientConfig config = SphereClientConfig.of("projectKey", "clientId", "clientSecret", authUrl, apiUrl); try (final SphereAccessTokenSupplier supplier = SphereAccessTokenSupplier.ofAutoRefresh(config, httpClient, false)) { supplier.close(); assertThatThrownBy(() -> supplier.get()).isInstanceOf(IllegalStateException.class); } } } }
@Test public void invalidJson() throws Throwable { final String response = "{invalid}"; final SphereAuthConfig config = SphereClientConfig.of(getSphereClientConfig().getProjectKey(), "foo", "bar"); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplier.ofOneTimeFetchingToken(config, new HttpClient() { @Override public CompletionStage<HttpResponse> execute(final HttpRequest httpRequest) { return CompletableFuture.completedFuture(HttpResponse.of(200, response)); } @Override public void close() { } }, true); assertThat(tokenSupplier.get().toCompletableFuture()) .hasFailed() .hasFailedWithThrowableThat() .isInstanceOf(JsonException.class) .hasMessageContaining("{invalid}") .hasMessageContaining("http request: HttpRequestImpl[httpMethod=POST,url=https://auth.sphere.io/oauth/token"); }
private void checkInvalidCredentialForAuthTokenSupplier(final Function<SphereAuthConfig, SphereAccessTokenSupplier> authTokenSupplierSupplier) { final SphereAuthConfig config = SphereAuthConfig.of(getSphereClientConfig().getProjectKey(), getSphereClientConfig().getClientId(), "wrong-password", getSphereClientConfig().getAuthUrl()); final SphereAccessTokenSupplier authTokenSupplier = authTokenSupplierSupplier.apply(config); assertThatThrownBy(() -> authTokenSupplier.get().toCompletableFuture().join()) .hasCauseInstanceOf(InvalidClientCredentialsException.class); final Throwable throwable = catchThrowable(() -> authTokenSupplier.get().toCompletableFuture().get(20, TimeUnit.MILLISECONDS)); assertThat(throwable) .as("further requests get also an exception and do not hang") .isInstanceOfAny(IllegalStateException.class, ExecutionException.class); authTokenSupplier.close(); } }
@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()); } }
public static SphereClientConfig toSphereClientConfig(final SphereClientConfig sphereClientConfig, final ApiClient apiClient) { final List<SphereScope> scopes = Arrays.stream(apiClient.getScope() .split("\\s")).map(s -> s.split(":")) .map(strings -> strings[0]) .map(SphereProjectScope::of) .collect(Collectors.toList()); return SphereClientConfigBuilder.ofKeyIdSecret(apiClient.getProjectKey(),apiClient.getId(),apiClient.getSecret()) .scopes(scopes) .authUrl(sphereClientConfig.getAuthUrl()) .apiUrl(sphereClientConfig.getApiUrl()) .build(); } }
@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())); }
private static SphereClientConfig loadViaEnvironmentArgs() { return SphereClientConfig.ofEnvironmentVariables("JVM_SDK_IT"); }
@Override public SphereClientConfig build() { return new SphereClientConfig(projectKey, clientId, clientSecret, authUrl, apiUrl, scopes, correlationIdGenerator); } }
@Test public void invalidCredentialsToGetToken() throws Throwable { final SphereAuthConfig config = SphereAuthConfig.of(getSphereClientConfig().getProjectKey(), getSphereClientConfig().getClientId(), "wrong-password", getSphereClientConfig().getAuthUrl()); final SphereAccessTokenSupplier supplierOfOneTimeFetchingToken = SphereAccessTokenSupplier.ofOneTimeFetchingToken(config, newHttpClient(), true); final CompletionStage<String> future = supplierOfOneTimeFetchingToken.get(); expectException(InvalidClientCredentialsException.class, future); supplierOfOneTimeFetchingToken.close(); }
@Test public void useToken() { if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { final SphereClientFactory sphereClientFactory = SphereClientFactory.of(); final SphereApiConfig sphereClientConfig = getSphereClientConfig(); try(final SphereClient client = sphereClientFactory.createClientOfApiConfigAndAccessToken(sphereClientConfig, accessToken())){ final Project project = client.execute(ProjectGet.of()).toCompletableFuture().join(); assertThat(project.getKey()).isEqualTo(getSphereClientConfig().getProjectKey()); } } }
private static SphereClientConfig loadViaEnvironmentArgs() { return SphereClientConfig.ofEnvironmentVariables("JVM_SDK_IT"); }
public static SphereClientConfig of(final String projectKey, final String clientId, final String clientSecret, final String authUrl, final String apiUrl) { return new SphereClientConfig(projectKey, clientId, clientSecret, authUrl, apiUrl); }
public static SphereClientConfig of(final String projectKey, final String clientId, final String clientSecret) { return of(projectKey, clientId, clientSecret, AUTH_URL, API_URL); }