|
| 1 | + |
| 2 | +package com.commercetools.checkout.defaultconfig; |
| 3 | + |
| 4 | +import java.time.Duration; |
| 5 | +import java.util.UUID; |
| 6 | +import java.util.function.Consumer; |
| 7 | + |
| 8 | +import com.commercetools.checkout.client.ProjectApiRoot; |
| 9 | + |
| 10 | +import io.vrap.rmf.base.client.oauth2.ClientCredentials; |
| 11 | + |
| 12 | +import org.apache.commons.lang3.StringUtils; |
| 13 | +import org.assertj.core.api.SoftAssertions; |
| 14 | + |
| 15 | +public class CheckoutApiTestUtils { |
| 16 | + |
| 17 | + private static final ProjectApiRoot projectRoot; |
| 18 | + |
| 19 | + static { |
| 20 | + String logLevel = System.getenv("CTP_JVM_SDK_LOG_LEVEL"); |
| 21 | + if ("OFF".equals(logLevel)) { |
| 22 | + projectRoot = CheckoutApiRootBuilder.of() |
| 23 | + .defaultClient( |
| 24 | + ClientCredentials.of().withClientId(getClientId()).withClientSecret(getClientSecret()).build(), |
| 25 | + ServiceRegion.GCP_EUROPE_WEST1) |
| 26 | + .build(getProjectKey()); |
| 27 | + } |
| 28 | + else { |
| 29 | + projectRoot = CheckoutApiRootBuilder.of() |
| 30 | + .defaultClient( |
| 31 | + ClientCredentials.of().withClientId(getClientId()).withClientSecret(getClientSecret()).build(), |
| 32 | + ServiceRegion.GCP_EUROPE_WEST1) |
| 33 | + .build(getProjectKey()); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public static String randomString() { |
| 38 | + return "random-string-" + UUID.randomUUID().toString(); |
| 39 | + } |
| 40 | + |
| 41 | + public static String randomId() { |
| 42 | + return "random-id-" + UUID.randomUUID().toString(); |
| 43 | + } |
| 44 | + |
| 45 | + public static String randomKey() { |
| 46 | + return "random-key-" + UUID.randomUUID().toString(); |
| 47 | + } |
| 48 | + |
| 49 | + public static String getProjectKey() { |
| 50 | + return System.getenv("CTP_PROJECT_KEY"); |
| 51 | + } |
| 52 | + |
| 53 | + public static String getClientId() { |
| 54 | + return System.getenv("CTP_CLIENT_ID"); |
| 55 | + } |
| 56 | + |
| 57 | + public static String getClientSecret() { |
| 58 | + return System.getenv("CTP_CLIENT_SECRET"); |
| 59 | + } |
| 60 | + |
| 61 | + public static ProjectApiRoot getProjectRoot() { |
| 62 | + return projectRoot; |
| 63 | + } |
| 64 | + |
| 65 | + public static void assertEventually(final Duration maxWaitTime, final Duration waitBeforeRetry, |
| 66 | + final Runnable block) { |
| 67 | + final long timeOutAt = System.currentTimeMillis() + maxWaitTime.toMillis(); |
| 68 | + while (true) { |
| 69 | + try { |
| 70 | + block.run(); |
| 71 | + |
| 72 | + // the block executed without throwing an exception, return |
| 73 | + return; |
| 74 | + } |
| 75 | + catch (AssertionError e) { |
| 76 | + if (System.currentTimeMillis() > timeOutAt) { |
| 77 | + throw e; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + try { |
| 82 | + Thread.sleep(waitBeforeRetry.toMillis()); |
| 83 | + } |
| 84 | + catch (InterruptedException e) { |
| 85 | + throw new RuntimeException(e); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public static void assertEventually(final Consumer<SoftAssertions> assertionsConsumer) { |
| 91 | + final Runnable block = () -> { |
| 92 | + final SoftAssertions softly = new SoftAssertions(); |
| 93 | + assertionsConsumer.accept(softly); |
| 94 | + softly.assertAll(); |
| 95 | + }; |
| 96 | + assertEventually(block); |
| 97 | + } |
| 98 | + |
| 99 | + public static void assertEventually(final Runnable block) { |
| 100 | + final Boolean useLongTimeout = "true".equals(System.getenv("TRAVIS")) |
| 101 | + || StringUtils.isNotEmpty(System.getenv("TEAMCITY_VERSION")) |
| 102 | + || StringUtils.isNoneEmpty(System.getenv("GITHUB_WORKSPACE")); |
| 103 | + final Duration maxWaitTime = Duration.ofSeconds(useLongTimeout ? 60 : 30); |
| 104 | + final Duration waitBeforeRetry = Duration.ofMillis(100); |
| 105 | + assertEventually(maxWaitTime, waitBeforeRetry, block); |
| 106 | + } |
| 107 | +} |
0 commit comments