/** * 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)); }
/** * Shows the initialization of a {@link SphereClient} with a custom {@link HttpClient} provider. */ private void demoCreateClient() { final SphereClient sphereClient = SphereClientFactory.of(CustomClientConfigDemoIntegrationTest::createCustomHttpClient) .createClient("your projectKey", "your clientId", "your clientSecret"); }
public void instantiate() { final SphereClientFactory factory = SphereClientFactory.of(); final SphereClient client = factory.createClient( "jvm-sdk-dev-1", //replace with your project key "ELqF0rykXD2fyS8s-IhIPKfQ", //replace with your client id "222222222222222222222222222222226"); //replace with your client secret } }
private BlockingSphereClient createSphereClient() { final SphereClientFactory factory = SphereClientFactory.of(); final SphereClient asyncSphereClient = factory.createClient( "jvm-sdk-dev-1", //replace with your project key "ELqF0rykXD2fyS8s-IhIPKfQ", //replace with your client id "222222222222222222222222222222226"); //replace with your client secret return BlockingSphereClient.of(asyncSphereClient, 20, TimeUnit.SECONDS); } }
@Test public void autoLoadHttpClientWithoutExplicitSpecification() { if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { final SphereClientFactory sphereClientFactory = SphereClientFactory.of(); final SphereClient client = sphereClientFactory.createClient(getSphereClientConfig()); final PagedQueryResult<Category> queryResult = client.execute(CategoryQuery.of()).toCompletableFuture().join(); assertThat(queryResult).isNotNull(); client.close(); } }
@Test public void clientAlreadyClosed() throws Exception { if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { try (final SphereClient client = SphereClientFactory.of().createClient(getSphereClientConfig())) { client.close(); assertThatThrownBy(() -> client.execute(ProjectGet.of())) .isInstanceOf(IllegalStateException.class); } } }
@Test public void create() { //we cannot check this in TeamCity with sphere CI, but with travis if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { final SphereClientFactory clientFactory = SphereClientFactory.of(); try (final SphereClient client = clientFactory.createClient("empty-for-test", "empty", "empty")) { final Throwable throwable = catchThrowable(() -> doRequest(client)); assertThat(throwable) .as("if the client works then we know that the credentials are wrong") .isInstanceOf(CompletionException.class) .hasCauseInstanceOf(InvalidClientCredentialsException.class); } } }
/** * This is a test if no threads are blocking the the termination of the application. Needs to be executed on the ci server. * In case it has a leak, it just hangs. * This is necessary, since a unit test cannot cover this termination problem. * @param args unused command line parameters */ public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException { final SphereClient client = SphereClientFactory.of() .createClient(SphereClientConfig.ofEnvironmentVariables("JVM_SDK_IT")); final CompletionStage<PagedQueryResult<ProductProjection>> stage = client.execute(ProductProjectionQuery.ofStaged()); stage.toCompletableFuture().get(10, TimeUnit.SECONDS); client.close(); } }
@Test public void userAgent() throws Exception { try (final FakeHttpClient httpClient = new FakeHttpClient()) { try (final SphereClient client = SphereClientFactory.of(() -> httpClient).createClient(getSphereClientConfig())) { client.execute(ProjectGet.of()).toCompletableFuture().join(); assertThat(httpClient.getLastUserAgent()) .startsWith("commercetools-jvm-sdk") .contains("JVM-SDK-integration-tests") .contains(BuildInfo.version()); } } }
@Test public void misconfiguredApiUrl() throws Exception { if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { final String invalidApiUrl = "http://sdfjifdsjifdsjfdsjdfsjidsfjidfs.de"; final SphereClientConfig config = getSphereClientConfig().withApiUrl(invalidApiUrl); try (final SphereClient client = SphereClientFactory.of().createClient(config)) { final Throwable throwable = catchThrowable(() -> blockingWait(client.execute(ProjectGet.of()), Duration.ofMillis(1000))); assertThat(throwable).isInstanceOf(HttpException.class).hasCauseInstanceOf(UnknownHostException.class); assertThat(client).isNot(closed()); } } }
@Test public void misconfiguredAuthUrl() throws Exception { if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { final String invalidAuthUrl = "http://sdfjifdsjifdsjfdsjdfsjidsfjidfs.de"; final SphereClientConfig config = getSphereClientConfig().withAuthUrl(invalidAuthUrl); try (final SphereClient client = SphereClientFactory.of().createClient(config)) { final Throwable throwable = catchThrowable(() -> blockingWait(client.execute(ProjectGet.of()), Duration.ofMillis(1000))); assertThat(throwable).isInstanceOf(HttpException.class).hasCauseInstanceOf(UnknownHostException.class); assertThat(client).isNot(closed()); } } }
@Test public void invalidCredentialsClient() throws Exception { if (!"false".equals(System.getenv("JVM_SDK_IT_SSL_VALIDATION"))) { final SphereClientConfig config = SphereClientConfigBuilder.ofClientConfig(getSphereClientConfig()) .clientSecret("wrong") .build(); try (final SphereClient client = SphereClientFactory.of().createClient(config)) { final CompletionStage<Project> completionStage = client.execute(ProjectGet.of()); final Throwable throwable = catchThrowable(() -> blockingWait(completionStage, Duration.ofMillis(5000))); assertThat(throwable).isInstanceOf(InvalidClientCredentialsException.class); assertEventually(() -> { assertThat(client).is(closed()); }); } } }
private void permissionsExceeded() { final List<SphereScope> scopes = singletonList(SphereProjectScope.VIEW_PRODUCTS); ApiClientFixtures.withApiClient(client(), scopes, apiClient -> { final SphereClientConfig clientConfig = toSphereClientConfig(getSphereClientConfig(), apiClient); try (final SphereClient client = SphereClientFactory.of(IntegrationTest::newHttpClient) .createClient(clientConfig)) { assertThatThrownBy(() -> { final CustomerQuery request = CustomerQuery.of(); SphereClientUtils.blockingWait(client.execute(request), 5, SECONDS); }) .as("since the allowed scope is only to view products, customer data should not be loadable") .isInstanceOf(ForbiddenException.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; }
private void scopedTokenBody() { withApiClient(client(), asList(SphereProjectScope.VIEW_CUSTOMERS,SphereProjectScope.VIEW_ORDERS), apiClient -> { final SphereAuthConfig config = toSphereAuthConfig(getSphereClientConfig(),apiClient); assertThat(config.getScopes()).containsExactly("view_customers", "view_orders"); final CompletionStage<String> stringCompletionStage = TokensFacade.fetchAccessToken(config); final String accessToken = blockingWait(stringCompletionStage, 2, SECONDS); assertThat(accessToken).isNotEmpty(); try (final SphereClient client = SphereClientFactory.of() .createClient(getSphereClientConfig(), SphereAccessTokenSupplier.ofConstantToken(accessToken))) { final PagedQueryResult<Customer> customerPagedQueryResult = blockingWait(client.execute(CustomerQuery.of().withLimit(1)), 2, SECONDS); assertThat(customerPagedQueryResult).isNotNull(); } }); }
@Test public void integrationTest() { final HttpClient httpClient = AsyncHttpClientAdapter.of(new AsyncHttpClient()); final SphereClientConfig sphereClientConfig = IntegrationTest.getSphereClientConfig(); final SphereClientFactory factory = SphereClientFactory.of(() -> httpClient); try (final BlockingSphereClient sphereClient = BlockingSphereClient.of(factory.createClient(sphereClientConfig), 10, TimeUnit.SECONDS)) { final Project project = sphereClient.executeBlocking(ProjectGet.of()); assertThat(project.getKey()).isEqualTo(sphereClientConfig.getProjectKey()); } } }
private void passwordFlowDemo() { withApiClient(client(), singletonList(SphereProjectScope.VIEW_PRODUCTS), apiClient -> { withCustomer(client(), (Customer customer) -> { final SphereAuthConfig authConfig = toSphereAuthConfig(getSphereClientConfig(),apiClient); final String email = customer.getEmail(); // final String pw = customer.getPassword();//won;t work since it is obfusciated final String pw = CustomerFixtures.PASSWORD; final Tokens tokens = blockingWait(TokensFacade. fetchCustomerPasswordFlowTokens(authConfig, email, pw), 2, SECONDS); final String accessToken = tokens.getAccessToken(); assertThat(accessToken).isNotEmpty(); try (final SphereClient client = SphereClientFactory.of() .createClient(getSphereClientConfig(), SphereAccessTokenSupplier.ofConstantToken(accessToken))) { final PagedQueryResult<ProductProjection> customerPagedQueryResult = blockingWait(client.execute(ProductProjectionQuery.ofCurrent().withLimit(1)), 2, SECONDS); assertThat(customerPagedQueryResult).isNotNull(); } }); }); } }