Skip to content

Instantly share code, notes, and snippets.

@shivangi1015
Created April 8, 2019 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shivangi1015/252173e397bddb145730e0c5c2122d50 to your computer and use it in GitHub Desktop.
Save shivangi1015/252173e397bddb145730e0c5c2122d50 to your computer and use it in GitHub Desktop.
import akka.Done;
import akka.NotUsed;
import com.lightbend.lagom.javadsl.api.ServiceCall;
import com.lightbend.lagom.javadsl.api.broker.Topic;
import com.lightbend.lagom.javadsl.testkit.ProducerStub;
import com.lightbend.lagom.javadsl.testkit.ProducerStubFactory;
import com.lightbend.lagom.javadsl.testkit.ServiceTest;
import org.junit.BeforeClass;
import org.junit.Test;
import org.knoldus.hello.api.GreetingMessage;
import org.knoldus.hello.api.HelloEvent;
import org.knoldus.hello.api.HelloService;
import scala.concurrent.duration.FiniteDuration;
import javax.inject.Inject;
import static com.lightbend.lagom.javadsl.testkit.ServiceTest.defaultSetup;
import static com.lightbend.lagom.javadsl.testkit.ServiceTest.eventually;
import static com.lightbend.lagom.javadsl.testkit.ServiceTest.startServer;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertEquals;
import static play.inject.Bindings.bind;
public class ConsumerTest {
private static volatile ServiceTest.TestServer testServer;
private static ProducerStub<HelloEvent> producer;
private static HelloService service;
@BeforeClass
public static void setUp() {
final ServiceTest.Setup setup = defaultSetup().withCassandra(true).configureBuilder(builder -> builder.overrides(
bind(HelloService.class).to(HelloServiceStub.class)
));
testServer = startServer(setup);
service = testServer.client(HelloService.class);
}
@Test
public void shouldEventsConsumeMessageFromMessageService() {
producer.send(new HelloEvent.GreetingMessageChanged("Shivangi", "hello"));
eventually(new FiniteDuration(10, SECONDS), () -> {
//here you can use a service client instance to interact with the service
// and assert that the message was processed as expected.
});
assertEquals("hello-events", service.helloEvents().topicId().value());
}
static class HelloServiceStub implements HelloService {
@Inject
HelloServiceStub(ProducerStubFactory producerStubFactory) {
producer = producerStubFactory.producer("hello-events");
}
@Override
public ServiceCall<NotUsed, String> hello(String id) {
return null;
}
@Override
public ServiceCall<GreetingMessage, Done> useGreeting(String id) {
return null;
}
@Override
public Topic<HelloEvent> helloEvents() {
return producer.topic();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment