-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sort out a solution for testing that works for Spring Boot too
- Loading branch information
1 parent
c7bcbee
commit 0576768
Showing
12 changed files
with
318 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ring-boot-starter/src/test/java/dev/restate/sdk/springboot/SdkTestingIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH | ||
// | ||
// This file is part of the Restate Java SDK, | ||
// which is released under the MIT license. | ||
// | ||
// You can find a copy of the license in file LICENSE in the root | ||
// directory of this repository or package, or at | ||
// https://github.com/restatedev/sdk-java/blob/main/LICENSE | ||
package dev.restate.sdk.springboot; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import dev.restate.sdk.client.Client; | ||
import dev.restate.sdk.testing.*; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.Timeout; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest( | ||
classes = Greeter.class, | ||
properties = {"greetingPrefix=Something something "}) | ||
@RestateTest(restateContainerImage = "ghcr.io/restatedev/restate:main") | ||
public class SdkTestingIntegrationTest { | ||
|
||
@Autowired @BindService private Greeter greeter; | ||
|
||
@Test | ||
@Timeout(value = 10) | ||
void greet(@RestateClient Client ingressClient) { | ||
var client = greeterClient.fromClient(ingressClient); | ||
|
||
assertThat(client.greet("Francesco")).isEqualTo("Something something Francesco"); | ||
} | ||
} |
64 changes: 0 additions & 64 deletions
64
sdk-testing/src/main/java/dev/restate/sdk/testing/BaseRestateRunner.java
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
sdk-testing/src/main/java/dev/restate/sdk/testing/BindService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH | ||
// | ||
// This file is part of the Restate Java SDK, | ||
// which is released under the MIT license. | ||
// | ||
// You can find a copy of the license in file LICENSE in the root | ||
// directory of this repository or package, or at | ||
// https://github.com/restatedev/sdk-java/blob/main/LICENSE | ||
package dev.restate.sdk.testing; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @see RestateTest | ||
*/ | ||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface BindService {} |
80 changes: 80 additions & 0 deletions
80
sdk-testing/src/main/java/dev/restate/sdk/testing/RestateExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH | ||
// | ||
// This file is part of the Restate Java SDK, | ||
// which is released under the MIT license. | ||
// | ||
// You can find a copy of the license in file LICENSE in the root | ||
// directory of this repository or package, or at | ||
// https://github.com/restatedev/sdk-java/blob/main/LICENSE | ||
package dev.restate.sdk.testing; | ||
|
||
import java.util.List; | ||
import org.junit.jupiter.api.extension.*; | ||
import org.junit.platform.commons.support.AnnotationSupport; | ||
|
||
/** | ||
* @see RestateTest | ||
*/ | ||
public class RestateExtension implements BeforeAllCallback, ParameterResolver { | ||
|
||
static final ExtensionContext.Namespace NAMESPACE = | ||
ExtensionContext.Namespace.create(RestateExtension.class); | ||
static final String RUNNER = "Runner"; | ||
|
||
@Override | ||
public void beforeAll(ExtensionContext extensionContext) { | ||
extensionContext | ||
.getStore(NAMESPACE) | ||
.getOrComputeIfAbsent( | ||
RUNNER, ignored -> initializeRestateRunner(extensionContext), RestateRunner.class) | ||
.beforeAll(extensionContext); | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter( | ||
ParameterContext parameterContext, ExtensionContext extensionContext) | ||
throws ParameterResolutionException { | ||
return extensionContext | ||
.getStore(NAMESPACE) | ||
.getOrComputeIfAbsent( | ||
RUNNER, ignored -> initializeRestateRunner(extensionContext), RestateRunner.class) | ||
.supportsParameter(parameterContext, extensionContext); | ||
} | ||
|
||
@Override | ||
public Object resolveParameter( | ||
ParameterContext parameterContext, ExtensionContext extensionContext) | ||
throws ParameterResolutionException { | ||
return extensionContext | ||
.getStore(NAMESPACE) | ||
.getOrComputeIfAbsent( | ||
RUNNER, ignored -> initializeRestateRunner(extensionContext), RestateRunner.class) | ||
.resolveParameter(parameterContext, extensionContext); | ||
} | ||
|
||
private RestateRunner initializeRestateRunner(ExtensionContext extensionContext) { | ||
// Discover services | ||
List<Object> servicesToBind = | ||
AnnotationSupport.findAnnotatedFieldValues( | ||
extensionContext.getRequiredTestInstance(), BindService.class); | ||
if (servicesToBind.isEmpty()) { | ||
throw new IllegalStateException( | ||
"The class " | ||
+ extensionContext.getRequiredTestClass().getName() | ||
+ " is annotated with @RestateTest, but there are no fields annotated with @BindService"); | ||
} | ||
|
||
RestateTest testAnnotation = | ||
AnnotationSupport.findAnnotation(extensionContext.getRequiredTestClass(), RestateTest.class) | ||
.orElseThrow( | ||
() -> | ||
new IllegalStateException( | ||
"Expecting @RestateTest annotation on the test class")); | ||
|
||
// Build runner discovering services to bind | ||
var runnerBuilder = RestateRunnerBuilder.create(); | ||
servicesToBind.forEach(runnerBuilder::bind); | ||
runnerBuilder.withRestateContainerImage(testAnnotation.restateContainerImage()); | ||
return runnerBuilder.buildRunner(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
sdk-testing/src/main/java/dev/restate/sdk/testing/RestateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH | ||
// | ||
// This file is part of the Restate Java SDK, | ||
// which is released under the MIT license. | ||
// | ||
// You can find a copy of the license in file LICENSE in the root | ||
// directory of this repository or package, or at | ||
// https://github.com/restatedev/sdk-java/blob/main/LICENSE | ||
package dev.restate.sdk.testing; | ||
|
||
import java.lang.annotation.*; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
/** | ||
* Annotation to enable the Restate extension for JUnit 5. The annotation will bootstrap a Restate | ||
* environment using TestContainers, and will automatically register all services field of the class | ||
* annotated with {@link BindService}. | ||
* | ||
* <p>Example: | ||
* | ||
* <pre> | ||
* // Annotate the class as RestateTest to start a Restate environment | ||
* {@code @RestateTest} | ||
* class CounterTest { | ||
* | ||
* // Annotate the service to bind | ||
* {@code @BindService} private final Counter counter = new Counter(); | ||
* | ||
* // Inject the client to send requests | ||
* {@code @Test} | ||
* void testGreet({@code @RestateClient} Client ingressClient) { | ||
* var client = CounterClient.fromClient(ingressClient, "my-counter"); | ||
* | ||
* long response = client.get(); | ||
* assertThat(response).isEqualTo(0L); | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* <p>The runner will deploy the services locally, execute Restate as container using <a | ||
* href="https://java.testcontainers.org/">Testcontainers</a>, and register the services. | ||
* | ||
* <p>This extension is scoped per test class, meaning that the restate runner will be shared among | ||
* test methods. Because of the aforementioned issue, the extension sets the {@link TestInstance} | ||
* {@link TestInstance.Lifecycle#PER_CLASS} automatically. | ||
* | ||
* <p>Use the annotations {@link RestateClient}, {@link RestateURL} and {@link RestateAdminClient} | ||
* to interact with the deployed environment: | ||
* | ||
* <pre> | ||
* {@code @Test} | ||
* void initialCountIsZero({@code @RestateClient} Client client) { | ||
* var client = CounterClient.fromClient(ingressClient, "my-counter"); | ||
* | ||
* // Use client as usual | ||
* long response = client.get(); | ||
* assertThat(response).isEqualTo(0L); | ||
* }</pre> | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Inherited | ||
@ExtendWith(RestateExtension.class) | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
public @interface RestateTest { | ||
|
||
/** Restate container image to use */ | ||
String restateContainerImage() default "docker.io/restatedev/restate:latest"; | ||
} |
Oops, something went wrong.