|
1 | 1 |
|
2 | 2 | package com.commercetools.checkout.defaultconfig; |
3 | 3 |
|
| 4 | +import com.commercetools.api.defaultconfig.ApiRootBuilder; |
| 5 | +import com.commercetools.api.models.cart.Cart; |
| 6 | +import com.commercetools.api.models.cart.CartDraft; |
| 7 | +import com.commercetools.api.client.ProjectApiRoot; |
| 8 | +import com.commercetools.checkout.models.transaction.TransactionDraft; |
| 9 | +import io.vrap.rmf.base.client.ApiHttpException; |
| 10 | +import io.vrap.rmf.base.client.oauth2.ClientCredentials; |
| 11 | +import org.junit.jupiter.api.Assertions; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.mockito.Mockito; |
| 14 | + |
4 | 15 | public class CheckoutIntegrationTests { |
5 | 16 |
|
6 | | - // @Test |
7 | | - // public void createAndDelete() { |
8 | | - // final ApiHttpResponse<RecordPagedQueryResponse> response = CheckoutApiTestUtils.getProjectRoot() |
9 | | - // .() |
10 | | - // .executeBlocking(); |
11 | | - // |
12 | | - // Assertions.assertNotNull(response.getBody()); |
13 | | - // } |
| 17 | + private static final ProjectApiRoot projectApiRoot; |
| 18 | + private static final com.commercetools.checkout.client.ProjectApiRoot checkoutApiRoot; |
| 19 | + |
| 20 | + static { |
| 21 | + projectApiRoot = createApiClient(); |
| 22 | + checkoutApiRoot = createCheckoutClient(); |
| 23 | + } |
| 24 | + public static ProjectApiRoot createApiClient() { |
| 25 | + return ApiRootBuilder.ofEnvironmentVariables() |
| 26 | + .buildProjectRoot(); |
| 27 | + } |
| 28 | + |
| 29 | + public static com.commercetools.checkout.client.ProjectApiRoot createCheckoutClient() { |
| 30 | + return CheckoutApiRootBuilder.of() |
| 31 | + .defaultClient(ClientCredentials.of() |
| 32 | + .withClientId(System.getenv("CTP_CLIENT_ID")) |
| 33 | + .withClientSecret(System.getenv("CTP_CLIENT_SECRET")) |
| 34 | + .build(), |
| 35 | + ServiceRegion.GCP_EUROPE_WEST1) |
| 36 | + .build(System.getenv("CTP_PROJECT_KEY")); |
| 37 | + } |
| 38 | + |
| 39 | + // create client, then cart, then transaction |
| 40 | + @Test |
| 41 | + public void createAndGetTransactionTest() { |
| 42 | + var newCart = CartDraft.builder().currency("EUR").build(); |
| 43 | + |
| 44 | + var cart = projectApiRoot |
| 45 | + .carts() |
| 46 | + .post(newCart) |
| 47 | + .executeBlocking() |
| 48 | + .getBody(); |
| 49 | + Assertions.assertNotNull(cart); |
| 50 | + |
| 51 | + var transactionKey = "transaction-" + CheckoutApiTestUtils.randomKey(); |
| 52 | + var transaction = checkoutApiRoot.with().transactions().post( |
| 53 | + TransactionDraft.builder() |
| 54 | + .key(transactionKey) |
| 55 | + .application(a -> a.key("demo-commercetools-checkout")) |
| 56 | + .cart(c -> c.id(cart.getId())) |
| 57 | + .plusTransactionItems(t -> t |
| 58 | + .amount(a -> a.centAmount(100).currencyCode("EUR")) |
| 59 | + .paymentIntegration(p -> p.key("sample-invoice-payment"))) |
| 60 | + .build() |
| 61 | + |
| 62 | + ).executeBlocking().getBody(); |
| 63 | + |
| 64 | + // Create transaction |
| 65 | + Assertions.assertNotNull(transaction); |
| 66 | + |
| 67 | + Assertions.assertNotNull(checkoutApiRoot.with().transactions().withId(transaction.getId()).get().executeBlocking().getBody()); |
| 68 | + Assertions.assertEquals(transactionKey, transaction.getKey()); |
| 69 | + Assertions.assertNotNull(checkoutApiRoot.with().transactions().withKey(transaction.getKey()).get().executeBlocking().getBody()); |
| 70 | + } |
14 | 71 | } |
0 commit comments