From 3c07ac1c9903a4964a828f8ba65ec031d2ba107e Mon Sep 17 00:00:00 2001 From: Paul Ebermann Date: Wed, 19 Dec 2018 17:17:26 +0100 Subject: [PATCH] Make the test with a mocked server actually work --- .../nakadiproducer/tests/Application.java | 2 +- ....java => ApplicationWithMockServerIT.java} | 33 ++++++++---------- .../tests/MockNakadiServerConfig.java | 34 ------------------- .../tests/NakadiServerMockInitializer.java | 33 ++++++++++++++++++ 4 files changed, 49 insertions(+), 53 deletions(-) rename nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/{ApplicationWithMockNakadiIT.java => ApplicationWithMockServerIT.java} (70%) delete mode 100644 nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/MockNakadiServerConfig.java create mode 100644 nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/NakadiServerMockInitializer.java diff --git a/nakadi-producer-starter-spring-boot-2-test/src/main/java/org/zalando/nakadiproducer/tests/Application.java b/nakadi-producer-starter-spring-boot-2-test/src/main/java/org/zalando/nakadiproducer/tests/Application.java index d0292ee1..edb78e48 100644 --- a/nakadi-producer-starter-spring-boot-2-test/src/main/java/org/zalando/nakadiproducer/tests/Application.java +++ b/nakadi-producer-starter-spring-boot-2-test/src/main/java/org/zalando/nakadiproducer/tests/Application.java @@ -47,7 +47,7 @@ public SnapshotEventGenerator snapshotEventGenerator() { // Todo: Test that some events arrive at a local nakadi mock } - static class Data { + public static class Data { public String id; public String filter; public Data(String id, String filter) { diff --git a/nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/ApplicationWithMockNakadiIT.java b/nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/ApplicationWithMockServerIT.java similarity index 70% rename from nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/ApplicationWithMockNakadiIT.java rename to nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/ApplicationWithMockServerIT.java index ca8712c1..53ed3f80 100644 --- a/nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/ApplicationWithMockNakadiIT.java +++ b/nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/ApplicationWithMockServerIT.java @@ -11,27 +11,26 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import org.zalando.nakadi_mock.EventSubmissionCallback.CollectingCallback; +import org.zalando.nakadi_mock.EventSubmissionCallback.DataChangeEvent; import org.zalando.nakadi_mock.NakadiMock; import org.zalando.nakadiproducer.tests.Application.Data; -import org.zalando.nakadiproducer.transmission.impl.EventTransmitter; - import java.io.File; import java.util.List; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @RunWith(SpringRunner.class) @SpringBootTest( - // This line looks like that by intention: We want to test that the MockNakadiPublishingClient will be picked up - // by our starter *even if* it has been defined *after* the application itself. This has been a problem until - // this commit. - classes = { MockNakadiServerConfig.class, Application.class }, + classes = { Application.class }, + properties = { "nakadi-producer.transmission-polling-delay=30"}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT ) -@ContextConfiguration(initializers=MockNakadiServerConfig.MockPropertyInitializer.class) -public class ApplicationWithMockNakadiIT { +@ContextConfiguration(initializers=NakadiServerMockInitializer.class) +public class ApplicationWithMockServerIT { + @LocalManagementPort private int localManagementPort; @@ -44,15 +43,12 @@ public static void fakeCredentialsDir() { environmentVariables.set("CREDENTIALS_DIR", new File("src/test/resources/tokens").getAbsolutePath()); } - @Autowired - EventTransmitter transmitter; - @Autowired NakadiMock nakadiMock; @Test public void shouldSuccessfullyStartAndSnapshotCanBeTriggered() throws InterruptedException { - CollectingCallback collector = new CollectingCallback() {}; + CollectingCallback> collector = new CollectingCallback>() {}; nakadiMock.eventType("eventtype").setSubmissionCallback(collector); given().baseUri("http://localhost:" + localManagementPort) @@ -61,13 +57,14 @@ public void shouldSuccessfullyStartAndSnapshotCanBeTriggered() throws Interrupte .when().post("/actuator/snapshot-event-creation/eventtype") .then().statusCode(204); - Thread.sleep(500); + Thread.sleep(1200); - transmitter.sendEvents(); - - Thread.sleep(500); - - List events = collector.getSubmittedEvents(); + List> events = collector.getSubmittedEvents(); assertThat(events, hasSize(2)); + assertThat(events.get(0).getDataOp(), is("S")); + assertThat(events.get(0).getData().id, is("1")); + + assertThat(events.get(1).getDataOp(), is("S")); + assertThat(events.get(1).getData().id, is("2")); } } diff --git a/nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/MockNakadiServerConfig.java b/nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/MockNakadiServerConfig.java deleted file mode 100644 index 7fb4a1d8..00000000 --- a/nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/MockNakadiServerConfig.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.zalando.nakadiproducer.tests; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.Configuration; -import org.zalando.nakadi_mock.NakadiMock; - -import java.net.URL; - -@Configuration -public class MockNakadiServerConfig { - - static class MockPropertyInitializer implements ApplicationContextInitializer{ - - private static final Logger LOG = LoggerFactory.getLogger(MockPropertyInitializer.class); - - @Override - public void initialize(ConfigurableApplicationContext context) { - // TODO: setup NakadiMock, inject URL into nakadi-producer - - NakadiMock mock = NakadiMock.make(); - context.getBeanFactory().registerSingleton("nakadiMock", mock); - mock.start(); - URL url = mock.getRootUrl(); - - LOG.info("started mock nakadi on {}", url); - - TestPropertyValues.of("nakadi-producer.nakadi-base-url="+url).applyTo(context); - } - } -} diff --git a/nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/NakadiServerMockInitializer.java b/nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/NakadiServerMockInitializer.java new file mode 100644 index 00000000..9929d3c1 --- /dev/null +++ b/nakadi-producer-starter-spring-boot-2-test/src/test/java/org/zalando/nakadiproducer/tests/NakadiServerMockInitializer.java @@ -0,0 +1,33 @@ +package org.zalando.nakadiproducer.tests; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.zalando.nakadi_mock.NakadiMock; + +import java.net.URL; + + +/** + * An application context initializer which sets up a NakadiMock bean and registers the server URL as a property. + */ +class NakadiServerMockInitializer implements ApplicationContextInitializer{ + + private static final Logger LOG = LoggerFactory.getLogger(NakadiServerMockInitializer.class); + + @Override + public void initialize(ConfigurableApplicationContext context) { + // setup NakadiMock, inject URL into nakadi-producer + + NakadiMock mock = NakadiMock.make(); + context.getBeanFactory().registerSingleton("nakadiMock", mock); + mock.start(); + URL url = mock.getRootUrl(); + + LOG.info("started mock nakadi on {}", url); + + TestPropertyValues.of("nakadi-producer.nakadi-base-uri="+url).applyTo(context); + } +} \ No newline at end of file