Skip to content

Avoid calling external services in our integration tests #668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/restjson-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
`java-library`
application
id("software.amazon.smithy.gradle.smithy-base")
id("me.champeau.jmh") version "0.7.3"
}
Expand All @@ -17,6 +18,10 @@ dependencies {
testImplementation(libs.assertj.core)
}

application {
mainClass = "software.amazon.smithy.java.example.ClientExample"
}

// Add generated Java sources to the main sourceset
afterEvaluate {
val clientPath = smithy.getPluginProjectionPath(smithy.sourceProjection.get(), "java-client-codegen")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.java.example;

import static java.nio.ByteBuffer.wrap;

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.java.client.core.endpoint.EndpointResolver;
import software.amazon.smithy.java.client.core.interceptors.ClientInterceptor;
import software.amazon.smithy.java.client.core.interceptors.RequestHook;
Expand All @@ -28,64 +16,75 @@
import software.amazon.smithy.java.io.datastream.DataStream;
import software.amazon.smithy.java.json.JsonCodec;

public class GenericTest {
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.ExecutionException;

import static java.nio.ByteBuffer.wrap;

public final class ClientExample {
public static void main(String[] args) throws Exception {
putPerson();
getPersonImage();
streamingRequestPayload();
testDocument();
serde();
supportsInterceptors();
}

@Test
public void putPerson() throws ExecutionException, InterruptedException {
public static void putPerson() throws ExecutionException, InterruptedException {
// Create a generated client using rest-json and a fixed endpoint.
var client = PersonDirectoryClient.builder()
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
.build();
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
.build();

PutPersonInput input = PutPersonInput.builder()
.name("Michael")
.age(999)
.favoriteColor("Green")
.birthday(Instant.now())
.build();
.name("Michael")
.age(999)
.favoriteColor("Green")
.birthday(Instant.now())
.build();

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

@Test
public void getPersonImage() {
public static void getPersonImage() {
PersonDirectoryClient client = PersonDirectoryClient.builder()
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
.build();
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
.build();

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

@Test
public void streamingRequestPayload() {
public static void streamingRequestPayload() {
PersonDirectoryClient client = PersonDirectoryClient.builder()
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
.build();
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
.build();

PutPersonImageInput input = PutPersonImageInput.builder()
.name("Michael")
.tags(List.of("Foo", "Bar"))
.moreTags(List.of("Abc", "one two"))
.image(DataStream.ofString("image..."))
.build();
.name("Michael")
.tags(List.of("Foo", "Bar"))
.moreTags(List.of("Abc", "one two"))
.image(DataStream.ofString("image..."))
.build();
PutPersonImageOutput output = client.putPersonImage(input);
System.out.println("Output: " + output);
}

@Test
public void testDocument() {
public static void testDocument() {
Codec codec = JsonCodec.builder().useJsonName(true).useTimestampFormat(true).build();

PutPersonInput input = PutPersonInput.builder()
.name("Michael")
.age(999)
.favoriteColor("Green")
.birthday(Instant.now())
.binary(wrap("Hello".getBytes(StandardCharsets.UTF_8)))
.build();
.name("Michael")
.age(999)
.favoriteColor("Green")
.birthday(Instant.now())
.binary(wrap("Hello".getBytes(StandardCharsets.UTF_8)))
.build();

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

@Test
public void serde() {
public static void serde() {
PutPersonInput input = PutPersonInput.builder()
.name("Michael")
.age(999)
.favoriteColor("Green")
.birthday(Instant.now())
.build();
.name("Michael")
.age(999)
.favoriteColor("Green")
.birthday(Instant.now())
.build();

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

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

@Test
public void supportsInterceptors() throws Exception {
public static void supportsInterceptors() throws Exception {
var interceptor = new ClientInterceptor() {
@Override
public void readBeforeTransmit(RequestHook<?, ?, ?> hook) {
Expand All @@ -133,15 +130,15 @@ public void readBeforeTransmit(RequestHook<?, ?, ?> hook) {
@Override
public <RequestT> RequestT modifyBeforeTransmit(RequestHook<?, ?, RequestT> hook) {
return hook.mapRequest(
HttpRequest.class,
h -> h.request().toBuilder().withAddedHeader("X-Foo", "Bar").build());
HttpRequest.class,
h -> h.request().toBuilder().withAddedHeader("X-Foo", "Bar").build());
}
};

PersonDirectoryClient client = PersonDirectoryClient.builder()
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
.addInterceptor(interceptor)
.build();
.endpointResolver(EndpointResolver.staticHost("http://httpbin.org/anything"))
.addInterceptor(interceptor)
.build();

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