Skip to content

Commit b7fb8b6

Browse files
Брахин Борисbrakhin
authored andcommitted
Integration tests like in Maven Template
1 parent 40854a6 commit b7fb8b6

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ bin
1111
.factorypath
1212
.settings
1313
logs
14+
.idea/

src/main/g8/build.sbt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ lazy val `$name;format="norm"$-stream-impl` = (project in file("$name;format="no
5252
)
5353
.dependsOn(`$name;format="norm"$-stream-api`, `$name;format="norm"$-api`)
5454

55+
lazy val `integration-tests` = (project in file("integration-tests"))
56+
.enablePlugins(LagomJava)
57+
.settings(common)
58+
.settings(
59+
libraryDependencies ++= Seq(
60+
lagomJavadslApi,
61+
lagomJavadslIntegrationClient,
62+
lagomLogback
63+
)
64+
)
65+
.dependsOn(`$name;format="norm"$-stream-api`, `$name;format="norm"$-api`)
66+
5567
val lombok = "org.projectlombok" % "lombok" % "1.18.8"
5668

5769
def common = Seq(
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package $package$.it;
2+
3+
import akka.actor.ActorSystem;
4+
import akka.stream.javadsl.Sink;
5+
import akka.stream.javadsl.Source;
6+
import com.lightbend.lagom.javadsl.client.integration.LagomClientFactory;
7+
import org.junit.AfterClass;
8+
import org.junit.BeforeClass;
9+
import org.junit.Test;
10+
import $package$stream.api.$name;format="Camel"$StreamService;
11+
import $package$.api.GreetingMessage;
12+
import $package$.api.$name;format="Camel"$Service;
13+
14+
import java.net.URI;
15+
import java.util.Arrays;
16+
import java.util.List;
17+
import java.util.concurrent.CompletionStage;
18+
import java.util.concurrent.TimeUnit;
19+
20+
import static org.junit.Assert.assertEquals;
21+
22+
public class $name;format="Camel"$StreamIT {
23+
24+
private static final String SERVICE_LOCATOR_URI = "http://localhost:9008";
25+
26+
private static LagomClientFactory clientFactory;
27+
private static $name;format="Camel"$Service $name;format="camel"$Service;
28+
private static $name;format="Camel"$StreamService $name;format="camel"$streamService;
29+
private static ActorSystem system;
30+
31+
@BeforeClass
32+
public static void setup() {
33+
clientFactory = LagomClientFactory.create("integration-test", $name;format="Camel"$StreamIT.class.getClassLoader());
34+
// One of the clients can use the service locator, the other can use the service gateway, to test them both.
35+
$name;format="camel"$Service = clientFactory.createDevClient($name;format="Camel"$Service.class, URI.create(SERVICE_LOCATOR_URI));
36+
$name;format="camel"$streamService = clientFactory.createDevClient($name;format="Camel"$StreamService.class, URI.create(SERVICE_LOCATOR_URI));
37+
38+
system = ActorSystem.create();
39+
}
40+
41+
@Test
42+
public void helloWorld() throws Exception {
43+
String answer = await($name;format="camel"$Service.hello("foo").invoke());
44+
assertEquals("Hello, foo!", answer);
45+
await($name;format="camel"$Service.useGreeting("bar").invoke(new GreetingMessage("Hi")));
46+
String answer2 = await($name;format="camel"$Service.hello("bar").invoke());
47+
assertEquals("Hi, bar!", answer2);
48+
}
49+
50+
@Test
51+
public void helloStream() throws Exception {
52+
// Important to concat our source with a maybe, this ensures the connection doesn't get closed once we've
53+
// finished feeding our elements in, and then also to take 3 from the response stream, this ensures our
54+
// connection does get closed once we've received the 3 elements.
55+
Source<String, ?> response = await($name;format="camel"$streamService.directStream().invoke(
56+
Source.from(Arrays.asList("a", "b", "c"))
57+
.concat(Source.maybe())));
58+
List<String> messages = await(response.take(3).runWith(Sink.seq(), system));
59+
assertEquals(Arrays.asList("Hello, a!", "Hello, b!", "Hello, c!"), messages);
60+
}
61+
62+
private <T> T await(CompletionStage<T> future) throws Exception {
63+
return future.toCompletableFuture().get(10, TimeUnit.SECONDS);
64+
}
65+
66+
@AfterClass
67+
public static void tearDown() {
68+
if (clientFactory != null) {
69+
clientFactory.close();
70+
}
71+
if (system != null) {
72+
system.terminate();
73+
}
74+
}
75+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Because the client factory needs it
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
4+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
5+
<encoder>
6+
<pattern>%date{ISO8601} %-5level %logger - %msg%n</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<root level="INFO">
11+
<appender-ref ref="STDOUT" />
12+
</root>
13+
14+
</configuration>

0 commit comments

Comments
 (0)