From 2cc860ff61431cbd9c5bef3d9e146dc052f0d754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D1=85=D0=B8=D0=BD=20=D0=91=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D1=81?= Date: Thu, 2 Jul 2020 19:07:12 +0300 Subject: [PATCH] Integration tests like in Maven Template --- src/main/g8/build.sbt | 12 +++ .../$package$/it/$name__Camel$StreamIT.java | 75 +++++++++++++++++++ .../src/test/resources/application.conf | 1 + .../src/test/resources/logback-test.xml | 14 ++++ 4 files changed, 102 insertions(+) create mode 100644 src/main/g8/integration-tests/src/test/java/$package$/it/$name__Camel$StreamIT.java create mode 100644 src/main/g8/integration-tests/src/test/resources/application.conf create mode 100644 src/main/g8/integration-tests/src/test/resources/logback-test.xml diff --git a/src/main/g8/build.sbt b/src/main/g8/build.sbt index 4c64cf5..3117a2d 100644 --- a/src/main/g8/build.sbt +++ b/src/main/g8/build.sbt @@ -52,6 +52,18 @@ lazy val `$name;format="norm"$-stream-impl` = (project in file("$name;format="no ) .dependsOn(`$name;format="norm"$-stream-api`, `$name;format="norm"$-api`) +lazy val `integration-tests` = (project in file("integration-tests")) + .enablePlugins(LagomJava) + .settings(common) + .settings( + libraryDependencies ++= Seq( + lagomJavadslApi, + lagomJavadslIntegrationClient, + lagomLogback + ) + ) + .dependsOn(`$name;format="norm"$-stream-api`, `$name;format="norm"$-api`) + val lombok = "org.projectlombok" % "lombok" % "1.18.8" def common = Seq( diff --git a/src/main/g8/integration-tests/src/test/java/$package$/it/$name__Camel$StreamIT.java b/src/main/g8/integration-tests/src/test/java/$package$/it/$name__Camel$StreamIT.java new file mode 100644 index 0000000..9005815 --- /dev/null +++ b/src/main/g8/integration-tests/src/test/java/$package$/it/$name__Camel$StreamIT.java @@ -0,0 +1,75 @@ +package $package$.it; + +import akka.actor.ActorSystem; +import akka.stream.javadsl.Sink; +import akka.stream.javadsl.Source; +import com.lightbend.lagom.javadsl.client.integration.LagomClientFactory; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import $package$stream.api.$name;format="Camel"$StreamService; +import $package$.api.GreetingMessage; +import $package$.api.$name;format="Camel"$Service; + +import java.net.URI; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletionStage; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; + +public class $name;format="Camel"$StreamIT { + + private static final String SERVICE_LOCATOR_URI = "http://localhost:9008"; + + private static LagomClientFactory clientFactory; + private static $name;format="Camel"$Service $name;format="camel"$Service; + private static $name;format="Camel"$StreamService $name;format="camel"$streamService; + private static ActorSystem system; + + @BeforeClass + public static void setup() { + clientFactory = LagomClientFactory.create("integration-test", $name;format="Camel"$StreamIT.class.getClassLoader()); + // One of the clients can use the service locator, the other can use the service gateway, to test them both. + $name;format="camel"$Service = clientFactory.createDevClient($name;format="Camel"$Service.class, URI.create(SERVICE_LOCATOR_URI)); + $name;format="camel"$streamService = clientFactory.createDevClient($name;format="Camel"$StreamService.class, URI.create(SERVICE_LOCATOR_URI)); + + system = ActorSystem.create(); + } + + @Test + public void helloWorld() throws Exception { + String answer = await($name;format="camel"$Service.hello("foo").invoke()); + assertEquals("Hello, foo!", answer); + await($name;format="camel"$Service.useGreeting("bar").invoke(new GreetingMessage("Hi"))); + String answer2 = await($name;format="camel"$Service.hello("bar").invoke()); + assertEquals("Hi, bar!", answer2); + } + + @Test + public void helloStream() throws Exception { + // Important to concat our source with a maybe, this ensures the connection doesn't get closed once we've + // finished feeding our elements in, and then also to take 3 from the response stream, this ensures our + // connection does get closed once we've received the 3 elements. + Source response = await($name;format="camel"$streamService.directStream().invoke( + Source.from(Arrays.asList("a", "b", "c")) + .concat(Source.maybe()))); + List messages = await(response.take(3).runWith(Sink.seq(), system)); + assertEquals(Arrays.asList("Hello, a!", "Hello, b!", "Hello, c!"), messages); + } + + private T await(CompletionStage future) throws Exception { + return future.toCompletableFuture().get(10, TimeUnit.SECONDS); + } + + @AfterClass + public static void tearDown() { + if (clientFactory != null) { + clientFactory.close(); + } + if (system != null) { + system.terminate(); + } + } +} diff --git a/src/main/g8/integration-tests/src/test/resources/application.conf b/src/main/g8/integration-tests/src/test/resources/application.conf new file mode 100644 index 0000000..ff1d3f9 --- /dev/null +++ b/src/main/g8/integration-tests/src/test/resources/application.conf @@ -0,0 +1 @@ +# Because the client factory needs it diff --git a/src/main/g8/integration-tests/src/test/resources/logback-test.xml b/src/main/g8/integration-tests/src/test/resources/logback-test.xml new file mode 100644 index 0000000..19dbd71 --- /dev/null +++ b/src/main/g8/integration-tests/src/test/resources/logback-test.xml @@ -0,0 +1,14 @@ + + + + + + %date{ISO8601} %-5level %logger - %msg%n + + + + + + + + \ No newline at end of file