|
| 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 | +} |
0 commit comments