Skip to content

Commit 6466ede

Browse files
committed
WIP: add checkout sdk
1 parent ed7c2eb commit 6466ede

File tree

109 files changed

+5394
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+5394
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
dependencies {
3+
api project(':rmf:rmf-java-base')
4+
api jackson_core.annotations
5+
api jackson_core.databind
6+
implementation google.findbugs
7+
implementation javax.validation
8+
api commons.lang3
9+
10+
integrationTestImplementation project(':commercetools:commercetools-http-client')
11+
}
12+
13+
sourceSets.main.java.srcDirs += "src/main/java-generated"
14+
sourceSets.test.java.srcDirs += "src/test/java-generated"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
package com.commercetools.checkout.client;
3+
4+
import java.lang.reflect.Method;
5+
import java.lang.reflect.Modifier;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
import org.assertj.core.api.Assertions;
10+
import org.assertj.core.util.Lists;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class ProjectApiRootTest {
14+
15+
private static final List<String> ignoreMethods = Lists.newArrayList();
16+
17+
/**
18+
* Retrieves all public methods of the {@link ProjectScopedApiRoot} and the public methods of the {@link ByProjectKeyRequestBuilder} and
19+
* checks if project request builder methods are present in ProjectApiRoot methods
20+
*/
21+
@Test
22+
public void allSubResourcesSupported() {
23+
24+
final List<String> projectApiRootMethods = Lists.newArrayList(ProjectScopedApiRoot.class.getDeclaredMethods())
25+
.stream()
26+
.filter(method -> Modifier.isPublic(method.getModifiers()))
27+
.map(Method::getName)
28+
.distinct()
29+
.collect(Collectors.toList());
30+
31+
final List<String> resourceMethods = Lists.newArrayList(ByProjectKeyRequestBuilder.class.getDeclaredMethods())
32+
.stream()
33+
.filter(method -> Modifier.isPublic(method.getModifiers()))
34+
.map(Method::getName)
35+
.filter(methodName -> !ignoreMethods.contains(methodName))
36+
.filter(methodName -> !projectApiRootMethods.contains(methodName))
37+
.collect(Collectors.toList());
38+
39+
Assertions.assertThat(resourceMethods)
40+
.withFailMessage("missing endpoints in ProjectApiRoot: %s", resourceMethods)
41+
.isEmpty();
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
package com.commercetools.checkout.defaultconfig;
3+
4+
public class CheckoutIntegrationTests {
5+
6+
// @Test
7+
// public void createAndDelete() {
8+
// final ApiHttpResponse<RecordPagedQueryResponse> response = CheckoutApiTestUtils.getProjectRoot()
9+
// .()
10+
// .executeBlocking();
11+
//
12+
// Assertions.assertNotNull(response.getBody());
13+
// }
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<configuration debug="false">
2+
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
3+
<resetJUL>true</resetJUL>
4+
</contextListener>
5+
6+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
7+
<encoder>
8+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
9+
</encoder>
10+
</appender>
11+
12+
<logger name="commercetools" level="WARN"/>
13+
14+
<root level="INFO">
15+
<appender-ref ref="STDOUT" />
16+
</root>
17+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
package com.commercetools.checkout.client;
3+
4+
import java.util.UUID;
5+
6+
import io.vrap.rmf.base.client.http.CorrelationIdProvider;
7+
8+
public class CheckoutCorrelationIdProvider implements CorrelationIdProvider {
9+
private final String projectKey;
10+
11+
public CheckoutCorrelationIdProvider(String projectKey) {
12+
this.projectKey = projectKey;
13+
}
14+
15+
@Override
16+
public String getCorrelationId() {
17+
return projectKey + "/" + UUID.randomUUID();
18+
}
19+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
package com.commercetools.checkout.client;
3+
4+
import java.io.Closeable;
5+
6+
import io.vrap.rmf.base.client.ApiHttpClient;
7+
import io.vrap.rmf.base.client.SerializerOnlyApiHttpClient;
8+
9+
public class ProjectApiRoot implements Closeable, ProjectScopedApiRoot {
10+
private final String projectKey;
11+
private final ApiHttpClient apiHttpClient;
12+
13+
private ProjectApiRoot(final String projectKey, final ApiHttpClient apiHttpClient) {
14+
this.projectKey = projectKey;
15+
this.apiHttpClient = apiHttpClient;
16+
}
17+
18+
public static ProjectApiRoot of(final String projectKey) {
19+
return new ProjectApiRoot(projectKey, SerializerOnlyApiHttpClient.of());
20+
}
21+
22+
public static ProjectApiRoot fromClient(final String projectKey, final ApiHttpClient apiHttpClient) {
23+
return new ProjectApiRoot(projectKey, apiHttpClient);
24+
}
25+
26+
@Override
27+
public ByProjectKeyRequestBuilder with() {
28+
return ApiRoot.fromClient(apiHttpClient).withProjectKey(projectKey);
29+
}
30+
31+
public ByProjectKeyRequestBuilder withProjectKey(final String projectKey) {
32+
return ApiRoot.fromClient(apiHttpClient).withProjectKey(projectKey);
33+
}
34+
35+
@Override
36+
public void close() {
37+
if (apiHttpClient == null) {
38+
return;
39+
}
40+
try {
41+
apiHttpClient.close();
42+
}
43+
catch (final Throwable ignored) {
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
package com.commercetools.checkout.client;
3+
4+
public interface ProjectScopedApiRoot {
5+
ByProjectKeyRequestBuilder with();
6+
}

0 commit comments

Comments
 (0)