Skip to content

Commit 9870d1f

Browse files
committed
Avoid calling external services in our integration tests
Our integ tests randomly fail when httpbin.org starts rate-limiting us.
1 parent 788ffe0 commit 9870d1f

File tree

2 files changed

+59
-57
lines changed

2 files changed

+59
-57
lines changed

examples/restjson-client/build.gradle.kts

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
`java-library`
3+
application
34
id("software.amazon.smithy.gradle.smithy-base")
45
id("me.champeau.jmh") version "0.7.3"
56
}
@@ -17,6 +18,10 @@ dependencies {
1718
testImplementation(libs.assertj.core)
1819
}
1920

21+
application {
22+
mainClass = "software.amazon.smithy.java.example.ClientExample"
23+
}
24+
2025
// Add generated Java sources to the main sourceset
2126
afterEvaluate {
2227
val clientPath = smithy.getPluginProjectionPath(smithy.sourceProjection.get(), "java-client-codegen")

examples/restjson-client/src/it/java/software/amazon/smithy/java/example/GenericTest.java renamed to examples/restjson-client/src/main/java/software/amazon/smithy/java/example/ClientExample.java

+54-57
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
/*
2-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
* SPDX-License-Identifier: Apache-2.0
4-
*/
5-
61
package software.amazon.smithy.java.example;
72

8-
import static java.nio.ByteBuffer.wrap;
9-
10-
import java.nio.charset.StandardCharsets;
11-
import java.time.Instant;
12-
import java.util.List;
13-
import java.util.concurrent.ExecutionException;
14-
import org.junit.jupiter.api.Test;
153
import software.amazon.smithy.java.client.core.endpoint.EndpointResolver;
164
import software.amazon.smithy.java.client.core.interceptors.ClientInterceptor;
175
import software.amazon.smithy.java.client.core.interceptors.RequestHook;
@@ -28,64 +16,75 @@
2816
import software.amazon.smithy.java.io.datastream.DataStream;
2917
import software.amazon.smithy.java.json.JsonCodec;
3018

31-
public class GenericTest {
19+
import java.nio.charset.StandardCharsets;
20+
import java.time.Instant;
21+
import java.util.List;
22+
import java.util.concurrent.ExecutionException;
23+
24+
import static java.nio.ByteBuffer.wrap;
25+
26+
public final class ClientExample {
27+
public static void main(String[] args) throws Exception {
28+
putPerson();
29+
getPersonImage();
30+
streamingRequestPayload();
31+
testDocument();
32+
serde();
33+
supportsInterceptors();
34+
}
3235

33-
@Test
34-
public void putPerson() throws ExecutionException, InterruptedException {
36+
public static void putPerson() throws ExecutionException, InterruptedException {
3537
// Create a generated client using rest-json and a fixed endpoint.
3638
var client = PersonDirectoryClient.builder()
37-
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
38-
.build();
39+
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
40+
.build();
3941

4042
PutPersonInput input = PutPersonInput.builder()
41-
.name("Michael")
42-
.age(999)
43-
.favoriteColor("Green")
44-
.birthday(Instant.now())
45-
.build();
43+
.name("Michael")
44+
.age(999)
45+
.favoriteColor("Green")
46+
.birthday(Instant.now())
47+
.build();
4648

4749
PutPersonOutput output = client.putPerson(input);
4850
System.out.println("Output: " + output);
4951
}
5052

51-
@Test
52-
public void getPersonImage() {
53+
public static void getPersonImage() {
5354
PersonDirectoryClient client = PersonDirectoryClient.builder()
54-
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
55-
.build();
55+
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
56+
.build();
5657

5758
GetPersonImageInput input = GetPersonImageInput.builder().name("Michael").build();
5859
GetPersonImageOutput output = client.getPersonImage(input);
5960
System.out.println("Output: " + output);
6061
}
6162

62-
@Test
63-
public void streamingRequestPayload() {
63+
public static void streamingRequestPayload() {
6464
PersonDirectoryClient client = PersonDirectoryClient.builder()
65-
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
66-
.build();
65+
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
66+
.build();
6767

6868
PutPersonImageInput input = PutPersonImageInput.builder()
69-
.name("Michael")
70-
.tags(List.of("Foo", "Bar"))
71-
.moreTags(List.of("Abc", "one two"))
72-
.image(DataStream.ofString("image..."))
73-
.build();
69+
.name("Michael")
70+
.tags(List.of("Foo", "Bar"))
71+
.moreTags(List.of("Abc", "one two"))
72+
.image(DataStream.ofString("image..."))
73+
.build();
7474
PutPersonImageOutput output = client.putPersonImage(input);
7575
System.out.println("Output: " + output);
7676
}
7777

78-
@Test
79-
public void testDocument() {
78+
public static void testDocument() {
8079
Codec codec = JsonCodec.builder().useJsonName(true).useTimestampFormat(true).build();
8180

8281
PutPersonInput input = PutPersonInput.builder()
83-
.name("Michael")
84-
.age(999)
85-
.favoriteColor("Green")
86-
.birthday(Instant.now())
87-
.binary(wrap("Hello".getBytes(StandardCharsets.UTF_8)))
88-
.build();
82+
.name("Michael")
83+
.age(999)
84+
.favoriteColor("Green")
85+
.birthday(Instant.now())
86+
.binary(wrap("Hello".getBytes(StandardCharsets.UTF_8)))
87+
.build();
8988

9089
// Serialize directly to JSON.
9190
System.out.println(codec.serializeToString(input));
@@ -101,14 +100,13 @@ public void testDocument() {
101100
System.out.println(codec.serializeToString(inputCopy));
102101
}
103102

104-
@Test
105-
public void serde() {
103+
public static void serde() {
106104
PutPersonInput input = PutPersonInput.builder()
107-
.name("Michael")
108-
.age(999)
109-
.favoriteColor("Green")
110-
.birthday(Instant.now())
111-
.build();
105+
.name("Michael")
106+
.age(999)
107+
.favoriteColor("Green")
108+
.birthday(Instant.now())
109+
.build();
112110

113111
JsonCodec codec = JsonCodec.builder().useJsonName(true).useTimestampFormat(true).build();
114112

@@ -122,8 +120,7 @@ public void serde() {
122120
System.out.println(codec.serializeToString(copy));
123121
}
124122

125-
@Test
126-
public void supportsInterceptors() throws Exception {
123+
public static void supportsInterceptors() throws Exception {
127124
var interceptor = new ClientInterceptor() {
128125
@Override
129126
public void readBeforeTransmit(RequestHook<?, ?, ?> hook) {
@@ -133,15 +130,15 @@ public void readBeforeTransmit(RequestHook<?, ?, ?> hook) {
133130
@Override
134131
public <RequestT> RequestT modifyBeforeTransmit(RequestHook<?, ?, RequestT> hook) {
135132
return hook.mapRequest(
136-
HttpRequest.class,
137-
h -> h.request().toBuilder().withAddedHeader("X-Foo", "Bar").build());
133+
HttpRequest.class,
134+
h -> h.request().toBuilder().withAddedHeader("X-Foo", "Bar").build());
138135
}
139136
};
140137

141138
PersonDirectoryClient client = PersonDirectoryClient.builder()
142-
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
143-
.addInterceptor(interceptor)
144-
.build();
139+
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
140+
.addInterceptor(interceptor)
141+
.build();
145142

146143
GetPersonImageInput input = GetPersonImageInput.builder().name("Michael").build();
147144
GetPersonImageOutput output = client.getPersonImage(input);

0 commit comments

Comments
 (0)