From 49d1ebba822b5df3d7615e86bdcb055e4d48e9e2 Mon Sep 17 00:00:00 2001 From: antban Date: Tue, 11 Jul 2017 10:11:23 +0200 Subject: [PATCH 01/21] ARUHA-882 Remove unneeded tests, optimize some other tests --- build.gradle | 4 ++++ .../controller/EventStreamControllerTest.java | 8 ++++---- ...entStreamSendBatchViaOutputStreamTest.java | 13 ------------- .../EventStreamSendBatchViaStringTest.java | 8 -------- .../nakadi/service/EventStreamTest.java | 19 ++++++++++--------- 5 files changed, 18 insertions(+), 34 deletions(-) delete mode 100644 src/test/java/org/zalando/nakadi/service/EventStreamSendBatchViaOutputStreamTest.java delete mode 100644 src/test/java/org/zalando/nakadi/service/EventStreamSendBatchViaStringTest.java diff --git a/build.gradle b/build.gradle index 5b795d549f..733fa1344c 100644 --- a/build.gradle +++ b/build.gradle @@ -198,6 +198,10 @@ tasks.withType(FindBugs) { } } +//tasks.withType(Test) { +// maxParallelForks = Runtime.runtime.availableProcessors() +//} + task generateScmSourceJson() { def rev = new ByteArrayOutputStream() exec { diff --git a/src/test/java/org/zalando/nakadi/controller/EventStreamControllerTest.java b/src/test/java/org/zalando/nakadi/controller/EventStreamControllerTest.java index e9d738c01a..40f37fa10a 100644 --- a/src/test/java/org/zalando/nakadi/controller/EventStreamControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/EventStreamControllerTest.java @@ -14,6 +14,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; @@ -28,6 +29,7 @@ import static org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy.retryForSpecifiedTimeOf; import static org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry; import static org.echocat.jomon.runtime.util.Duration.duration; +import static org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; import org.junit.Before; @@ -425,11 +427,9 @@ public void reportCurrentNumberOfConsumers() throws Exception { client.start(); clients.add(client); - Thread.sleep(500); - - executeWithRetry( + TestUtils.waitFor( () -> assertThat(counter.getCount(), equalTo((long) clients.size())), - retryForSpecifiedTimeOf(duration("5s")) + TimeUnit.SECONDS.toMillis(5) ); } diff --git a/src/test/java/org/zalando/nakadi/service/EventStreamSendBatchViaOutputStreamTest.java b/src/test/java/org/zalando/nakadi/service/EventStreamSendBatchViaOutputStreamTest.java deleted file mode 100644 index 4e66659b24..0000000000 --- a/src/test/java/org/zalando/nakadi/service/EventStreamSendBatchViaOutputStreamTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.zalando.nakadi.service; - -/* - This is a full clone of EventStreamTest with SEND_BATCH_VIA_OUTPUT_STREAM enabled. - - todo: When the flag is lifted and the approach becomes the default, this can be removed. - */ -public class EventStreamSendBatchViaOutputStreamTest extends EventStreamTest { - @Override - protected EventStreamWriter createWriter() { - return new EventStreamWriterBinary(); - } -} diff --git a/src/test/java/org/zalando/nakadi/service/EventStreamSendBatchViaStringTest.java b/src/test/java/org/zalando/nakadi/service/EventStreamSendBatchViaStringTest.java deleted file mode 100644 index 661798d5fa..0000000000 --- a/src/test/java/org/zalando/nakadi/service/EventStreamSendBatchViaStringTest.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.zalando.nakadi.service; - -public class EventStreamSendBatchViaStringTest extends EventStreamTest { - @Override - protected EventStreamWriter createWriter() { - return new EventStreamWriterString(); - } -} diff --git a/src/test/java/org/zalando/nakadi/service/EventStreamTest.java b/src/test/java/org/zalando/nakadi/service/EventStreamTest.java index 97b6855945..2d6dc10ea9 100644 --- a/src/test/java/org/zalando/nakadi/service/EventStreamTest.java +++ b/src/test/java/org/zalando/nakadi/service/EventStreamTest.java @@ -52,6 +52,7 @@ import static org.zalando.nakadi.service.EventStreamWriter.BATCH_SEPARATOR; import org.zalando.nakadi.service.converter.CursorConverterImpl; import org.zalando.nakadi.service.timeline.TimelineService; +import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.createFakeTimeline; import static org.zalando.nakadi.utils.TestUtils.randomString; import static org.zalando.nakadi.utils.TestUtils.waitFor; @@ -59,7 +60,7 @@ import org.zalando.nakadi.view.SubscriptionCursor; import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs; -public abstract class EventStreamTest { +public class EventStreamTest { private static final String TOPIC = randomString(); private static final byte[] DUMMY = "DUMMY".getBytes(UTF_8); @@ -84,7 +85,9 @@ public void setupWriter() { when(writerProvider.getWriter()).thenReturn(createWriter()); } - protected abstract EventStreamWriter createWriter(); + protected EventStreamWriter createWriter() { + return new EventStreamWriterBinary(); + } @Test(timeout = 15000) public void whenIOExceptionThenStreamIsClosed() throws NakadiException, InterruptedException, IOException { @@ -103,16 +106,14 @@ public void whenIOExceptionThenStreamIsClosed() throws NakadiException, Interrup })); thread.start(); - Thread.sleep(3000); - assertThat("As there are no exit conditions in config - the thread should be running", - thread.isAlive(), is(true)); + TestUtils.waitFor(() -> assertThat("Thread should be running", thread.isAlive(), is(true)), 5000); // simulation of client closing the connection: this will end the eventStream doThrow(new IOException()).when(outputStreamMock).flush(); - Thread.sleep(5000); - assertThat("The thread should be dead now, as we simulated that client closed connection", - thread.isAlive(), is(false)); + TestUtils.waitFor( + () -> assertThat("The thread should be dead now, as we simulated that client closed connection", + thread.isAlive(), is(false)), 10000); thread.join(); } @@ -241,7 +242,7 @@ public void whenNoEventsToReadThenKeepAliveIsSent() throws NakadiException, IOEx .withCursors(ImmutableList.of(new NakadiCursor(TIMELINE, "0", "000000000000000000"))) .withBatchLimit(1) .withBatchTimeout(1) - .withStreamTimeout(3) + .withStreamTimeout(2) .build(); final ByteArrayOutputStream out = new ByteArrayOutputStream(); From d6bf81038663bd4f559015f8133fae217b43ac52 Mon Sep 17 00:00:00 2001 From: antban Date: Tue, 11 Jul 2017 11:25:30 +0200 Subject: [PATCH 02/21] ARUHA-882 Get rid of many calls to initialize same data Remember, this is not an optimization, I just do not like the same lines everywhere --- .../controller/CursorsControllerTest.java | 19 ++--- .../EventPublishingControllerTest.java | 56 +++++++-------- .../controller/EventStreamControllerTest.java | 38 ++++------ .../controller/EventTypeControllerTest.java | 69 +++++++++---------- .../EventTypeControllerTestCase.java | 58 +++++++--------- .../controller/PartitionsControllerTest.java | 28 +++----- .../PostSubscriptionControllerTest.java | 21 ++---- .../controller/StoragesControllerTest.java | 35 ++++------ .../SubscriptionControllerTest.java | 28 +++----- .../controller/TimelinesControllerTest.java | 27 +++----- .../nakadi/domain/BatchItemResponseTest.java | 19 ++--- .../zalando/nakadi/domain/EventTypeTest.java | 18 ++--- .../kafka/PartitionsCalculatorTest.java | 16 ++--- .../nakadi/service/EventStreamTest.java | 8 +-- .../nakadi/service/StorageServiceTest.java | 21 +++--- .../org/zalando/nakadi/utils/TestUtils.java | 38 +++++----- 16 files changed, 195 insertions(+), 304 deletions(-) diff --git a/src/test/java/org/zalando/nakadi/controller/CursorsControllerTest.java b/src/test/java/org/zalando/nakadi/controller/CursorsControllerTest.java index 46745f46ec..dabeb0127b 100644 --- a/src/test/java/org/zalando/nakadi/controller/CursorsControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/CursorsControllerTest.java @@ -1,6 +1,5 @@ package org.zalando.nakadi.controller; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableList; import java.util.List; import java.util.stream.Collectors; @@ -17,7 +16,6 @@ import org.springframework.http.HttpStatus; import static org.springframework.http.MediaType.APPLICATION_JSON; import org.springframework.http.converter.StringHttpMessageConverter; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; @@ -26,7 +24,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; -import org.zalando.nakadi.config.JsonConfig; import org.zalando.nakadi.config.SecuritySettings; import org.zalando.nakadi.domain.CursorError; import org.zalando.nakadi.domain.ItemsWrapper; @@ -45,8 +42,8 @@ import org.zalando.nakadi.service.CursorsService; import org.zalando.nakadi.util.FeatureToggleService; import static org.zalando.nakadi.util.FeatureToggleService.Feature.HIGH_LEVEL_API; -import org.zalando.nakadi.utils.JsonTestHelper; import org.zalando.nakadi.utils.RandomSubscriptionBuilder; +import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.buildDefaultEventType; import static org.zalando.nakadi.utils.TestUtils.createFakeTimeline; import static org.zalando.nakadi.utils.TestUtils.invalidProblem; @@ -75,15 +72,12 @@ public class CursorsControllerTest { new SubscriptionCursor("1", "10", MY_ET, TOKEN)); private final CursorsService cursorsService = mock(CursorsService.class); - private final ObjectMapper objectMapper = new JsonConfig().jacksonObjectMapper(); private final MockMvc mockMvc; - private final JsonTestHelper jsonHelper; private final FeatureToggleService featureToggleService; private final SubscriptionDbRepository subscriptionRepository; private final CursorConverter cursorConverter; public CursorsControllerTest() throws Exception { - jsonHelper = new JsonTestHelper(objectMapper); featureToggleService = mock(FeatureToggleService.class); when(featureToggleService.isFeatureEnabled(any())).thenReturn(true); @@ -104,15 +98,12 @@ public CursorsControllerTest() throws Exception { final CursorsController controller = new CursorsController(cursorsService, featureToggleService, cursorConverter, tokenService); - final MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = - new MappingJackson2HttpMessageConverter(objectMapper); - final SecuritySettings settings = mock(SecuritySettings.class); doReturn(SecuritySettings.AuthMode.OFF).when(settings).getAuthMode(); doReturn("nakadi").when(settings).getAdminClientId(); mockMvc = standaloneSetup(controller) - .setMessageConverters(new StringHttpMessageConverter(), jackson2HttpMessageConverter) + .setMessageConverters(new StringHttpMessageConverter(), TestUtils.JACKSON_2_HTTP_MESSAGE_CONVERTER) .setCustomArgumentResolvers(new ClientResolver(settings, featureToggleService)) .build(); } @@ -135,7 +126,7 @@ public void whenCommitInvalidCursorsThenOk() throws Exception { .collect(Collectors.toList())); postCursors(DUMMY_CURSORS) .andExpect(status().isOk()) - .andExpect(content().string(jsonHelper.matchesObject(expectation))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(expectation))); } @Test @@ -205,12 +196,12 @@ private void checkForProblem(final ResultActions resultActions, final Problem ex resultActions .andExpect(status().is(expectedProblem.getStatus().getStatusCode())) .andExpect(content().contentType(PROBLEM_CONTENT_TYPE)) - .andExpect(content().string(jsonHelper.matchesObject(expectedProblem))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem))); } private ResultActions postCursors(final List cursors) throws Exception { final ItemsWrapper cursorsWrapper = new ItemsWrapper<>(cursors); - return postCursorsString(objectMapper.writeValueAsString(cursorsWrapper)); + return postCursorsString(TestUtils.OBJECT_MAPPER.writeValueAsString(cursorsWrapper)); } private ResultActions postCursorsString(final String cursors) throws Exception { diff --git a/src/test/java/org/zalando/nakadi/controller/EventPublishingControllerTest.java b/src/test/java/org/zalando/nakadi/controller/EventPublishingControllerTest.java index 3e7776b6db..d9225044cb 100644 --- a/src/test/java/org/zalando/nakadi/controller/EventPublishingControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/EventPublishingControllerTest.java @@ -1,22 +1,37 @@ package org.zalando.nakadi.controller; import com.codahale.metrics.MetricRegistry; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.ArrayList; +import java.util.List; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; import org.json.JSONException; import org.junit.Before; import org.junit.Test; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; import org.mockito.Mockito; +import static org.mockito.Mockito.mock; +import static org.springframework.http.MediaType.APPLICATION_JSON; import org.springframework.http.converter.StringHttpMessageConverter; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; -import org.zalando.nakadi.config.JsonConfig; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; import org.zalando.nakadi.config.SecuritySettings; import org.zalando.nakadi.domain.BatchItemResponse; import org.zalando.nakadi.domain.EventPublishResult; import org.zalando.nakadi.domain.EventPublishingStatus; +import static org.zalando.nakadi.domain.EventPublishingStatus.ABORTED; +import static org.zalando.nakadi.domain.EventPublishingStatus.FAILED; +import static org.zalando.nakadi.domain.EventPublishingStatus.SUBMITTED; import org.zalando.nakadi.domain.EventPublishingStep; +import static org.zalando.nakadi.domain.EventPublishingStep.PARTITIONING; +import static org.zalando.nakadi.domain.EventPublishingStep.PUBLISHING; +import static org.zalando.nakadi.domain.EventPublishingStep.VALIDATING; import org.zalando.nakadi.exceptions.EventTypeTimeoutException; import org.zalando.nakadi.exceptions.InternalNakadiException; import org.zalando.nakadi.exceptions.NoSuchEventTypeException; @@ -24,39 +39,17 @@ import org.zalando.nakadi.metrics.EventTypeMetrics; import org.zalando.nakadi.security.Client; import org.zalando.nakadi.security.ClientResolver; -import org.zalando.nakadi.service.EventPublisher; import org.zalando.nakadi.service.BlacklistService; +import org.zalando.nakadi.service.EventPublisher; import org.zalando.nakadi.util.FeatureToggleService; -import org.zalando.nakadi.utils.JsonTestHelper; - -import java.util.ArrayList; -import java.util.List; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.springframework.http.MediaType.APPLICATION_JSON; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; -import static org.zalando.nakadi.domain.EventPublishingStatus.ABORTED; -import static org.zalando.nakadi.domain.EventPublishingStatus.FAILED; -import static org.zalando.nakadi.domain.EventPublishingStatus.SUBMITTED; -import static org.zalando.nakadi.domain.EventPublishingStep.PARTITIONING; -import static org.zalando.nakadi.domain.EventPublishingStep.PUBLISHING; -import static org.zalando.nakadi.domain.EventPublishingStep.VALIDATING; +import org.zalando.nakadi.utils.TestUtils; public class EventPublishingControllerTest { public static final String TOPIC = "my-topic"; private static final String EVENT_BATCH = "[{\"payload\": \"My Event Payload\"}]"; - private ObjectMapper objectMapper = new JsonConfig().jacksonObjectMapper(); private MetricRegistry metricRegistry; - private JsonTestHelper jsonHelper; private EventPublisher publisher; private FeatureToggleService featureToggleService; private SecuritySettings settings; @@ -67,7 +60,6 @@ public class EventPublishingControllerTest { @Before public void setUp() throws Exception { - jsonHelper = new JsonTestHelper(objectMapper); metricRegistry = new MetricRegistry(); publisher = mock(EventPublisher.class); eventTypeMetricRegistry = new EventTypeMetricRegistry(metricRegistry); @@ -79,10 +71,8 @@ public void setUp() throws Exception { final EventPublishingController controller = new EventPublishingController(publisher, eventTypeMetricRegistry, blacklistService); - final MappingJackson2HttpMessageConverter jackson2HttpMessageConverter - = new MappingJackson2HttpMessageConverter(objectMapper); mockMvc = standaloneSetup(controller) - .setMessageConverters(new StringHttpMessageConverter(), jackson2HttpMessageConverter) + .setMessageConverters(new StringHttpMessageConverter(), TestUtils.JACKSON_2_HTTP_MESSAGE_CONVERTER) .setCustomArgumentResolvers(new ClientResolver(settings, featureToggleService)) .build(); } @@ -131,7 +121,7 @@ public void whenResultIsAbortedThen422() throws Exception { postBatch(TOPIC, EVENT_BATCH) .andExpect(status().isUnprocessableEntity()) - .andExpect(content().string(jsonHelper.matchesObject(responses()))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(responses()))); } @Test @@ -145,7 +135,7 @@ public void whenResultIsAbortedThen207() throws Exception { postBatch(TOPIC, EVENT_BATCH) .andExpect(status().isMultiStatus()) - .andExpect(content().string(jsonHelper.matchesObject(responses()))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(responses()))); } @Test diff --git a/src/test/java/org/zalando/nakadi/controller/EventStreamControllerTest.java b/src/test/java/org/zalando/nakadi/controller/EventStreamControllerTest.java index 40f37fa10a..978c3d82f2 100644 --- a/src/test/java/org/zalando/nakadi/controller/EventStreamControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/EventStreamControllerTest.java @@ -2,7 +2,6 @@ import com.codahale.metrics.Counter; import com.codahale.metrics.MetricRegistry; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableList; import java.io.ByteArrayOutputStream; import java.io.Closeable; @@ -46,13 +45,11 @@ import org.mockito.exceptions.base.MockitoException; import org.springframework.http.HttpStatus; import org.springframework.http.converter.StringHttpMessageConverter; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody; -import org.zalando.nakadi.config.JsonConfig; import org.zalando.nakadi.config.SecuritySettings; import org.zalando.nakadi.domain.CursorError; import org.zalando.nakadi.domain.EventType; @@ -86,7 +83,6 @@ import org.zalando.nakadi.service.converter.CursorConverterImpl; import org.zalando.nakadi.service.timeline.TimelineService; import org.zalando.nakadi.util.FeatureToggleService; -import org.zalando.nakadi.utils.JsonTestHelper; import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.createFakeTimeline; import static org.zalando.nakadi.utils.TestUtils.mockAccessDeniedException; @@ -109,9 +105,7 @@ public class EventStreamControllerTest { private EventTypeRepository eventTypeRepository; private EventStreamFactory eventStreamFactoryMock; - private ObjectMapper objectMapper; private EventStreamController controller; - private JsonTestHelper jsonHelper; private MetricRegistry metricRegistry; private MetricRegistry streamMetrics; private FeatureToggleService featureToggleService; @@ -129,9 +123,6 @@ public void setup() throws NakadiException, UnknownHostException, InvalidCursorE EVENT_TYPE.setName(TEST_EVENT_TYPE_NAME); EVENT_TYPE.setTopic(TEST_TOPIC); - objectMapper = new JsonConfig().jacksonObjectMapper(); - jsonHelper = new JsonTestHelper(objectMapper); - fakeTimeline = createFakeTimeline(TEST_TOPIC); eventTypeRepository = mock(EventTypeRepository.class); @@ -170,7 +161,7 @@ public void setup() throws NakadiException, UnknownHostException, InvalidCursorE eventTypeChangeListener = mock(EventTypeChangeListener.class); when(eventTypeChangeListener.registerListener(any(), any())).thenReturn(mock(Closeable.class)); controller = new EventStreamController( - eventTypeRepository, timelineService, objectMapper, eventStreamFactoryMock, metricRegistry, + eventTypeRepository, timelineService, TestUtils.OBJECT_MAPPER, eventStreamFactoryMock, metricRegistry, streamMetrics, crutch, blacklistService, consumerLimitingService, featureToggleService, new CursorConverterImpl(eventTypeCache, timelineService), authorizationValidator, eventTypeChangeListener); @@ -178,8 +169,7 @@ public void setup() throws NakadiException, UnknownHostException, InvalidCursorE settings = mock(SecuritySettings.class); mockMvc = standaloneSetup(controller) - .setMessageConverters(new StringHttpMessageConverter(), - new MappingJackson2HttpMessageConverter(objectMapper)) + .setMessageConverters(new StringHttpMessageConverter(), TestUtils.JACKSON_2_HTTP_MESSAGE_CONVERTER) .setCustomArgumentResolvers(new ClientResolver(settings, featureToggleService)) .build(); } @@ -189,10 +179,12 @@ public void testCursorsForNulls() throws Exception { when(eventTypeRepository.findByName(TEST_EVENT_TYPE_NAME)).thenReturn(EVENT_TYPE); assertThat( responseToString(createStreamingResponseBody("[{\"partition\":null,\"offset\":\"0\"}]")), - jsonHelper.matchesObject(Problem.valueOf(PRECONDITION_FAILED, "partition must not be null"))); + TestUtils.JSON_TEST_HELPER.matchesObject( + Problem.valueOf(PRECONDITION_FAILED, "partition must not be null"))); assertThat( responseToString(createStreamingResponseBody("[{\"partition\":\"0\",\"offset\":null}]")), - jsonHelper.matchesObject(Problem.valueOf(PRECONDITION_FAILED, "offset must not be null"))); + TestUtils.JSON_TEST_HELPER.matchesObject( + Problem.valueOf(PRECONDITION_FAILED, "offset must not be null"))); } @Test @@ -240,7 +232,7 @@ public void whenTopicNotExistsThenTopicNotFound() throws IOException, NakadiExce final StreamingResponseBody responseBody = createStreamingResponseBody(); final Problem expectedProblem = Problem.valueOf(NOT_FOUND, "topic not found"); - assertThat(responseToString(responseBody), jsonHelper.matchesObject(expectedProblem)); + assertThat(responseToString(responseBody), TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem)); } @Test @@ -251,7 +243,7 @@ public void whenStreamLimitLowerThanBatchLimitThenUnprocessableEntity() throws N final Problem expectedProblem = Problem.valueOf(UNPROCESSABLE_ENTITY, "stream_limit can't be lower than batch_limit"); - assertThat(responseToString(responseBody), jsonHelper.matchesObject(expectedProblem)); + assertThat(responseToString(responseBody), TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem)); } @Test @@ -262,7 +254,7 @@ public void whenStreamTimeoutLowerThanBatchTimeoutThenUnprocessableEntity() thro final Problem expectedProblem = Problem.valueOf(UNPROCESSABLE_ENTITY, "stream_timeout can't be lower than batch_flush_timeout"); - assertThat(responseToString(responseBody), jsonHelper.matchesObject(expectedProblem)); + assertThat(responseToString(responseBody), TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem)); } @Test @@ -272,7 +264,7 @@ public void whenBatchLimitLowerThan1ThenUnprocessableEntity() throws NakadiExcep final StreamingResponseBody responseBody = createStreamingResponseBody(0, 0, 0, 0, 0, null); final Problem expectedProblem = Problem.valueOf(UNPROCESSABLE_ENTITY, "batch_limit can't be lower than 1"); - assertThat(responseToString(responseBody), jsonHelper.matchesObject(expectedProblem)); + assertThat(responseToString(responseBody), TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem)); } @Test @@ -283,7 +275,7 @@ public void whenWrongCursorsFormatThenBadRequest() throws NakadiException, IOExc "cursors_with_wrong_format"); final Problem expectedProblem = Problem.valueOf(BAD_REQUEST, "incorrect syntax of X-nakadi-cursors header"); - assertThat(responseToString(responseBody), jsonHelper.matchesObject(expectedProblem)); + assertThat(responseToString(responseBody), TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem)); } @Test @@ -298,7 +290,7 @@ public void whenInvalidCursorsThenPreconditionFailed() throws Exception { final Problem expectedProblem = Problem.valueOf(PRECONDITION_FAILED, "offset 000000000000000000 for partition 0 is unavailable"); - assertThat(responseToString(responseBody), jsonHelper.matchesObject(expectedProblem)); + assertThat(responseToString(responseBody), TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem)); } @Test @@ -381,7 +373,7 @@ public void whenNakadiExceptionIsThrownThenServiceUnavailable() throws NakadiExc final StreamingResponseBody responseBody = createStreamingResponseBody(); final Problem expectedProblem = Problem.valueOf(SERVICE_UNAVAILABLE); - assertThat(responseToString(responseBody), jsonHelper.matchesObject(expectedProblem)); + assertThat(responseToString(responseBody), TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem)); } @Test @@ -391,7 +383,7 @@ public void whenExceptionIsThrownThenInternalServerError() throws NakadiExceptio final StreamingResponseBody responseBody = createStreamingResponseBody(); final Problem expectedProblem = Problem.valueOf(INTERNAL_SERVER_ERROR); - assertThat(responseToString(responseBody), jsonHelper.matchesObject(expectedProblem)); + assertThat(responseToString(responseBody), TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem)); } @Test @@ -490,7 +482,7 @@ public void testAccessDenied() throws Exception { final StreamingResponseBody responseBody = createStreamingResponseBody(0, 0, 0, 0, 0, null); final Problem expectedProblem = Problem.valueOf(FORBIDDEN, "Access on READ some-type:some-name denied"); - assertThat(responseToString(responseBody), jsonHelper.matchesObject(expectedProblem)); + assertThat(responseToString(responseBody), TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem)); } private void clearScopes() { diff --git a/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTest.java b/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTest.java index df1a7c065f..a4a2f1a54e 100644 --- a/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTest.java @@ -7,49 +7,22 @@ import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import com.google.common.io.Resources; -import org.hamcrest.core.StringContains; -import org.json.JSONObject; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; -import org.zalando.nakadi.config.SecuritySettings; -import org.zalando.nakadi.domain.EnrichmentStrategyDescriptor; -import org.zalando.nakadi.domain.EventType; -import org.zalando.nakadi.domain.EventTypeAuthorization; -import org.zalando.nakadi.domain.EventTypeAuthorizationAttribute; -import org.zalando.nakadi.domain.EventTypeBase; -import org.zalando.nakadi.domain.EventTypeOptions; -import org.zalando.nakadi.domain.Subscription; -import org.zalando.nakadi.domain.Timeline; -import org.zalando.nakadi.exceptions.DuplicatedEventTypeNameException; -import org.zalando.nakadi.exceptions.InternalNakadiException; -import org.zalando.nakadi.exceptions.InvalidEventTypeException; -import org.zalando.nakadi.exceptions.NoSuchEventTypeException; -import org.zalando.nakadi.exceptions.TopicCreationException; -import org.zalando.nakadi.exceptions.TopicDeletionException; -import org.zalando.nakadi.exceptions.UnableProcessException; -import org.zalando.nakadi.exceptions.UnprocessableEntityException; -import org.zalando.nakadi.exceptions.runtime.TopicConfigException; -import org.zalando.nakadi.partitioning.PartitionStrategy; -import org.zalando.nakadi.repository.TopicRepository; -import org.zalando.nakadi.utils.EventTypeTestBuilder; -import org.zalando.problem.MoreStatus; -import org.zalando.problem.Problem; -import org.zalando.problem.ThrowableProblem; - -import javax.ws.rs.core.Response; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.Optional; import java.util.concurrent.TimeoutException; - +import javax.ws.rs.core.Response; import static javax.ws.rs.core.Response.Status.FORBIDDEN; import static javax.ws.rs.core.Response.Status.NOT_FOUND; import static javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE; import static org.hamcrest.Matchers.containsString; +import org.hamcrest.core.StringContains; +import org.json.JSONObject; import static org.junit.Assert.assertEquals; +import org.junit.Test; +import org.mockito.ArgumentCaptor; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyLong; @@ -63,14 +36,40 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.springframework.http.MediaType.APPLICATION_JSON; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import org.zalando.nakadi.config.SecuritySettings; +import org.zalando.nakadi.domain.EnrichmentStrategyDescriptor; import static org.zalando.nakadi.domain.EventCategory.BUSINESS; +import org.zalando.nakadi.domain.EventType; +import org.zalando.nakadi.domain.EventTypeAuthorization; +import org.zalando.nakadi.domain.EventTypeAuthorizationAttribute; +import org.zalando.nakadi.domain.EventTypeBase; +import org.zalando.nakadi.domain.EventTypeOptions; +import org.zalando.nakadi.domain.Subscription; +import org.zalando.nakadi.domain.Timeline; +import org.zalando.nakadi.exceptions.DuplicatedEventTypeNameException; +import org.zalando.nakadi.exceptions.InternalNakadiException; +import org.zalando.nakadi.exceptions.InvalidEventTypeException; +import org.zalando.nakadi.exceptions.NoSuchEventTypeException; +import org.zalando.nakadi.exceptions.TopicCreationException; +import org.zalando.nakadi.exceptions.TopicDeletionException; +import org.zalando.nakadi.exceptions.UnableProcessException; +import org.zalando.nakadi.exceptions.UnprocessableEntityException; +import org.zalando.nakadi.exceptions.runtime.TopicConfigException; +import org.zalando.nakadi.partitioning.PartitionStrategy; +import org.zalando.nakadi.repository.TopicRepository; import static org.zalando.nakadi.util.FeatureToggleService.Feature.CHECK_APPLICATION_LEVEL_PERMISSIONS; +import org.zalando.nakadi.utils.EventTypeTestBuilder; +import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.buildDefaultEventType; import static org.zalando.nakadi.utils.TestUtils.invalidProblem; import static org.zalando.nakadi.utils.TestUtils.randomValidEventTypeName; +import org.zalando.problem.MoreStatus; +import org.zalando.problem.Problem; +import org.zalando.problem.ThrowableProblem; public class EventTypeControllerTest extends EventTypeControllerTestCase { @@ -116,7 +115,7 @@ public void whenPostWithEventTypeNameNotSetThenReturn422() throws Exception { @Test public void whenPostWithNoCategoryThenReturn422() throws Exception { final EventType invalidEventType = buildDefaultEventType(); - final JSONObject jsonObject = new JSONObject(objectMapper.writeValueAsString(invalidEventType)); + final JSONObject jsonObject = new JSONObject(TestUtils.OBJECT_MAPPER.writeValueAsString(invalidEventType)); jsonObject.remove("category"); @@ -657,7 +656,7 @@ public void whenTopicCreationFailsRemoveEventTypeFromRepositoryAnd500() throws E @Test public void whenPUTInvalidEventTypeThen422() throws Exception { final EventType invalidEventType = buildDefaultEventType(); - final JSONObject jsonObject = new JSONObject(objectMapper.writeValueAsString(invalidEventType)); + final JSONObject jsonObject = new JSONObject(TestUtils.OBJECT_MAPPER.writeValueAsString(invalidEventType)); jsonObject.remove("category"); @@ -722,7 +721,7 @@ public void canExposeASingleEventType() throws Exception { mockMvc.perform(requestBuilder).andExpect(status().is(200)) .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)).andExpect(content().json( - asJsonString(expectedEventType))); + TestUtils.OBJECT_MAPPER.writeValueAsString(expectedEventType))); } diff --git a/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTestCase.java b/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTestCase.java index b2ce5c1457..0ab6297864 100644 --- a/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTestCase.java +++ b/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTestCase.java @@ -1,16 +1,25 @@ package org.zalando.nakadi.controller; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.util.UUID; import org.junit.Before; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.springframework.http.MediaType.APPLICATION_JSON; import org.springframework.http.converter.StringHttpMessageConverter; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; import org.springframework.transaction.support.TransactionCallback; import org.springframework.transaction.support.TransactionTemplate; -import org.zalando.nakadi.config.JsonConfig; import org.zalando.nakadi.config.NakadiSettings; import org.zalando.nakadi.config.SecuritySettings; import org.zalando.nakadi.config.ValidatorConfig; @@ -31,29 +40,16 @@ import org.zalando.nakadi.service.timeline.TimelineService; import org.zalando.nakadi.service.timeline.TimelineSync; import org.zalando.nakadi.util.FeatureToggleService; +import static org.zalando.nakadi.util.FeatureToggleService.Feature.CHECK_APPLICATION_LEVEL_PERMISSIONS; +import static org.zalando.nakadi.util.FeatureToggleService.Feature.CHECK_PARTITIONS_KEYS; +import static org.zalando.nakadi.util.FeatureToggleService.Feature.DISABLE_EVENT_TYPE_DELETION; +import static org.zalando.nakadi.util.PrincipalMockFactory.mockPrincipal; import org.zalando.nakadi.util.UUIDGenerator; +import org.zalando.nakadi.utils.TestUtils; import org.zalando.nakadi.validation.EventTypeOptionsValidator; import org.zalando.nakadi.validation.SchemaEvolutionService; import org.zalando.problem.Problem; import uk.co.datumedge.hamcrest.json.SameJSONAs; - -import java.io.IOException; -import java.util.UUID; - -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.springframework.http.MediaType.APPLICATION_JSON; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; -import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; -import static org.zalando.nakadi.util.FeatureToggleService.Feature.CHECK_APPLICATION_LEVEL_PERMISSIONS; -import static org.zalando.nakadi.util.FeatureToggleService.Feature.CHECK_PARTITIONS_KEYS; -import static org.zalando.nakadi.util.FeatureToggleService.Feature.DISABLE_EVENT_TYPE_DELETION; -import static org.zalando.nakadi.util.PrincipalMockFactory.mockPrincipal; import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs; public class EventTypeControllerTestCase { @@ -71,7 +67,6 @@ public class EventTypeControllerTestCase { protected final Enrichment enrichment = mock(Enrichment.class); protected final UUIDGenerator uuid = mock(UUIDGenerator.class); protected final UUID randomUUID = new UUIDGenerator().randomUUID(); - protected final ObjectMapper objectMapper = new JsonConfig().jacksonObjectMapper(); protected final FeatureToggleService featureToggleService = mock(FeatureToggleService.class); protected final SecuritySettings settings = mock(SecuritySettings.class); protected final ApplicationService applicationService = mock(ApplicationService.class); @@ -95,7 +90,7 @@ public void init() throws Exception { NAKADI_POLL_TIMEOUT, NAKADI_SEND_TIMEOUT, 0, NAKADI_EVENT_MAX_BYTES, NAKADI_SUBSCRIPTION_MAX_PARTITIONS); final PartitionsCalculator partitionsCalculator = new KafkaConfig().createPartitionsCalculator( - "t2.large", objectMapper, nakadiSettings); + "t2.large", TestUtils.OBJECT_MAPPER, nakadiSettings); when(timelineService.getDefaultTopicRepository()).thenReturn(topicRepository); when(timelineService.getTopicRepository((Timeline) any())).thenReturn(topicRepository); when(timelineService.getTopicRepository((EventTypeBase) any())).thenReturn(topicRepository); @@ -114,16 +109,13 @@ public void init() throws Exception { featureToggleService, eventTypeOptionsValidator, applicationService, nakadiSettings, settings); doReturn(randomUUID).when(uuid).randomUUID(); - final MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = - new MappingJackson2HttpMessageConverter(objectMapper); - doReturn(true).when(applicationService).exists(any()); doReturn(SecuritySettings.AuthMode.OFF).when(settings).getAuthMode(); doReturn("nakadi").when(settings).getAdminClientId(); doReturn(true).when(featureToggleService).isFeatureEnabled(CHECK_PARTITIONS_KEYS); mockMvc = standaloneSetup(controller) - .setMessageConverters(new StringHttpMessageConverter(), jackson2HttpMessageConverter) + .setMessageConverters(new StringHttpMessageConverter(), TestUtils.JACKSON_2_HTTP_MESSAGE_CONVERTER) .setCustomArgumentResolvers(new ClientResolver(settings, featureToggleService)) .setControllerAdvice(new ExceptionHandling()) .build(); @@ -138,7 +130,7 @@ protected ResultActions deleteEventType(final String eventTypeName, final String } protected ResultActions postEventType(final EventType eventType) throws Exception { - final String content = objectMapper.writeValueAsString(eventType); + final String content = TestUtils.OBJECT_MAPPER.writeValueAsString(eventType); return postEventType(content); } @@ -152,13 +144,13 @@ protected ResultActions postEventType(final String content) throws Exception { protected ResultActions putEventType(final EventType eventType, final String name, final String clientId) throws Exception { - final String content = objectMapper.writeValueAsString(eventType); + final String content = TestUtils.OBJECT_MAPPER.writeValueAsString(eventType); return putEventType(content, name, clientId); } protected ResultActions putEventType(final EventType eventType, final String name) throws Exception { - final String content = objectMapper.writeValueAsString(eventType); + final String content = TestUtils.OBJECT_MAPPER.writeValueAsString(eventType); return putEventType(content, name); } @@ -184,11 +176,7 @@ protected ResultActions getEventType(final String eventTypeName) throws Exceptio } protected SameJSONAs matchesProblem(final Problem expectedProblem) throws JsonProcessingException { - return sameJSONAs(asJsonString(expectedProblem)); - } - - protected String asJsonString(final Object object) throws JsonProcessingException { - return objectMapper.writeValueAsString(object); + return sameJSONAs(TestUtils.OBJECT_MAPPER.writeValueAsString(expectedProblem)); } protected void disableETDeletionFeature() { diff --git a/src/test/java/org/zalando/nakadi/controller/PartitionsControllerTest.java b/src/test/java/org/zalando/nakadi/controller/PartitionsControllerTest.java index 3a14225374..bb310a3039 100644 --- a/src/test/java/org/zalando/nakadi/controller/PartitionsControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/PartitionsControllerTest.java @@ -1,6 +1,5 @@ package org.zalando.nakadi.controller; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import java.util.Collections; @@ -16,13 +15,11 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import org.springframework.http.converter.StringHttpMessageConverter; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; -import org.zalando.nakadi.config.JsonConfig; import org.zalando.nakadi.config.SecuritySettings; import org.zalando.nakadi.domain.EventType; import org.zalando.nakadi.domain.NakadiCursor; @@ -43,7 +40,6 @@ import org.zalando.nakadi.service.converter.CursorConverterImpl; import org.zalando.nakadi.service.timeline.TimelineService; import org.zalando.nakadi.util.FeatureToggleService; -import org.zalando.nakadi.utils.JsonTestHelper; import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.createFakeTimeline; import static org.zalando.nakadi.utils.TestUtils.mockAccessDeniedException; @@ -87,8 +83,6 @@ public class PartitionsControllerTest { private CursorOperationsService cursorOperationsService; - private JsonTestHelper jsonHelper; - private MockMvc mockMvc; private SecuritySettings settings; @@ -99,9 +93,6 @@ public class PartitionsControllerTest { @Before public void before() throws InternalNakadiException, NoSuchEventTypeException { - final ObjectMapper objectMapper = new JsonConfig().jacksonObjectMapper(); - jsonHelper = new JsonTestHelper(objectMapper); - eventTypeRepositoryMock = mock(EventTypeRepository.class); topicRepositoryMock = mock(TopicRepository.class); eventTypeCache = mock(EventTypeCache.class); @@ -119,8 +110,7 @@ public void before() throws InternalNakadiException, NoSuchEventTypeException { settings = mock(SecuritySettings.class); mockMvc = standaloneSetup(controller) - .setMessageConverters(new StringHttpMessageConverter(), - new MappingJackson2HttpMessageConverter(objectMapper)) + .setMessageConverters(new StringHttpMessageConverter(), TestUtils.JACKSON_2_HTTP_MESSAGE_CONVERTER) .setCustomArgumentResolvers(new ClientResolver(settings, featureToggleService)) .setControllerAdvice(new ExceptionHandling()) .build(); @@ -137,7 +127,7 @@ public void whenListPartitionsThenOk() throws Exception { mockMvc.perform( get(String.format("/event-types/%s/partitions", TEST_EVENT_TYPE))) .andExpect(status().isOk()) - .andExpect(content().string(jsonHelper.matchesObject(TEST_TOPIC_PARTITIONS))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(TEST_TOPIC_PARTITIONS))); } @Test @@ -147,7 +137,7 @@ public void whenListPartitionsForWrongTopicThenNotFound() throws Exception { mockMvc.perform( get(String.format("/event-types/%s/partitions", UNKNOWN_EVENT_TYPE))) .andExpect(status().isNotFound()) - .andExpect(content().string(jsonHelper.matchesObject(expectedProblem))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem))); } @Test @@ -159,7 +149,7 @@ public void whenListPartitionsAndNakadiExceptionThenServiceUnavaiable() throws E mockMvc.perform( get(String.format("/event-types/%s/partitions", TEST_EVENT_TYPE))) .andExpect(status().isServiceUnavailable()) - .andExpect(content().string(jsonHelper.matchesObject(expectedProblem))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem))); } @Test @@ -172,7 +162,7 @@ public void whenGetPartitionThenOk() throws Exception { mockMvc.perform( get(String.format("/event-types/%s/partitions/%s", TEST_EVENT_TYPE, TEST_PARTITION))) .andExpect(status().isOk()) - .andExpect(content().string(jsonHelper.matchesObject(TEST_TOPIC_PARTITION_0))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(TEST_TOPIC_PARTITION_0))); } @Test @@ -209,7 +199,7 @@ public void whenGetPartitionWithConsumedOffsetThenOk() throws Exception { mockMvc.perform( get(String.format("/event-types/%s/partitions/%s?consumed_offset=1", TEST_EVENT_TYPE, TEST_PARTITION))) .andExpect(status().isOk()) - .andExpect(content().string(jsonHelper.matchesObject(new CursorLag( + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(new CursorLag( "0", "001-0000-0", "001-0000-1", @@ -237,7 +227,7 @@ public void whenGetPartitionForWrongTopicThenNotFound() throws Exception { mockMvc.perform( get(String.format("/event-types/%s/partitions/%s", UNKNOWN_EVENT_TYPE, TEST_PARTITION))) .andExpect(status().isNotFound()) - .andExpect(content().string(jsonHelper.matchesObject(expectedProblem))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem))); } @Test @@ -252,7 +242,7 @@ public void whenGetPartitionForWrongPartitionThenNotFound() throws Exception { mockMvc.perform( get(String.format("/event-types/%s/partitions/%s", TEST_EVENT_TYPE, UNKNOWN_PARTITION))) .andExpect(status().isNotFound()) - .andExpect(content().string(jsonHelper.matchesObject(expectedProblem))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem))); } @Test @@ -264,7 +254,7 @@ public void whenGetPartitionAndNakadiExceptionThenServiceUnavaiable() throws Exc mockMvc.perform( get(String.format("/event-types/%s/partitions/%s", TEST_EVENT_TYPE, TEST_PARTITION))) .andExpect(status().isServiceUnavailable()) - .andExpect(content().string(jsonHelper.matchesObject(expectedProblem))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem))); } } diff --git a/src/test/java/org/zalando/nakadi/controller/PostSubscriptionControllerTest.java b/src/test/java/org/zalando/nakadi/controller/PostSubscriptionControllerTest.java index e3041647ea..281ae94767 100644 --- a/src/test/java/org/zalando/nakadi/controller/PostSubscriptionControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/PostSubscriptionControllerTest.java @@ -1,6 +1,5 @@ package org.zalando.nakadi.controller; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import java.util.HashSet; @@ -18,7 +17,6 @@ import org.springframework.http.HttpStatus; import static org.springframework.http.MediaType.APPLICATION_JSON; import org.springframework.http.converter.StringHttpMessageConverter; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -31,7 +29,6 @@ import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; -import org.zalando.nakadi.config.JsonConfig; import org.zalando.nakadi.domain.EventType; import org.zalando.nakadi.domain.Subscription; import org.zalando.nakadi.domain.SubscriptionBase; @@ -48,6 +45,7 @@ import static org.zalando.nakadi.util.FeatureToggleService.Feature.DISABLE_SUBSCRIPTION_CREATION; import org.zalando.nakadi.utils.JsonTestHelper; import static org.zalando.nakadi.utils.RandomSubscriptionBuilder.builder; +import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.invalidProblem; import static org.zalando.nakadi.utils.TestUtils.mockAccessDeniedException; import static org.zalando.problem.MoreStatus.UNPROCESSABLE_ENTITY; @@ -58,8 +56,6 @@ public class PostSubscriptionControllerTest { private static final String PROBLEM_CONTENT_TYPE = "application/problem+json"; - private final ObjectMapper objectMapper = new JsonConfig().jacksonObjectMapper(); - private final JsonTestHelper jsonHelper; private final StandaloneMockMvcBuilder mockMvcBuilder; private final ApplicationService applicationService = mock(ApplicationService.class); @@ -70,7 +66,6 @@ public class PostSubscriptionControllerTest { public PostSubscriptionControllerTest() throws Exception { - jsonHelper = new JsonTestHelper(objectMapper); when(featureToggleService.isFeatureEnabled(any())).thenReturn(true); when(featureToggleService.isFeatureEnabled(DISABLE_SUBSCRIPTION_CREATION)) @@ -85,11 +80,9 @@ public PostSubscriptionControllerTest() throws Exception { final PostSubscriptionController controller = new PostSubscriptionController(featureToggleService, applicationService, subscriptionService, authorizationValidator); - final MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = - new MappingJackson2HttpMessageConverter(objectMapper); mockMvcBuilder = standaloneSetup(controller) - .setMessageConverters(new StringHttpMessageConverter(), jackson2HttpMessageConverter) + .setMessageConverters(new StringHttpMessageConverter(), TestUtils.JACKSON_2_HTTP_MESSAGE_CONVERTER) .setControllerAdvice(new ExceptionHandling()) .setCustomArgumentResolvers(new TestHandlerMethodArgumentResolver()); } @@ -116,7 +109,7 @@ public void whenSubscriptionCreationDisabledThenReturnExistentSubscription() thr postSubscription(subscriptionBase) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) - .andExpect(content().string(sameJSONAs(jsonHelper.asJsonString(existingSubscription)))) + .andExpect(content().string(sameJSONAs(TestUtils.JSON_TEST_HELPER.asJsonString(existingSubscription)))) .andExpect(header().string("Location", "/subscriptions/123")) .andExpect(header().doesNotExist("Content-Location")); } @@ -132,7 +125,7 @@ public void whenPostValidSubscriptionThenOk() throws Exception { postSubscription(subscriptionBase) .andExpect(status().isCreated()) .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) - .andExpect(content().string(sameJSONAs(jsonHelper.asJsonString(subscription)))) + .andExpect(content().string(sameJSONAs(TestUtils.JSON_TEST_HELPER.asJsonString(subscription)))) .andExpect(header().string("Location", "/subscriptions/123")) .andExpect(header().string("Content-Location", "/subscriptions/123")); } @@ -230,7 +223,7 @@ public void whenSubscriptionExistsThenReturnIt() throws Exception { postSubscription(subscriptionBase) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) - .andExpect(content().string(sameJSONAs(jsonHelper.asJsonString(existingSubscription)))) + .andExpect(content().string(sameJSONAs(TestUtils.JSON_TEST_HELPER.asJsonString(existingSubscription)))) .andExpect(header().string("Location", "/subscriptions/123")) .andExpect(header().doesNotExist("Content-Location")); } @@ -262,11 +255,11 @@ private void checkForProblem(final ResultActions resultActions, final Problem ex resultActions .andExpect(status().is(expectedProblem.getStatus().getStatusCode())) .andExpect(content().contentType(PROBLEM_CONTENT_TYPE)) - .andExpect(content().string(jsonHelper.matchesObject(expectedProblem))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem))); } private ResultActions postSubscription(final SubscriptionBase subscriptionBase) throws Exception { - return postSubscriptionAsJson(objectMapper.writeValueAsString(subscriptionBase)); + return postSubscriptionAsJson(TestUtils.OBJECT_MAPPER.writeValueAsString(subscriptionBase)); } private ResultActions postSubscriptionAsJson(final String subscription) throws Exception { diff --git a/src/test/java/org/zalando/nakadi/controller/StoragesControllerTest.java b/src/test/java/org/zalando/nakadi/controller/StoragesControllerTest.java index 37bfe8181b..b4ab88ca4c 100644 --- a/src/test/java/org/zalando/nakadi/controller/StoragesControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/StoragesControllerTest.java @@ -1,37 +1,33 @@ package org.zalando.nakadi.controller; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.json.JSONObject; -import org.junit.Before; -import org.junit.Test; -import org.springframework.http.converter.StringHttpMessageConverter; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; -import org.springframework.test.web.servlet.MockMvc; -import org.zalando.nakadi.config.JsonConfig; -import org.zalando.nakadi.config.SecuritySettings; -import org.zalando.nakadi.domain.Storage; -import org.zalando.nakadi.security.ClientResolver; -import org.zalando.nakadi.service.Result; -import org.zalando.nakadi.service.StorageService; -import org.zalando.nakadi.util.FeatureToggleService; -import org.zalando.problem.Problem; - import java.util.ArrayList; import java.util.List; - import static javax.ws.rs.core.Response.Status.CONFLICT; +import org.json.JSONObject; +import org.junit.Before; +import org.junit.Test; import static org.mockito.Matchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.springframework.http.MediaType.APPLICATION_JSON; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; +import org.zalando.nakadi.config.SecuritySettings; +import org.zalando.nakadi.domain.Storage; +import org.zalando.nakadi.security.ClientResolver; +import org.zalando.nakadi.service.Result; +import org.zalando.nakadi.service.StorageService; +import org.zalando.nakadi.util.FeatureToggleService; import static org.zalando.nakadi.util.PrincipalMockFactory.mockPrincipal; +import org.zalando.nakadi.utils.TestUtils; import static org.zalando.problem.MoreStatus.UNPROCESSABLE_ENTITY; +import org.zalando.problem.Problem; public class StoragesControllerTest { @@ -44,14 +40,11 @@ public class StoragesControllerTest { @Before public void before() { - final ObjectMapper objectMapper = new JsonConfig().jacksonObjectMapper(); - final StoragesController controller = new StoragesController(securitySettings, storageService); doReturn("nakadi").when(securitySettings).getAdminClientId(); mockMvc = standaloneSetup(controller) - .setMessageConverters(new StringHttpMessageConverter(), - new MappingJackson2HttpMessageConverter(objectMapper)) + .setMessageConverters(new StringHttpMessageConverter(), TestUtils.JACKSON_2_HTTP_MESSAGE_CONVERTER) .setCustomArgumentResolvers(new ClientResolver(securitySettings, featureToggleService)) .build(); } diff --git a/src/test/java/org/zalando/nakadi/controller/SubscriptionControllerTest.java b/src/test/java/org/zalando/nakadi/controller/SubscriptionControllerTest.java index c49199d563..7c014a1d75 100644 --- a/src/test/java/org/zalando/nakadi/controller/SubscriptionControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/SubscriptionControllerTest.java @@ -1,6 +1,5 @@ package org.zalando.nakadi.controller; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableSet; import static java.text.MessageFormat.format; import java.util.Collections; @@ -25,7 +24,6 @@ import static org.mockito.Mockito.when; import org.springframework.core.MethodParameter; import org.springframework.http.converter.StringHttpMessageConverter; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.web.servlet.ResultActions; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -37,7 +35,6 @@ import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; -import org.zalando.nakadi.config.JsonConfig; import org.zalando.nakadi.config.NakadiSettings; import org.zalando.nakadi.domain.EventTypeBase; import org.zalando.nakadi.domain.EventTypePartition; @@ -70,9 +67,9 @@ import org.zalando.nakadi.util.FeatureToggleService; import static org.zalando.nakadi.util.SubscriptionsUriHelper.createSubscriptionListUri; import org.zalando.nakadi.utils.EventTypeTestBuilder; -import org.zalando.nakadi.utils.JsonTestHelper; import org.zalando.nakadi.utils.RandomSubscriptionBuilder; import static org.zalando.nakadi.utils.RandomSubscriptionBuilder.builder; +import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.createFakeTimeline; import static org.zalando.nakadi.utils.TestUtils.createRandomSubscriptions; import org.zalando.nakadi.view.SubscriptionCursorWithoutToken; @@ -86,8 +83,6 @@ public class SubscriptionControllerTest { private final SubscriptionDbRepository subscriptionRepository = mock(SubscriptionDbRepository.class); private final EventTypeRepository eventTypeRepository = mock(EventTypeRepository.class); - private final ObjectMapper objectMapper = new JsonConfig().jacksonObjectMapper(); - private final JsonTestHelper jsonHelper; private final StandaloneMockMvcBuilder mockMvcBuilder; private final TopicRepository topicRepository; private final ZkSubscriptionClient zkSubscriptionClient; @@ -97,8 +92,6 @@ public class SubscriptionControllerTest { private static final Timeline TIMELINE = createFakeTimeline("topic"); public SubscriptionControllerTest() throws Exception { - jsonHelper = new JsonTestHelper(objectMapper); - final FeatureToggleService featureToggleService = mock(FeatureToggleService.class); when(featureToggleService.isFeatureEnabled(any())).thenReturn(true); when(featureToggleService.isFeatureEnabled(FeatureToggleService.Feature.DISABLE_SUBSCRIPTION_CREATION)) @@ -120,13 +113,11 @@ public SubscriptionControllerTest() throws Exception { zkSubscriptionClientFactory, timelineService, eventTypeRepository, null, cursorConverter, cursorOperationsService); final SubscriptionController controller = new SubscriptionController(featureToggleService, subscriptionService); - final MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = - new MappingJackson2HttpMessageConverter(objectMapper); final ApplicationService applicationService = mock(ApplicationService.class); doReturn(true).when(applicationService).exists(any()); mockMvcBuilder = standaloneSetup(controller) - .setMessageConverters(new StringHttpMessageConverter(), jackson2HttpMessageConverter) + .setMessageConverters(new StringHttpMessageConverter(), TestUtils.JACKSON_2_HTTP_MESSAGE_CONVERTER) .setControllerAdvice(new ExceptionHandling()) .setCustomArgumentResolvers(new TestHandlerMethodArgumentResolver()); } @@ -138,7 +129,7 @@ public void whenGetSubscriptionThenOk() throws Exception { getSubscription(subscription.getId()) .andExpect(status().isOk()) - .andExpect(content().string(sameJSONAs(objectMapper.writeValueAsString(subscription)))); + .andExpect(content().string(sameJSONAs(TestUtils.OBJECT_MAPPER.writeValueAsString(subscription)))); } @Test @@ -150,7 +141,7 @@ public void whenGetNoneExistingSubscriptionThenNotFound() throws Exception { getSubscription(subscription.getId()) .andExpect(status().isNotFound()) - .andExpect(content().string(jsonHelper.matchesObject(expectedProblem))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem))); } @Test @@ -162,7 +153,7 @@ public void whenListSubscriptionsWithoutQueryParamsThenOk() throws Exception { getSubscriptions() .andExpect(status().isOk()) - .andExpect(content().string(jsonHelper.matchesObject(subscriptionList))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(subscriptionList))); verify(subscriptionRepository, times(1)).listSubscriptions(ImmutableSet.of(), Optional.empty(), 0, 20); } @@ -176,7 +167,7 @@ public void whenListSubscriptionsWithQueryParamsThenOk() throws Exception { getSubscriptions(ImmutableSet.of("et1", "et2"), "app", 0, 30) .andExpect(status().isOk()) - .andExpect(content().string(jsonHelper.matchesObject(subscriptionList))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(subscriptionList))); verify(subscriptionRepository, times(1)) .listSubscriptions(ImmutableSet.of("et1", "et2"), Optional.of("app"), 0, 30); @@ -217,7 +208,7 @@ public void whenListSubscriptionsThenPaginationIsOk() throws Exception { getSubscriptions(ImmutableSet.of("et1", "et2"), "app", 5, 10) .andExpect(status().isOk()) - .andExpect(content().string(jsonHelper.matchesObject(expectedResult))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(expectedResult))); } @Test @@ -259,7 +250,8 @@ public void whenGetSubscriptionStatThenOk() throws Exception { getSubscriptionStats(subscription.getId()) .andExpect(status().isOk()) - .andExpect(content().string(jsonHelper.matchesObject(new ItemsWrapper<>(expectedStats)))); + .andExpect(content().string( + TestUtils.JSON_TEST_HELPER.matchesObject(new ItemsWrapper<>(expectedStats)))); } @Test @@ -332,7 +324,7 @@ private void checkForProblem(final ResultActions resultActions, final Problem ex resultActions .andExpect(status().is(expectedProblem.getStatus().getStatusCode())) .andExpect(content().contentType(PROBLEM_CONTENT_TYPE)) - .andExpect(content().string(jsonHelper.matchesObject(expectedProblem))); + .andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem))); } private class TestHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver { diff --git a/src/test/java/org/zalando/nakadi/controller/TimelinesControllerTest.java b/src/test/java/org/zalando/nakadi/controller/TimelinesControllerTest.java index 8cd8e5638d..d1b8646d2d 100644 --- a/src/test/java/org/zalando/nakadi/controller/TimelinesControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/TimelinesControllerTest.java @@ -1,18 +1,19 @@ package org.zalando.nakadi.controller; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableList; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; +import javax.ws.rs.core.Response; import org.json.JSONObject; import org.junit.Test; import org.mockito.Mockito; import org.springframework.http.MediaType; import org.springframework.http.converter.StringHttpMessageConverter; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.zalando.nakadi.config.JsonConfig; import org.zalando.nakadi.config.SecuritySettings; import org.zalando.nakadi.domain.Storage; import org.zalando.nakadi.domain.Timeline; @@ -23,30 +24,23 @@ import org.zalando.nakadi.service.timeline.TimelineService; import org.zalando.nakadi.util.FeatureToggleService; import org.zalando.nakadi.util.PrincipalMockFactory; +import org.zalando.nakadi.utils.TestUtils; import org.zalando.nakadi.view.TimelineView; import org.zalando.problem.MoreStatus; import org.zalando.problem.Problem; -import javax.ws.rs.core.Response; -import java.util.Date; -import java.util.List; -import java.util.stream.Collectors; - public class TimelinesControllerTest { private final TimelineService timelineService = Mockito.mock(TimelineService.class); private final SecuritySettings securitySettings = Mockito.mock(SecuritySettings.class); private final FeatureToggleService featureToggleService = Mockito.mock(FeatureToggleService.class); - private final ObjectMapper objectMapper; private MockMvc mockMvc; public TimelinesControllerTest() { final TimelinesController controller = new TimelinesController(timelineService); - objectMapper = new JsonConfig().jacksonObjectMapper(); mockMvc = MockMvcBuilders.standaloneSetup(controller) - .setMessageConverters(new StringHttpMessageConverter(), - new MappingJackson2HttpMessageConverter(objectMapper)) + .setMessageConverters(new StringHttpMessageConverter(), TestUtils.JACKSON_2_HTTP_MESSAGE_CONVERTER) .setCustomArgumentResolvers(new ClientResolver(securitySettings, featureToggleService)) .build(); } @@ -74,7 +68,8 @@ public void whenGetTimelinesThenOk() throws Exception { .contentType(MediaType.APPLICATION_JSON) .principal(PrincipalMockFactory.mockPrincipal("nakadi"))) .andExpect(MockMvcResultMatchers.status().isOk()) - .andExpect(MockMvcResultMatchers.content().json(objectMapper.writeValueAsString(timelineViews))); + .andExpect(MockMvcResultMatchers.content().json( + TestUtils.OBJECT_MAPPER.writeValueAsString(timelineViews))); } @Test @@ -94,7 +89,7 @@ public void whenForbiddenAccessExceptionThen403() throws Exception { .contentType(MediaType.APPLICATION_JSON) .principal(PrincipalMockFactory.mockPrincipal("nakadi"))) .andExpect(MockMvcResultMatchers.status().isForbidden()) - .andExpect(MockMvcResultMatchers.content().json(objectMapper.writeValueAsString( + .andExpect(MockMvcResultMatchers.content().json(TestUtils.OBJECT_MAPPER.writeValueAsString( Problem.valueOf(Response.Status.FORBIDDEN, "whenForbiddenAccessExceptionThen403")))); } @@ -106,7 +101,7 @@ public void whenNotFoundExceptionThen404() throws Exception { .contentType(MediaType.APPLICATION_JSON) .principal(PrincipalMockFactory.mockPrincipal("nakadi"))) .andExpect(MockMvcResultMatchers.status().isNotFound()) - .andExpect(MockMvcResultMatchers.content().json(objectMapper.writeValueAsString( + .andExpect(MockMvcResultMatchers.content().json(TestUtils.OBJECT_MAPPER.writeValueAsString( Problem.valueOf(Response.Status.NOT_FOUND, "whenNotFoundExceptionThen404")))); } @@ -118,7 +113,7 @@ public void whenUnableProcessExceptionThen422() throws Exception { .contentType(MediaType.APPLICATION_JSON) .principal(PrincipalMockFactory.mockPrincipal("nakadi"))) .andExpect(MockMvcResultMatchers.status().isUnprocessableEntity()) - .andExpect(MockMvcResultMatchers.content().json(objectMapper.writeValueAsString( + .andExpect(MockMvcResultMatchers.content().json(TestUtils.OBJECT_MAPPER.writeValueAsString( Problem.valueOf(MoreStatus.UNPROCESSABLE_ENTITY, "whenUnableProcessExceptionThen422")))); } diff --git a/src/test/java/org/zalando/nakadi/domain/BatchItemResponseTest.java b/src/test/java/org/zalando/nakadi/domain/BatchItemResponseTest.java index feed06a47f..0d679eda08 100644 --- a/src/test/java/org/zalando/nakadi/domain/BatchItemResponseTest.java +++ b/src/test/java/org/zalando/nakadi/domain/BatchItemResponseTest.java @@ -1,26 +1,15 @@ package org.zalando.nakadi.domain; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.zalando.nakadi.config.JsonConfig; -import org.junit.Before; -import org.junit.Test; - import java.util.Map; import java.util.UUID; - import static org.junit.Assert.assertEquals; +import org.junit.Test; +import org.zalando.nakadi.utils.TestUtils; public class BatchItemResponseTest { - private ObjectMapper mapper; - - @Before - public void setUp() { - mapper = (new JsonConfig()).jacksonObjectMapper(); - } - @Test public void testEnumsHaveLowerCaseSerialization() throws Exception { @@ -35,8 +24,8 @@ public void testEnumsHaveLowerCaseSerialization() throws Exception { bir.setPublishingStatus(status); bir.setDetail(detail); bir.setEid(eid); - final String json = mapper.writeValueAsString(bir); - final Map jsonMap = mapper.readValue(json, typeRef); + final String json = TestUtils.OBJECT_MAPPER.writeValueAsString(bir); + final Map jsonMap = TestUtils.OBJECT_MAPPER.readValue(json, typeRef); assertEquals(status.name().toLowerCase(), jsonMap.get("publishing_status")); assertEquals(step.name().toLowerCase(), jsonMap.get("step")); } diff --git a/src/test/java/org/zalando/nakadi/domain/EventTypeTest.java b/src/test/java/org/zalando/nakadi/domain/EventTypeTest.java index 98987cdda2..028085eef7 100644 --- a/src/test/java/org/zalando/nakadi/domain/EventTypeTest.java +++ b/src/test/java/org/zalando/nakadi/domain/EventTypeTest.java @@ -1,25 +1,17 @@ package org.zalando.nakadi.domain; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.zalando.nakadi.config.JsonConfig; -import org.junit.Test; - -import static org.zalando.nakadi.utils.TestUtils.resourceAsString; import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertThat; +import org.junit.Test; +import org.zalando.nakadi.utils.TestUtils; +import static org.zalando.nakadi.utils.TestUtils.resourceAsString; public class EventTypeTest { - private final ObjectMapper objectMapper; - - public EventTypeTest() { - objectMapper = new JsonConfig().jacksonObjectMapper(); - } - @Test public void canDeserializeWithoutPartitionKeyFields() throws Exception { final String json = resourceAsString("event-type.without.partition-key-fields.json", this.getClass()); - final EventType eventType = objectMapper.readValue(json, EventType.class); + final EventType eventType = TestUtils.OBJECT_MAPPER.readValue(json, EventType.class); assertThat(eventType, notNullValue()); } @@ -27,7 +19,7 @@ public void canDeserializeWithoutPartitionKeyFields() throws Exception { @Test public void canDeserializeWithPartitionKeyFields() throws Exception { final String json = resourceAsString("event-type.with.partition-key-fields.json", this.getClass()); - final EventType eventType = objectMapper.readValue(json, EventType.class); + final EventType eventType = TestUtils.OBJECT_MAPPER.readValue(json, EventType.class); assertThat(eventType, notNullValue()); } diff --git a/src/test/java/org/zalando/nakadi/repository/kafka/PartitionsCalculatorTest.java b/src/test/java/org/zalando/nakadi/repository/kafka/PartitionsCalculatorTest.java index cce3a91158..389aae83a1 100644 --- a/src/test/java/org/zalando/nakadi/repository/kafka/PartitionsCalculatorTest.java +++ b/src/test/java/org/zalando/nakadi/repository/kafka/PartitionsCalculatorTest.java @@ -1,25 +1,23 @@ package org.zalando.nakadi.repository.kafka; -import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.io.InputStream; -import org.junit.Assert; -import org.junit.Test; -import org.zalando.nakadi.config.JsonConfig; -import org.zalando.nakadi.domain.EventTypeStatistics; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.lessThanOrEqualTo; import static org.hamcrest.Matchers.notNullValue; +import org.junit.Assert; +import org.junit.Test; +import org.zalando.nakadi.domain.EventTypeStatistics; +import org.zalando.nakadi.utils.TestUtils; public class PartitionsCalculatorTest { - private static final ObjectMapper OBJECT_MAPPER = new JsonConfig().jacksonObjectMapper(); private static final int MAX_PARTITION_COUNT = 1000; private static PartitionsCalculator buildTest() throws IOException { - return PartitionsCalculator.load(OBJECT_MAPPER, "t2.large", getTestStream(), 0, MAX_PARTITION_COUNT); + return PartitionsCalculator.load(TestUtils.OBJECT_MAPPER, "t2.large", getTestStream(), 0, MAX_PARTITION_COUNT); } private static InputStream getTestStream() { @@ -28,13 +26,13 @@ private static InputStream getTestStream() { @Test(expected = IllegalArgumentException.class) public void testLoadFailForUnknownName() throws IOException { - PartitionsCalculator.load(OBJECT_MAPPER, "null", getTestStream(), 0, 1000); + PartitionsCalculator.load(TestUtils.OBJECT_MAPPER, "null", getTestStream(), 0, 1000); } @Test public void testLoadCorrectForCorrectName() throws IOException { for (final String name : new String[]{"t2.large", "c4.xlarge"}) { - final PartitionsCalculator calculatorMap = PartitionsCalculator.load(OBJECT_MAPPER, name, + final PartitionsCalculator calculatorMap = PartitionsCalculator.load(TestUtils.OBJECT_MAPPER, name, getTestStream(), 0, MAX_PARTITION_COUNT); assertThat(calculatorMap, notNullValue()); } diff --git a/src/test/java/org/zalando/nakadi/service/EventStreamTest.java b/src/test/java/org/zalando/nakadi/service/EventStreamTest.java index 2d6dc10ea9..93e2312d2d 100644 --- a/src/test/java/org/zalando/nakadi/service/EventStreamTest.java +++ b/src/test/java/org/zalando/nakadi/service/EventStreamTest.java @@ -3,7 +3,6 @@ import com.codahale.metrics.Meter; import com.codahale.metrics.MetricRegistry; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import java.io.ByteArrayOutputStream; @@ -40,7 +39,6 @@ import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import org.zalando.nakadi.config.JsonConfig; import org.zalando.nakadi.domain.ConsumedEvent; import org.zalando.nakadi.domain.NakadiCursor; import org.zalando.nakadi.domain.Timeline; @@ -70,8 +68,6 @@ public class EventStreamTest { private static CursorConverter cursorConverter; private static EventStreamWriterProvider writerProvider; - private final ObjectMapper mapper = new JsonConfig().jacksonObjectMapper(); - @BeforeClass public static void setupMocks() { final TimelineService timelineService = mock(TimelineService.class); @@ -455,7 +451,7 @@ public void testWriteStreamEvent() { try { writerProvider.getWriter().writeBatch(baos, cursor, events); final Map batch = - mapper.readValue(baos.toString(), new TypeReference>() { + TestUtils.OBJECT_MAPPER.readValue(baos.toString(), new TypeReference>() { }); final Map cursorM = (Map) batch.get("cursor"); @@ -490,7 +486,7 @@ public void testWriteStreamEventEmptyBatchProducesNoEventArray() { assertEquals("{\"cursor\":{\"partition\":\"11\",\"offset\":\"000000000000000012\"}}\n", json); final Map batch = - mapper.readValue(json, new TypeReference>() { + TestUtils.OBJECT_MAPPER.readValue(json, new TypeReference>() { }); final Map cursorM = (Map) batch.get("cursor"); diff --git a/src/test/java/org/zalando/nakadi/service/StorageServiceTest.java b/src/test/java/org/zalando/nakadi/service/StorageServiceTest.java index b115f322b8..cb01181383 100644 --- a/src/test/java/org/zalando/nakadi/service/StorageServiceTest.java +++ b/src/test/java/org/zalando/nakadi/service/StorageServiceTest.java @@ -1,33 +1,30 @@ package org.zalando.nakadi.service; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.json.JSONObject; -import org.junit.Before; -import org.junit.Test; -import org.zalando.nakadi.config.JsonConfig; -import org.zalando.nakadi.domain.Storage; -import org.zalando.nakadi.exceptions.runtime.NoStorageException; -import org.zalando.nakadi.exceptions.runtime.StorageIsUsedException; -import org.zalando.nakadi.repository.db.StorageDbRepository; - import static org.hamcrest.CoreMatchers.equalTo; +import org.json.JSONObject; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import org.junit.Before; +import org.junit.Test; import static org.mockito.Matchers.any; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import org.zalando.nakadi.domain.Storage; +import org.zalando.nakadi.exceptions.runtime.NoStorageException; +import org.zalando.nakadi.exceptions.runtime.StorageIsUsedException; +import org.zalando.nakadi.repository.db.StorageDbRepository; +import org.zalando.nakadi.utils.TestUtils; public class StorageServiceTest { private StorageService storageService; private StorageDbRepository storageDbRepository; - private final ObjectMapper objectMapper = new JsonConfig().jacksonObjectMapper(); @Before public void setUp() { storageDbRepository = mock(StorageDbRepository.class); - storageService = new StorageService(objectMapper, storageDbRepository); + storageService = new StorageService(TestUtils.OBJECT_MAPPER, storageDbRepository); } @Test diff --git a/src/test/java/org/zalando/nakadi/utils/TestUtils.java b/src/test/java/org/zalando/nakadi/utils/TestUtils.java index ad866a0ac6..e1d2a57e73 100644 --- a/src/test/java/org/zalando/nakadi/utils/TestUtils.java +++ b/src/test/java/org/zalando/nakadi/utils/TestUtils.java @@ -3,14 +3,26 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Charsets; import com.google.common.io.Resources; +import java.io.IOException; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.Random; +import java.util.UUID; +import static java.util.stream.Collectors.toList; +import static java.util.stream.IntStream.range; import org.apache.commons.io.IOUtils; import org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy; +import static org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.json.JSONObject; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.web.servlet.MockMvc; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; import org.springframework.validation.Errors; import org.springframework.validation.FieldError; import org.zalando.nakadi.config.JsonConfig; @@ -24,29 +36,17 @@ import org.zalando.nakadi.plugin.api.authz.AuthorizationService; import org.zalando.nakadi.plugin.api.authz.Resource; import org.zalando.nakadi.problem.ValidationProblem; -import org.zalando.problem.Problem; - -import java.io.IOException; -import java.util.Arrays; -import java.util.Date; -import java.util.List; -import java.util.Random; -import java.util.UUID; - -import static java.util.stream.Collectors.toList; -import static java.util.stream.IntStream.range; -import static org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; import static org.zalando.nakadi.utils.RandomSubscriptionBuilder.builder; +import org.zalando.problem.Problem; public class TestUtils { private static final String VALID_EVENT_TYPE_NAME_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ"; private static final Random RANDOM = new Random(); - private static final ObjectMapper OBJECT_MAPPER = new JsonConfig().jacksonObjectMapper(); + public static final ObjectMapper OBJECT_MAPPER = new JsonConfig().jacksonObjectMapper(); + public static final JsonTestHelper JSON_TEST_HELPER = new JsonTestHelper(OBJECT_MAPPER); + public static final MappingJackson2HttpMessageConverter JACKSON_2_HTTP_MESSAGE_CONVERTER = new MappingJackson2HttpMessageConverter(OBJECT_MAPPER); public static String randomUUID() { return UUID.randomUUID().toString(); @@ -152,13 +152,9 @@ public static EventType loadEventType(final String filename) throws IOException return OBJECT_MAPPER.readValue(json, EventType.class); } - public static MappingJackson2HttpMessageConverter createMessageConverter() { - return new MappingJackson2HttpMessageConverter(new JsonConfig().jacksonObjectMapper()); - } - public static MockMvc mockMvcForController(final Object controller) { return standaloneSetup(controller) - .setMessageConverters(new StringHttpMessageConverter(), createMessageConverter()) + .setMessageConverters(new StringHttpMessageConverter(), JACKSON_2_HTTP_MESSAGE_CONVERTER) .build(); } From 62c67920a0421956b64c2415c0ef5d663e199fdd Mon Sep 17 00:00:00 2001 From: antban Date: Tue, 11 Jul 2017 15:49:07 +0200 Subject: [PATCH 03/21] ARUHA-882 Optimize tests, 10/10 is passing --- .../subscription/state/PartitionData.java | 9 +++-- .../subscription/state/StreamingState.java | 32 +++++++-------- .../controller/EventTypeControllerTest.java | 2 +- .../nakadi/service/EventStreamTest.java | 5 +-- .../subscription/state/PartitionDataTest.java | 40 ++++++++++--------- 5 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/main/java/org/zalando/nakadi/service/subscription/state/PartitionData.java b/src/main/java/org/zalando/nakadi/service/subscription/state/PartitionData.java index fd699ce5cb..cc7cf33663 100644 --- a/src/main/java/org/zalando/nakadi/service/subscription/state/PartitionData.java +++ b/src/main/java/org/zalando/nakadi/service/subscription/state/PartitionData.java @@ -26,17 +26,18 @@ class PartitionData { private int keepAliveInARow; @VisibleForTesting - PartitionData(final ZKSubscription subscription, final NakadiCursor commitOffset) { - this(subscription, commitOffset, LoggerFactory.getLogger(PartitionData.class)); + PartitionData(final ZKSubscription subscription, final NakadiCursor commitOffset, final long currentTime) { + this(subscription, commitOffset, LoggerFactory.getLogger(PartitionData.class), currentTime); } - PartitionData(final ZKSubscription subscription, final NakadiCursor commitOffset, final Logger log) { + PartitionData( + final ZKSubscription subscription, final NakadiCursor commitOffset, final Logger log, final long currentTime) { this.subscription = subscription; this.log = log; this.commitOffset = commitOffset; this.sentOffset = commitOffset; - this.lastSendMillis = System.currentTimeMillis(); + this.lastSendMillis = currentTime; } @Nullable diff --git a/src/main/java/org/zalando/nakadi/service/subscription/state/StreamingState.java b/src/main/java/org/zalando/nakadi/service/subscription/state/StreamingState.java index d214907f3f..d49a666e2d 100644 --- a/src/main/java/org/zalando/nakadi/service/subscription/state/StreamingState.java +++ b/src/main/java/org/zalando/nakadi/service/subscription/state/StreamingState.java @@ -2,6 +2,20 @@ import com.codahale.metrics.Meter; import com.google.common.base.Preconditions; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.OptionalLong; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.slf4j.LoggerFactory; import org.zalando.nakadi.domain.ConsumedEvent; import org.zalando.nakadi.domain.EventTypePartition; @@ -21,21 +35,6 @@ import org.zalando.nakadi.view.SubscriptionCursor; import org.zalando.nakadi.view.SubscriptionCursorWithoutToken; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.OptionalLong; -import java.util.Set; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; -import java.util.stream.Stream; - class StreamingState extends State { private final Map offsets = new HashMap<>(); @@ -445,7 +444,8 @@ private void addToStreaming(final Partition partition) { final PartitionData pd = new PartitionData( subscription, cursor, - LoggerFactory.getLogger("subscription." + getSessionId() + "." + partition.getKey())); + LoggerFactory.getLogger("subscription." + getSessionId() + "." + partition.getKey()), + System.currentTimeMillis()); offsets.put(partition.getKey(), pd); } diff --git a/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTest.java b/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTest.java index a4a2f1a54e..186ee8bddd 100644 --- a/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTest.java @@ -690,7 +690,7 @@ public void whenPUTInexistingEventTypeThen404() throws Exception { final Problem expectedProblem = Problem.valueOf(NOT_FOUND); - doThrow(NoSuchEventTypeException.class).when(eventTypeRepository).findByName(eventType.getName()); + doThrow(NoSuchEventTypeException.class).when(eventTypeRepository).findByName(eq(eventType.getName())); putEventType(eventType, eventType.getName()).andExpect(status().isNotFound()) .andExpect(content().contentType("application/problem+json")) diff --git a/src/test/java/org/zalando/nakadi/service/EventStreamTest.java b/src/test/java/org/zalando/nakadi/service/EventStreamTest.java index 93e2312d2d..f3127793cf 100644 --- a/src/test/java/org/zalando/nakadi/service/EventStreamTest.java +++ b/src/test/java/org/zalando/nakadi/service/EventStreamTest.java @@ -18,6 +18,7 @@ import java.util.Map; import java.util.Optional; import static java.util.Optional.empty; +import java.util.UUID; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -52,7 +53,6 @@ import org.zalando.nakadi.service.timeline.TimelineService; import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.createFakeTimeline; -import static org.zalando.nakadi.utils.TestUtils.randomString; import static org.zalando.nakadi.utils.TestUtils.waitFor; import org.zalando.nakadi.view.Cursor; import org.zalando.nakadi.view.SubscriptionCursor; @@ -60,7 +60,7 @@ public class EventStreamTest { - private static final String TOPIC = randomString(); + private static final String TOPIC = UUID.randomUUID().toString(); private static final byte[] DUMMY = "DUMMY".getBytes(UTF_8); private static final Meter BYTES_FLUSHED_METER = new MetricRegistry().meter("mock"); @@ -129,7 +129,6 @@ public void whenCrutchWorkedThenStreamIsClosed() throws NakadiException, Interru })); thread.start(); - Thread.sleep(TimeUnit.SECONDS.toMillis(1)); waitFor(() -> Assert.assertTrue(thread.isAlive())); // simulation of client closing the connection using crutch diff --git a/src/test/java/org/zalando/nakadi/service/subscription/state/PartitionDataTest.java b/src/test/java/org/zalando/nakadi/service/subscription/state/PartitionDataTest.java index f13486fcaf..aac344e0d9 100644 --- a/src/test/java/org/zalando/nakadi/service/subscription/state/PartitionDataTest.java +++ b/src/test/java/org/zalando/nakadi/service/subscription/state/PartitionDataTest.java @@ -1,18 +1,18 @@ package org.zalando.nakadi.service.subscription.state; +import static java.lang.System.currentTimeMillis; import java.util.List; import java.util.concurrent.TimeUnit; -import org.junit.Test; -import org.zalando.nakadi.domain.ConsumedEvent; -import org.zalando.nakadi.domain.NakadiCursor; -import org.zalando.nakadi.domain.Timeline; -import org.zalando.nakadi.repository.kafka.KafkaCursor; -import static java.lang.System.currentTimeMillis; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import org.junit.Test; import static org.mockito.Mockito.mock; +import org.zalando.nakadi.domain.ConsumedEvent; +import org.zalando.nakadi.domain.NakadiCursor; +import org.zalando.nakadi.domain.Timeline; +import org.zalando.nakadi.repository.kafka.KafkaCursor; public class PartitionDataTest { @@ -24,7 +24,7 @@ private static NakadiCursor createCursor(final long offset) { @Test public void onNewOffsetsShouldSupportRollback() { - final PartitionData pd = new PartitionData(null, createCursor(100L)); + final PartitionData pd = new PartitionData(null, createCursor(100L), System.currentTimeMillis()); final PartitionData.CommitResult cr = pd.onCommitOffset(createCursor(90L)); assertEquals(0L, cr.committedCount); @@ -35,7 +35,7 @@ public void onNewOffsetsShouldSupportRollback() { @Test public void onNewOffsetsShouldSupportCommitInFuture() { - final PartitionData pd = new PartitionData(null, createCursor(100L)); + final PartitionData pd = new PartitionData(null, createCursor(100L), System.currentTimeMillis()); final PartitionData.CommitResult cr = pd.onCommitOffset(createCursor(110L)); assertEquals(0L, cr.committedCount); @@ -46,7 +46,7 @@ public void onNewOffsetsShouldSupportCommitInFuture() { @Test public void normalOperationShouldNotReconfigureKafkaConsumer() { - final PartitionData pd = new PartitionData(null, createCursor(100L)); + final PartitionData pd = new PartitionData(null, createCursor(100L), System.currentTimeMillis()); for (long i = 0; i < 100; ++i) { pd.addEvent(new ConsumedEvent(("test_" + i).getBytes(), createCursor(100L + i + 1))); } @@ -63,7 +63,7 @@ public void normalOperationShouldNotReconfigureKafkaConsumer() { @Test public void keepAliveCountShouldIncreaseOnEachEmptyCall() { - final PartitionData pd = new PartitionData(null, createCursor(100L)); + final PartitionData pd = new PartitionData(null, createCursor(100L), System.currentTimeMillis()); for (int i = 0; i < 100; ++i) { pd.takeEventsToStream(currentTimeMillis(), 10, 0L); assertEquals(i + 1, pd.getKeepAliveInARow()); @@ -79,28 +79,32 @@ public void keepAliveCountShouldIncreaseOnEachEmptyCall() { @Test public void eventsShouldBeStreamedOnTimeout() throws InterruptedException { final long timeout = TimeUnit.SECONDS.toMillis(1); - final PartitionData pd = new PartitionData(null, createCursor(100L)); + long currentTime = System.currentTimeMillis(); + + final PartitionData pd = new PartitionData(null, createCursor(100L), currentTime); for (int i = 0; i < 100; ++i) { pd.addEvent(new ConsumedEvent("test".getBytes(), createCursor(i + 100L + 1))); } - List data = pd.takeEventsToStream(currentTimeMillis(), 1000, timeout); + List data = pd.takeEventsToStream(currentTime, 1000, timeout); assertNull(data); assertEquals(0, pd.getKeepAliveInARow()); - Thread.sleep(timeout); - data = pd.takeEventsToStream(currentTimeMillis(), 1000, timeout); + currentTime += timeout + 1; + + data = pd.takeEventsToStream(currentTime, 1000, timeout); assertNotNull(data); assertEquals(100, data.size()); for (int i = 100; i < 200; ++i) { pd.addEvent(new ConsumedEvent("test".getBytes(), createCursor(i + 100L + 1))); } - data = pd.takeEventsToStream(currentTimeMillis(), 1000, timeout); + data = pd.takeEventsToStream(currentTime, 1000, timeout); assertNull(data); assertEquals(0, pd.getKeepAliveInARow()); - Thread.sleep(timeout); - data = pd.takeEventsToStream(currentTimeMillis(), 1000, timeout); + currentTime += timeout + 1; + + data = pd.takeEventsToStream(currentTime, 1000, timeout); assertNotNull(data); assertEquals(100, data.size()); } @@ -108,7 +112,7 @@ public void eventsShouldBeStreamedOnTimeout() throws InterruptedException { @Test public void eventsShouldBeStreamedOnBatchSize() { final long timeout = TimeUnit.SECONDS.toMillis(1); - final PartitionData pd = new PartitionData(null, createCursor(100L)); + final PartitionData pd = new PartitionData(null, createCursor(100L), System.currentTimeMillis()); for (int i = 0; i < 100; ++i) { pd.addEvent(new ConsumedEvent("test".getBytes(), createCursor(i + 100L + 1))); } From 340c7c3455518abd968c65598fea509bfdb53320 Mon Sep 17 00:00:00 2001 From: antban Date: Tue, 11 Jul 2017 16:04:47 +0200 Subject: [PATCH 04/21] ARUHA-882 Fixes after rebase --- .../service/subscription/state/PartitionData.java | 5 ++++- .../nakadi/controller/EventStreamControllerTest.java | 11 ++--------- .../controller/PostSubscriptionControllerTest.java | 1 - src/test/java/org/zalando/nakadi/utils/TestUtils.java | 11 ++++++++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/zalando/nakadi/service/subscription/state/PartitionData.java b/src/main/java/org/zalando/nakadi/service/subscription/state/PartitionData.java index cc7cf33663..6ad6297246 100644 --- a/src/main/java/org/zalando/nakadi/service/subscription/state/PartitionData.java +++ b/src/main/java/org/zalando/nakadi/service/subscription/state/PartitionData.java @@ -31,7 +31,10 @@ class PartitionData { } PartitionData( - final ZKSubscription subscription, final NakadiCursor commitOffset, final Logger log, final long currentTime) { + final ZKSubscription subscription, + final NakadiCursor commitOffset, + final Logger log, + final long currentTime) { this.subscription = subscription; this.log = log; diff --git a/src/test/java/org/zalando/nakadi/controller/EventStreamControllerTest.java b/src/test/java/org/zalando/nakadi/controller/EventStreamControllerTest.java index 978c3d82f2..cf65974a04 100644 --- a/src/test/java/org/zalando/nakadi/controller/EventStreamControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/EventStreamControllerTest.java @@ -24,11 +24,6 @@ import static javax.ws.rs.core.Response.Status.NOT_FOUND; import static javax.ws.rs.core.Response.Status.PRECONDITION_FAILED; import static javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE; -import org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy; -import static org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy.retryForSpecifiedTimeOf; -import static org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry; -import static org.echocat.jomon.runtime.util.Duration.duration; -import static org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; import org.junit.Before; @@ -219,10 +214,8 @@ public void whenNoParamsThenDefaultsAreUsed() throws Exception { .withStreamTimeout(0) .build(); // we have to retry here as mockMvc exits at the very beginning, before the body starts streaming - executeWithRetry(() -> assertThat(configCaptor.getValue(), equalTo(expectedConfig)), - new RetryForSpecifiedTimeStrategy(2000) - .withExceptionsThatForceRetry(MockitoException.class) - .withWaitBetweenEachTry(50)); + TestUtils.waitFor( + () -> assertThat(configCaptor.getValue(), equalTo(expectedConfig)), 2000, 50, MockitoException.class); } @Test diff --git a/src/test/java/org/zalando/nakadi/controller/PostSubscriptionControllerTest.java b/src/test/java/org/zalando/nakadi/controller/PostSubscriptionControllerTest.java index 281ae94767..53edb7a147 100644 --- a/src/test/java/org/zalando/nakadi/controller/PostSubscriptionControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/PostSubscriptionControllerTest.java @@ -43,7 +43,6 @@ import org.zalando.nakadi.service.subscription.SubscriptionService; import org.zalando.nakadi.util.FeatureToggleService; import static org.zalando.nakadi.util.FeatureToggleService.Feature.DISABLE_SUBSCRIPTION_CREATION; -import org.zalando.nakadi.utils.JsonTestHelper; import static org.zalando.nakadi.utils.RandomSubscriptionBuilder.builder; import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.invalidProblem; diff --git a/src/test/java/org/zalando/nakadi/utils/TestUtils.java b/src/test/java/org/zalando/nakadi/utils/TestUtils.java index e1d2a57e73..c17f3f8700 100644 --- a/src/test/java/org/zalando/nakadi/utils/TestUtils.java +++ b/src/test/java/org/zalando/nakadi/utils/TestUtils.java @@ -46,7 +46,8 @@ public class TestUtils { private static final Random RANDOM = new Random(); public static final ObjectMapper OBJECT_MAPPER = new JsonConfig().jacksonObjectMapper(); public static final JsonTestHelper JSON_TEST_HELPER = new JsonTestHelper(OBJECT_MAPPER); - public static final MappingJackson2HttpMessageConverter JACKSON_2_HTTP_MESSAGE_CONVERTER = new MappingJackson2HttpMessageConverter(OBJECT_MAPPER); + public static final MappingJackson2HttpMessageConverter JACKSON_2_HTTP_MESSAGE_CONVERTER = + new MappingJackson2HttpMessageConverter(OBJECT_MAPPER); public static String randomUUID() { return UUID.randomUUID().toString(); @@ -175,11 +176,15 @@ public static void waitFor(final Runnable runnable, final long timeoutMs) { } @SuppressWarnings("unchecked") - public static void waitFor(final Runnable runnable, final long timeoutMs, final int intervalMs) { + public static void waitFor(final Runnable runnable, final long timeoutMs, final int intervalMs, + final Class... additionalException) { + final Class[] leadToRetry = new Class[additionalException.length + 1]; + System.arraycopy(additionalException, 0, leadToRetry, 1, additionalException.length); + leadToRetry[0] = AssertionError.class; executeWithRetry( runnable, new RetryForSpecifiedTimeStrategy(timeoutMs) - .withExceptionsThatForceRetry(AssertionError.class) + .withExceptionsThatForceRetry(leadToRetry) .withWaitBetweenEachTry(intervalMs)); } From 23a1e48414235e622b36a2bc3b013cb48b4231d8 Mon Sep 17 00:00:00 2001 From: antban Date: Tue, 11 Jul 2017 17:52:13 +0200 Subject: [PATCH 05/21] ARUHA-882 Use batch publishing in HilaAT, set timeout for request --- .../org/zalando/nakadi/webservice/BaseAT.java | 4 + .../nakadi/webservice/hila/HilaAT.java | 77 +++++++++---------- .../webservice/utils/NakadiTestUtils.java | 33 +++++--- 3 files changed, 61 insertions(+), 53 deletions(-) diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java index aaeebf7fd4..21269b6933 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java @@ -4,6 +4,8 @@ import com.jayway.restassured.RestAssured; import com.jayway.restassured.parsing.Parser; import java.util.Optional; +import java.util.concurrent.TimeUnit; +import org.apache.http.params.CoreConnectionPNames; import org.junit.BeforeClass; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; @@ -46,6 +48,8 @@ public abstract class BaseAT { static { RestAssured.port = PORT; RestAssured.defaultParser = Parser.JSON; + RestAssured.config().getHttpClientConfig().setParam(CoreConnectionPNames.SO_TIMEOUT, TimeUnit.SECONDS.toMillis(3)); + RestAssured.config().getHttpClientConfig().setParam(CoreConnectionPNames.CONNECTION_TIMEOUT, 100); } @BeforeClass diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/HilaAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/HilaAT.java index e362415b0c..9972b350db 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/HilaAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/HilaAT.java @@ -5,7 +5,25 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import static com.jayway.restassured.RestAssured.given; +import static com.jayway.restassured.RestAssured.when; +import static com.jayway.restassured.http.ContentType.JSON; +import java.io.IOException; +import static java.text.MessageFormat.format; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import org.apache.http.HttpStatus; +import static org.apache.http.HttpStatus.SC_CONFLICT; +import static org.apache.http.HttpStatus.SC_NO_CONTENT; +import static org.apache.http.HttpStatus.SC_OK; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.not; import org.hamcrest.core.StringContains; import org.junit.Assert; import org.junit.Before; @@ -15,48 +33,26 @@ import org.zalando.nakadi.domain.ItemsWrapper; import org.zalando.nakadi.domain.Subscription; import org.zalando.nakadi.domain.SubscriptionBase; +import static org.zalando.nakadi.domain.SubscriptionBase.InitialPosition.BEGIN; +import static org.zalando.nakadi.domain.SubscriptionBase.InitialPosition.END; import org.zalando.nakadi.domain.SubscriptionEventTypeStats; import org.zalando.nakadi.service.BlacklistService; import org.zalando.nakadi.utils.JsonTestHelper; import org.zalando.nakadi.utils.RandomSubscriptionBuilder; +import static org.zalando.nakadi.utils.TestUtils.waitFor; import org.zalando.nakadi.view.Cursor; import org.zalando.nakadi.view.SubscriptionCursor; import org.zalando.nakadi.webservice.BaseAT; import org.zalando.nakadi.webservice.SettingsControllerAT; -import org.zalando.nakadi.webservice.utils.NakadiTestUtils; -import org.zalando.nakadi.webservice.utils.TestStreamingClient; - -import java.io.IOException; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -import static com.jayway.restassured.RestAssured.given; -import static com.jayway.restassured.RestAssured.when; -import static com.jayway.restassured.http.ContentType.JSON; -import static java.text.MessageFormat.format; -import static java.util.stream.IntStream.range; -import static java.util.stream.IntStream.rangeClosed; -import static org.apache.http.HttpStatus.SC_CONFLICT; -import static org.apache.http.HttpStatus.SC_NO_CONTENT; -import static org.apache.http.HttpStatus.SC_OK; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.not; -import static org.zalando.nakadi.domain.SubscriptionBase.InitialPosition.BEGIN; -import static org.zalando.nakadi.domain.SubscriptionBase.InitialPosition.END; -import static org.zalando.nakadi.utils.TestUtils.waitFor; import static org.zalando.nakadi.webservice.hila.StreamBatch.MatcherIgnoringToken.equalToBatchIgnoringToken; import static org.zalando.nakadi.webservice.hila.StreamBatch.singleEventBatch; +import org.zalando.nakadi.webservice.utils.NakadiTestUtils; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.commitCursors; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.publishEvent; +import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.publishEvents; +import org.zalando.nakadi.webservice.utils.TestStreamingClient; import static org.zalando.nakadi.webservice.utils.TestStreamingClient.SESSION_ID_UNKNOWN; public class HilaAT extends BaseAT { @@ -99,8 +95,7 @@ public void whenStreamTimeoutReachedPossibleToCommit() throws Exception { @Test(timeout = 30000) public void whenOffsetIsCommittedNextSessionStartsFromNextEventAfterCommitted() throws Exception { // write 4 events to event-type - rangeClosed(0, 3) - .forEach(x -> publishEvent(eventType.getName(), "{\"foo\":\"bar" + x + "\"}")); + publishEvents(eventType.getName(), 4, x -> "{\"foo\":\"bar" + x + "\"}"); // create session, read from subscription and wait for events to be sent final TestStreamingClient client = TestStreamingClient @@ -175,7 +170,7 @@ public void whenCommitVeryFirstEventThenOk() throws Exception { @Test(timeout = 15000) public void whenWindowSizeIsSetItIsConsidered() throws Exception { - range(0, 15).forEach(x -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 15, i -> "{\"foo\":\"bar\"}"); final TestStreamingClient client = TestStreamingClient .create(URL, subscription.getId(), "max_uncommitted_events=5") @@ -231,7 +226,7 @@ public void whenStreamTimeoutReachedSessionIsClosed() throws Exception { @Test(timeout = 10000) public void whenBatchLimitAndTimeoutAreSetTheyAreConsidered() throws Exception { - range(0, 12).forEach(x -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 12, i -> "{\"foo\":\"bar\"}"); final TestStreamingClient client = TestStreamingClient .create(URL, subscription.getId(), "batch_limit=5&batch_flush_timeout=1&max_uncommitted_events=20") @@ -282,9 +277,9 @@ public void whenConnectionIsClosedByClientNakadiRecognizesIt() throws Exception waitFor(() -> assertThat(anotherClient.getBatches(), hasSize(1))); } - @Test + @Test(timeout = 10000) public void testGetSubscriptionStat() throws Exception { - IntStream.range(0, 15).forEach(x -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 15, i -> "{\"foo\":\"bar\"}"); final TestStreamingClient client = TestStreamingClient .create(URL, subscription.getId(), "max_uncommitted_events=20") @@ -319,8 +314,8 @@ public void testGetSubscriptionStat() throws Exception { @Test public void testSubscriptionStatsMultiET() throws IOException { final List eventTypes = Lists.newArrayList(createEventType(), createEventType()); - IntStream.range(0, 10).forEach(x -> publishEvent(eventTypes.get(0).getName(), "{\"foo\":\"bar\"}")); - IntStream.range(0, 20).forEach(x -> publishEvent(eventTypes.get(1).getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventTypes.get(0).getName(), 10, i -> "{\"foo\":\"bar\"}"); + publishEvents(eventTypes.get(1).getName(), 20, i -> "{\"foo\":\"bar\"}"); final Subscription subscription = NakadiTestUtils.createSubscription(RandomSubscriptionBuilder.builder() .withEventTypes(eventTypes.stream().map(EventType::getName).collect(Collectors.toSet())) @@ -333,8 +328,8 @@ public void testSubscriptionStatsMultiET() throws IOException { waitFor(() -> assertThat(client.getBatches().isEmpty(), is(false))); - IntStream.range(0, 1).forEach(x -> publishEvent(eventTypes.get(0).getName(), "{\"foo\":\"bar\"}")); - IntStream.range(0, 2).forEach(x -> publishEvent(eventTypes.get(1).getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventTypes.get(0).getName(), 1, i -> "{\"foo\":\"bar\"}"); + publishEvents(eventTypes.get(1).getName(), 2, i -> "{\"foo\":\"bar\"}"); NakadiTestUtils.getSubscriptionStat(subscription) .then() @@ -376,7 +371,7 @@ public void whenConsumerIsBlocked403() throws Exception { @Test(timeout = 10000) public void whenConsumerIsBlockedDuringConsumption() throws Exception { - IntStream.range(0, 5).forEach(x -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 5, i -> "{\"foo\":\"bar\"}"); final TestStreamingClient client = TestStreamingClient .create(subscription.getId()) .start(); @@ -392,7 +387,7 @@ public void whenConsumerIsBlockedDuringConsumption() throws Exception { @Test(timeout = 15000) public void whenStreamTimeout0ThenInfiniteStreaming() throws Exception { - IntStream.range(0, 5).forEach(x -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 5, i -> "{\"foo\":\"bar\"}"); final TestStreamingClient client = TestStreamingClient .create(URL, subscription.getId(), "stream_timeout=0") .start(); @@ -405,7 +400,7 @@ public void whenStreamTimeout0ThenInfiniteStreaming() throws Exception { @Test(timeout = 15000) public void whenResetCursorsThenStreamFromResetCursorOffset() throws Exception { - IntStream.range(0, 20).forEach(x -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 20, i -> "{\"foo\":\"bar\"}"); final TestStreamingClient client1 = TestStreamingClient .create(subscription.getId()) .start(); diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/utils/NakadiTestUtils.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/utils/NakadiTestUtils.java index 5f1981b6c3..6f96c36348 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/utils/NakadiTestUtils.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/utils/NakadiTestUtils.java @@ -5,15 +5,26 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableList; import com.jayway.restassured.RestAssured; +import static com.jayway.restassured.RestAssured.given; import com.jayway.restassured.http.ContentType; +import static com.jayway.restassured.http.ContentType.JSON; import com.jayway.restassured.response.Response; import com.jayway.restassured.specification.RequestSpecification; +import java.io.IOException; +import static java.text.MessageFormat.format; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.function.IntFunction; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import org.apache.http.HttpStatus; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.json.JSONArray; import org.json.JSONObject; import org.junit.Assert; +import static org.springframework.http.HttpStatus.OK; import org.zalando.nakadi.config.JsonConfig; import org.zalando.nakadi.domain.EnrichmentStrategyDescriptor; import org.zalando.nakadi.domain.EventCategory; @@ -28,17 +39,6 @@ import org.zalando.nakadi.view.SubscriptionCursor; import org.zalando.nakadi.view.TimelineView; -import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import java.util.stream.Collectors; - -import static com.jayway.restassured.RestAssured.given; -import static com.jayway.restassured.http.ContentType.JSON; -import static java.text.MessageFormat.format; -import static org.springframework.http.HttpStatus.OK; - public class NakadiTestUtils { private static final ObjectMapper MAPPER = (new JsonConfig()).jacksonObjectMapper(); @@ -92,6 +92,14 @@ public static void publishEvent(final String eventType, final String event) { .post(format("/event-types/{0}/events", eventType)); } + public static void publishEvents(final String eventType, final int count, final IntFunction generator) { + final String events = IntStream.range(0, count).mapToObj(generator).collect(Collectors.joining(",")); + given() + .body("[" + events + "]") + .contentType(JSON) + .post(format("/event-types/{0}/events", eventType)); + } + public static void createTimeline(final String eventType) { given() .body("{\"storage_id\": \"default\"}") @@ -106,7 +114,8 @@ public static List listTimelines(final String eventType) throws IOException .accept(JSON) .get(format("/event-types/{0}/timelines", eventType)); final String data = response.print(); - final TypeReference> typeReference = new TypeReference>(){}; + final TypeReference> typeReference = new TypeReference>() { + }; return MAPPER.readValue(data, typeReference); } From f8d5188d09eda05c10d6a2b91a943c869f99ca2a Mon Sep 17 00:00:00 2001 From: antban Date: Tue, 11 Jul 2017 18:05:13 +0200 Subject: [PATCH 06/21] ARUHA-882 Use batch publishing in HilaRebalanceAT --- .../webservice/hila/HilaRebalanceAT.java | 48 +++++++++---------- .../webservice/hila/SubscriptionAT.java | 9 ++-- .../webservice/utils/NakadiTestUtils.java | 31 +++++++----- 3 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/HilaRebalanceAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/HilaRebalanceAT.java index 6dc1251442..37ba8bd189 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/HilaRebalanceAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/HilaRebalanceAT.java @@ -1,21 +1,10 @@ package org.zalando.nakadi.webservice.hila; -import org.junit.Before; -import org.junit.Test; -import org.zalando.nakadi.domain.EventType; -import org.zalando.nakadi.domain.Subscription; -import org.zalando.nakadi.domain.SubscriptionBase; -import org.zalando.nakadi.view.SubscriptionCursor; -import org.zalando.nakadi.utils.RandomSubscriptionBuilder; -import org.zalando.nakadi.webservice.BaseAT; -import org.zalando.nakadi.webservice.utils.TestStreamingClient; - +import static com.google.common.collect.Sets.intersection; import java.io.IOException; import java.util.List; import java.util.Optional; import java.util.Set; - -import static com.google.common.collect.Sets.intersection; import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toSet; import static java.util.stream.IntStream.range; @@ -23,12 +12,21 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; +import org.junit.Before; +import org.junit.Test; +import org.zalando.nakadi.domain.EventType; +import org.zalando.nakadi.domain.Subscription; +import org.zalando.nakadi.domain.SubscriptionBase; import static org.zalando.nakadi.domain.SubscriptionBase.InitialPosition.BEGIN; +import org.zalando.nakadi.utils.RandomSubscriptionBuilder; import static org.zalando.nakadi.utils.TestUtils.waitFor; +import org.zalando.nakadi.view.SubscriptionCursor; +import org.zalando.nakadi.webservice.BaseAT; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.commitCursors; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.createBusinessEventTypeWithPartitions; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.publishBusinessEventWithUserDefinedPartition; +import org.zalando.nakadi.webservice.utils.TestStreamingClient; public class HilaRebalanceAT extends BaseAT { @@ -48,9 +46,11 @@ public void before() throws IOException { @Test(timeout = 30000) public void whenRebalanceThenPartitionsAreEquallyDistributedAndCommittedOffsetsAreConsidered() throws Exception { // write 5 events to each partition - range(0, 40) - .forEach(x -> publishBusinessEventWithUserDefinedPartition( - eventType.getName(), "blah" + x, String.valueOf(x % 8))); + publishBusinessEventWithUserDefinedPartition( + eventType.getName(), + 40, + x -> "blah" + x, + x -> String.valueOf(x % 8)); // create a session final TestStreamingClient clientA = TestStreamingClient @@ -82,9 +82,11 @@ public void whenRebalanceThenPartitionsAreEquallyDistributedAndCommittedOffsetsA Thread.sleep(1000); // write 5 more events to each partition - range(0, 40) - .forEach(x -> publishBusinessEventWithUserDefinedPartition( - eventType.getName(), "blah_" + x, String.valueOf(x % 8))); + publishBusinessEventWithUserDefinedPartition( + eventType.getName(), + 40, + x -> "blah_" + x, + x -> String.valueOf(x % 8)); // wait till all event arrive waitFor(() -> assertThat(clientB.getBatches(), hasSize(20))); @@ -114,9 +116,8 @@ public void whenRebalanceThenPartitionsAreEquallyDistributedAndCommittedOffsetsA Thread.sleep(1000); // write 5 more events to each partition - range(0, 40) - .forEach(x -> publishBusinessEventWithUserDefinedPartition( - eventType.getName(), "blah__" + x, String.valueOf(x % 8))); + publishBusinessEventWithUserDefinedPartition( + eventType.getName(), 40, x -> "blah__" + x, x -> String.valueOf(x % 8)); // check that after second rebalance all events were consumed by first client waitFor(() -> assertThat(clientA.getBatches(), hasSize(100))); @@ -124,9 +125,8 @@ public void whenRebalanceThenPartitionsAreEquallyDistributedAndCommittedOffsetsA @Test(timeout = 15000) public void whenNotCommittedThenEventsAreReplayedAfterRebalance() { - range(0, 2) - .forEach(x -> publishBusinessEventWithUserDefinedPartition( - eventType.getName(), "blah" + x, String.valueOf(x % 8))); + publishBusinessEventWithUserDefinedPartition( + eventType.getName(), 2, x -> "blah" + x, x -> String.valueOf(x % 8)); final TestStreamingClient clientA = TestStreamingClient .create(URL, subscription.getId(), "") diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/SubscriptionAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/SubscriptionAT.java index 48d3e71959..1371ffb9f5 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/SubscriptionAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/SubscriptionAT.java @@ -17,7 +17,6 @@ import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.IntStream; -import static java.util.stream.IntStream.range; import org.apache.curator.framework.CuratorFramework; import org.apache.http.HttpStatus; import org.apache.zookeeper.data.Stat; @@ -241,10 +240,10 @@ public void testSubscriptionWithInitialCursors() throws Exception { final EventType et2 = createBusinessEventTypeWithPartitions(2); // write 10 events to each partition of two event-types - range(0, 10).forEach(x -> publishBusinessEventWithUserDefinedPartition(et1.getName(), "dummy", "0")); - range(0, 10).forEach(x -> publishBusinessEventWithUserDefinedPartition(et1.getName(), "dummy", "1")); - range(0, 10).forEach(x -> publishBusinessEventWithUserDefinedPartition(et2.getName(), "dummy", "0")); - range(0, 10).forEach(x -> publishBusinessEventWithUserDefinedPartition(et2.getName(), "dummy", "1")); + publishBusinessEventWithUserDefinedPartition(et1.getName(), 10, i -> "dummy", i -> "0"); + publishBusinessEventWithUserDefinedPartition(et1.getName(), 10, i -> "dummy", i -> "1"); + publishBusinessEventWithUserDefinedPartition(et2.getName(), 10, i -> "dummy", i -> "0"); + publishBusinessEventWithUserDefinedPartition(et2.getName(), 10, i -> "dummy", i -> "1"); // create subscription with initial cursors final SubscriptionBase subscriptionBase = RandomSubscriptionBuilder.builder() diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/utils/NakadiTestUtils.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/utils/NakadiTestUtils.java index 6f96c36348..d75e4bf11d 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/utils/NakadiTestUtils.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/utils/NakadiTestUtils.java @@ -132,19 +132,24 @@ public static void deleteTimeline(final String eventType) throws IOException { .statusCode(HttpStatus.SC_OK); } - public static void publishBusinessEventWithUserDefinedPartition(final String eventType, - final String foo, - final String partition) { - final JSONObject metadata = new JSONObject(); - metadata.put("eid", UUID.randomUUID().toString()); - metadata.put("occurred_at", (new DateTime(DateTimeZone.UTC)).toString()); - metadata.put("partition", partition); - - final JSONObject event = new JSONObject(); - event.put("metadata", metadata); - event.put("foo", foo); - - publishEvent(eventType, event.toString()); + public static void publishBusinessEventWithUserDefinedPartition( + final String eventType, + final int count, + final IntFunction fooGenerator, + final IntFunction partitionGenerator) { + publishEvents( + eventType, + count, i -> { + final JSONObject metadata = new JSONObject(); + metadata.put("eid", UUID.randomUUID().toString()); + metadata.put("occurred_at", (new DateTime(DateTimeZone.UTC)).toString()); + metadata.put("partition", partitionGenerator.apply(i)); + + final JSONObject event = new JSONObject(); + event.put("metadata", metadata); + event.put("foo", fooGenerator.apply(i)); + return event.toString(); + }); } public static void publishBusinessEventsWithUserDefinedPartition( From e1a40bd03b0bf3a9014f2acbd076219c32a10bd2 Mon Sep 17 00:00:00 2001 From: antban Date: Tue, 11 Jul 2017 18:08:25 +0200 Subject: [PATCH 07/21] ARUHA-882 Optimize SubscriptionAT (create less event types) --- .../java/org/zalando/nakadi/webservice/hila/SubscriptionAT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/SubscriptionAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/SubscriptionAT.java index 1371ffb9f5..740064d106 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/SubscriptionAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/hila/SubscriptionAT.java @@ -122,7 +122,7 @@ public void testSubscriptionBaseOperations() throws IOException { @Test public void testSubscriptionWithManyEventTypesIsCreated() throws IOException { - final List eventTypes = IntStream.range(0, 30).mapToObj(i -> createEventType()) + final List eventTypes = IntStream.range(0, 10).mapToObj(i -> createEventType()) .map(EventTypeBase::getName) .collect(Collectors.toList()); final String subscription = "{\"owning_application\":\"app\",\"event_types\":" + From 4865d5578a8ee7b55a1e02d2d21cf9a5ef0d8f24 Mon Sep 17 00:00:00 2001 From: antban Date: Wed, 12 Jul 2017 08:53:22 +0200 Subject: [PATCH 08/21] ARUHA-882 Do not delete data from db each time for db repository tests --- .../db/AbstractDbRepositoryTest.java | 18 --- .../db/EventTypeDbRepositoryTest.java | 92 +++++++-------- .../repository/db/SchemaRepositoryTest.java | 32 ++--- .../db/StorageDbRepositoryTest.java | 57 +++++---- .../db/SubscriptionDbRepositoryTest.java | 110 +++++++++--------- .../db/TimelineDbRepositoryTest.java | 43 ++++--- .../org/zalando/nakadi/webservice/BaseAT.java | 3 +- .../SubscriptionConsumptionTest.java | 25 ++-- .../org/zalando/nakadi/utils/TestUtils.java | 8 +- 9 files changed, 186 insertions(+), 202 deletions(-) diff --git a/src/acceptance-test/java/org/zalando/nakadi/repository/db/AbstractDbRepositoryTest.java b/src/acceptance-test/java/org/zalando/nakadi/repository/db/AbstractDbRepositoryTest.java index 59019174cf..36dcd3d3de 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/repository/db/AbstractDbRepositoryTest.java +++ b/src/acceptance-test/java/org/zalando/nakadi/repository/db/AbstractDbRepositoryTest.java @@ -1,15 +1,12 @@ package org.zalando.nakadi.repository.db; -import com.fasterxml.jackson.databind.ObjectMapper; import java.sql.Connection; import java.sql.SQLException; -import java.util.stream.Stream; import javax.sql.DataSource; import org.junit.After; import org.junit.Before; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; -import org.zalando.nakadi.config.JsonConfig; import static org.zalando.nakadi.webservice.BaseAT.POSTGRES_PWD; import static org.zalando.nakadi.webservice.BaseAT.POSTGRES_URL; import static org.zalando.nakadi.webservice.BaseAT.POSTGRES_USER; @@ -18,21 +15,13 @@ public abstract class AbstractDbRepositoryTest { protected JdbcTemplate template; protected Connection connection; - protected ObjectMapper mapper; - protected String[] repositoryTables; - - public AbstractDbRepositoryTest(final String... repositoryTables) { - this.repositoryTables = repositoryTables; - } @Before public void setUp() throws Exception { try { - mapper = (new JsonConfig()).jacksonObjectMapper(); final DataSource datasource = new DriverManagerDataSource(POSTGRES_URL, POSTGRES_USER, POSTGRES_PWD); template = new JdbcTemplate(datasource); connection = datasource.getConnection(); - clearRepositoryTables(); } catch (final SQLException e) { e.printStackTrace(); } @@ -40,13 +29,6 @@ public void setUp() throws Exception { @After public void tearDown() throws SQLException { - clearRepositoryTables(); connection.close(); } - - private void clearRepositoryTables() { - Stream.of(repositoryTables) - .map(table -> "DELETE FROM " + table) - .forEach(template::execute); - } } diff --git a/src/acceptance-test/java/org/zalando/nakadi/repository/db/EventTypeDbRepositoryTest.java b/src/acceptance-test/java/org/zalando/nakadi/repository/db/EventTypeDbRepositoryTest.java index 22a235c37c..37dda85d67 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/repository/db/EventTypeDbRepositoryTest.java +++ b/src/acceptance-test/java/org/zalando/nakadi/repository/db/EventTypeDbRepositoryTest.java @@ -1,7 +1,15 @@ package org.zalando.nakadi.repository.db; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.zalando.nakadi.config.JsonConfig; +import java.io.IOException; +import java.util.List; +import java.util.stream.Collectors; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsNull.notNullValue; +import org.junit.Before; +import org.junit.Test; +import org.springframework.jdbc.support.rowset.SqlRowSet; import org.zalando.nakadi.domain.EventCategory; import org.zalando.nakadi.domain.EventType; import org.zalando.nakadi.domain.EventTypeSchema; @@ -10,17 +18,7 @@ import org.zalando.nakadi.exceptions.NakadiException; import org.zalando.nakadi.exceptions.NoSuchEventTypeException; import org.zalando.nakadi.repository.EventTypeRepository; -import org.junit.Before; -import org.junit.Test; -import org.springframework.jdbc.support.rowset.SqlRowSet; - -import java.io.IOException; -import java.util.List; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.hamcrest.core.IsNull.notNullValue; +import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.buildDefaultEventType; import static org.zalando.nakadi.utils.TestUtils.randomUUID; import static org.zalando.nakadi.utils.TestUtils.randomValidEventTypeName; @@ -29,14 +27,10 @@ public class EventTypeDbRepositoryTest extends AbstractDbRepositoryTest { private EventTypeRepository repository; - public EventTypeDbRepositoryTest() { - super("zn_data.event_type_schema", "zn_data.event_type"); - } - @Before public void setUp() throws Exception { super.setUp(); - repository = new EventTypeDbRepository(template, mapper); + repository = new EventTypeDbRepository(template, TestUtils.OBJECT_MAPPER); } @Test @@ -45,18 +39,14 @@ public void whenCreateNewEventTypePersistItInTheDatabase() throws Exception { repository.saveEventType(eventType); - final int rows = template.queryForObject("SELECT count(*) FROM zn_data.event_type", Integer.class); - assertThat("Number of rows should increase", rows, equalTo(1)); - final SqlRowSet rs = - template.queryForRowSet("SELECT et_name, et_topic, et_event_type_object FROM zn_data.event_type"); + template.queryForRowSet("SELECT et_topic, et_event_type_object FROM zn_data.event_type WHERE et_name=?", + eventType.getName()); rs.next(); - assertThat("Name is persisted", rs.getString(1), equalTo(eventType.getName())); - assertThat("Topic is persisted", rs.getString(2), equalTo(eventType.getTopic())); + assertThat("Topic is persisted", rs.getString(1), equalTo(eventType.getTopic())); - final ObjectMapper mapper = (new JsonConfig()).jacksonObjectMapper(); - final EventType persisted = mapper.readValue(rs.getString(3), EventType.class); + final EventType persisted = TestUtils.OBJECT_MAPPER.readValue(rs.getString(2), EventType.class); assertThat(persisted.getCategory(), equalTo(eventType.getCategory())); assertThat(persisted.getName(), equalTo(eventType.getName())); @@ -70,17 +60,17 @@ public void whenCreateNewEventTypeAlsoInsertIntoSchemaTable() throws Exception { repository.saveEventType(eventType); - final int rows = template.queryForObject("SELECT count(*) FROM zn_data.event_type_schema", Integer.class); + final int rows = template.queryForObject( + "SELECT count(*) FROM zn_data.event_type_schema where ets_event_type_name=?", + Integer.class, eventType.getName()); assertThat("Number of rows should increase", rows, equalTo(1)); - final SqlRowSet rs = - template.queryForRowSet("SELECT ets_event_type_name, ets_schema_object FROM zn_data.event_type_schema"); + final SqlRowSet rs = template.queryForRowSet( + "SELECT ets_schema_object FROM zn_data.event_type_schema where ets_event_type_name=?", + eventType.getName()); rs.next(); - assertThat("Name is persisted", rs.getString(1), equalTo(eventType.getName())); - - final ObjectMapper mapper = (new JsonConfig()).jacksonObjectMapper(); - final EventTypeSchema persisted = mapper.readValue(rs.getString(2), EventTypeSchema.class); + final EventTypeSchema persisted = TestUtils.OBJECT_MAPPER.readValue(rs.getString(1), EventTypeSchema.class); assertThat(persisted.getVersion(), equalTo(eventType.getSchema().getVersion())); assertThat(persisted.getCreatedAt(), notNullValue()); @@ -135,16 +125,16 @@ public void whenUpdateExistingEventTypeItUpdates() throws NakadiException, IOExc repository.update(eventType); - final int rows = template.queryForObject("SELECT count(*) FROM zn_data.event_type_schema", Integer.class); + final int rows = template.queryForObject( + "SELECT count(*) FROM zn_data.event_type_schema WHERE ets_event_type_name=?", + Integer.class, eventType.getName()); assertThat("Number of rows should increase", rows, equalTo(1)); - final SqlRowSet rs = template.queryForRowSet("SELECT et_name, et_event_type_object FROM zn_data.event_type"); + final SqlRowSet rs = template.queryForRowSet( + "SELECT et_event_type_object FROM zn_data.event_type WHERE et_name=?", eventType.getName()); rs.next(); - assertThat("Name is persisted", rs.getString(1), equalTo(eventType.getName())); - - final ObjectMapper mapper = (new JsonConfig()).jacksonObjectMapper(); - final EventType persisted = mapper.readValue(rs.getString(2), EventType.class); + final EventType persisted = TestUtils.OBJECT_MAPPER.readValue(rs.getString(1), EventType.class); assertThat(persisted.getCategory(), equalTo(eventType.getCategory())); assertThat(persisted.getOwningApplication(), equalTo(eventType.getOwningApplication())); @@ -164,7 +154,9 @@ public void whenUpdateDifferentSchemaVersionThenInsertIt() throws NakadiExceptio repository.update(eventType); - final int rows = template.queryForObject("SELECT count(*) FROM zn_data.event_type_schema", Integer.class); + final int rows = template.queryForObject( + "SELECT count(*) FROM zn_data.event_type_schema where ets_event_type_name=?", + Integer.class, eventType.getName()); assertThat("Number of rows should increase", rows, equalTo(2)); } @@ -176,7 +168,10 @@ public void whenListExistingEventTypesAreListed() throws NakadiException { repository.saveEventType(eventType1); repository.saveEventType(eventType2); - final List eventTypes = repository.list(); + final List eventTypes = repository.list().stream() + .filter(et -> et.getName() != null) + .filter(et -> et.getName().equals(eventType1.getName()) || et.getName().equals(eventType2.getName())) + .collect(Collectors.toList()); assertThat(eventTypes, hasSize(2)); } @@ -184,15 +179,18 @@ public void whenListExistingEventTypesAreListed() throws NakadiException { @Test public void whenRemoveThenDeleteFromDatabase() throws Exception { final EventType eventType = buildDefaultEventType(); + insertEventType(eventType); - repository.removeEventType(eventType.getName()); + int rows = template.queryForObject("SELECT count(*) FROM zn_data.event_type where et_name=?", Integer.class, + eventType.getName()); + assertThat("After inserting event type it is present in db", rows, equalTo(1)); - final int rows = template.queryForObject("SELECT count(*) FROM zn_data.event_type", Integer.class); - assertThat("Number of rows should encrease", rows, equalTo(0)); + repository.removeEventType(eventType.getName()); - final int schemaRows = template.queryForObject("SELECT count(*) FROM zn_data.event_type_schema", Integer.class); - assertThat("Number of rows should decrease", schemaRows, equalTo(0)); + rows = template.queryForObject("SELECT count(*) FROM zn_data.event_type where et_name=?", Integer.class, + eventType.getName()); + assertThat("After deleting event type it is not present in db", rows, equalTo(0)); } @Test @@ -217,6 +215,6 @@ private void insertEventType(final EventType eventType) throws Exception { template.update(insertSQL, eventType.getName(), eventType.getTopic(), - mapper.writer().writeValueAsString(eventType)); + TestUtils.OBJECT_MAPPER.writer().writeValueAsString(eventType)); } } diff --git a/src/acceptance-test/java/org/zalando/nakadi/repository/db/SchemaRepositoryTest.java b/src/acceptance-test/java/org/zalando/nakadi/repository/db/SchemaRepositoryTest.java index 8085a6686a..511481dcd5 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/repository/db/SchemaRepositoryTest.java +++ b/src/acceptance-test/java/org/zalando/nakadi/repository/db/SchemaRepositoryTest.java @@ -1,6 +1,7 @@ package org.zalando.nakadi.repository.db; import com.fasterxml.jackson.core.JsonProcessingException; +import java.util.List; import org.joda.time.DateTime; import org.junit.Assert; import org.junit.Before; @@ -10,49 +11,48 @@ import org.zalando.nakadi.domain.EventTypeSchemaBase; import org.zalando.nakadi.domain.Version; import org.zalando.nakadi.utils.EventTypeTestBuilder; - -import java.util.List; +import org.zalando.nakadi.utils.TestUtils; +import static org.zalando.nakadi.utils.TestUtils.randomUUID; public class SchemaRepositoryTest extends AbstractDbRepositoryTest { private SchemaRepository repository; - public SchemaRepositoryTest() { - super(new String[]{"zn_data.event_type_schema"}); - } - @Before public void setUp() throws Exception { super.setUp(); - repository = new SchemaRepository(template, mapper); + repository = new SchemaRepository(template, TestUtils.OBJECT_MAPPER); } @Test public void whenListVersionsListedOrdered() throws Exception { - buildEventWithMultipleSchemas("test_et_name_schemarepositorytest"); + final String name = randomUUID(); + buildEventWithMultipleSchemas(name); - final List schemas = repository.getSchemas("test_et_name_schemarepositorytest", 0, 3); + final List schemas = repository.getSchemas(name, 0, 3); Assert.assertEquals(3, schemas.size()); Assert.assertEquals(new Version("10.0.0"), schemas.get(0).getVersion()); Assert.assertEquals(new Version("2.10.3"), schemas.get(1).getVersion()); Assert.assertEquals(new Version("1.0.2"), schemas.get(2).getVersion()); - final int count = repository.getSchemasCount("test_et_name_schemarepositorytest"); + final int count = repository.getSchemasCount(name); Assert.assertEquals(3, count); } @Test public void whenGetLatestSchemaReturnLatest() throws Exception { - buildEventWithMultipleSchemas("test_latest_schema_event"); - final EventTypeSchema schema = repository.getSchemaVersion("test_latest_schema_event", "10.0.0"); + final String name = randomUUID(); + buildEventWithMultipleSchemas(name); + final EventTypeSchema schema = repository.getSchemaVersion(name, "10.0.0"); Assert.assertEquals("10.0.0", schema.getVersion().toString()); Assert.assertEquals("schema", schema.getSchema()); } @Test public void whenGetOldSchemaReturnOld() throws Exception { - buildEventWithMultipleSchemas("test_old_schema_event"); - final EventTypeSchema schema = repository.getSchemaVersion("test_old_schema_event", "1.0.2"); + final String name = randomUUID(); + buildEventWithMultipleSchemas(name); + final EventTypeSchema schema = repository.getSchemaVersion(name, "1.0.2"); Assert.assertEquals("1.0.2", schema.getVersion().toString()); Assert.assertEquals("schema", schema.getSchema()); } @@ -75,7 +75,7 @@ private void insertSchema(final EventType eventType) throws JsonProcessingExcept template.update( "INSERT INTO zn_data.event_type_schema (ets_event_type_name, ets_schema_object) VALUES (?, ?::jsonb)", eventType.getName(), - mapper.writer().writeValueAsString(eventType.getSchema())); + TestUtils.OBJECT_MAPPER.writer().writeValueAsString(eventType.getSchema())); } private void insertEventType(final EventType eventType) throws Exception { @@ -84,7 +84,7 @@ private void insertEventType(final EventType eventType) throws Exception { template.update(insertSQL, eventType.getName(), eventType.getTopic(), - mapper.writer().writeValueAsString(eventType)); + TestUtils.OBJECT_MAPPER.writer().writeValueAsString(eventType)); } } \ No newline at end of file diff --git a/src/acceptance-test/java/org/zalando/nakadi/repository/db/StorageDbRepositoryTest.java b/src/acceptance-test/java/org/zalando/nakadi/repository/db/StorageDbRepositoryTest.java index 328c1c5c78..4ead7e24b3 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/repository/db/StorageDbRepositoryTest.java +++ b/src/acceptance-test/java/org/zalando/nakadi/repository/db/StorageDbRepositoryTest.java @@ -1,40 +1,36 @@ package org.zalando.nakadi.repository.db; -import org.junit.Before; -import org.junit.Test; -import org.zalando.nakadi.domain.EventType; -import org.zalando.nakadi.domain.Storage; -import org.zalando.nakadi.domain.Timeline; -import org.zalando.nakadi.exceptions.runtime.NoStorageException; -import org.zalando.nakadi.exceptions.runtime.StorageIsUsedException; -import org.zalando.nakadi.utils.TestUtils; - import java.util.Date; import java.util.List; import java.util.Optional; import java.util.UUID; - +import java.util.stream.Collectors; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertTrue; +import org.junit.Before; +import org.junit.Test; +import org.zalando.nakadi.domain.EventType; +import org.zalando.nakadi.domain.Storage; +import org.zalando.nakadi.domain.Timeline; +import org.zalando.nakadi.exceptions.runtime.NoStorageException; +import org.zalando.nakadi.exceptions.runtime.StorageIsUsedException; +import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.randomUUID; +import static org.zalando.nakadi.utils.TestUtils.randomValidStringOfLength; public class StorageDbRepositoryTest extends AbstractDbRepositoryTest { private StorageDbRepository repository; private TimelineDbRepository timelineDbRepository; private EventTypeDbRepository eventTypeDbRepository; - public StorageDbRepositoryTest() { - super("zn_data.timeline", "zn_data.event_type_schema", "zn_data.event_type", "zn_data.storage"); - } - @Before public void setUp() throws Exception { super.setUp(); - repository = new StorageDbRepository(template, mapper); - timelineDbRepository = new TimelineDbRepository(template, mapper); - eventTypeDbRepository = new EventTypeDbRepository(template, mapper); + repository = new StorageDbRepository(template, TestUtils.OBJECT_MAPPER); + timelineDbRepository = new TimelineDbRepository(template, TestUtils.OBJECT_MAPPER); + eventTypeDbRepository = new EventTypeDbRepository(template, TestUtils.OBJECT_MAPPER); } static Storage createStorage(final String name, @@ -53,7 +49,8 @@ static Storage createStorage(final String name, @Test public void testStorageCreated() throws Exception { - final Storage storage = createStorage("default", "exaddress", 8181, "address", "path"); + final String name = randomUUID(); + final Storage storage = createStorage(name, "exaddress", 8181, "address", "path"); repository.createStorage(storage); @@ -66,11 +63,20 @@ public void testStorageCreated() throws Exception { @Test public void testStorageOrdered() throws Exception { - final Storage storage2 = repository.createStorage(createStorage("2", "exaddress", 8181, "address1", "path3")); - final Storage storage1 = repository.createStorage(createStorage("1", "exaddress", 8181, "address2", "path2")); - final Storage storage3 = repository.createStorage(createStorage("3", "exaddress", 8181, "address3", "path1")); + final String namePrefix = randomValidStringOfLength(31); + + final Storage storage2 = repository.createStorage( + createStorage(namePrefix + "2", "exaddress", 8181, "address1", "path3")); + final Storage storage1 = repository.createStorage( + createStorage(namePrefix + "1", "exaddress", 8181, "address2", "path2")); + final Storage storage3 = repository.createStorage( + createStorage(namePrefix + "3", "exaddress", 8181, "address3", "path1")); + + final List storages = repository.listStorages().stream() + .filter(st -> st.getId() != null) + .filter(st -> st.getId().startsWith(namePrefix)) + .collect(Collectors.toList()); - final List storages = repository.listStorages(); assertEquals(3, storages.size()); assertEquals(storage1, storages.get(0)); assertEquals(storage2, storages.get(1)); @@ -79,11 +85,11 @@ public void testStorageOrdered() throws Exception { @Test public void testStorageDeleted() throws Exception { - final Storage storage = repository.createStorage(createStorage("1", "exaddress", 8181, "address2", "path2")); + final String name = randomUUID(); + final Storage storage = repository.createStorage(createStorage(name, "exaddress", 8181, "address2", "path2")); assertEquals(storage, repository.getStorage(storage.getId()).get()); repository.deleteStorage(storage.getId()); assertFalse(repository.getStorage(storage.getId()).isPresent()); - assertEquals(0, repository.listStorages().size()); } @Test(expected = NoStorageException.class) @@ -93,7 +99,8 @@ public void testDeleteNoneExistingStorage() throws Exception { @Test(expected = StorageIsUsedException.class) public void testDeleteUsedStorage() throws Exception { - final Storage storage = repository.createStorage(createStorage("1", "exaddress", 8181, "address", "path")); + final String name = randomUUID(); + final Storage storage = repository.createStorage(createStorage(name, "exaddress", 8181, "address", "path")); final EventType eventType = eventTypeDbRepository.saveEventType(TestUtils.buildDefaultEventType()); final Timeline timeline = TimelineDbRepositoryTest.createTimeline(storage, UUID.randomUUID(), 0, "topic", diff --git a/src/acceptance-test/java/org/zalando/nakadi/repository/db/SubscriptionDbRepositoryTest.java b/src/acceptance-test/java/org/zalando/nakadi/repository/db/SubscriptionDbRepositoryTest.java index 4ebebe0a6a..c45ffe50e3 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/repository/db/SubscriptionDbRepositoryTest.java +++ b/src/acceptance-test/java/org/zalando/nakadi/repository/db/SubscriptionDbRepositoryTest.java @@ -3,31 +3,29 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; -import org.junit.Before; -import org.junit.Test; -import org.zalando.nakadi.config.JsonConfig; -import org.zalando.nakadi.domain.Subscription; -import org.zalando.nakadi.domain.SubscriptionBase; -import org.zalando.nakadi.exceptions.runtime.DuplicatedSubscriptionException; -import org.zalando.nakadi.exceptions.NoSuchSubscriptionException; -import org.zalando.nakadi.exceptions.ServiceUnavailableException; -import org.zalando.nakadi.util.HashGenerator; -import org.zalando.nakadi.util.UUIDGenerator; -import org.zalando.nakadi.utils.RandomSubscriptionBuilder; - +import static java.util.Collections.emptySet; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Optional; - -import static java.util.Collections.emptySet; import static java.util.stream.Collectors.toList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.isEmptyOrNullString; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.core.IsEqual.equalTo; +import org.junit.Before; +import org.junit.Test; +import org.zalando.nakadi.config.JsonConfig; +import org.zalando.nakadi.domain.Subscription; +import org.zalando.nakadi.domain.SubscriptionBase; +import org.zalando.nakadi.exceptions.NoSuchSubscriptionException; +import org.zalando.nakadi.exceptions.ServiceUnavailableException; +import org.zalando.nakadi.exceptions.runtime.DuplicatedSubscriptionException; +import org.zalando.nakadi.util.HashGenerator; +import org.zalando.nakadi.util.UUIDGenerator; +import org.zalando.nakadi.utils.RandomSubscriptionBuilder; +import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.createRandomSubscriptions; public class SubscriptionDbRepositoryTest extends AbstractDbRepositoryTest { @@ -38,14 +36,11 @@ public class SubscriptionDbRepositoryTest extends AbstractDbRepositoryTest { private SubscriptionDbRepository repository; private HashGenerator hashGenerator = new HashGenerator(); - public SubscriptionDbRepositoryTest() { - super("zn_data.subscription"); - } - @Before public void setUp() throws Exception { super.setUp(); - repository = new SubscriptionDbRepository(template, mapper, new UUIDGenerator(), hashGenerator); + repository = new SubscriptionDbRepository( + template, TestUtils.OBJECT_MAPPER, new UUIDGenerator(), hashGenerator); } @Test @@ -55,12 +50,12 @@ public void whenCreateSubscriptionThenOk() throws Exception { final Subscription createdSubscription = repository.createSubscription(subscription); checkSubscriptionCreatedFromSubscriptionBase(createdSubscription, subscription); - final int rows = template.queryForObject("SELECT count(*) FROM zn_data.subscription", Integer.class); + final int rows = template.queryForObject("SELECT count(*) FROM zn_data.subscription where s_id=?", + Integer.class, createdSubscription.getId()); assertThat("Number of rows should be 1", rows, equalTo(1)); - final Map result = - template.queryForMap("SELECT s_id, s_subscription_object FROM zn_data.subscription"); - assertThat("Id is persisted", result.get("s_id"), equalTo(createdSubscription.getId())); + final Map result = template.queryForMap( + "SELECT s_subscription_object FROM zn_data.subscription WHERE s_id=?", createdSubscription.getId()); final ObjectMapper mapper = (new JsonConfig()).jacksonObjectMapper(); final Subscription saved = mapper.readValue(result.get("s_subscription_object").toString(), Subscription.class); @@ -91,88 +86,87 @@ public void whenGetSubscriptionByIdThenOk() throws Exception { @Test public void whenGetSubscriptionByKeyPropertiesThenOk() throws Exception { - + final String owningApplication = TestUtils.randomUUID(); // insert subscription into DB final Subscription subscription = RandomSubscriptionBuilder.builder() - .withOwningApplication("myapp") + .withOwningApplication(owningApplication) .withEventTypes(ImmutableSet.of("my-et", "second-et")) .withConsumerGroup("my-cg") .build(); insertSubscriptionToDB(subscription); // get subscription by key properties and compare to original - final Subscription gotSubscription = repository.getSubscription("myapp", ImmutableSet.of("second-et", "my-et"), + final Subscription gotSubscription = repository.getSubscription(owningApplication, + ImmutableSet.of("second-et", "my-et"), "my-cg"); assertThat("We found the needed subscription", gotSubscription, equalTo(subscription)); } - @Test - public void whenListSubscriptionsThenOk() throws ServiceUnavailableException { - - final List testSubscriptions = Lists.newArrayList( - RandomSubscriptionBuilder.builder().build(), RandomSubscriptionBuilder.builder().build()); - testSubscriptions.sort(SUBSCRIPTION_CREATION_DATE_DESC_COMPARATOR); - testSubscriptions.forEach(this::insertSubscriptionToDB); - - final List subscriptions = repository.listSubscriptions(emptySet(), Optional.empty(), 0, 10); - assertThat(subscriptions, equalTo(testSubscriptions)); - } - @Test public void whenListSubscriptionsByOwningApplicationAndEventTypeThenOk() throws ServiceUnavailableException { + final String owningApp = TestUtils.randomUUID(); + final String owningApp2 = TestUtils.randomUUID(); + final List testSubscriptions = ImmutableList.of( - RandomSubscriptionBuilder.builder().withOwningApplication("app").withEventType("et1").build(), - RandomSubscriptionBuilder.builder().withOwningApplication("app") + RandomSubscriptionBuilder.builder().withOwningApplication(owningApp).withEventType("et1").build(), + RandomSubscriptionBuilder.builder().withOwningApplication(owningApp) .withEventTypes(ImmutableSet.of("et2", "et1")).build(), - RandomSubscriptionBuilder.builder().withOwningApplication("app").withEventType("et1").build(), - RandomSubscriptionBuilder.builder().withOwningApplication("app").withEventType("et2").build(), - RandomSubscriptionBuilder.builder().withOwningApplication("app") + RandomSubscriptionBuilder.builder().withOwningApplication(owningApp).withEventType("et1").build(), + RandomSubscriptionBuilder.builder().withOwningApplication(owningApp).withEventType("et2").build(), + RandomSubscriptionBuilder.builder().withOwningApplication(owningApp) .withEventTypes(ImmutableSet.of("et2", "et3")).build(), - RandomSubscriptionBuilder.builder().withOwningApplication("app2").withEventType("et1").build(), - RandomSubscriptionBuilder.builder().withOwningApplication("app2").withEventType("et2").build()); + RandomSubscriptionBuilder.builder().withOwningApplication(owningApp2).withEventType("et1").build(), + RandomSubscriptionBuilder.builder().withOwningApplication(owningApp2).withEventType("et2").build()); testSubscriptions.forEach(this::insertSubscriptionToDB); final List expectedSubscriptions = testSubscriptions.stream() - .filter(sub -> "app".equals(sub.getOwningApplication()) && sub.getEventTypes().contains("et1")) + .filter(sub -> owningApp.equals(sub.getOwningApplication()) && sub.getEventTypes().contains("et1")) .sorted(SUBSCRIPTION_CREATION_DATE_DESC_COMPARATOR) .collect(toList()); final List subscriptions = repository.listSubscriptions(ImmutableSet.of("et1"), - Optional.of("app"), 0, 10); + Optional.of(owningApp), 0, 10); assertThat(subscriptions, equalTo(expectedSubscriptions)); } @Test public void whenListSubscriptionsByMultipleEventTypesThenOk() throws ServiceUnavailableException { + final String et1 = TestUtils.randomUUID(); + final String et2 = TestUtils.randomUUID(); + final String et3 = TestUtils.randomUUID(); + final String et4 = TestUtils.randomUUID(); + final String et5 = TestUtils.randomUUID(); + final List testSubscriptions = ImmutableList.of( - RandomSubscriptionBuilder.builder().withEventTypes(ImmutableSet.of("et1", "et2")).build(), - RandomSubscriptionBuilder.builder().withEventTypes(ImmutableSet.of("et1", "et2", "et3")).build(), - RandomSubscriptionBuilder.builder().withEventTypes(ImmutableSet.of("et1")).build(), - RandomSubscriptionBuilder.builder().withEventTypes(ImmutableSet.of("et2")).build(), - RandomSubscriptionBuilder.builder().withEventTypes(ImmutableSet.of("et3", "et4", "et5")).build()); + RandomSubscriptionBuilder.builder().withEventTypes(ImmutableSet.of(et1, et2)).build(), + RandomSubscriptionBuilder.builder().withEventTypes(ImmutableSet.of(et1, et2, et3)).build(), + RandomSubscriptionBuilder.builder().withEventTypes(ImmutableSet.of(et1)).build(), + RandomSubscriptionBuilder.builder().withEventTypes(ImmutableSet.of(et2)).build(), + RandomSubscriptionBuilder.builder().withEventTypes(ImmutableSet.of(et3, et4, et5)).build()); testSubscriptions.forEach(this::insertSubscriptionToDB); final List expectedSubscriptions = testSubscriptions.stream() - .filter(sub -> sub.getEventTypes().containsAll(ImmutableSet.of("et1", "et2"))) + .filter(sub -> sub.getEventTypes().containsAll(ImmutableSet.of(et1, et2))) .sorted(SUBSCRIPTION_CREATION_DATE_DESC_COMPARATOR) .collect(toList()); - final List subscriptions = repository.listSubscriptions(ImmutableSet.of("et1", "et2"), + final List subscriptions = repository.listSubscriptions(ImmutableSet.of(et1, et2), Optional.empty(), 0, 10); assertThat(subscriptions, equalTo(expectedSubscriptions)); } @Test public void whenListSubscriptionsLimitAndOffsetAreRespected() throws ServiceUnavailableException { - final List testSubscriptions = createRandomSubscriptions(10); + final String owningApp = TestUtils.randomUUID(); + final List testSubscriptions = createRandomSubscriptions(10, owningApp); testSubscriptions.forEach(this::insertSubscriptionToDB); testSubscriptions.sort(SUBSCRIPTION_CREATION_DATE_DESC_COMPARATOR); testSubscriptions.subList(0, 2).clear(); testSubscriptions.subList(3, testSubscriptions.size()).clear(); - final List subscriptions = repository.listSubscriptions(emptySet(), Optional.empty(), 2, 3); + final List subscriptions = repository.listSubscriptions(emptySet(), Optional.of(owningApp), 2, 3); assertThat(subscriptions, equalTo(testSubscriptions)); } @@ -199,7 +193,7 @@ private void insertSubscriptionToDB(final Subscription subscription) { template.update("INSERT INTO zn_data.subscription (s_id, s_subscription_object, s_key_fields_hash) " + "VALUES (?, ?::JSONB, ?)", subscription.getId(), - mapper.writer().writeValueAsString(subscription), + TestUtils.OBJECT_MAPPER.writer().writeValueAsString(subscription), hashGenerator.generateSubscriptionKeyFieldsHash(subscription)); } catch (final Exception e) { throw new RuntimeException(e); diff --git a/src/acceptance-test/java/org/zalando/nakadi/repository/db/TimelineDbRepositoryTest.java b/src/acceptance-test/java/org/zalando/nakadi/repository/db/TimelineDbRepositoryTest.java index 0fe49a677a..015012f944 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/repository/db/TimelineDbRepositoryTest.java +++ b/src/acceptance-test/java/org/zalando/nakadi/repository/db/TimelineDbRepositoryTest.java @@ -1,6 +1,11 @@ package org.zalando.nakadi.repository.db; -import com.google.common.collect.ImmutableList; +import java.util.Date; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Collectors; +import java.util.stream.LongStream; import org.joda.time.DateTime; import org.junit.Assert; import org.junit.Before; @@ -10,13 +15,7 @@ import org.zalando.nakadi.domain.Timeline; import org.zalando.nakadi.exceptions.runtime.DuplicatedTimelineException; import org.zalando.nakadi.utils.TestUtils; - -import java.util.Date; -import java.util.List; -import java.util.Optional; -import java.util.UUID; -import java.util.stream.Collectors; -import java.util.stream.LongStream; +import static org.zalando.nakadi.utils.TestUtils.randomUUID; public class TimelineDbRepositoryTest extends AbstractDbRepositoryTest { @@ -24,19 +23,15 @@ public class TimelineDbRepositoryTest extends AbstractDbRepositoryTest { private EventType testEt; private Storage storage; - public TimelineDbRepositoryTest() { - super("zn_data.timeline", "zn_data.storage", "zn_data.event_type_schema", "zn_data.event_type"); - } - @Before public void setUp() throws Exception { super.setUp(); - this.tRepository = new TimelineDbRepository(template, mapper); - final StorageDbRepository sRepository = new StorageDbRepository(template, mapper); - final EventTypeDbRepository eRepository = new EventTypeDbRepository(template, mapper); + this.tRepository = new TimelineDbRepository(template, TestUtils.OBJECT_MAPPER); + final StorageDbRepository sRepository = new StorageDbRepository(template, TestUtils.OBJECT_MAPPER); + final EventTypeDbRepository eRepository = new EventTypeDbRepository(template, TestUtils.OBJECT_MAPPER); storage = sRepository.createStorage( - StorageDbRepositoryTest.createStorage("default", "localhost", 8181, "test", "path")); + StorageDbRepositoryTest.createStorage(randomUUID(), "localhost", 8181, "test", "path")); testEt = eRepository.saveEventType(TestUtils.buildDefaultEventType()); } @@ -102,17 +97,21 @@ public void testGetExpiredTimelines() { final DateTime yesterday = now.minusDays(1); final DateTime twoDaysAgo = now.minusDays(2); - insertTimeline(tomorrow.toDate(), true, 0); - insertTimeline(tomorrow.toDate(), false, 1); - insertTimeline(yesterday.toDate(), true, 2); + final Timeline t1 = insertTimeline(tomorrow.toDate(), true, 0); + final Timeline t2 = insertTimeline(tomorrow.toDate(), false, 1); + final Timeline t3 = insertTimeline(yesterday.toDate(), true, 2); final Timeline t4 = insertTimeline(yesterday.toDate(), false, 3); final Timeline t5 = insertTimeline(twoDaysAgo.toDate(), false, 4); - insertTimeline(null, false, 5); + final Timeline t6 = insertTimeline(null, false, 5); final List expiredTimelines = tRepository.getExpiredTimelines(); - Assert.assertEquals(2, expiredTimelines.size()); - Assert.assertEquals(ImmutableList.of(t4, t5), expiredTimelines); + Assert.assertFalse(expiredTimelines.contains(t1)); + Assert.assertFalse(expiredTimelines.contains(t2)); + Assert.assertFalse(expiredTimelines.contains(t3)); + Assert.assertTrue(expiredTimelines.contains(t4)); + Assert.assertTrue(expiredTimelines.contains(t5)); + Assert.assertFalse(expiredTimelines.contains(t6)); } private Timeline insertTimeline(final int order) { diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java index 21269b6933..89f5274ab2 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java @@ -48,7 +48,8 @@ public abstract class BaseAT { static { RestAssured.port = PORT; RestAssured.defaultParser = Parser.JSON; - RestAssured.config().getHttpClientConfig().setParam(CoreConnectionPNames.SO_TIMEOUT, TimeUnit.SECONDS.toMillis(3)); + RestAssured.config().getHttpClientConfig() + .setParam(CoreConnectionPNames.SO_TIMEOUT, TimeUnit.SECONDS.toMillis(3)); RestAssured.config().getHttpClientConfig().setParam(CoreConnectionPNames.CONNECTION_TIMEOUT, 100); } diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/timelines/SubscriptionConsumptionTest.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/timelines/SubscriptionConsumptionTest.java index 50164483f9..320cac5c67 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/timelines/SubscriptionConsumptionTest.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/timelines/SubscriptionConsumptionTest.java @@ -8,7 +8,6 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import java.util.stream.Collectors; -import java.util.stream.IntStream; import java.util.stream.Stream; import org.junit.Assert; import org.junit.BeforeClass; @@ -24,7 +23,7 @@ import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.createTimeline; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.deleteTimeline; -import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.publishEvent; +import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.publishEvents; import org.zalando.nakadi.webservice.utils.TestStreamingClient; public class SubscriptionConsumptionTest { @@ -42,13 +41,13 @@ public static void setupEventTypeWithEvents() throws IOException, InterruptedExc final AtomicReference inTimeCursors = new AtomicReference<>(); createParallelConsumer(subscription, 8, finished, inTimeCursors::set); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); createTimeline(eventType.getName()); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); createTimeline(eventType.getName()); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); createTimeline(eventType.getName()); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); finished.await(); cursorsDuringPublish = inTimeCursors.get(); @@ -63,11 +62,11 @@ public void testTimelineDelete() throws IOException, InterruptedException { final CountDownLatch finished = new CountDownLatch(1); final AtomicReference inTimelineCursors = new AtomicReference<>(); createParallelConsumer(subscription, 6, finished, inTimelineCursors::set); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); createTimeline(eventType.getName()); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); deleteTimeline(eventType.getName()); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); finished.await(); Assert.assertArrayEquals( new String[]{ @@ -89,14 +88,14 @@ public void test2TimelinesInaRow() throws IOException, InterruptedException { final CountDownLatch finished = new CountDownLatch(1); final AtomicReference inTimelineCursors = new AtomicReference<>(); createParallelConsumer(subscription, 5, finished, inTimelineCursors::set); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); createTimeline(eventType.getName()); // Still old topic createTimeline(eventType.getName()); // New topic createTimeline(eventType.getName()); // Another new topic - IntStream.range(0, 1).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 1, i ->"{\"foo\":\"bar\"}"); createTimeline(eventType.getName()); createTimeline(eventType.getName()); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); finished.await(); Assert.assertArrayEquals( new String[]{ @@ -140,7 +139,7 @@ public void test2TimelinesInaRowNoBegin() throws IOException, InterruptedExcepti createTimeline(eventType.getName()); // Still old topic createTimeline(eventType.getName()); // New topic createTimeline(eventType.getName()); // Another new topic - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); finished.await(); Assert.assertArrayEquals( new String[]{ diff --git a/src/test/java/org/zalando/nakadi/utils/TestUtils.java b/src/test/java/org/zalando/nakadi/utils/TestUtils.java index c17f3f8700..3fb879bba5 100644 --- a/src/test/java/org/zalando/nakadi/utils/TestUtils.java +++ b/src/test/java/org/zalando/nakadi/utils/TestUtils.java @@ -202,12 +202,16 @@ public static DateTime randomDate() { return new DateTime(randomMillis, DateTimeZone.UTC); } - public static List createRandomSubscriptions(final int count) { + public static List createRandomSubscriptions(final int count, final String owningApp) { return range(0, count) - .mapToObj(i -> builder().build()) + .mapToObj(i -> builder().withOwningApplication(owningApp).build()) .collect(toList()); } + public static List createRandomSubscriptions(final int count) { + return createRandomSubscriptions(count, randomTextString()); + } + public static Timeline buildTimeline(final String etName) { return new Timeline(etName, 0, new Storage(), randomUUID(), new Date()); } From 6b2b3f4536c784f9b3b7f94ab136daa5ed893bd0 Mon Sep 17 00:00:00 2001 From: antban Date: Wed, 12 Jul 2017 15:01:09 +0200 Subject: [PATCH 09/21] ARUHA-882 Avoid KAFKA-2948 during tests, do not log full consumer configuration on each call --- .../nakadi/webservice/UserJourneyAT.java | 53 +++++++++---------- src/main/resources/log4j.properties | 2 + 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/UserJourneyAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/UserJourneyAT.java index a6187150b1..24543fe283 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/UserJourneyAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/UserJourneyAT.java @@ -6,46 +6,43 @@ import com.google.common.collect.ImmutableMap; import com.google.common.io.Resources; import com.jayway.restassured.RestAssured; +import static com.jayway.restassured.http.ContentType.JSON; import com.jayway.restassured.response.Header; import com.jayway.restassured.specification.RequestSpecification; -import org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy; -import org.hamcrest.Matchers; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.zalando.nakadi.config.JsonConfig; -import org.zalando.nakadi.domain.EventType; -import org.zalando.nakadi.domain.StreamMetadata; -import org.zalando.nakadi.domain.Subscription; -import org.zalando.nakadi.domain.SubscriptionBase; -import org.zalando.nakadi.repository.kafka.KafkaCursor; -import org.zalando.nakadi.utils.RandomSubscriptionBuilder; -import org.zalando.nakadi.view.SubscriptionCursor; -import org.zalando.nakadi.webservice.hila.StreamBatch; -import org.zalando.nakadi.webservice.utils.TestStreamingClient; - import java.io.IOException; import java.util.List; import java.util.stream.Collectors; - -import static com.jayway.restassured.http.ContentType.JSON; import static java.util.stream.IntStream.rangeClosed; +import org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy; import static org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry; import static org.hamcrest.MatcherAssert.assertThat; +import org.hamcrest.Matchers; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.core.IsEqual.equalTo; +import org.junit.Before; +import org.junit.Test; import static org.springframework.http.HttpStatus.CREATED; import static org.springframework.http.HttpStatus.NOT_FOUND; import static org.springframework.http.HttpStatus.NO_CONTENT; import static org.springframework.http.HttpStatus.OK; +import org.zalando.nakadi.config.JsonConfig; +import org.zalando.nakadi.domain.EventType; +import org.zalando.nakadi.domain.StreamMetadata; +import org.zalando.nakadi.domain.Subscription; +import org.zalando.nakadi.domain.SubscriptionBase; import static org.zalando.nakadi.domain.SubscriptionBase.InitialPosition.BEGIN; +import org.zalando.nakadi.repository.kafka.KafkaCursor; +import org.zalando.nakadi.utils.RandomSubscriptionBuilder; import static org.zalando.nakadi.utils.TestUtils.randomTextString; import static org.zalando.nakadi.utils.TestUtils.randomValidEventTypeName; import static org.zalando.nakadi.utils.TestUtils.waitFor; +import org.zalando.nakadi.view.SubscriptionCursor; +import org.zalando.nakadi.webservice.hila.StreamBatch; import static org.zalando.nakadi.webservice.hila.StreamBatch.MatcherIgnoringToken.equalToBatchIgnoringToken; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.commitCursors; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription; +import org.zalando.nakadi.webservice.utils.TestStreamingClient; public class UserJourneyAT extends RealEnvironmentAT { @@ -64,13 +61,12 @@ public void before() throws IOException { eventTypeName = randomValidEventTypeName(); eventTypeBody = getEventTypeJsonFromFile("sample-event-type.json", eventTypeName, owningApp); eventTypeBodyUpdate = getEventTypeJsonFromFile("sample-event-type-update.json", eventTypeName, owningApp); + createEventType(); } @SuppressWarnings("unchecked") @Test(timeout = 15000) public void userJourneyM1() throws InterruptedException, IOException { - // create event-type - createEventType(); // get event type jsonRequestSpec() @@ -195,6 +191,13 @@ public void userJourneyM1() throws InterruptedException, IOException { .body("oldest_available_offset[0]", equalTo("000000000000000000")) .body("unconsumed_events[0]", equalTo(1)); + } + + @Test(timeout = 3000) + public void testEventTypeDeletion() { + // The reason for separating this test is https://issues.apache.org/jira/browse/KAFKA-2948 + // If producer was used to publish an event, than it is impossible to delete this event type anymore, cause + // producer will not clean up metadata cache, trying to log that everything is very bad. // delete event type jsonRequestSpec() .when() @@ -225,11 +228,8 @@ private String getUpdateEventType() throws IOException { @Test(timeout = 15000) public void userJourneyHila() throws InterruptedException, IOException { - // create event-type and push some events - createEventType(); postEvents(rangeClosed(0, 3) - .boxed() - .map(x -> "{\"foo\":\"bar" + x + "\"}") + .mapToObj(x -> "{\"foo\":\"bar" + x + "\"}") .collect(Collectors.toList()) .toArray(new String[4])); @@ -304,11 +304,6 @@ public void userJourneyHila() throws InterruptedException, IOException { .statusCode(NO_CONTENT.value()); } - @After - public void after() { - jsonRequestSpec().delete("/event-types/" + eventTypeName); - } - private void createEventType() { jsonRequestSpec() .body(eventTypeBody) diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties index 49dc72a108..fabe775e3f 100644 --- a/src/main/resources/log4j.properties +++ b/src/main/resources/log4j.properties @@ -18,3 +18,5 @@ log4j.category.org.hibernate.validator.internal.util.Version=WARN log4j.category.org.springframework.boot.actuate.autoconfigure.CrshAutoConfiguration=WARN log4j.category.org.springframework.boot.actuate.endpoint.jmx=WARN log4j.category.org.thymeleaf=WARN +# Do not print consumer configuration on each consumer creation +log4j.category.org.apache.kafka.clients.consumer.ConsumerConfig=WARN \ No newline at end of file From cde01c8dd936f98c53b93deadf06adfc9a271f79 Mon Sep 17 00:00:00 2001 From: antban Date: Thu, 13 Jul 2017 08:59:05 +0200 Subject: [PATCH 10/21] ARUHA-882 Do not delete event types where there was a publish because of kafka bug For more info: https://issues.apache.org/jira/browse/KAFKA-2948 --- .../nakadi/webservice/CursorOperationsAT.java | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/CursorOperationsAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/CursorOperationsAT.java index 7b1e150e9b..1d1ec3f7ff 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/CursorOperationsAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/CursorOperationsAT.java @@ -3,18 +3,15 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.jayway.restassured.RestAssured; import com.jayway.restassured.http.ContentType; -import org.junit.After; +import static org.hamcrest.core.IsEqual.equalTo; import org.junit.Before; import org.junit.Test; -import org.zalando.nakadi.domain.EventType; -import org.zalando.nakadi.utils.EventTypeTestBuilder; -import org.zalando.nakadi.webservice.utils.NakadiTestUtils; - -import static org.hamcrest.core.IsEqual.equalTo; import static org.springframework.http.HttpStatus.OK; import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY; +import org.zalando.nakadi.domain.EventType; +import org.zalando.nakadi.utils.EventTypeTestBuilder; import static org.zalando.nakadi.utils.TestUtils.randomTextString; -import static org.zalando.nakadi.webservice.BaseAT.TIMELINE_REPOSITORY; +import org.zalando.nakadi.webservice.utils.NakadiTestUtils; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.postEvents; public class CursorOperationsAT { @@ -27,13 +24,6 @@ public void setUp() throws JsonProcessingException { NakadiTestUtils.createEventTypeInNakadi(eventType); } - @After - public void tearDown() { - TIMELINE_REPOSITORY.listTimelinesOrdered(eventType.getName()).stream() - .forEach(timeline -> TIMELINE_REPOSITORY.deleteTimeline(timeline.getId())); - RestAssured.given().delete("/event-types/{name}", eventType.getName()); - } - @Test public void calculateCursorLag() { RestAssured.given() From 507ed50ebe0fb8f97885726b9bc921f672a43e55 Mon Sep 17 00:00:00 2001 From: antban Date: Thu, 13 Jul 2017 13:16:31 +0200 Subject: [PATCH 11/21] ARUHA-882 Get rid of generic event type/topic, each test should be autonomus in some way --- docker-compose.yml | 2 - .../org/zalando/nakadi/webservice/BaseAT.java | 27 ++-- .../webservice/BlockEventPublishingAT.java | 30 +++-- .../webservice/EventStreamReadingAT.java | 126 ++++++++++-------- .../nakadi/webservice/EventTypeAT.java | 66 +++++---- .../webservice/PartitionsControllerAT.java | 58 ++++---- .../webservice/utils/NakadiTestUtils.java | 2 +- .../nakadi/domain/EventTypeStatistics.java | 10 ++ 8 files changed, 181 insertions(+), 140 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index da8df13393..636ca6e497 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -50,8 +50,6 @@ services: KAFKA_ZOOKEEPER_CONNECT: localhost:2181 KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'false' KAFKA_DELETE_TOPIC_ENABLE: 'true' - KAFKA_CREATE_TOPICS: "test-topic:8:1" - KAFKA_NUM_PARTITIONS: 8 KAFKA_BROKER_ID: 0 volumes: - /var/run/docker.sock:/var/run/docker.sock \ No newline at end of file diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java index 89f5274ab2..c95b64170f 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java @@ -10,14 +10,10 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.zalando.nakadi.config.JsonConfig; -import org.zalando.nakadi.domain.EventType; import org.zalando.nakadi.domain.Storage; -import org.zalando.nakadi.exceptions.NoSuchEventTypeException; import org.zalando.nakadi.repository.db.EventTypeDbRepository; import org.zalando.nakadi.repository.db.StorageDbRepository; import org.zalando.nakadi.repository.db.TimelineDbRepository; -import org.zalando.nakadi.utils.EventTypeTestBuilder; -import static org.zalando.nakadi.utils.TestUtils.buildDefaultEventType; public abstract class BaseAT { @@ -31,13 +27,6 @@ public abstract class BaseAT { protected static final String ZOOKEEPER_URL = "localhost:2181"; protected static final String KAFKA_URL = "localhost:9092"; - protected static final String EVENT_TYPE_NAME = "test-event-type-name"; - protected static final String TEST_TOPIC = "test-topic"; - protected static final EventType EVENT_TYPE = EventTypeTestBuilder.builder() - .name(EVENT_TYPE_NAME) - .topic(TEST_TOPIC).build(); - protected static final int PARTITIONS_NUM = 8; - private static final JdbcTemplate JDBC_TEMPLATE = new JdbcTemplate( new DriverManagerDataSource(POSTGRES_URL, POSTGRES_USER, POSTGRES_PWD)); protected static final ObjectMapper MAPPER = (new JsonConfig()).jacksonObjectMapper(); @@ -55,14 +44,14 @@ public abstract class BaseAT { @BeforeClass public static void initDB() throws Exception { - try { - EVENT_TYPE_REPO.findByName(EVENT_TYPE_NAME); - } catch (final NoSuchEventTypeException e) { - final EventType eventType = buildDefaultEventType(); - eventType.setName(EVENT_TYPE_NAME); - eventType.setTopic(TEST_TOPIC); - EVENT_TYPE_REPO.saveEventType(eventType); - } +// try { +// EVENT_TYPE_REPO.findByName(EVENT_TYPE_NAME); +// } catch (final NoSuchEventTypeException e) { +// final EventType eventType = buildDefaultEventType(); +// eventType.setName(EVENT_TYPE_NAME); +// eventType.setTopic(TEST_TOPIC); +// EVENT_TYPE_REPO.saveEventType(eventType); +// } final Optional defaultStorage = STORAGE_DB_REPOSITORY.getStorage("default"); if (!defaultStorage.isPresent()) { diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/BlockEventPublishingAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/BlockEventPublishingAT.java index f5d1621cb6..b010de11b8 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/BlockEventPublishingAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/BlockEventPublishingAT.java @@ -1,37 +1,47 @@ package org.zalando.nakadi.webservice; +import com.fasterxml.jackson.core.JsonProcessingException; +import static com.jayway.restassured.RestAssured.given; import com.jayway.restassured.http.ContentType; import com.jayway.restassured.response.Response; +import java.io.IOException; +import java.text.MessageFormat; import org.apache.http.HttpStatus; import org.hamcrest.Matchers; +import org.junit.Before; import org.junit.Test; import org.zalando.nakadi.domain.EventType; import org.zalando.nakadi.service.BlacklistService; - -import java.io.IOException; -import java.text.MessageFormat; - -import static com.jayway.restassured.RestAssured.given; +import org.zalando.nakadi.utils.EventTypeTestBuilder; import static org.zalando.nakadi.utils.TestUtils.waitFor; +import org.zalando.nakadi.webservice.utils.NakadiTestUtils; public class BlockEventPublishingAT extends BaseAT { + private EventType eventType; + + @Before + public void setUp() throws JsonProcessingException { + eventType = EventTypeTestBuilder.builder().build(); + NakadiTestUtils.createEventTypeInNakadi(eventType); + } + @Test public void whenPublishingToBlockedEventTypeThen403() throws IOException { - publishEvent(EVENT_TYPE) + publishEvent(eventType) .then() .statusCode(HttpStatus.SC_OK); - SettingsControllerAT.blacklist(EVENT_TYPE.getName(), BlacklistService.Type.PRODUCER_ET); + SettingsControllerAT.blacklist(eventType.getName(), BlacklistService.Type.PRODUCER_ET); - waitFor(() -> publishEvent(EVENT_TYPE) + waitFor(() -> publishEvent(eventType) .then() .statusCode(403) .body("detail", Matchers.equalTo("Application or event type is blocked"))); - SettingsControllerAT.whitelist(EVENT_TYPE.getName(), BlacklistService.Type.PRODUCER_ET); + SettingsControllerAT.whitelist(eventType.getName(), BlacklistService.Type.PRODUCER_ET); - waitFor(() -> publishEvent(EVENT_TYPE) + waitFor(() -> publishEvent(eventType) .then() .statusCode(HttpStatus.SC_OK)); } diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java index ac222cc39b..1dfd2a4b98 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java @@ -4,24 +4,13 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.Sets; +import static com.jayway.restassured.RestAssured.given; import com.jayway.restassured.response.Header; import com.jayway.restassured.response.Response; -import org.hamcrest.Matchers; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.zalando.nakadi.repository.kafka.KafkaTestHelper; -import org.zalando.nakadi.service.BlacklistService; -import org.zalando.nakadi.utils.TestUtils; -import org.zalando.nakadi.view.Cursor; -import org.zalando.nakadi.webservice.utils.NakadiTestUtils; - import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; +import static java.text.MessageFormat.format; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -33,42 +22,64 @@ import java.util.concurrent.TimeoutException; import java.util.function.Predicate; import java.util.stream.Collectors; - -import static com.jayway.restassured.RestAssured.given; -import static java.text.MessageFormat.format; import static java.util.stream.IntStream.range; +import org.hamcrest.Matchers; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.zalando.nakadi.domain.EventType; +import org.zalando.nakadi.domain.EventTypeStatistics; +import org.zalando.nakadi.exceptions.NoSuchEventTypeException; +import org.zalando.nakadi.repository.kafka.KafkaTestHelper; +import org.zalando.nakadi.service.BlacklistService; +import org.zalando.nakadi.utils.EventTypeTestBuilder; +import org.zalando.nakadi.utils.TestUtils; +import org.zalando.nakadi.view.Cursor; +import org.zalando.nakadi.webservice.utils.NakadiTestUtils; public class EventStreamReadingAT extends BaseAT { private static final String TEST_PARTITION = "0"; + private static final int PARTITIONS_NUM = 8; private static final String DUMMY_EVENT = "Dummy"; - private static final String STREAM_ENDPOINT = createStreamEndpointUrl(EVENT_TYPE_NAME); private static final String SEPARATOR = "\n"; + private static String streamEndpoint; + private static String topicName; + private static EventType eventType; + private final ObjectMapper jsonMapper = new ObjectMapper(); private KafkaTestHelper kafkaHelper; private String xNakadiCursors; private List initialCursors; private List kafkaInitialNextOffsets; + @BeforeClass + public static void setupClass() throws JsonProcessingException, NoSuchEventTypeException { + eventType = EventTypeTestBuilder.builder() + .defaultStatistic(new EventTypeStatistics(PARTITIONS_NUM, PARTITIONS_NUM)) + .build(); + NakadiTestUtils.createEventTypeInNakadi(eventType); + streamEndpoint = createStreamEndpointUrl(eventType.getName()); + topicName = BaseAT.EVENT_TYPE_REPO.findByName(eventType.getName()).getTopic(); + } + @Before public void setUp() throws InterruptedException, JsonProcessingException { kafkaHelper = new KafkaTestHelper(KAFKA_URL); - initialCursors = kafkaHelper.getOffsetsToReadFromLatest(TEST_TOPIC); - kafkaInitialNextOffsets = kafkaHelper.getNextOffsets(TEST_TOPIC); + initialCursors = kafkaHelper.getOffsetsToReadFromLatest(topicName); + kafkaInitialNextOffsets = kafkaHelper.getNextOffsets(topicName); xNakadiCursors = jsonMapper.writeValueAsString(initialCursors); } - @After - public void tearDown() throws Exception { - SettingsControllerAT.whitelist(EVENT_TYPE_NAME, BlacklistService.Type.CONSUMER_ET); - } - @Test(timeout = 10000) @SuppressWarnings("unchecked") public void whenPushFewEventsAndReadThenGetEventsInStream() @@ -77,7 +88,7 @@ public void whenPushFewEventsAndReadThenGetEventsInStream() // ARRANGE // // push events to one of the partitions final int eventsPushed = 2; - kafkaHelper.writeMultipleMessageToPartition(TEST_PARTITION, TEST_TOPIC, DUMMY_EVENT, eventsPushed); + kafkaHelper.writeMultipleMessageToPartition(TEST_PARTITION, topicName, DUMMY_EVENT, eventsPushed); // ACT // final Response response = readEvents(); @@ -118,7 +129,7 @@ public void whenAcceptEncodingGzipReceiveCompressedStream() // ARRANGE // // push events to one of the partitions final int eventsPushed = 2; - kafkaHelper.writeMultipleMessageToPartition(TEST_PARTITION, TEST_TOPIC, DUMMY_EVENT, eventsPushed); + kafkaHelper.writeMultipleMessageToPartition(TEST_PARTITION, topicName, DUMMY_EVENT, eventsPushed); // ACT // final Response response = given() @@ -128,7 +139,7 @@ public void whenAcceptEncodingGzipReceiveCompressedStream() .param("stream_timeout", "2") .param("batch_flush_timeout", "2") .when() - .get(STREAM_ENDPOINT); + .get(streamEndpoint); // ASSERT // response.then().statusCode(HttpStatus.OK.value()).header(HttpHeaders.TRANSFER_ENCODING, "chunked"); @@ -144,7 +155,7 @@ public void whenPushedAmountOfEventsMoreThanBatchSizeAndReadThenGetEventsInMulti // push events to one of the partitions so that they don't fit into one branch final int batchLimit = 5; final int eventsPushed = 8; - kafkaHelper.writeMultipleMessageToPartition(TEST_PARTITION, TEST_TOPIC, DUMMY_EVENT, eventsPushed); + kafkaHelper.writeMultipleMessageToPartition(TEST_PARTITION, topicName, DUMMY_EVENT, eventsPushed); // ACT // final Response response = given() @@ -153,7 +164,7 @@ public void whenPushedAmountOfEventsMoreThanBatchSizeAndReadThenGetEventsInMulti .param("stream_timeout", "2") .param("batch_flush_timeout", "2") .when() - .get(STREAM_ENDPOINT); + .get(streamEndpoint); // ASSERT // response.then().statusCode(HttpStatus.OK.value()).header(HttpHeaders.TRANSFER_ENCODING, "chunked"); @@ -198,7 +209,7 @@ public void whenReadFromTheEndThenLatestOffsetsInStream() .param("stream_timeout", "2") .param("batch_flush_timeout", "2") .when() - .get(STREAM_ENDPOINT); + .get(streamEndpoint); // ASSERT // response.then().statusCode(HttpStatus.OK.value()).header(HttpHeaders.TRANSFER_ENCODING, "chunked"); @@ -231,7 +242,7 @@ public void whenReachKeepAliveLimitThenStreamIsClosed() .param("batch_flush_timeout", "1") .param("stream_keep_alive_limit", keepAliveLimit) .when() - .get(STREAM_ENDPOINT); + .get(streamEndpoint); // ASSERT // response.then().statusCode(HttpStatus.OK.value()).header(HttpHeaders.TRANSFER_ENCODING, "chunked"); @@ -263,7 +274,7 @@ public void whenStreamLimitLowerThanBatchLimitThenUnprocessableEntity() { .param("batch_limit", "10") .param("stream_limit", "5") .when() - .get(STREAM_ENDPOINT) + .get(streamEndpoint) .then() .statusCode(HttpStatus.UNPROCESSABLE_ENTITY.value()) .and() @@ -278,7 +289,7 @@ public void whenStreamTimeoutLowerThanBatchTimeoutThenUnprocessableEntity() { .param("batch_timeout", "10") .param("stream_timeout", "5") .when() - .get(STREAM_ENDPOINT) + .get(streamEndpoint) .then() .statusCode(HttpStatus.UNPROCESSABLE_ENTITY.value()) .and() @@ -292,7 +303,7 @@ public void whenIncorrectCursorsFormatThenBadRequest() { given() .header(new Header("X-nakadi-cursors", "this_is_definitely_not_a_json")) .when() - .get(STREAM_ENDPOINT) + .get(streamEndpoint) .then() .statusCode(HttpStatus.BAD_REQUEST.value()) .and() @@ -306,7 +317,7 @@ public void whenInvalidCursorsThenPreconditionFailed() { given() .header(new Header("X-nakadi-cursors", "[{\"partition\":\"very_wrong_partition\",\"offset\":\"3\"}]")) .when() - .get(STREAM_ENDPOINT) + .get(streamEndpoint) .then() .statusCode(HttpStatus.PRECONDITION_FAILED.value()) .and() @@ -321,14 +332,16 @@ public void whenReadEventsForBlockedConsumerThen403() throws Exception { .then() .statusCode(HttpStatus.OK.value()); - SettingsControllerAT.blacklist(EVENT_TYPE_NAME, BlacklistService.Type.CONSUMER_ET); - - TestUtils.waitFor(() -> readEvents() - .then() - .statusCode(403) - .body("detail", Matchers.equalTo("Application or event type is blocked")), 1000, 200); + SettingsControllerAT.blacklist(eventType.getName(), BlacklistService.Type.CONSUMER_ET); + try { + TestUtils.waitFor(() -> readEvents() + .then() + .statusCode(403) + .body("detail", Matchers.equalTo("Application or event type is blocked")), 1000, 200); + } finally { + SettingsControllerAT.whitelist(eventType.getName(), BlacklistService.Type.CONSUMER_ET); + } - SettingsControllerAT.whitelist(EVENT_TYPE_NAME, BlacklistService.Type.CONSUMER_ET); readEvents() .then() .statusCode(HttpStatus.OK.value()); @@ -341,7 +354,7 @@ private Response readEvents() { .param("stream_timeout", "2") .param("batch_flush_timeout", "2") .when() - .get(STREAM_ENDPOINT); + .get(streamEndpoint); } @Ignore @@ -422,23 +435,24 @@ public void whenReadEventsConsumerIsBlocked() throws Exception { new Thread(() -> { try { Thread.sleep(3000); - SettingsControllerAT.blacklist(EVENT_TYPE_NAME, BlacklistService.Type.CONSUMER_ET); + SettingsControllerAT.blacklist(eventType.getName(), BlacklistService.Type.CONSUMER_ET); } catch (final Exception e) { e.printStackTrace(); } }).start(); - - // read events from the stream until we are blocked otherwise TestTimedOutException will be thrown and test - // is considered to be failed - given() - .header(new Header("X-nakadi-cursors", xNakadiCursors)) - .param("batch_limit", "1") - .param("stream_timeout", "60") - .param("batch_flush_timeout", "10") - .when() - .get(STREAM_ENDPOINT); - - SettingsControllerAT.whitelist(EVENT_TYPE_NAME, BlacklistService.Type.CONSUMER_ET); + try { + // read events from the stream until we are blocked otherwise TestTimedOutException will be thrown and test + // is considered to be failed + given() + .header(new Header("X-nakadi-cursors", xNakadiCursors)) + .param("batch_limit", "1") + .param("stream_timeout", "60") + .param("batch_flush_timeout", "10") + .when() + .get(streamEndpoint); + } finally { + SettingsControllerAT.whitelist(eventType.getName(), BlacklistService.Type.CONSUMER_ET); + } } diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/EventTypeAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/EventTypeAT.java index 9400708a74..e2ad7cbe82 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/EventTypeAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/EventTypeAT.java @@ -1,17 +1,32 @@ package org.zalando.nakadi.webservice; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableList; +import static com.jayway.restassured.RestAssured.given; +import static com.jayway.restassured.RestAssured.when; +import static com.jayway.restassured.http.ContentType.JSON; +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import org.apache.http.HttpStatus; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.core.IsEqual.equalTo; import org.joda.time.DateTime; import org.json.JSONObject; import org.junit.After; import org.junit.Assert; +import static org.junit.Assert.assertEquals; import org.junit.Test; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; -import org.zalando.nakadi.config.JsonConfig; import org.zalando.nakadi.domain.EventType; import org.zalando.nakadi.domain.EventTypeAuthorization; import org.zalando.nakadi.domain.EventTypeAuthorizationAttribute; @@ -20,48 +35,41 @@ import org.zalando.nakadi.partitioning.PartitionStrategy; import org.zalando.nakadi.repository.kafka.KafkaTestHelper; import org.zalando.nakadi.utils.EventTypeTestBuilder; -import org.zalando.nakadi.webservice.utils.NakadiTestUtils; -import org.zalando.problem.MoreStatus; -import org.zalando.problem.Problem; - -import java.io.IOException; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -import static com.jayway.restassured.RestAssured.given; -import static com.jayway.restassured.RestAssured.when; -import static com.jayway.restassured.http.ContentType.JSON; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.assertEquals; import static org.zalando.nakadi.utils.TestUtils.buildDefaultEventType; import static org.zalando.nakadi.utils.TestUtils.resourceAsString; import static org.zalando.nakadi.utils.TestUtils.waitFor; +import org.zalando.nakadi.webservice.utils.NakadiTestUtils; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.publishEvent; +import org.zalando.problem.MoreStatus; +import org.zalando.problem.Problem; public class EventTypeAT extends BaseAT { private static final String ENDPOINT = "/event-types"; - private static final ObjectMapper MAPPER = (new JsonConfig()).jacksonObjectMapper(); @Test public void whenGETThenListsEventTypes() throws JsonProcessingException { final EventType eventType = buildDefaultEventType(); final String body = MAPPER.writer().writeValueAsString(eventType); - given().body(body).header("accept", "application/json").contentType(JSON).post(ENDPOINT).then().statusCode( - HttpStatus.SC_CREATED); + given() + .body(body) + .header("accept", "application/json") + .contentType(JSON) + .post(ENDPOINT) + .then() + .statusCode(HttpStatus.SC_CREATED); - given().header("accept", "application/json").contentType(JSON).when().get(ENDPOINT).then() - .statusCode(HttpStatus.SC_OK).body("size()", equalTo(1)).body("name[0]", equalTo(eventType.getName())); + // TODO: Do not depend on order of the tests (last event type created is not the one on previous line) + // TODO: 2 body matchers in a row are not working correctly (size() here is much more than > 1) + given() + .header("accept", "application/json") + .contentType(JSON) + .get(ENDPOINT) + .then() + .statusCode(HttpStatus.SC_OK) + .body("size()", equalTo(1)) + .body("name[0]", equalTo(eventType.getName())); } @Test diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/PartitionsControllerAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/PartitionsControllerAT.java index 90bf290e93..b9de85cf5e 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/PartitionsControllerAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/PartitionsControllerAT.java @@ -1,39 +1,51 @@ package org.zalando.nakadi.webservice; +import com.fasterxml.jackson.core.JsonProcessingException; +import static com.jayway.restassured.RestAssured.get; +import static com.jayway.restassured.RestAssured.when; import com.jayway.restassured.response.Response; -import org.apache.kafka.clients.producer.KafkaProducer; -import org.apache.kafka.clients.producer.ProducerRecord; -import org.apache.kafka.common.PartitionInfo; -import org.hamcrest.Matchers; -import org.junit.Before; -import org.junit.Test; -import org.springframework.http.HttpStatus; -import org.zalando.nakadi.domain.EventType; -import org.zalando.nakadi.view.Cursor; -import org.zalando.nakadi.repository.kafka.KafkaTestHelper; - import java.io.IOException; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; -import org.zalando.nakadi.webservice.utils.NakadiTestUtils; - -import static com.jayway.restassured.RestAssured.get; -import static com.jayway.restassured.RestAssured.when; +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.common.PartitionInfo; import static org.hamcrest.MatcherAssert.assertThat; +import org.hamcrest.Matchers; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.http.HttpStatus; +import org.zalando.nakadi.domain.EventType; +import org.zalando.nakadi.exceptions.NoSuchEventTypeException; +import org.zalando.nakadi.repository.kafka.KafkaTestHelper; +import org.zalando.nakadi.utils.EventTypeTestBuilder; +import org.zalando.nakadi.view.Cursor; import static org.zalando.nakadi.webservice.utils.JsonTestHelper.asMap; import static org.zalando.nakadi.webservice.utils.JsonTestHelper.asMapsList; +import org.zalando.nakadi.webservice.utils.NakadiTestUtils; public class PartitionsControllerAT extends BaseAT { + private static String eventTypeName; + private static String topicName; private KafkaTestHelper kafkaHelper; private Map> actualTopics; + @BeforeClass + public static void setupClass() throws JsonProcessingException, NoSuchEventTypeException { + final EventType eventType = EventTypeTestBuilder.builder().build(); + NakadiTestUtils.createEventTypeInNakadi(eventType); + topicName = BaseAT.EVENT_TYPE_REPO.findByName(eventType.getName()).getTopic(); + eventTypeName = eventType.getName(); + } + @Before public void before() { kafkaHelper = new KafkaTestHelper(KAFKA_URL); @@ -43,7 +55,7 @@ public void before() { @Test public void whenListPartitionsThenOk() throws IOException { // ACT // - final Response response = when().get(String.format("/event-types/%s/partitions", EVENT_TYPE_NAME)); + final Response response = when().get(String.format("/event-types/%s/partitions", eventTypeName)); // ASSERT // response.then().statusCode(HttpStatus.OK.value()); @@ -56,7 +68,7 @@ public void whenListPartitionsThenOk() throws IOException { .map(map -> map.get("partition")) .collect(Collectors.toSet()); final Set actualPartitions = actualTopics - .get(TEST_TOPIC) + .get(topicName) .stream() .map(pInfo -> Integer.toString(pInfo.partition())) .collect(Collectors.toSet()); @@ -90,7 +102,7 @@ public void whenListPartitionsThenTopicNotFound() throws IOException { public void whenListPartitionsAndWriteMessageThenOffsetInPartitionIsIncreased() throws ExecutionException, InterruptedException, IOException { // ACT // - final String url = String.format("/event-types/%s/partitions", EVENT_TYPE_NAME); + final String url = String.format("/event-types/%s/partitions", eventTypeName); final List> partitionsInfoBefore = asMapsList(get(url).print()); writeMessageToPartition(0); @@ -105,7 +117,7 @@ public void whenListPartitionsAndWriteMessageThenOffsetInPartitionIsIncreased() @Test public void whenGetPartitionThenOk() throws IOException { // ACT // - final Response response = when().get(String.format("/event-types/%s/partitions/0", EVENT_TYPE_NAME)); + final Response response = when().get(String.format("/event-types/%s/partitions/0", eventTypeName)); // ASSERT // response.then().statusCode(HttpStatus.OK.value()); @@ -116,7 +128,7 @@ public void whenGetPartitionThenOk() throws IOException { public void whenGetPartitionWithConsumedOffsetThenOk() throws IOException { // ACT // final Response response = when().get(String.format("/event-types/%s/partitions/0?consumed_offset=BEGIN", - EVENT_TYPE_NAME)); + eventTypeName)); // ASSERT // response.then().statusCode(HttpStatus.OK.value()); @@ -136,7 +148,7 @@ public void whenGetPartitionThenTopicNotFound() throws IOException { @Test public void whenGetPartitionThenPartitionNotFound() throws IOException { when() - .get(String.format("/event-types/%s/partitions/43766", EVENT_TYPE_NAME)) + .get(String.format("/event-types/%s/partitions/43766", eventTypeName)) .then() .statusCode(HttpStatus.NOT_FOUND.value()) .and() @@ -147,7 +159,7 @@ public void whenGetPartitionThenPartitionNotFound() throws IOException { public void whenGetPartitionAndWriteMessageThenOffsetInPartitionIsIncreased() throws ExecutionException, InterruptedException, IOException { // ACT // - final String url = String.format("/event-types/%s/partitions/0", EVENT_TYPE_NAME); + final String url = String.format("/event-types/%s/partitions/0", eventTypeName); final Map partitionInfoBefore = asMap(get(url).print()); writeMessageToPartition(0); @@ -180,7 +192,7 @@ private Long getNewestOffsetAsLong(final Map partitionInfo) { private void writeMessageToPartition(final int partition) throws InterruptedException, ExecutionException { final KafkaProducer producer = kafkaHelper.createProducer(); - final ProducerRecord producerRecord = new ProducerRecord<>(TEST_TOPIC, partition, "blahKey", + final ProducerRecord producerRecord = new ProducerRecord<>(topicName, partition, "blahKey", "blahValue"); producer.send(producerRecord).get(); } diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/utils/NakadiTestUtils.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/utils/NakadiTestUtils.java index d75e4bf11d..cd502d3153 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/utils/NakadiTestUtils.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/utils/NakadiTestUtils.java @@ -53,7 +53,7 @@ public static void createEventTypeInNakadi(final EventType eventType) throws Jso given() .body(MAPPER.writeValueAsString(eventType)) .contentType(JSON) - .post("/event-types"); + .post("/event-types").print(); } public static void updateEventTypeInNakadi(final EventType eventType) throws JsonProcessingException { diff --git a/src/main/java/org/zalando/nakadi/domain/EventTypeStatistics.java b/src/main/java/org/zalando/nakadi/domain/EventTypeStatistics.java index fc0f83dfea..027d1aac5d 100644 --- a/src/main/java/org/zalando/nakadi/domain/EventTypeStatistics.java +++ b/src/main/java/org/zalando/nakadi/domain/EventTypeStatistics.java @@ -50,6 +50,16 @@ public void setWriteParallelism(final Integer writeParallelism) { this.writeParallelism = writeParallelism; } + public EventTypeStatistics() { + } + + public EventTypeStatistics(final int readParallelism, final int writeParallelism) { + this.readParallelism = readParallelism; + this.writeParallelism = writeParallelism; + this.messageSize = 1; + this.messagesPerMinute = 1; + } + @Override public boolean equals(final Object o) { if (this == o) { From a590c9deb98c39976c64317eec9cdd1ab0db2564 Mon Sep 17 00:00:00 2001 From: antban Date: Thu, 13 Jul 2017 13:20:06 +0200 Subject: [PATCH 12/21] ARUHA-882 Optimistic default storage creation --- .../org/zalando/nakadi/webservice/BaseAT.java | 24 ++++++------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java index c95b64170f..5a5267aac9 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java @@ -3,7 +3,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.jayway.restassured.RestAssured; import com.jayway.restassured.parsing.Parser; -import java.util.Optional; import java.util.concurrent.TimeUnit; import org.apache.http.params.CoreConnectionPNames; import org.junit.BeforeClass; @@ -43,23 +42,14 @@ public abstract class BaseAT { } @BeforeClass - public static void initDB() throws Exception { -// try { -// EVENT_TYPE_REPO.findByName(EVENT_TYPE_NAME); -// } catch (final NoSuchEventTypeException e) { -// final EventType eventType = buildDefaultEventType(); -// eventType.setName(EVENT_TYPE_NAME); -// eventType.setTopic(TEST_TOPIC); -// EVENT_TYPE_REPO.saveEventType(eventType); -// } - - final Optional defaultStorage = STORAGE_DB_REPOSITORY.getStorage("default"); - if (!defaultStorage.isPresent()) { - final Storage storage = new Storage(); - storage.setId("default"); - storage.setType(Storage.Type.KAFKA); - storage.setConfiguration(new Storage.KafkaConfiguration(null, null, ZOOKEEPER_URL, "")); + public static void createDefaultStorage() { + final Storage storage = new Storage(); + storage.setId("default"); + storage.setType(Storage.Type.KAFKA); + storage.setConfiguration(new Storage.KafkaConfiguration(null, null, ZOOKEEPER_URL, "")); + try { STORAGE_DB_REPOSITORY.createStorage(storage); + } catch (final Exception ignore) { } } } From 873130675bfcc5ba6a41689ad26b764162f4fefa Mon Sep 17 00:00:00 2001 From: antban Date: Thu, 13 Jul 2017 17:30:41 +0200 Subject: [PATCH 13/21] ARUHA-882 Increase tests stability --- .../webservice/EventStreamReadingAT.java | 4 +- .../nakadi/webservice/EventTypeAT.java | 30 ++------ .../webservice/SettingsControllerAT.java | 68 ++++++------------- .../timelines/TimelineConsumptionTest.java | 42 ++++++------ 4 files changed, 48 insertions(+), 96 deletions(-) diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java index 1dfd2a4b98..df461530c4 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java @@ -326,7 +326,7 @@ public void whenInvalidCursorsThenPreconditionFailed() { .body("detail", equalTo("non existing partition very_wrong_partition")); } - @Test(timeout = 10000) + @Test(timeout = 30000) public void whenReadEventsForBlockedConsumerThen403() throws Exception { readEvents() .then() @@ -429,7 +429,7 @@ private CompletableFuture createConsumingConnection(final Str return future; } - @Test(timeout = 10000) + @Test(timeout = 30000) public void whenReadEventsConsumerIsBlocked() throws Exception { // blocking streaming client after 3 seconds new Thread(() -> { diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/EventTypeAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/EventTypeAT.java index e2ad7cbe82..8a00309bab 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/EventTypeAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/EventTypeAT.java @@ -17,16 +17,14 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.not; import static org.hamcrest.core.IsEqual.equalTo; import org.joda.time.DateTime; import org.json.JSONObject; -import org.junit.After; import org.junit.Assert; import static org.junit.Assert.assertEquals; import org.junit.Test; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.zalando.nakadi.domain.EventType; import org.zalando.nakadi.domain.EventTypeAuthorization; import org.zalando.nakadi.domain.EventTypeAuthorizationAttribute; @@ -60,16 +58,13 @@ public void whenGETThenListsEventTypes() throws JsonProcessingException { .then() .statusCode(HttpStatus.SC_CREATED); - // TODO: Do not depend on order of the tests (last event type created is not the one on previous line) - // TODO: 2 body matchers in a row are not working correctly (size() here is much more than > 1) given() .header("accept", "application/json") .contentType(JSON) .get(ENDPOINT) .then() .statusCode(HttpStatus.SC_OK) - .body("size()", equalTo(1)) - .body("name[0]", equalTo(eventType.getName())); + .body("name", hasItems(eventType.getName())); } @Test @@ -301,20 +296,6 @@ private void assertRetentionTime(final Long checkingRetentionTime, final String KafkaTestHelper.getTopicRetentionTime(topic, ZOOKEEPER_URL)))); } - @After - public void tearDown() { - final DriverManagerDataSource datasource = new DriverManagerDataSource( - POSTGRES_URL, - POSTGRES_USER, - POSTGRES_PWD - ); - final JdbcTemplate template = new JdbcTemplate(datasource); - - template.execute("DELETE FROM zn_data.timeline"); - template.execute("DELETE FROM zn_data.event_type_schema"); - template.execute("DELETE FROM zn_data.event_type"); - } - private void postTimeline(final EventType eventType) { given().contentType(JSON) .body(new JSONObject().put("storage_id", "default")) @@ -327,8 +308,11 @@ private void checkEventTypeIsDeleted(final EventType eventType, final List allTopics = kafkaHelper.createConsumer().listTopics().keySet(); - topics.forEach(topic -> assertThat(allTopics, not(hasItem(topic)))); + // Kafka deletes topics asynchronously, so there may be a delay + waitFor(() -> { + final Set allTopics = kafkaHelper.createConsumer().listTopics().keySet(); + topics.forEach(topic -> assertThat(allTopics, not(hasItem(topic)))); + }, 10000); } private void postEventType(final EventType eventType) throws JsonProcessingException { diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/SettingsControllerAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/SettingsControllerAT.java index 06567b62aa..60df2b04d8 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/SettingsControllerAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/SettingsControllerAT.java @@ -2,46 +2,37 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.collect.ImmutableMap; +import static com.jayway.restassured.RestAssured.given; import com.jayway.restassured.http.ContentType; +import java.io.IOException; import org.apache.curator.framework.CuratorFramework; import org.apache.http.HttpStatus; -import org.junit.After; +import static org.hamcrest.Matchers.hasItems; import org.junit.Assert; import org.junit.Test; import org.zalando.nakadi.config.JsonConfig; import org.zalando.nakadi.domain.EventType; import org.zalando.nakadi.service.BlacklistService; -import org.zalando.nakadi.utils.JsonTestHelper; import org.zalando.nakadi.utils.TestUtils; import org.zalando.nakadi.webservice.utils.NakadiTestUtils; import org.zalando.nakadi.webservice.utils.ZookeeperTestUtils; -import uk.co.datumedge.hamcrest.json.SameJSONAs; - -import java.io.IOException; -import java.util.Collections; - -import static com.jayway.restassured.RestAssured.given; public class SettingsControllerAT extends BaseAT { private static final String BLACKLIST_URL = "/settings/blacklist"; private static final ObjectMapper MAPPER = (new JsonConfig()).jacksonObjectMapper(); - private static final JsonTestHelper JSON_HELPER = new JsonTestHelper(MAPPER); private static final CuratorFramework CURATOR = ZookeeperTestUtils.createCurator(ZOOKEEPER_URL); - @After - public void setUp() { - clearFloodersData(); - } - @Test public void testBlockFlooder() throws Exception { final EventType eventType = NakadiTestUtils.createEventType(); blacklist(eventType.getName(), BlacklistService.Type.CONSUMER_ET); - - Assert.assertNotNull(CURATOR.checkExists() - .forPath("/nakadi/blacklist/consumers/event_types/" + eventType.getName())); + try { + Assert.assertNotNull(CURATOR.checkExists() + .forPath("/nakadi/blacklist/consumers/event_types/" + eventType.getName())); + } finally { + whitelist(eventType.getName(), BlacklistService.Type.CONSUMER_ET); + } } @Test @@ -59,38 +50,17 @@ public void testUnblockFlooder() throws Exception { public void testGetFlooders() throws Exception { final EventType eventType = NakadiTestUtils.createEventType(); blacklist(eventType.getName(), BlacklistService.Type.CONSUMER_ET); - - TestUtils.waitFor(() -> given() - .contentType(ContentType.JSON) - .get(BLACKLIST_URL) - .then() - .statusCode(HttpStatus.SC_OK) - .content(getFloodersMatcher(eventType)), 1000, 200); - - } - - private SameJSONAs getFloodersMatcher(final EventType eventType) { - try { - return JSON_HELPER.matchesObject( - ImmutableMap.of( - "consumers", ImmutableMap.of( - "event_types", Collections.singleton(eventType.getName()), - "apps", Collections.emptySet()), - "producers", ImmutableMap.of( - "event_types", Collections.emptySet(), - "apps", Collections.emptySet())) - ); - } catch (JsonProcessingException e) { - throw new RuntimeException(); - } - } - - private void clearFloodersData() { try { - CURATOR.delete().deletingChildrenIfNeeded().forPath("/nakadi/blacklist/consumers"); - CURATOR.delete().deletingChildrenIfNeeded().forPath("/nakadi/blacklist/producers"); - } catch (final Exception exception) { - // nothing to do + TestUtils.waitFor( + () -> given() + .contentType(ContentType.JSON) + .get(BLACKLIST_URL) + .then() + .statusCode(HttpStatus.SC_OK) + .body("consumers.event_types", hasItems(eventType.getName())), + 1000, 200); + } finally { + whitelist(eventType.getName(), BlacklistService.Type.CONSUMER_ET); } } diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/timelines/TimelineConsumptionTest.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/timelines/TimelineConsumptionTest.java index 4051ad1455..bdbf881bde 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/timelines/TimelineConsumptionTest.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/timelines/TimelineConsumptionTest.java @@ -13,7 +13,6 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; -import java.util.stream.IntStream; import org.apache.http.HttpStatus; import org.junit.Assert; import org.junit.BeforeClass; @@ -22,7 +21,7 @@ import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.createTimeline; import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.deleteTimeline; -import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.publishEvent; +import static org.zalando.nakadi.webservice.utils.NakadiTestUtils.publishEvents; public class TimelineConsumptionTest { private static EventType eventType; @@ -34,16 +33,17 @@ public static void setupEventTypeWithEvents() throws JsonProcessingException, In final CountDownLatch finished = new CountDownLatch(1); final AtomicReference inTimeCursors = new AtomicReference<>(); createParallelConsumer(eventType.getName(), 8, finished, inTimeCursors::set); - - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + final long start = System.currentTimeMillis(); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); createTimeline(eventType.getName()); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); createTimeline(eventType.getName()); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); createTimeline(eventType.getName()); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); finished.await(); cursorsDuringPublish = inTimeCursors.get(); + System.out.println("Total took: " + (System.currentTimeMillis() - start)/1000); } private static void createParallelConsumer( @@ -55,8 +55,7 @@ private static void createParallelConsumer( new Thread(() -> { started.countDown(); try { - // Suppose that everything will take less then 30 seconds - inTimeCursors.accept(readCursors(eventTypeName, "BEGIN", expectedEvents, 30)); + inTimeCursors.accept(readCursors(eventTypeName, "BEGIN", expectedEvents)); } catch (Exception ex) { throw new RuntimeException(ex); } finally { @@ -72,11 +71,11 @@ public void testTimelineDelete() throws IOException, InterruptedException { final CountDownLatch finished = new CountDownLatch(1); final AtomicReference inTimelineCursors = new AtomicReference<>(); createParallelConsumer(eventType.getName(), 6, finished, inTimelineCursors::set); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); createTimeline(eventType.getName()); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); deleteTimeline(eventType.getName()); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); finished.await(); Assert.assertArrayEquals( new String[]{ @@ -96,14 +95,14 @@ public void test2TimelinesInaRow() throws IOException, InterruptedException { final CountDownLatch finished = new CountDownLatch(1); final AtomicReference inTimelineCursors = new AtomicReference<>(); createParallelConsumer(eventType.getName(), 5, finished, inTimelineCursors::set); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); createTimeline(eventType.getName()); // Still old topic createTimeline(eventType.getName()); // New topic createTimeline(eventType.getName()); // Another new topic - IntStream.range(0, 1).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 1, i -> "{\"foo\":\"bar\"}"); createTimeline(eventType.getName()); createTimeline(eventType.getName()); - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); finished.await(); Assert.assertArrayEquals( new String[]{ @@ -116,7 +115,7 @@ public void test2TimelinesInaRow() throws IOException, InterruptedException { inTimelineCursors.get() ); - final String[] receivedOffsets = readCursors(eventType.getName(), "BEGIN", 5, 2); + final String[] receivedOffsets = readCursors(eventType.getName(), "BEGIN", 5); Assert.assertArrayEquals( new String[]{ "001-0001-000000000000000000", @@ -138,7 +137,7 @@ public void test2TimelinesInaRowNoBegin() throws IOException, InterruptedExcepti createTimeline(eventType.getName()); // Still old topic createTimeline(eventType.getName()); // New topic createTimeline(eventType.getName()); // Another new topic - IntStream.range(0, 2).forEach(idx -> publishEvent(eventType.getName(), "{\"foo\":\"bar\"}")); + publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); finished.await(); Assert.assertArrayEquals( new String[]{ @@ -147,7 +146,7 @@ public void test2TimelinesInaRowNoBegin() throws IOException, InterruptedExcepti }, inTimelineCursors.get() ); - final String[] receivedOffsets = readCursors(eventType.getName(), "BEGIN", 2, 1); + final String[] receivedOffsets = readCursors(eventType.getName(), "BEGIN", 2); Assert.assertArrayEquals( new String[]{ "001-0003-000000000000000000", @@ -190,7 +189,7 @@ public void testAllEventsConsumed() throws IOException { // Do not test last case, because it makes no sense... for (int idx = -1; idx < expected.length - 1; ++idx) { final String[] receivedOffsets = readCursors(eventType.getName(), - idx == -1 ? "BEGIN" : expected[idx], expected.length - 1 - idx, 1); + idx == -1 ? "BEGIN" : expected[idx], expected.length - 1 - idx); final String[] testedOffsets = Arrays.copyOfRange(expected, idx + 1, expected.length); Assert.assertArrayEquals(testedOffsets, receivedOffsets); } @@ -212,15 +211,14 @@ public void testConsumptionFromErroredPositionBlocked() { } - private static String[] readCursors( - final String eventTypeName, final String startOffset, final int streamLimit, final int streamTimeout) + private static String[] readCursors(final String eventTypeName, final String startOffset, final int streamLimit) throws IOException { final Response response = given() .header(new Header("X-nakadi-cursors", "[{\"partition\": \"0\", \"offset\": \"" + startOffset + "\"}]")) .param("batch_limit", "1") .param("batch_flush_timeout", "1") .param("stream_limit", streamLimit) - .param("stream_timeout", streamTimeout) + .param("stream_timeout", 60) .when() .get("/event-types/" + eventTypeName + "/events"); From 06ad1091e4b6c46504f4ac2d0eb4d5a848794170 Mon Sep 17 00:00:00 2001 From: antban Date: Thu, 13 Jul 2017 17:45:47 +0200 Subject: [PATCH 14/21] ARUHA-882 Increase tests stability --- .../java/org/zalando/nakadi/webservice/BaseAT.java | 5 +++-- .../org/zalando/nakadi/webservice/EventStreamReadingAT.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java index 5a5267aac9..5205126488 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java @@ -37,8 +37,9 @@ public abstract class BaseAT { RestAssured.port = PORT; RestAssured.defaultParser = Parser.JSON; RestAssured.config().getHttpClientConfig() - .setParam(CoreConnectionPNames.SO_TIMEOUT, TimeUnit.SECONDS.toMillis(3)); - RestAssured.config().getHttpClientConfig().setParam(CoreConnectionPNames.CONNECTION_TIMEOUT, 100); + .setParam(CoreConnectionPNames.SO_TIMEOUT, TimeUnit.SECONDS.toMillis(5)); + RestAssured.config().getHttpClientConfig().setParam( + CoreConnectionPNames.CONNECTION_TIMEOUT, TimeUnit.SECONDS.toMillis(1)); } @BeforeClass diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java index df461530c4..43c0a44b48 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java @@ -429,7 +429,7 @@ private CompletableFuture createConsumingConnection(final Str return future; } - @Test(timeout = 30000) + @Test(timeout = 60000) public void whenReadEventsConsumerIsBlocked() throws Exception { // blocking streaming client after 3 seconds new Thread(() -> { From 68e3eeca24f41b5e1fd1bfef4b99c2b7948fb9cc Mon Sep 17 00:00:00 2001 From: antban Date: Fri, 14 Jul 2017 11:09:22 +0200 Subject: [PATCH 15/21] ARUHA-882 Do not put wrong data to db --- build.gradle | 1 + .../repository/db/EventTypeDbRepositoryTest.java | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 733fa1344c..3e0012c0d9 100644 --- a/build.gradle +++ b/build.gradle @@ -319,6 +319,7 @@ task fullAcceptanceTest(type: GradleBuild) { task acceptanceTest(type: Test) { testClassesDir = sourceSets.acceptanceTest.output.classesDir classpath = sourceSets.acceptanceTest.runtimeClasspath + maxParallelForks = 4 } test { diff --git a/src/acceptance-test/java/org/zalando/nakadi/repository/db/EventTypeDbRepositoryTest.java b/src/acceptance-test/java/org/zalando/nakadi/repository/db/EventTypeDbRepositoryTest.java index 37dda85d67..35d61a9925 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/repository/db/EventTypeDbRepositoryTest.java +++ b/src/acceptance-test/java/org/zalando/nakadi/repository/db/EventTypeDbRepositoryTest.java @@ -1,5 +1,7 @@ package org.zalando.nakadi.repository.db; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.databind.node.TextNode; import java.io.IOException; import java.util.List; import java.util.stream.Collectors; @@ -21,7 +23,6 @@ import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.buildDefaultEventType; import static org.zalando.nakadi.utils.TestUtils.randomUUID; -import static org.zalando.nakadi.utils.TestUtils.randomValidEventTypeName; public class EventTypeDbRepositoryTest extends AbstractDbRepositoryTest { @@ -195,14 +196,19 @@ public void whenRemoveThenDeleteFromDatabase() throws Exception { @Test public void unknownAttributesAreIgnoredWhenDesserializing() throws Exception { - final String eventTypeName = randomValidEventTypeName(); + final EventType eventType = buildDefaultEventType(); + final ObjectNode node = (ObjectNode) TestUtils.OBJECT_MAPPER.readTree( + TestUtils.OBJECT_MAPPER.writeValueAsString(eventType)); + node.set("unknown_attribute", new TextNode("will just be ignored")); + + final String eventTypeName = eventType.getName(); final String topic = randomUUID(); final String insertSQL = "INSERT INTO zn_data.event_type (et_name, et_topic, et_event_type_object) " + "VALUES (?, ?, to_json(?::json))"; template.update(insertSQL, eventTypeName, topic, - "{\"unknow_attribute\": \"will just be ignored\"}"); + TestUtils.OBJECT_MAPPER.writeValueAsString(node)); final EventType persistedEventType = repository.findByName(eventTypeName); From 3d56baea8e4e1cff0dd161d5ca5b25e695004a97 Mon Sep 17 00:00:00 2001 From: antban Date: Fri, 14 Jul 2017 13:54:31 +0200 Subject: [PATCH 16/21] ARUHA-882 Wait for complete event type creation --- .../repository/kafka/KafkaRepositoryAT.java | 34 +++++++++++-------- .../exceptions/TopicCreationException.java | 4 +++ .../kafka/KafkaTopicRepository.java | 22 ++++++++++-- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/acceptance-test/java/org/zalando/nakadi/repository/kafka/KafkaRepositoryAT.java b/src/acceptance-test/java/org/zalando/nakadi/repository/kafka/KafkaRepositoryAT.java index 5c1eb66e87..52cad9477e 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/repository/kafka/KafkaRepositoryAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/repository/kafka/KafkaRepositoryAT.java @@ -1,14 +1,29 @@ package org.zalando.nakadi.repository.kafka; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; import org.apache.curator.CuratorZookeeperClient; import org.apache.curator.framework.CuratorFramework; +import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.KafkaConsumer; import org.apache.kafka.common.PartitionInfo; import org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy; +import static org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.arrayWithSize; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.not; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import static org.mockito.Matchers.any; import org.mockito.Mockito; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import org.zalando.nakadi.config.NakadiSettings; import org.zalando.nakadi.domain.BatchItem; import org.zalando.nakadi.domain.EventPublishingStatus; @@ -18,21 +33,6 @@ import org.zalando.nakadi.utils.TestUtils; import org.zalando.nakadi.webservice.BaseAT; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.UUID; - -import static org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.arrayWithSize; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.not; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - public class KafkaRepositoryAT extends BaseAT { private static final int DEFAULT_PARTITION_COUNT = 8; @@ -178,7 +178,11 @@ private KafkaTopicRepository createKafkaTopicRepository() { final ZooKeeperHolder zooKeeperHolder = mock(ZooKeeperHolder.class); when(zooKeeperHolder.get()).thenReturn(curatorFramework); + final Consumer consumer = mock(Consumer.class); + when(consumer.partitionsFor(any())).thenReturn(new ArrayList<>()); + final KafkaFactory factory = mock(KafkaFactory.class); + when(factory.getConsumer()).thenReturn(consumer); Mockito .doReturn(kafkaHelper.createProducer()) diff --git a/src/main/java/org/zalando/nakadi/exceptions/TopicCreationException.java b/src/main/java/org/zalando/nakadi/exceptions/TopicCreationException.java index 72b2939064..f1762f0778 100644 --- a/src/main/java/org/zalando/nakadi/exceptions/TopicCreationException.java +++ b/src/main/java/org/zalando/nakadi/exceptions/TopicCreationException.java @@ -4,6 +4,10 @@ public class TopicCreationException extends NakadiException { + public TopicCreationException(final String msg) { + super(msg); + } + public TopicCreationException(final String msg, final Exception cause) { super(msg, cause); } diff --git a/src/main/java/org/zalando/nakadi/repository/kafka/KafkaTopicRepository.java b/src/main/java/org/zalando/nakadi/repository/kafka/KafkaTopicRepository.java index 996b4be84d..3673de67a5 100644 --- a/src/main/java/org/zalando/nakadi/repository/kafka/KafkaTopicRepository.java +++ b/src/main/java/org/zalando/nakadi/repository/kafka/KafkaTopicRepository.java @@ -134,6 +134,21 @@ private void createTopic(final String topic, final int partitionsNum, final int } catch (final Exception e) { throw new TopicCreationException("Unable to create topic " + topic, e); } + // Next step is to wait for topic initialization. On can not skip this task, cause kafka instances may not + // receive information about topic creation, which in turn will block publishing. + // This kind of behavior was observed during tests, but may also present on highly loaded event types. + final long timeoutMillis = TimeUnit.SECONDS.toMillis(5); + final long borderTime = System.currentTimeMillis() + timeoutMillis; + boolean creationConfirmed = false; + while (!creationConfirmed && borderTime >= System.currentTimeMillis()) { + try (Consumer consumer = kafkaFactory.getConsumer()) { + final List partitions = consumer.partitionsFor(topic); + creationConfirmed = null != partitions; + } + } + if (!creationConfirmed) { + throw new TopicCreationException("Failed to confirm topic creation within " + timeoutMillis + " millis"); + } } @Override @@ -289,7 +304,9 @@ private void failUnpublished(final List batch, final String reason) { public Optional loadPartitionStatistics(final Timeline timeline, final String partition) throws ServiceUnavailableException { try (Consumer consumer = kafkaFactory.getConsumer()) { - final Optional tp = consumer.partitionsFor(timeline.getTopic()).stream() + final List topicPartitions = consumer.partitionsFor(timeline.getTopic()); + + final Optional tp = topicPartitions.stream() .filter(p -> KafkaCursor.toNakadiPartition(p.partition()).equals(partition)) .findAny(); if (!tp.isPresent()) { @@ -305,7 +322,8 @@ public Optional loadPartitionStatistics(final Timeline time return Optional.of(new KafkaPartitionStatistics(timeline, kafkaTP.partition(), begin, end - 1)); } catch (final Exception e) { - throw new ServiceUnavailableException("Error occurred when fetching partitions offsets", e); + throw new ServiceUnavailableException("Error occurred when fetching partitions offsets from " + + timeline + " for partition " + partition, e); } } From 61499ffc1c15fc136b1a9b785e697d2211b0a6ee Mon Sep 17 00:00:00 2001 From: antban Date: Fri, 14 Jul 2017 16:21:49 +0200 Subject: [PATCH 17/21] ARUHA-882 Fix non-stoppable growth of shutdown hooks --- .../org/zalando/nakadi/webservice/BaseAT.java | 4 +- .../org/zalando/nakadi/ShutdownHooks.java | 50 +++++++++++++++++++ .../subscription/StreamingContext.java | 12 +++-- 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/zalando/nakadi/ShutdownHooks.java diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java index 5205126488..e46e66f1d2 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java @@ -37,9 +37,9 @@ public abstract class BaseAT { RestAssured.port = PORT; RestAssured.defaultParser = Parser.JSON; RestAssured.config().getHttpClientConfig() - .setParam(CoreConnectionPNames.SO_TIMEOUT, TimeUnit.SECONDS.toMillis(5)); + .setParam(CoreConnectionPNames.SO_TIMEOUT, TimeUnit.SECONDS.toMillis(30)); RestAssured.config().getHttpClientConfig().setParam( - CoreConnectionPNames.CONNECTION_TIMEOUT, TimeUnit.SECONDS.toMillis(1)); + CoreConnectionPNames.CONNECTION_TIMEOUT, TimeUnit.SECONDS.toMillis(5)); } @BeforeClass diff --git a/src/main/java/org/zalando/nakadi/ShutdownHooks.java b/src/main/java/org/zalando/nakadi/ShutdownHooks.java new file mode 100644 index 0000000000..3559068f0a --- /dev/null +++ b/src/main/java/org/zalando/nakadi/ShutdownHooks.java @@ -0,0 +1,50 @@ +package org.zalando.nakadi; + +import java.io.Closeable; +import java.util.HashSet; +import java.util.Set; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ShutdownHooks { + + private static final Set HOOKS = new HashSet<>(); + private static final Logger LOG = LoggerFactory.getLogger(ShutdownHooks.class); + + static { + Runtime.getRuntime().addShutdownHook(new Thread(ShutdownHooks::onNodeShutdown)); + } + + private static void onNodeShutdown() { + boolean haveHooks = true; + while (haveHooks) { + final Runnable hook; + synchronized (HOOKS) { + hook = HOOKS.isEmpty() ? null : HOOKS.iterator().next(); + HOOKS.remove(hook); + haveHooks = !HOOKS.isEmpty(); + } + if (null != hook) { + try { + hook.run(); + } catch (final RuntimeException ex) { + LOG.warn("Failed to call on shutdown hook for {}", hook, ex); + } + } + } + } + + public static Closeable addHook(final Runnable runnable) { + synchronized (HOOKS) { + HOOKS.add(runnable); + } + return () -> removeHook(runnable); + } + + private static void removeHook(final Runnable runnable) { + synchronized (HOOKS) { + HOOKS.remove(runnable); + } + } + +} diff --git a/src/main/java/org/zalando/nakadi/service/subscription/StreamingContext.java b/src/main/java/org/zalando/nakadi/service/subscription/StreamingContext.java index dd600118af..bee55efe94 100644 --- a/src/main/java/org/zalando/nakadi/service/subscription/StreamingContext.java +++ b/src/main/java/org/zalando/nakadi/service/subscription/StreamingContext.java @@ -13,6 +13,7 @@ import java.util.function.BiFunction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.zalando.nakadi.ShutdownHooks; import org.zalando.nakadi.domain.Subscription; import org.zalando.nakadi.exceptions.NakadiRuntimeException; import org.zalando.nakadi.exceptions.runtime.AccessDeniedException; @@ -128,9 +129,14 @@ public EventStreamWriter getWriter() { @Override public void stream() throws InterruptedException { - // bugfix ARUHA-485 - Runtime.getRuntime().addShutdownHook(new Thread(() -> onNodeShutdown())); - streamInternal(new StartingState()); + try (Closeable ignore = ShutdownHooks.addHook(this::onNodeShutdown)) { // bugfix ARUHA-485 + streamInternal(new StartingState()); + } catch (IOException ex) { + log.error( + "Failed to delete shutdown hook for subscription {}. This method should not throw any exception", + getSubscription(), + ex); + } } void onNodeShutdown() { From ffddacaac32e6eb2834c34944c69b7c56edecb47 Mon Sep 17 00:00:00 2001 From: antban Date: Mon, 17 Jul 2017 09:58:35 +0200 Subject: [PATCH 18/21] ARUHA-882 Change parrallelism for build --- build.gradle | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 3e0012c0d9..cd93fa15b5 100644 --- a/build.gradle +++ b/build.gradle @@ -198,10 +198,6 @@ tasks.withType(FindBugs) { } } -//tasks.withType(Test) { -// maxParallelForks = Runtime.runtime.availableProcessors() -//} - task generateScmSourceJson() { def rev = new ByteArrayOutputStream() exec { @@ -319,7 +315,8 @@ task fullAcceptanceTest(type: GradleBuild) { task acceptanceTest(type: Test) { testClassesDir = sourceSets.acceptanceTest.output.classesDir classpath = sourceSets.acceptanceTest.runtimeClasspath - maxParallelForks = 4 +// maxParallelForks = Runtime.runtime.availableProcessors() + maxParallelForks = 1 } test { From 58d2987b0385385063d9f0524bc72179699483fd Mon Sep 17 00:00:00 2001 From: antban Date: Fri, 21 Jul 2017 17:47:13 +0200 Subject: [PATCH 19/21] ARUHA-882 Fixes after review --- build.gradle | 3 +- .../org/zalando/nakadi/webservice/BaseAT.java | 3 +- .../webservice/EventStreamReadingAT.java | 4 +-- .../webservice/SettingsControllerAT.java | 28 +++++++------------ .../timelines/TimelineConsumptionTest.java | 2 -- .../kafka/KafkaTopicRepository.java | 22 ++++++++------- .../subscription/StreamingContext.java | 2 +- .../org/zalando/nakadi/utils/TestUtils.java | 6 ++-- 8 files changed, 31 insertions(+), 39 deletions(-) diff --git a/build.gradle b/build.gradle index cd93fa15b5..ef18c92fdc 100644 --- a/build.gradle +++ b/build.gradle @@ -315,8 +315,7 @@ task fullAcceptanceTest(type: GradleBuild) { task acceptanceTest(type: Test) { testClassesDir = sourceSets.acceptanceTest.output.classesDir classpath = sourceSets.acceptanceTest.runtimeClasspath -// maxParallelForks = Runtime.runtime.availableProcessors() - maxParallelForks = 1 + maxParallelForks = Runtime.runtime.availableProcessors() } test { diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java index e46e66f1d2..1170f5b71f 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java @@ -10,6 +10,7 @@ import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.zalando.nakadi.config.JsonConfig; import org.zalando.nakadi.domain.Storage; +import org.zalando.nakadi.exceptions.runtime.DuplicatedStorageException; import org.zalando.nakadi.repository.db.EventTypeDbRepository; import org.zalando.nakadi.repository.db.StorageDbRepository; import org.zalando.nakadi.repository.db.TimelineDbRepository; @@ -50,7 +51,7 @@ public static void createDefaultStorage() { storage.setConfiguration(new Storage.KafkaConfiguration(null, null, ZOOKEEPER_URL, "")); try { STORAGE_DB_REPOSITORY.createStorage(storage); - } catch (final Exception ignore) { + } catch (final DuplicatedStorageException ignore) { } } } diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java index 43c0a44b48..1dfd2a4b98 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/EventStreamReadingAT.java @@ -326,7 +326,7 @@ public void whenInvalidCursorsThenPreconditionFailed() { .body("detail", equalTo("non existing partition very_wrong_partition")); } - @Test(timeout = 30000) + @Test(timeout = 10000) public void whenReadEventsForBlockedConsumerThen403() throws Exception { readEvents() .then() @@ -429,7 +429,7 @@ private CompletableFuture createConsumingConnection(final Str return future; } - @Test(timeout = 60000) + @Test(timeout = 10000) public void whenReadEventsConsumerIsBlocked() throws Exception { // blocking streaming client after 3 seconds new Thread(() -> { diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/SettingsControllerAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/SettingsControllerAT.java index 60df2b04d8..5daa992ef2 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/SettingsControllerAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/SettingsControllerAT.java @@ -27,12 +27,8 @@ public class SettingsControllerAT extends BaseAT { public void testBlockFlooder() throws Exception { final EventType eventType = NakadiTestUtils.createEventType(); blacklist(eventType.getName(), BlacklistService.Type.CONSUMER_ET); - try { - Assert.assertNotNull(CURATOR.checkExists() - .forPath("/nakadi/blacklist/consumers/event_types/" + eventType.getName())); - } finally { - whitelist(eventType.getName(), BlacklistService.Type.CONSUMER_ET); - } + Assert.assertNotNull(CURATOR.checkExists() + .forPath("/nakadi/blacklist/consumers/event_types/" + eventType.getName())); } @Test @@ -50,18 +46,14 @@ public void testUnblockFlooder() throws Exception { public void testGetFlooders() throws Exception { final EventType eventType = NakadiTestUtils.createEventType(); blacklist(eventType.getName(), BlacklistService.Type.CONSUMER_ET); - try { - TestUtils.waitFor( - () -> given() - .contentType(ContentType.JSON) - .get(BLACKLIST_URL) - .then() - .statusCode(HttpStatus.SC_OK) - .body("consumers.event_types", hasItems(eventType.getName())), - 1000, 200); - } finally { - whitelist(eventType.getName(), BlacklistService.Type.CONSUMER_ET); - } + TestUtils.waitFor( + () -> given() + .contentType(ContentType.JSON) + .get(BLACKLIST_URL) + .then() + .statusCode(HttpStatus.SC_OK) + .body("consumers.event_types", hasItems(eventType.getName())), + 1000, 200); } public static void blacklist(final String name, final BlacklistService.Type type) throws IOException { diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/timelines/TimelineConsumptionTest.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/timelines/TimelineConsumptionTest.java index bdbf881bde..f74a489a8b 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/timelines/TimelineConsumptionTest.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/timelines/TimelineConsumptionTest.java @@ -33,7 +33,6 @@ public static void setupEventTypeWithEvents() throws JsonProcessingException, In final CountDownLatch finished = new CountDownLatch(1); final AtomicReference inTimeCursors = new AtomicReference<>(); createParallelConsumer(eventType.getName(), 8, finished, inTimeCursors::set); - final long start = System.currentTimeMillis(); publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); createTimeline(eventType.getName()); publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); @@ -43,7 +42,6 @@ public static void setupEventTypeWithEvents() throws JsonProcessingException, In publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}"); finished.await(); cursorsDuringPublish = inTimeCursors.get(); - System.out.println("Total took: " + (System.currentTimeMillis() - start)/1000); } private static void createParallelConsumer( diff --git a/src/main/java/org/zalando/nakadi/repository/kafka/KafkaTopicRepository.java b/src/main/java/org/zalando/nakadi/repository/kafka/KafkaTopicRepository.java index 3673de67a5..84bd766783 100644 --- a/src/main/java/org/zalando/nakadi/repository/kafka/KafkaTopicRepository.java +++ b/src/main/java/org/zalando/nakadi/repository/kafka/KafkaTopicRepository.java @@ -37,6 +37,8 @@ import org.apache.kafka.common.errors.NotLeaderForPartitionException; import org.apache.kafka.common.errors.UnknownServerException; import org.apache.kafka.common.errors.UnknownTopicOrPartitionException; +import org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy; +import org.echocat.jomon.runtime.concurrent.Retryer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.zalando.nakadi.config.NakadiSettings; @@ -138,15 +140,15 @@ private void createTopic(final String topic, final int partitionsNum, final int // receive information about topic creation, which in turn will block publishing. // This kind of behavior was observed during tests, but may also present on highly loaded event types. final long timeoutMillis = TimeUnit.SECONDS.toMillis(5); - final long borderTime = System.currentTimeMillis() + timeoutMillis; - boolean creationConfirmed = false; - while (!creationConfirmed && borderTime >= System.currentTimeMillis()) { - try (Consumer consumer = kafkaFactory.getConsumer()) { - final List partitions = consumer.partitionsFor(topic); - creationConfirmed = null != partitions; - } - } - if (!creationConfirmed) { + final Boolean allowsConsumption = Retryer.executeWithRetry(() -> { + try (Consumer consumer = kafkaFactory.getConsumer()) { + return null != consumer.partitionsFor(topic); + } + }, + new RetryForSpecifiedTimeStrategy(timeoutMillis) + .withWaitBetweenEachTry(100L) + .withResultsThatForceRetry(Boolean.FALSE)); + if (!Boolean.TRUE.equals(allowsConsumption)) { throw new TopicCreationException("Failed to confirm topic creation within " + timeoutMillis + " millis"); } } @@ -504,7 +506,7 @@ public void validateCommitCursor(final NakadiCursor position) throws InvalidCurs @Override public void setRetentionTime(final String topic, final Long retentionMs) throws TopicConfigException { - try { + try { doWithZkUtils(zkUtils -> { final Properties topicProps = AdminUtils.fetchEntityConfig(zkUtils, ConfigType.Topic(), topic); topicProps.setProperty("retention.ms", Long.toString(retentionMs)); diff --git a/src/main/java/org/zalando/nakadi/service/subscription/StreamingContext.java b/src/main/java/org/zalando/nakadi/service/subscription/StreamingContext.java index bee55efe94..8db70c94cc 100644 --- a/src/main/java/org/zalando/nakadi/service/subscription/StreamingContext.java +++ b/src/main/java/org/zalando/nakadi/service/subscription/StreamingContext.java @@ -131,7 +131,7 @@ public EventStreamWriter getWriter() { public void stream() throws InterruptedException { try (Closeable ignore = ShutdownHooks.addHook(this::onNodeShutdown)) { // bugfix ARUHA-485 streamInternal(new StartingState()); - } catch (IOException ex) { + } catch (final IOException ex) { log.error( "Failed to delete shutdown hook for subscription {}. This method should not throw any exception", getSubscription(), diff --git a/src/test/java/org/zalando/nakadi/utils/TestUtils.java b/src/test/java/org/zalando/nakadi/utils/TestUtils.java index 3fb879bba5..5e8e88aae4 100644 --- a/src/test/java/org/zalando/nakadi/utils/TestUtils.java +++ b/src/test/java/org/zalando/nakadi/utils/TestUtils.java @@ -11,6 +11,7 @@ import java.util.UUID; import static java.util.stream.Collectors.toList; import static java.util.stream.IntStream.range; +import java.util.stream.Stream; import org.apache.commons.io.IOUtils; import org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy; import static org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry; @@ -178,9 +179,8 @@ public static void waitFor(final Runnable runnable, final long timeoutMs) { @SuppressWarnings("unchecked") public static void waitFor(final Runnable runnable, final long timeoutMs, final int intervalMs, final Class... additionalException) { - final Class[] leadToRetry = new Class[additionalException.length + 1]; - System.arraycopy(additionalException, 0, leadToRetry, 1, additionalException.length); - leadToRetry[0] = AssertionError.class; + final List> leadToRetry = + Stream.concat(Stream.of(additionalException), Stream.of(AssertionError.class)).collect(toList()); executeWithRetry( runnable, new RetryForSpecifiedTimeStrategy(timeoutMs) From 6c1fd650f7c9111cb2c7adee153f8b9ee9358543 Mon Sep 17 00:00:00 2001 From: antban Date: Tue, 25 Jul 2017 12:40:24 +0200 Subject: [PATCH 20/21] Merge remote-tracking branch 'origin/master' into ARUHA-882 # Conflicts resolved: # src/test/java/org/zalando/nakadi/controller/CursorsControllerTest.java # src/test/java/org/zalando/nakadi/controller/EventPublishingControllerTest.java # src/test/java/org/zalando/nakadi/controller/EventStreamControllerTest.java # src/test/java/org/zalando/nakadi/controller/EventTypeControllerTest.java # src/test/java/org/zalando/nakadi/controller/EventTypeControllerTestCase.java # src/test/java/org/zalando/nakadi/controller/PartitionsControllerTest.java # src/test/java/org/zalando/nakadi/controller/StoragesControllerTest.java # src/test/java/org/zalando/nakadi/controller/SubscriptionControllerTest.java # src/test/java/org/zalando/nakadi/controller/TimelinesControllerTest.java # src/test/java/org/zalando/nakadi/service/EventStreamTest.java --- .../nakadi/config/SecurityConfiguration.java | 12 +++-- .../controller/EventPublishingController.java | 12 +++-- .../controller/EventStreamController.java | 44 +++++++++---------- .../controller/EventTypeController.java | 16 +++---- .../nakadi/controller/ExceptionHandling.java | 6 +-- .../controller/SubscriptionController.java | 12 +++-- .../SubscriptionStreamController.java | 20 ++++----- .../controller/TimelinesController.java | 7 ++- .../nakadi/domain/EventTypeResource.java | 5 +-- .../org/zalando/nakadi/security/Client.java | 3 +- .../nakadi/security/ClientResolver.java | 10 ++--- .../nakadi/security/FullAccessClient.java | 3 +- .../zalando/nakadi/security/NakadiClient.java | 3 +- .../service/AuthorizationValidator.java | 20 ++++----- .../nakadi/service/EventPublisher.java | 13 +++--- .../nakadi/service/EventTypeService.java | 20 ++++----- .../subscription/SubscriptionService.java | 21 +++++---- .../service/timeline/TimelineService.java | 15 +++---- .../controller/CursorsControllerTest.java | 44 +++++++++---------- .../EventPublishingControllerTest.java | 1 - .../EventTypeAuthorizationTest.java | 18 ++++---- .../controller/EventTypeControllerTest.java | 6 +-- .../EventTypeControllerTestCase.java | 1 - .../controller/PartitionsControllerTest.java | 1 - .../controller/StoragesControllerTest.java | 1 - .../SubscriptionControllerTest.java | 9 ---- .../controller/TimelinesControllerTest.java | 1 + .../nakadi/security/AuthenticationTest.java | 44 +++++++++---------- .../security/RealmModeAuthenticationTest.java | 3 +- .../service/AuthorizationValidatorTest.java | 13 +++--- .../nakadi/service/EventPublisherTest.java | 44 +++++++++---------- .../nakadi/service/EventTypeServiceTest.java | 22 +++++----- 32 files changed, 198 insertions(+), 252 deletions(-) diff --git a/src/main/java/org/zalando/nakadi/config/SecurityConfiguration.java b/src/main/java/org/zalando/nakadi/config/SecurityConfiguration.java index 0a4d263497..b1eddfd7ee 100644 --- a/src/main/java/org/zalando/nakadi/config/SecurityConfiguration.java +++ b/src/main/java/org/zalando/nakadi/config/SecurityConfiguration.java @@ -1,10 +1,15 @@ package org.zalando.nakadi.config; +import java.text.MessageFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; +import static org.springframework.http.HttpMethod.DELETE; +import static org.springframework.http.HttpMethod.GET; +import static org.springframework.http.HttpMethod.POST; +import static org.springframework.http.HttpMethod.PUT; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; @@ -12,13 +17,6 @@ import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices; import org.zalando.stups.oauth2.spring.security.expression.ExtendedOAuth2WebSecurityExpressionHandler; -import java.text.MessageFormat; - -import static org.springframework.http.HttpMethod.DELETE; -import static org.springframework.http.HttpMethod.GET; -import static org.springframework.http.HttpMethod.POST; -import static org.springframework.http.HttpMethod.PUT; - @EnableResourceServer @Configuration public class SecurityConfiguration extends ResourceServerConfigurerAdapter { diff --git a/src/main/java/org/zalando/nakadi/controller/EventPublishingController.java b/src/main/java/org/zalando/nakadi/controller/EventPublishingController.java index 554b1aa81b..550643f6fd 100644 --- a/src/main/java/org/zalando/nakadi/controller/EventPublishingController.java +++ b/src/main/java/org/zalando/nakadi/controller/EventPublishingController.java @@ -1,15 +1,20 @@ package org.zalando.nakadi.controller; import com.google.common.base.Charsets; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import javax.ws.rs.core.Response; import org.json.JSONException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import static org.springframework.http.ResponseEntity.status; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import static org.springframework.web.bind.annotation.RequestMethod.POST; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.NativeWebRequest; import org.zalando.nakadi.domain.EventPublishResult; @@ -26,13 +31,6 @@ import org.zalando.problem.Problem; import org.zalando.problem.ThrowableProblem; import org.zalando.problem.spring.web.advice.Responses; - -import javax.ws.rs.core.Response; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; - -import static org.springframework.http.ResponseEntity.status; -import static org.springframework.web.bind.annotation.RequestMethod.POST; import static org.zalando.problem.spring.web.advice.Responses.create; @RestController diff --git a/src/main/java/org/zalando/nakadi/controller/EventStreamController.java b/src/main/java/org/zalando/nakadi/controller/EventStreamController.java index 57cd88c70b..577320d727 100644 --- a/src/main/java/org/zalando/nakadi/controller/EventStreamController.java +++ b/src/main/java/org/zalando/nakadi/controller/EventStreamController.java @@ -7,6 +7,25 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; +import java.io.Closeable; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; +import javax.annotation.Nullable; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.Response; +import static javax.ws.rs.core.Response.Status.BAD_REQUEST; +import static javax.ws.rs.core.Response.Status.FORBIDDEN; +import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; +import static javax.ws.rs.core.Response.Status.NOT_FOUND; +import static javax.ws.rs.core.Response.Status.PRECONDITION_FAILED; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -35,6 +54,7 @@ import org.zalando.nakadi.exceptions.runtime.AccessDeniedException; import org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException; import org.zalando.nakadi.metrics.MetricUtils; +import static org.zalando.nakadi.metrics.MetricUtils.metricNameFor; import org.zalando.nakadi.repository.EventConsumer; import org.zalando.nakadi.repository.EventTypeRepository; import org.zalando.nakadi.repository.TopicRepository; @@ -51,32 +71,10 @@ import org.zalando.nakadi.service.EventTypeChangeListener; import org.zalando.nakadi.service.timeline.TimelineService; import org.zalando.nakadi.util.FeatureToggleService; +import static org.zalando.nakadi.util.FeatureToggleService.Feature.LIMIT_CONSUMERS_NUMBER; import org.zalando.nakadi.view.Cursor; import org.zalando.problem.Problem; -import javax.annotation.Nullable; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.core.Response; -import java.io.Closeable; -import java.io.IOException; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.stream.Collectors; - -import static javax.ws.rs.core.Response.Status.BAD_REQUEST; -import static javax.ws.rs.core.Response.Status.FORBIDDEN; -import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; -import static javax.ws.rs.core.Response.Status.NOT_FOUND; -import static javax.ws.rs.core.Response.Status.PRECONDITION_FAILED; -import static org.zalando.nakadi.metrics.MetricUtils.metricNameFor; -import static org.zalando.nakadi.util.FeatureToggleService.Feature.LIMIT_CONSUMERS_NUMBER; - @RestController public class EventStreamController { diff --git a/src/main/java/org/zalando/nakadi/controller/EventTypeController.java b/src/main/java/org/zalando/nakadi/controller/EventTypeController.java index a70ccb1d74..d5ce641388 100644 --- a/src/main/java/org/zalando/nakadi/controller/EventTypeController.java +++ b/src/main/java/org/zalando/nakadi/controller/EventTypeController.java @@ -1,10 +1,14 @@ package org.zalando.nakadi.controller; +import java.util.List; +import javax.validation.Valid; +import javax.ws.rs.core.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import static org.springframework.http.ResponseEntity.status; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -35,20 +39,14 @@ import org.zalando.nakadi.service.EventTypeService; import org.zalando.nakadi.service.Result; import org.zalando.nakadi.util.FeatureToggleService; +import static org.zalando.nakadi.util.FeatureToggleService.Feature.CHECK_OWNING_APPLICATION; +import static org.zalando.nakadi.util.FeatureToggleService.Feature.DISABLE_EVENT_TYPE_CREATION; +import static org.zalando.nakadi.util.FeatureToggleService.Feature.DISABLE_EVENT_TYPE_DELETION; import org.zalando.nakadi.validation.EventTypeOptionsValidator; import org.zalando.problem.MoreStatus; import org.zalando.problem.Problem; import org.zalando.problem.spring.web.advice.Responses; -import javax.validation.Valid; -import javax.ws.rs.core.Response; -import java.util.List; - -import static org.springframework.http.ResponseEntity.status; -import static org.zalando.nakadi.util.FeatureToggleService.Feature.CHECK_OWNING_APPLICATION; -import static org.zalando.nakadi.util.FeatureToggleService.Feature.DISABLE_EVENT_TYPE_CREATION; -import static org.zalando.nakadi.util.FeatureToggleService.Feature.DISABLE_EVENT_TYPE_DELETION; - @RestController @RequestMapping(value = "/event-types") public class EventTypeController { diff --git a/src/main/java/org/zalando/nakadi/controller/ExceptionHandling.java b/src/main/java/org/zalando/nakadi/controller/ExceptionHandling.java index 9def832abb..3d4efa0eca 100644 --- a/src/main/java/org/zalando/nakadi/controller/ExceptionHandling.java +++ b/src/main/java/org/zalando/nakadi/controller/ExceptionHandling.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.JsonMappingException; import com.google.common.base.CaseFormat; +import javax.ws.rs.core.Response; import org.apache.commons.lang3.RandomStringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,14 +23,11 @@ import org.zalando.nakadi.exceptions.runtime.NoEventTypeException; import org.zalando.nakadi.exceptions.runtime.RepositoryProblemException; import org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException; +import static org.zalando.problem.MoreStatus.UNPROCESSABLE_ENTITY; import org.zalando.problem.Problem; import org.zalando.problem.spring.web.advice.ProblemHandling; import org.zalando.problem.spring.web.advice.Responses; -import javax.ws.rs.core.Response; - -import static org.zalando.problem.MoreStatus.UNPROCESSABLE_ENTITY; - @ControllerAdvice public final class ExceptionHandling implements ProblemHandling { diff --git a/src/main/java/org/zalando/nakadi/controller/SubscriptionController.java b/src/main/java/org/zalando/nakadi/controller/SubscriptionController.java index f02e9e0849..5998d80f67 100644 --- a/src/main/java/org/zalando/nakadi/controller/SubscriptionController.java +++ b/src/main/java/org/zalando/nakadi/controller/SubscriptionController.java @@ -1,5 +1,9 @@ package org.zalando.nakadi.controller; +import java.util.Set; +import javax.annotation.Nullable; +import static javax.ws.rs.core.Response.Status.NOT_IMPLEMENTED; +import static javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -21,16 +25,10 @@ import org.zalando.nakadi.service.WebResult; import org.zalando.nakadi.service.subscription.SubscriptionService; import org.zalando.nakadi.util.FeatureToggleService; +import static org.zalando.nakadi.util.FeatureToggleService.Feature.HIGH_LEVEL_API; import org.zalando.problem.Problem; import org.zalando.problem.spring.web.advice.Responses; -import javax.annotation.Nullable; -import java.util.Set; - -import static javax.ws.rs.core.Response.Status.NOT_IMPLEMENTED; -import static javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE; -import static org.zalando.nakadi.util.FeatureToggleService.Feature.HIGH_LEVEL_API; - @RestController @RequestMapping(value = "/subscriptions") diff --git a/src/main/java/org/zalando/nakadi/controller/SubscriptionStreamController.java b/src/main/java/org/zalando/nakadi/controller/SubscriptionStreamController.java index bd0016991c..7c502b668d 100644 --- a/src/main/java/org/zalando/nakadi/controller/SubscriptionStreamController.java +++ b/src/main/java/org/zalando/nakadi/controller/SubscriptionStreamController.java @@ -3,6 +3,13 @@ import com.codahale.metrics.Counter; import com.codahale.metrics.MetricRegistry; import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.io.OutputStream; +import java.util.concurrent.atomic.AtomicBoolean; +import javax.annotation.Nullable; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -18,6 +25,7 @@ import org.zalando.nakadi.domain.Subscription; import org.zalando.nakadi.exceptions.NakadiException; import org.zalando.nakadi.exceptions.runtime.AccessDeniedException; +import static org.zalando.nakadi.metrics.MetricUtils.metricNameForSubscription; import org.zalando.nakadi.repository.db.SubscriptionDbRepository; import org.zalando.nakadi.security.Client; import org.zalando.nakadi.service.BlacklistService; @@ -27,18 +35,8 @@ import org.zalando.nakadi.service.subscription.SubscriptionStreamer; import org.zalando.nakadi.service.subscription.SubscriptionStreamerFactory; import org.zalando.nakadi.util.FeatureToggleService; -import org.zalando.problem.Problem; - -import javax.annotation.Nullable; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.core.Response; -import java.io.IOException; -import java.io.OutputStream; -import java.util.concurrent.atomic.AtomicBoolean; - -import static org.zalando.nakadi.metrics.MetricUtils.metricNameForSubscription; import static org.zalando.nakadi.util.FeatureToggleService.Feature.HIGH_LEVEL_API; +import org.zalando.problem.Problem; @RestController public class SubscriptionStreamController { diff --git a/src/main/java/org/zalando/nakadi/controller/TimelinesController.java b/src/main/java/org/zalando/nakadi/controller/TimelinesController.java index dce68ac934..98b56de349 100644 --- a/src/main/java/org/zalando/nakadi/controller/TimelinesController.java +++ b/src/main/java/org/zalando/nakadi/controller/TimelinesController.java @@ -1,5 +1,8 @@ package org.zalando.nakadi.controller; +import java.util.stream.Collectors; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -28,10 +31,6 @@ import org.zalando.problem.Problem; import org.zalando.problem.spring.web.advice.Responses; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.util.stream.Collectors; - @RestController @RequestMapping(value = "/event-types/{name}/timelines", produces = MediaType.APPLICATION_JSON) public class TimelinesController { diff --git a/src/main/java/org/zalando/nakadi/domain/EventTypeResource.java b/src/main/java/org/zalando/nakadi/domain/EventTypeResource.java index cfe78ddea4..68a352d653 100644 --- a/src/main/java/org/zalando/nakadi/domain/EventTypeResource.java +++ b/src/main/java/org/zalando/nakadi/domain/EventTypeResource.java @@ -1,12 +1,11 @@ package org.zalando.nakadi.domain; +import java.util.List; +import java.util.Optional; import org.zalando.nakadi.plugin.api.authz.AuthorizationAttribute; import org.zalando.nakadi.plugin.api.authz.AuthorizationService; import org.zalando.nakadi.plugin.api.authz.Resource; -import java.util.List; -import java.util.Optional; - public class EventTypeResource implements Resource { private final String name; diff --git a/src/main/java/org/zalando/nakadi/security/Client.java b/src/main/java/org/zalando/nakadi/security/Client.java index e9f8f6c8f2..54c3f49b31 100644 --- a/src/main/java/org/zalando/nakadi/security/Client.java +++ b/src/main/java/org/zalando/nakadi/security/Client.java @@ -1,8 +1,7 @@ package org.zalando.nakadi.security; -import org.zalando.nakadi.exceptions.IllegalScopeException; - import java.util.Set; +import org.zalando.nakadi.exceptions.IllegalScopeException; public abstract class Client { diff --git a/src/main/java/org/zalando/nakadi/security/ClientResolver.java b/src/main/java/org/zalando/nakadi/security/ClientResolver.java index 96b19cbd1d..acf5e3541c 100644 --- a/src/main/java/org/zalando/nakadi/security/ClientResolver.java +++ b/src/main/java/org/zalando/nakadi/security/ClientResolver.java @@ -1,5 +1,9 @@ package org.zalando.nakadi.security; +import java.security.Principal; +import java.util.Collections; +import java.util.Optional; +import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.MethodParameter; import org.springframework.security.core.Authentication; @@ -12,12 +16,6 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; import org.zalando.nakadi.config.SecuritySettings; - -import java.security.Principal; -import java.util.Collections; -import java.util.Optional; -import java.util.Set; - import static org.zalando.nakadi.config.SecuritySettings.AuthMode.OFF; @Component diff --git a/src/main/java/org/zalando/nakadi/security/FullAccessClient.java b/src/main/java/org/zalando/nakadi/security/FullAccessClient.java index 6585b7b74b..97bc3e37d6 100644 --- a/src/main/java/org/zalando/nakadi/security/FullAccessClient.java +++ b/src/main/java/org/zalando/nakadi/security/FullAccessClient.java @@ -1,8 +1,7 @@ package org.zalando.nakadi.security; -import org.zalando.nakadi.exceptions.IllegalScopeException; - import java.util.Set; +import org.zalando.nakadi.exceptions.IllegalScopeException; public class FullAccessClient extends Client { diff --git a/src/main/java/org/zalando/nakadi/security/NakadiClient.java b/src/main/java/org/zalando/nakadi/security/NakadiClient.java index ce944fbec7..c3873dfec7 100644 --- a/src/main/java/org/zalando/nakadi/security/NakadiClient.java +++ b/src/main/java/org/zalando/nakadi/security/NakadiClient.java @@ -1,8 +1,7 @@ package org.zalando.nakadi.security; -import org.zalando.nakadi.exceptions.IllegalScopeException; - import java.util.Set; +import org.zalando.nakadi.exceptions.IllegalScopeException; public class NakadiClient extends Client { diff --git a/src/main/java/org/zalando/nakadi/service/AuthorizationValidator.java b/src/main/java/org/zalando/nakadi/service/AuthorizationValidator.java index 70a5ee8c3f..836d26bdb0 100644 --- a/src/main/java/org/zalando/nakadi/service/AuthorizationValidator.java +++ b/src/main/java/org/zalando/nakadi/service/AuthorizationValidator.java @@ -2,6 +2,15 @@ import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; +import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.collect.Sets.newHashSet; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import javax.annotation.Nullable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.zalando.nakadi.domain.EventType; @@ -19,17 +28,6 @@ import org.zalando.nakadi.plugin.api.authz.Resource; import org.zalando.nakadi.repository.EventTypeRepository; -import javax.annotation.Nullable; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - -import static com.google.common.collect.Lists.newArrayList; -import static com.google.common.collect.Sets.newHashSet; - @Service public class AuthorizationValidator { diff --git a/src/main/java/org/zalando/nakadi/service/EventPublisher.java b/src/main/java/org/zalando/nakadi/service/EventPublisher.java index 2c9f1e211c..f7faf0f111 100644 --- a/src/main/java/org/zalando/nakadi/service/EventPublisher.java +++ b/src/main/java/org/zalando/nakadi/service/EventPublisher.java @@ -1,5 +1,11 @@ package org.zalando.nakadi.service; +import java.io.Closeable; +import java.io.IOException; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.TimeoutException; +import java.util.stream.Collectors; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,13 +38,6 @@ import org.zalando.nakadi.validation.EventTypeValidator; import org.zalando.nakadi.validation.ValidationError; -import java.io.Closeable; -import java.io.IOException; -import java.util.List; -import java.util.Optional; -import java.util.concurrent.TimeoutException; -import java.util.stream.Collectors; - @Component public class EventPublisher { diff --git a/src/main/java/org/zalando/nakadi/service/EventTypeService.java b/src/main/java/org/zalando/nakadi/service/EventTypeService.java index db84220706..93c2a5285f 100644 --- a/src/main/java/org/zalando/nakadi/service/EventTypeService.java +++ b/src/main/java/org/zalando/nakadi/service/EventTypeService.java @@ -2,6 +2,14 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; +import java.io.Closeable; +import java.io.IOException; +import java.util.Date; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.TimeoutException; +import java.util.stream.Collectors; import org.everit.json.schema.Schema; import org.everit.json.schema.SchemaException; import org.everit.json.schema.loader.SchemaLoader; @@ -49,21 +57,11 @@ import org.zalando.nakadi.service.timeline.TimelineService; import org.zalando.nakadi.service.timeline.TimelineSync; import org.zalando.nakadi.util.FeatureToggleService; +import static org.zalando.nakadi.util.FeatureToggleService.Feature.CHECK_PARTITIONS_KEYS; import org.zalando.nakadi.util.JsonUtils; import org.zalando.nakadi.validation.SchemaEvolutionService; import org.zalando.nakadi.validation.SchemaIncompatibility; -import java.io.Closeable; -import java.io.IOException; -import java.util.Date; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.concurrent.TimeoutException; -import java.util.stream.Collectors; - -import static org.zalando.nakadi.util.FeatureToggleService.Feature.CHECK_PARTITIONS_KEYS; - @Component public class EventTypeService { diff --git a/src/main/java/org/zalando/nakadi/service/subscription/SubscriptionService.java b/src/main/java/org/zalando/nakadi/service/subscription/SubscriptionService.java index 60f009ee25..29066313e8 100644 --- a/src/main/java/org/zalando/nakadi/service/subscription/SubscriptionService.java +++ b/src/main/java/org/zalando/nakadi/service/subscription/SubscriptionService.java @@ -1,6 +1,16 @@ package org.zalando.nakadi.service.subscription; import com.google.common.collect.ImmutableSet; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import javax.annotation.Nullable; +import javax.ws.rs.core.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -49,17 +59,6 @@ import org.zalando.nakadi.view.SubscriptionCursorWithoutToken; import org.zalando.problem.Problem; -import javax.annotation.Nullable; -import javax.ws.rs.core.Response; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - @Component public class SubscriptionService { diff --git a/src/main/java/org/zalando/nakadi/service/timeline/TimelineService.java b/src/main/java/org/zalando/nakadi/service/timeline/TimelineService.java index 54da514f67..67bf52f372 100644 --- a/src/main/java/org/zalando/nakadi/service/timeline/TimelineService.java +++ b/src/main/java/org/zalando/nakadi/service/timeline/TimelineService.java @@ -2,6 +2,13 @@ import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Collectors; +import javax.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -45,14 +52,6 @@ import org.zalando.nakadi.security.Client; import org.zalando.nakadi.util.UUIDGenerator; -import javax.annotation.Nullable; -import java.util.Collections; -import java.util.Date; -import java.util.List; -import java.util.Optional; -import java.util.UUID; -import java.util.stream.Collectors; - @Service public class TimelineService { diff --git a/src/test/java/org/zalando/nakadi/controller/CursorsControllerTest.java b/src/test/java/org/zalando/nakadi/controller/CursorsControllerTest.java index 6b9ea38d94..6aba2cbbea 100644 --- a/src/test/java/org/zalando/nakadi/controller/CursorsControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/CursorsControllerTest.java @@ -1,13 +1,29 @@ package org.zalando.nakadi.controller; import com.google.common.collect.ImmutableList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import static javax.ws.rs.core.Response.Status.NOT_FOUND; +import static javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE; import org.junit.Test; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; import org.mockito.Mockito; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import org.springframework.http.HttpStatus; +import static org.springframework.http.MediaType.APPLICATION_JSON; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; import org.zalando.nakadi.config.SecuritySettings; import org.zalando.nakadi.domain.CursorError; import org.zalando.nakadi.domain.ItemsWrapper; @@ -25,34 +41,16 @@ import org.zalando.nakadi.service.CursorTokenService; import org.zalando.nakadi.service.CursorsService; import org.zalando.nakadi.util.FeatureToggleService; +import static org.zalando.nakadi.util.FeatureToggleService.Feature.HIGH_LEVEL_API; import org.zalando.nakadi.utils.RandomSubscriptionBuilder; -import org.zalando.nakadi.view.CursorCommitResult; -import org.zalando.nakadi.view.SubscriptionCursor; -import org.zalando.problem.Problem; import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.buildDefaultEventType; import static org.zalando.nakadi.utils.TestUtils.createFakeTimeline; import static org.zalando.nakadi.utils.TestUtils.invalidProblem; - -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -import static javax.ws.rs.core.Response.Status.NOT_FOUND; -import static javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.springframework.http.MediaType.APPLICATION_JSON; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; -import static org.zalando.nakadi.util.FeatureToggleService.Feature.HIGH_LEVEL_API; +import org.zalando.nakadi.view.CursorCommitResult; +import org.zalando.nakadi.view.SubscriptionCursor; import static org.zalando.problem.MoreStatus.UNPROCESSABLE_ENTITY; +import org.zalando.problem.Problem; public class CursorsControllerTest { @@ -106,7 +104,7 @@ public CursorsControllerTest() throws Exception { mockMvc = standaloneSetup(controller) .setMessageConverters(new StringHttpMessageConverter(), TestUtils.JACKSON_2_HTTP_MESSAGE_CONVERTER) - .setCustomArgumentResolvers(new ClientResolver(settings, featureToggleService)) + .setCustomArgumentResolvers(new ClientResolver(settings)) .build(); } diff --git a/src/test/java/org/zalando/nakadi/controller/EventPublishingControllerTest.java b/src/test/java/org/zalando/nakadi/controller/EventPublishingControllerTest.java index 7db613b9cf..52520c92fe 100644 --- a/src/test/java/org/zalando/nakadi/controller/EventPublishingControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/EventPublishingControllerTest.java @@ -43,7 +43,6 @@ import org.zalando.nakadi.security.ClientResolver; import org.zalando.nakadi.service.BlacklistService; import org.zalando.nakadi.service.EventPublisher; -import org.zalando.nakadi.util.FeatureToggleService; import org.zalando.nakadi.utils.TestUtils; public class EventPublishingControllerTest { diff --git a/src/test/java/org/zalando/nakadi/controller/EventTypeAuthorizationTest.java b/src/test/java/org/zalando/nakadi/controller/EventTypeAuthorizationTest.java index d4cb06f968..ef58111827 100644 --- a/src/test/java/org/zalando/nakadi/controller/EventTypeAuthorizationTest.java +++ b/src/test/java/org/zalando/nakadi/controller/EventTypeAuthorizationTest.java @@ -1,6 +1,14 @@ package org.zalando.nakadi.controller; +import java.io.IOException; +import java.util.Optional; +import javax.ws.rs.core.Response; import org.junit.Test; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.zalando.nakadi.domain.EventType; import org.zalando.nakadi.domain.EventTypeResource; import org.zalando.nakadi.exceptions.UnableProcessException; @@ -11,16 +19,6 @@ import org.zalando.problem.MoreStatus; import org.zalando.problem.Problem; -import javax.ws.rs.core.Response; -import java.io.IOException; -import java.util.Optional; - -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - public class EventTypeAuthorizationTest extends EventTypeControllerTestCase { public EventTypeAuthorizationTest() throws IOException { diff --git a/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTest.java b/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTest.java index 6252f6ed62..234612537d 100644 --- a/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTest.java @@ -14,7 +14,6 @@ import java.util.Optional; import java.util.concurrent.TimeoutException; import javax.ws.rs.core.Response; -import static javax.ws.rs.core.Response.Status.FORBIDDEN; import static javax.ws.rs.core.Response.Status.NOT_FOUND; import static javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE; import static org.hamcrest.Matchers.containsString; @@ -61,7 +60,6 @@ import org.zalando.nakadi.exceptions.runtime.TopicConfigException; import org.zalando.nakadi.partitioning.PartitionStrategy; import org.zalando.nakadi.repository.TopicRepository; -import static org.zalando.nakadi.util.FeatureToggleService.Feature.CHECK_APPLICATION_LEVEL_PERMISSIONS; import org.zalando.nakadi.utils.EventTypeTestBuilder; import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.buildDefaultEventType; @@ -603,7 +601,7 @@ public void whenTopicCreationFailsRemoveEventTypeFromRepositoryAnd500() throws E @Test public void whenPUTInvalidEventTypeThen422() throws Exception { final EventType invalidEventType = buildDefaultEventType(); - final JSONObject jsonObject = new JSONObject(objectMapper.writeValueAsString(invalidEventType)); + final JSONObject jsonObject = new JSONObject(TestUtils.OBJECT_MAPPER.writeValueAsString(invalidEventType)); jsonObject.remove("category"); @@ -668,7 +666,7 @@ public void canExposeASingleEventType() throws Exception { mockMvc.perform(requestBuilder).andExpect(status().is(200)) .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)).andExpect(content().json( - asJsonString(expectedEventType))); + TestUtils.OBJECT_MAPPER.writeValueAsString(expectedEventType))); } diff --git a/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTestCase.java b/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTestCase.java index 26f108f1fc..7a6a92b85b 100644 --- a/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTestCase.java +++ b/src/test/java/org/zalando/nakadi/controller/EventTypeControllerTestCase.java @@ -40,7 +40,6 @@ import org.zalando.nakadi.service.timeline.TimelineService; import org.zalando.nakadi.service.timeline.TimelineSync; import org.zalando.nakadi.util.FeatureToggleService; -import static org.zalando.nakadi.util.FeatureToggleService.Feature.CHECK_APPLICATION_LEVEL_PERMISSIONS; import static org.zalando.nakadi.util.FeatureToggleService.Feature.CHECK_PARTITIONS_KEYS; import static org.zalando.nakadi.util.FeatureToggleService.Feature.DISABLE_EVENT_TYPE_DELETION; import static org.zalando.nakadi.util.PrincipalMockFactory.mockPrincipal; diff --git a/src/test/java/org/zalando/nakadi/controller/PartitionsControllerTest.java b/src/test/java/org/zalando/nakadi/controller/PartitionsControllerTest.java index cbc96ff111..ef89ca7d16 100644 --- a/src/test/java/org/zalando/nakadi/controller/PartitionsControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/PartitionsControllerTest.java @@ -39,7 +39,6 @@ import org.zalando.nakadi.service.CursorOperationsService; import org.zalando.nakadi.service.converter.CursorConverterImpl; import org.zalando.nakadi.service.timeline.TimelineService; -import org.zalando.nakadi.util.FeatureToggleService; import org.zalando.nakadi.utils.TestUtils; import static org.zalando.nakadi.utils.TestUtils.createFakeTimeline; import static org.zalando.nakadi.utils.TestUtils.mockAccessDeniedException; diff --git a/src/test/java/org/zalando/nakadi/controller/StoragesControllerTest.java b/src/test/java/org/zalando/nakadi/controller/StoragesControllerTest.java index f2f5f37fd8..b6fd0f29a0 100644 --- a/src/test/java/org/zalando/nakadi/controller/StoragesControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/StoragesControllerTest.java @@ -23,7 +23,6 @@ import org.zalando.nakadi.security.ClientResolver; import org.zalando.nakadi.service.Result; import org.zalando.nakadi.service.StorageService; -import org.zalando.nakadi.util.FeatureToggleService; import static org.zalando.nakadi.util.PrincipalMockFactory.mockPrincipal; import org.zalando.nakadi.utils.TestUtils; import static org.zalando.problem.MoreStatus.UNPROCESSABLE_ENTITY; diff --git a/src/test/java/org/zalando/nakadi/controller/SubscriptionControllerTest.java b/src/test/java/org/zalando/nakadi/controller/SubscriptionControllerTest.java index 7c014a1d75..0fd80641f3 100644 --- a/src/test/java/org/zalando/nakadi/controller/SubscriptionControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/SubscriptionControllerTest.java @@ -9,7 +9,6 @@ import java.util.Set; import javax.ws.rs.core.Response; import static javax.ws.rs.core.Response.Status.BAD_REQUEST; -import static javax.ws.rs.core.Response.Status.FORBIDDEN; import static javax.ws.rs.core.Response.Status.NOT_FOUND; import static javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE; import org.junit.Test; @@ -299,14 +298,6 @@ public void whenDeleteSubscriptionThatDoesNotExistThenNotFound() throws Exceptio Problem.valueOf(NOT_FOUND, "dummy message")); } - @Test - public void whenDeleteSubscriptionAndOwningAppDoesNotMatchThenForbidden() throws Exception { - mockGetFromRepoSubscriptionWithOwningApp("sid", "wrongApp"); - checkForProblem( - mockMvcBuilder.build().perform(delete("/subscriptions/sid")), - Problem.valueOf(FORBIDDEN, "You don't have access to this subscription")); - } - private void mockGetFromRepoSubscriptionWithOwningApp(final String subscriptionId, final String owningApplication) throws NoSuchSubscriptionException, ServiceUnavailableException { final Subscription subscription = RandomSubscriptionBuilder.builder() diff --git a/src/test/java/org/zalando/nakadi/controller/TimelinesControllerTest.java b/src/test/java/org/zalando/nakadi/controller/TimelinesControllerTest.java index d9d46f2af6..c0f98fd0d0 100644 --- a/src/test/java/org/zalando/nakadi/controller/TimelinesControllerTest.java +++ b/src/test/java/org/zalando/nakadi/controller/TimelinesControllerTest.java @@ -49,6 +49,7 @@ public TimelinesControllerTest() { mockMvc = MockMvcBuilders.standaloneSetup(controller) .setMessageConverters(new StringHttpMessageConverter(), TestUtils.JACKSON_2_HTTP_MESSAGE_CONVERTER) .setCustomArgumentResolvers(new ClientResolver(securitySettings)) + .setControllerAdvice(new ExceptionHandling()) .build(); } diff --git a/src/test/java/org/zalando/nakadi/security/AuthenticationTest.java b/src/test/java/org/zalando/nakadi/security/AuthenticationTest.java index feb8e2351f..44d6e3baf7 100644 --- a/src/test/java/org/zalando/nakadi/security/AuthenticationTest.java +++ b/src/test/java/org/zalando/nakadi/security/AuthenticationTest.java @@ -4,9 +4,21 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import javax.annotation.PostConstruct; +import javax.servlet.Filter; +import static org.hamcrest.Matchers.isOneOf; +import static org.hamcrest.Matchers.not; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; @@ -15,16 +27,24 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; +import static org.springframework.http.HttpMethod.DELETE; +import static org.springframework.http.HttpMethod.GET; +import static org.springframework.http.HttpMethod.POST; +import static org.springframework.http.HttpMethod.PUT; +import static org.springframework.http.HttpStatus.FORBIDDEN; +import static org.springframework.http.HttpStatus.UNAUTHORIZED; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.security.oauth2.provider.OAuth2Request; import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices; import org.springframework.test.annotation.DirtiesContext; +import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultMatcher; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import org.zalando.nakadi.Application; @@ -47,30 +67,8 @@ import org.zalando.nakadi.service.timeline.TimelineSync; import org.zalando.nakadi.util.FeatureToggleService; import org.zalando.nakadi.util.UUIDGenerator; -import org.zalando.stups.oauth2.spring.security.expression.ExtendedOAuth2WebSecurityExpressionHandler; - -import javax.annotation.PostConstruct; -import javax.servlet.Filter; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import static org.hamcrest.Matchers.isOneOf; -import static org.hamcrest.Matchers.not; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.springframework.http.HttpMethod.DELETE; -import static org.springframework.http.HttpMethod.GET; -import static org.springframework.http.HttpMethod.POST; -import static org.springframework.http.HttpMethod.PUT; -import static org.springframework.http.HttpStatus.FORBIDDEN; -import static org.springframework.http.HttpStatus.UNAUTHORIZED; -import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.zalando.nakadi.utils.TestUtils.randomUUID; +import org.zalando.stups.oauth2.spring.security.expression.ExtendedOAuth2WebSecurityExpressionHandler; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes=Application.class, webEnvironment= WebEnvironment.RANDOM_PORT) diff --git a/src/test/java/org/zalando/nakadi/security/RealmModeAuthenticationTest.java b/src/test/java/org/zalando/nakadi/security/RealmModeAuthenticationTest.java index 7d9e304bb0..21e3f3b5df 100644 --- a/src/test/java/org/zalando/nakadi/security/RealmModeAuthenticationTest.java +++ b/src/test/java/org/zalando/nakadi/security/RealmModeAuthenticationTest.java @@ -1,9 +1,8 @@ package org.zalando.nakadi.security; import org.junit.Test; -import org.zalando.nakadi.config.SecuritySettings; - import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import org.zalando.nakadi.config.SecuritySettings; public class RealmModeAuthenticationTest extends AuthenticationTest { diff --git a/src/test/java/org/zalando/nakadi/service/AuthorizationValidatorTest.java b/src/test/java/org/zalando/nakadi/service/AuthorizationValidatorTest.java index c3641bbdfa..b05581d5a8 100644 --- a/src/test/java/org/zalando/nakadi/service/AuthorizationValidatorTest.java +++ b/src/test/java/org/zalando/nakadi/service/AuthorizationValidatorTest.java @@ -2,7 +2,13 @@ import com.google.common.collect.ImmutableList; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; import org.junit.Test; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import org.zalando.nakadi.domain.EventTypeAuthorization; import org.zalando.nakadi.domain.EventTypeAuthorizationAttribute; import org.zalando.nakadi.exceptions.UnableProcessException; @@ -14,13 +20,6 @@ import org.zalando.nakadi.repository.EventTypeRepository; import org.zalando.nakadi.utils.EventTypeTestBuilder; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - public class AuthorizationValidatorTest { private final AuthorizationValidator validator; diff --git a/src/test/java/org/zalando/nakadi/service/EventPublisherTest.java b/src/test/java/org/zalando/nakadi/service/EventPublisherTest.java index bde171aa7c..842b403cf0 100644 --- a/src/test/java/org/zalando/nakadi/service/EventPublisherTest.java +++ b/src/test/java/org/zalando/nakadi/service/EventPublisherTest.java @@ -1,10 +1,29 @@ package org.zalando.nakadi.service; +import java.io.Closeable; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.TimeoutException; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.isEmptyString; +import static org.hamcrest.Matchers.startsWith; +import static org.hamcrest.core.IsEqual.equalTo; import org.json.JSONArray; import org.json.JSONObject; import org.junit.Assert; import org.junit.Test; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyLong; +import static org.mockito.Matchers.eq; import org.mockito.Mockito; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import org.zalando.nakadi.config.NakadiSettings; import org.zalando.nakadi.domain.BatchItem; import org.zalando.nakadi.domain.BatchItemResponse; @@ -30,35 +49,14 @@ import org.zalando.nakadi.service.timeline.TimelineService; import org.zalando.nakadi.service.timeline.TimelineSync; import org.zalando.nakadi.utils.EventTypeTestBuilder; -import org.zalando.nakadi.validation.EventTypeValidator; -import org.zalando.nakadi.validation.ValidationError; - -import java.io.Closeable; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.Set; -import java.util.concurrent.TimeoutException; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.isEmptyString; -import static org.hamcrest.Matchers.startsWith; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyLong; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; import static org.zalando.nakadi.utils.TestUtils.buildBusinessEvent; import static org.zalando.nakadi.utils.TestUtils.buildDefaultEventType; import static org.zalando.nakadi.utils.TestUtils.createBatchItem; import static org.zalando.nakadi.utils.TestUtils.randomString; import static org.zalando.nakadi.utils.TestUtils.randomStringOfLength; import static org.zalando.nakadi.utils.TestUtils.randomValidStringOfLength; +import org.zalando.nakadi.validation.EventTypeValidator; +import org.zalando.nakadi.validation.ValidationError; public class EventPublisherTest { diff --git a/src/test/java/org/zalando/nakadi/service/EventTypeServiceTest.java b/src/test/java/org/zalando/nakadi/service/EventTypeServiceTest.java index ea5c68f0bd..0a85719635 100644 --- a/src/test/java/org/zalando/nakadi/service/EventTypeServiceTest.java +++ b/src/test/java/org/zalando/nakadi/service/EventTypeServiceTest.java @@ -2,8 +2,17 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; +import java.util.ArrayList; +import java.util.Optional; +import static org.junit.Assert.fail; import org.junit.Before; import org.junit.Test; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; import org.springframework.transaction.support.TransactionCallback; import org.springframework.transaction.support.TransactionTemplate; import org.zalando.nakadi.config.NakadiSettings; @@ -20,19 +29,8 @@ import org.zalando.nakadi.service.timeline.TimelineService; import org.zalando.nakadi.service.timeline.TimelineSync; import org.zalando.nakadi.util.FeatureToggleService; -import org.zalando.nakadi.validation.SchemaEvolutionService; - -import java.util.ArrayList; -import java.util.Optional; - -import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verifyZeroInteractions; -import static org.mockito.Mockito.when; import static org.zalando.nakadi.utils.TestUtils.buildDefaultEventType; +import org.zalando.nakadi.validation.SchemaEvolutionService; public class EventTypeServiceTest { From 70b3fd86680ddbf6a9a1c69839e81ff5c06bd46a Mon Sep 17 00:00:00 2001 From: antban Date: Mon, 31 Jul 2017 11:55:54 +0200 Subject: [PATCH 21/21] Merge remote-tracking branch 'origin/master' into ARUHA-882 # Conflicts: # src/acceptance-test/java/org/zalando/nakadi/repository/db/AbstractDbRepositoryTest.java # src/acceptance-test/java/org/zalando/nakadi/repository/db/EventTypeDbRepositoryTest.java # src/acceptance-test/java/org/zalando/nakadi/repository/db/SubscriptionDbRepositoryTest.java # src/acceptance-test/java/org/zalando/nakadi/webservice/BaseAT.java # src/acceptance-test/java/org/zalando/nakadi/webservice/PartitionsControllerAT.java # src/acceptance-test/java/org/zalando/nakadi/webservice/hila/HilaRebalanceAT.java # src/acceptance-test/java/org/zalando/nakadi/webservice/hila/SubscriptionAT.java # src/acceptance-test/java/org/zalando/nakadi/webservice/timelines/SubscriptionConsumptionTest.java # src/acceptance-test/java/org/zalando/nakadi/webservice/timelines/TimelineConsumptionTest.java # src/main/java/org/zalando/nakadi/security/ClientResolver.java # src/test/java/org/zalando/nakadi/controller/CursorsControllerTest.java # src/test/java/org/zalando/nakadi/controller/EventPublishingControllerTest.java # src/test/java/org/zalando/nakadi/controller/EventStreamControllerTest.java # src/test/java/org/zalando/nakadi/controller/EventTypeControllerTestCase.java # src/test/java/org/zalando/nakadi/controller/PartitionsControllerTest.java # src/test/java/org/zalando/nakadi/controller/PostSubscriptionControllerTest.java # src/test/java/org/zalando/nakadi/controller/StoragesControllerTest.java # src/test/java/org/zalando/nakadi/controller/TimelinesControllerTest.java # src/test/java/org/zalando/nakadi/domain/BatchItemResponseTest.java # src/test/java/org/zalando/nakadi/domain/EventTypeTest.java # src/test/java/org/zalando/nakadi/repository/kafka/PartitionsCalculatorTest.java # src/test/java/org/zalando/nakadi/service/subscription/state/PartitionDataTest.java --- .../webservice/PartitionsControllerAT.java | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/acceptance-test/java/org/zalando/nakadi/webservice/PartitionsControllerAT.java b/src/acceptance-test/java/org/zalando/nakadi/webservice/PartitionsControllerAT.java index 86acd4d31b..b0c87c3c12 100644 --- a/src/acceptance-test/java/org/zalando/nakadi/webservice/PartitionsControllerAT.java +++ b/src/acceptance-test/java/org/zalando/nakadi/webservice/PartitionsControllerAT.java @@ -1,18 +1,19 @@ package org.zalando.nakadi.webservice; import com.fasterxml.jackson.core.JsonProcessingException; -import static com.jayway.restassured.RestAssured.get; -import static com.jayway.restassured.RestAssured.when; import com.jayway.restassured.response.Response; import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.ProducerRecord; import org.apache.kafka.common.PartitionInfo; import org.hamcrest.Matchers; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.springframework.http.HttpStatus; import org.zalando.nakadi.domain.EventType; +import org.zalando.nakadi.exceptions.NoSuchEventTypeException; import org.zalando.nakadi.repository.kafka.KafkaTestHelper; +import org.zalando.nakadi.utils.EventTypeTestBuilder; import org.zalando.nakadi.view.Cursor; import org.zalando.nakadi.webservice.utils.NakadiTestUtils; @@ -22,28 +23,14 @@ import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; -import org.apache.kafka.clients.producer.KafkaProducer; -import org.apache.kafka.clients.producer.ProducerRecord; -import org.apache.kafka.common.PartitionInfo; import static com.jayway.restassured.RestAssured.get; import static com.jayway.restassured.RestAssured.when; import static org.hamcrest.MatcherAssert.assertThat; -import org.hamcrest.Matchers; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.springframework.http.HttpStatus; -import org.zalando.nakadi.domain.EventType; -import org.zalando.nakadi.exceptions.NoSuchEventTypeException; -import org.zalando.nakadi.repository.kafka.KafkaTestHelper; -import org.zalando.nakadi.utils.EventTypeTestBuilder; -import org.zalando.nakadi.view.Cursor; import static org.zalando.nakadi.webservice.utils.JsonTestHelper.asMap; import static org.zalando.nakadi.webservice.utils.JsonTestHelper.asMapsList; -import org.zalando.nakadi.webservice.utils.NakadiTestUtils; public class PartitionsControllerAT extends BaseAT {