-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP – use NakadiMock in the spring-boot-2-test #109
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,12 @@ | |
<artifactId>nakadi-producer-spring-boot-starter</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.zalando</groupId> | ||
<artifactId>nakadi-mock</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
|
@@ -63,6 +69,7 @@ | |
<groupId>io.rest-assured</groupId> | ||
<artifactId>rest-assured</artifactId> | ||
<version>3.1.0</version> | ||
<scope>test</scope> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unrelated, I just noted that this does need only test scope. |
||
</dependency> | ||
|
||
</dependencies> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,19 +5,16 @@ | |
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Primary; | ||
import org.zalando.nakadiproducer.EnableNakadiProducer; | ||
import org.zalando.nakadiproducer.snapshots.SimpleSnapshotEventGenerator; | ||
import org.zalando.nakadiproducer.snapshots.Snapshot; | ||
import org.zalando.nakadiproducer.snapshots.SnapshotEventGenerator; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
|
||
@EnableAutoConfiguration | ||
@EnableNakadiProducer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the deprecated annotation here. |
||
public class Application { | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
@@ -39,14 +36,24 @@ public EmbeddedPostgres embeddedPostgres() throws IOException { | |
public SnapshotEventGenerator snapshotEventGenerator() { | ||
return new SimpleSnapshotEventGenerator("eventtype", (withIdGreaterThan, filter) -> { | ||
if (withIdGreaterThan == null) { | ||
return Collections.singletonList(new Snapshot("1", "foo", filter)); | ||
return Collections.singletonList(new Snapshot("1", "foo", new Data("1", filter))); | ||
} else if (withIdGreaterThan.equals("1")) { | ||
return Collections.singletonList(new Snapshot("2", "foo", filter)); | ||
return Collections.singletonList(new Snapshot("2", "foo", new Data("2", filter))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding this so we actually get objects sent, not just |
||
} else { | ||
return new ArrayList<>(); | ||
return Collections.emptyList(); | ||
} | ||
}); | ||
|
||
// Todo: Test that some events arrive at a local nakadi mock | ||
} | ||
|
||
public static class Data { | ||
public String id; | ||
public String filter; | ||
public Data(String id, String filter) { | ||
super(); | ||
this.id = id; | ||
this.filter = filter; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.zalando.nakadiproducer.tests; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.contrib.java.lang.system.EnvironmentVariables; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.actuate.autoconfigure.web.server.LocalManagementPort; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
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 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( | ||
classes = { Application.class }, | ||
properties = { "nakadi-producer.transmission-polling-delay=30"}, | ||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT | ||
) | ||
@ContextConfiguration(initializers=NakadiServerMockInitializer.class) | ||
public class ApplicationWithMockServerIT { | ||
|
||
@LocalManagementPort | ||
private int localManagementPort; | ||
|
||
@ClassRule | ||
public static final EnvironmentVariables environmentVariables | ||
= new EnvironmentVariables(); | ||
|
||
@BeforeClass | ||
public static void fakeCredentialsDir() { | ||
environmentVariables.set("CREDENTIALS_DIR", new File("src/test/resources/tokens").getAbsolutePath()); | ||
} | ||
|
||
@Autowired | ||
NakadiMock nakadiMock; | ||
|
||
@Test | ||
public void shouldSuccessfullyStartAndSnapshotCanBeTriggered() throws InterruptedException { | ||
CollectingCallback<DataChangeEvent<Data>> collector = new CollectingCallback<DataChangeEvent<Application.Data>>() {}; | ||
nakadiMock.eventType("eventtype").setSubmissionCallback(collector); | ||
|
||
given().baseUri("http://localhost:" + localManagementPort) | ||
.contentType("application/json") | ||
.body("{'filter':'Example filter'}".replace('\'', '"')) | ||
.when().post("/actuator/snapshot-event-creation/eventtype") | ||
.then().statusCode(204); | ||
|
||
Thread.sleep(1200); | ||
|
||
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")); | ||
} | ||
} |
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>{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder whether it makes sense to move this class into either my Nakadi-Mock or even a new artifact, as this is useful whenever we are using both nakadi-producer and nakadi-mock together. |
||
|
||
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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is currently not yet released, and if it gets released, it might be under a different group name.