public static SphereClientConfig of(final String projectKey, final String clientId, final String clientSecret) { return of(projectKey, clientId, clientSecret, AUTH_URL, API_URL); }
public static SphereClientConfig of(final String projectKey, final String clientId, final String clientSecret) { return of(projectKey, clientId, clientSecret, AUTH_URL, API_URL); }
/** * Creates a standard client suitable for online shops. * * For the credentials consult <a href="https://admin.sphere.io">the Merchant Center</a>. * @param projectKey the project identifier * @param clientId username * @param clientSecret password * @return sphere client */ public SphereClient createClient(final String projectKey, final String clientId, final String clientSecret) { return createClient(SphereClientConfig.of(projectKey, clientId, clientSecret)); }
/** * Creates a standard client suitable for online shops. * * For the credentials consult <a href="https://admin.sphere.io">the Merchant Center</a>. * @param projectKey the project identifier * @param clientId username * @param clientSecret password * @return sphere client */ public SphereClient createClient(final String projectKey, final String clientId, final String clientSecret) { return createClient(SphereClientConfig.of(projectKey, clientId, clientSecret)); }
/** * Creates a standard client suitable for online shops. * * For the credentials consult <a href="https://admin.sphere.io">the Merchant Center</a>. * @param projectKey the project identifier * @param clientId username * @param clientSecret password * @return sphere client */ default SphereClient createClient(String projectKey, String clientId, String clientSecret) { return createClient(SphereClientConfig.of(projectKey, clientId, clientSecret)); }
/** * Creates a standard client suitable for online shops. * * For the credentials consult <a href="https://admin.sphere.io">the Merchant Center</a>. * @param projectKey the project identifier * @param clientId username * @param clientSecret password * @return sphere client */ default SphereClient createClient(String projectKey, String clientId, String clientSecret) { return createClient(SphereClientConfig.of(projectKey, clientId, clientSecret)); }
@Test public void validateClientId() throws Exception { assertThatThrownBy(() -> SphereClientConfig.of("here", null, "here", "here", "here")) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("clientId"); assertThatThrownBy(() -> SphereClientConfig.of("here", "", "here", "here", "here")) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("clientId"); }
@Test public void validateAuthUrl() throws Exception { assertThatThrownBy(() -> SphereClientConfig.of("here", "here", "here", null, "here")) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("authUrl"); assertThatThrownBy(() -> SphereClientConfig.of("here", "here", "here", "", "here")) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("authUrl"); }
@Test public void validateApiUrl() throws Exception { assertThatThrownBy(() -> SphereClientConfig.of("here", "here", "here", "here", null)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("apiUrl"); assertThatThrownBy(() -> SphereClientConfig.of("here", "here", "here", "here", "")) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("apiUrl"); }
@Test public void allowMissingAuthUrl() throws Exception { final SphereClientConfig config = SphereClientConfigUtils.ofEnvironmentVariables("foo", key -> { final Map<String, String> map = new HashMap<>(); map.put("foo_PROJECT_KEY", "a"); map.put("foo_CLIENT_ID", "b"); map.put("foo_CLIENT_SECRET", "c"); return map.get(key); }); assertThat(config).isEqualTo(SphereClientConfig.of("a", "b", "c", "https://auth.sphere.io", "https://api.sphere.io")); } }
@Test public void validateProjectKey() throws Exception { assertThatThrownBy(() -> SphereClientConfig.of(null, "here", "here", "here", "here")) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining(""); assertThatThrownBy(() -> SphereClientConfig.of("", "here", "here", "here", "here")) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining(""); }
@Test public void validateClientSecret() throws Exception { assertThatThrownBy(() -> SphereClientConfig.of("here", "here", null, "here", "here")) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("clientSecret"); assertThatThrownBy(() -> SphereClientConfig.of("here", "here", "", "here", "here")) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("clientSecret"); }
@Test public void credentialsFromEnvironmentVariables() throws Exception { final SphereClientConfig config = SphereClientConfigUtils.ofEnvironmentVariables("foo", key -> { final Map<String, String> map = new HashMap<>(); map.put("foo_API_URL", "e"); map.put("foo_AUTH_URL", "d"); map.put("foo_PROJECT_KEY", "a"); map.put("foo_CLIENT_ID", "b"); map.put("foo_CLIENT_SECRET", "c"); return map.get(key); }); assertThat(config).isEqualTo(SphereClientConfig.of("a", "b", "c", "d", "e")); }
@Override public SphereClientConfig get() { final String project = getValue(CONFIG_PROJECT_KEY); final String clientId = getValue(CONFIG_CLIENT_ID); final String clientSecret = getValue(CONFIG_CLIENT_SECRET); final String authUrl = configuration.getString(CONFIG_AUTH_URL, DEFAULT_AUTH_URL); final String apiUrl = configuration.getString(CONFIG_API_URL, DEFAULT_API_URL); return SphereClientConfig.of(project, clientId, clientSecret, authUrl, apiUrl); }
@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 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"); }
protected static SphereClientConfig getSphereConfig() { return SphereClientConfig.of(projectKey(), clientId(), clientSecret(), authUrl(), apiUrl()); }
@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); } } } }
protected synchronized static TestClient client() { if (client == null) { final SphereClientFactory factory = SphereClientFactory.of(); final SphereClientConfig config = SphereClientConfig.of(projectKey(), clientId(), clientSecret(), authUrl(), apiUrl()); final SphereAccessTokenSupplier tokenSupplier = SphereAccessTokenSupplierFactory.of().createSupplierOfOneTimeFetchingToken(config); final SphereClient underlying = factory.createClient(config, tokenSupplier); final SphereClient underlyingWithDeprecationExceptions = DeprecationExceptionSphereClientDecorator.of(underlying); client = new TestClient(underlyingWithDeprecationExceptions); } return client; }
@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"); }