Skip to content

Commit

Permalink
Make the test with a mocked server actually work
Browse files Browse the repository at this point in the history
  • Loading branch information
ePaul committed Dec 20, 2018
1 parent bce4b62 commit 3c07ac1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Application.Data> collector = new CollectingCallback<Application.Data>() {};
CollectingCallback<DataChangeEvent<Data>> collector = new CollectingCallback<DataChangeEvent<Application.Data>>() {};
nakadiMock.eventType("eventtype").setSubmissionCallback(collector);

given().baseUri("http://localhost:" + localManagementPort)
Expand All @@ -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<Data> events = collector.getSubmittedEvents();
List<DataChangeEvent<Data>> 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"));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<ConfigurableApplicationContext>{

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);
}
}

0 comments on commit 3c07ac1

Please sign in to comment.