Search in sources :

Example 1 with BatchItem

use of org.zalando.nakadi.domain.BatchItem in project nakadi by zalando.

the class EventPublisherTest method whenPartitionFailsThenResultIsAborted.

@Test
public void whenPartitionFailsThenResultIsAborted() throws Exception {
    final EventType eventType = buildDefaultEventType();
    final List<BatchItem> batch = new ArrayList<>();
    batch.add(createBatchItem(buildDefaultBatch(1).getJSONObject(0)));
    final JSONObject event = batch.get(0).getEvent();
    mockSuccessfulValidation(eventType);
    mockFaultPartition();
    final EventPublishResult result = publisher.publish(createStringFromBatchItems(batch), eventType.getName());
    assertThat(result.getStatus(), equalTo(EventPublishingStatus.ABORTED));
}
Also used : EventPublishResult(org.zalando.nakadi.domain.EventPublishResult) JSONObject(org.json.JSONObject) TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) EventType(org.zalando.nakadi.domain.EventType) TestUtils.createBatchItem(org.zalando.nakadi.utils.TestUtils.createBatchItem) BatchItem(org.zalando.nakadi.domain.BatchItem) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with BatchItem

use of org.zalando.nakadi.domain.BatchItem in project nakadi by zalando.

the class EventPublisherTest method whenPartitionFailsThenSubsequentItemsAreAborted.

@Test
public void whenPartitionFailsThenSubsequentItemsAreAborted() throws Exception {
    final EventType eventType = buildDefaultEventType();
    final JSONArray array = buildDefaultBatch(2);
    final List<BatchItem> batch = new ArrayList<>();
    batch.add(createBatchItem(array.getJSONObject(0)));
    batch.add(createBatchItem(array.getJSONObject(1)));
    mockSuccessfulValidation(eventType);
    mockFaultPartition();
    final EventPublishResult result = publisher.publish(createStringFromBatchItems(batch), eventType.getName());
    assertThat(result.getStatus(), equalTo(EventPublishingStatus.ABORTED));
    final BatchItemResponse first = result.getResponses().get(0);
    assertThat(first.getPublishingStatus(), equalTo(EventPublishingStatus.FAILED));
    assertThat(first.getStep(), equalTo(EventPublishingStep.PARTITIONING));
    assertThat(first.getDetail(), equalTo("partition error"));
    final BatchItemResponse second = result.getResponses().get(1);
    assertThat(second.getPublishingStatus(), equalTo(EventPublishingStatus.ABORTED));
    assertThat(second.getStep(), equalTo(EventPublishingStep.VALIDATING));
    assertThat(second.getDetail(), is(isEmptyString()));
    verify(cache, times(2)).getValidator(any());
    verify(partitionResolver, times(1)).resolvePartition(any(), any());
}
Also used : EventPublishResult(org.zalando.nakadi.domain.EventPublishResult) TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) EventType(org.zalando.nakadi.domain.EventType) JSONArray(org.json.JSONArray) TestUtils.createBatchItem(org.zalando.nakadi.utils.TestUtils.createBatchItem) BatchItem(org.zalando.nakadi.domain.BatchItem) ArrayList(java.util.ArrayList) BatchItemResponse(org.zalando.nakadi.domain.BatchItemResponse) Test(org.junit.Test)

Example 3 with BatchItem

use of org.zalando.nakadi.domain.BatchItem in project nakadi by zalando.

the class EnrichmentTest method enrichAppliesStrategies.

@Test
public void enrichAppliesStrategies() throws Exception {
    final EventType eventType = buildDefaultEventType();
    eventType.getEnrichmentStrategies().add(EnrichmentStrategyDescriptor.METADATA_ENRICHMENT);
    final JSONObject event = new JSONObject();
    final BatchItem batchItem = createBatchItem(event);
    final EnrichmentStrategy strategy = mock(EnrichmentStrategy.class);
    Mockito.doReturn(strategy).when(registry).getStrategy(EnrichmentStrategyDescriptor.METADATA_ENRICHMENT);
    enrichment.enrich(batchItem, eventType);
    verify(strategy, times(1)).enrich(batchItem, eventType);
}
Also used : JSONObject(org.json.JSONObject) EventType(org.zalando.nakadi.domain.EventType) TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) TestUtils.createBatchItem(org.zalando.nakadi.utils.TestUtils.createBatchItem) BatchItem(org.zalando.nakadi.domain.BatchItem) Test(org.junit.Test)

Example 4 with BatchItem

use of org.zalando.nakadi.domain.BatchItem in project nakadi by zalando.

the class KafkaTopicRepositoryTest method whenPostEventTimesOutThenUpdateItemStatus.

@Test
public void whenPostEventTimesOutThenUpdateItemStatus() throws Exception {
    final BatchItem item = new BatchItem("{}", BatchItem.EmptyInjectionConfiguration.build(1, true), new BatchItem.InjectionConfiguration[BatchItem.Injection.values().length], Collections.emptyList());
    item.setPartition("1");
    final List<BatchItem> batch = new ArrayList<>();
    batch.add(item);
    when(kafkaProducer.partitionsFor(EXPECTED_PRODUCER_RECORD.topic())).thenReturn(ImmutableList.of(new PartitionInfo(EXPECTED_PRODUCER_RECORD.topic(), 1, new Node(1, "host", 9091), null, null)));
    when(nakadiSettings.getKafkaSendTimeoutMs()).thenReturn((long) 100);
    Mockito.doReturn(mock(Future.class)).when(kafkaProducer).send(any(), any());
    try {
        kafkaTopicRepository.syncPostBatch(EXPECTED_PRODUCER_RECORD.topic(), batch);
        fail();
    } catch (final EventPublishingException e) {
        assertThat(item.getResponse().getPublishingStatus(), equalTo(EventPublishingStatus.FAILED));
        assertThat(item.getResponse().getDetail(), equalTo("timed out"));
    }
}
Also used : Node(org.apache.kafka.common.Node) BatchItem(org.zalando.nakadi.domain.BatchItem) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) PartitionInfo(org.apache.kafka.common.PartitionInfo) EventPublishingException(org.zalando.nakadi.exceptions.EventPublishingException) Test(org.junit.Test)

Example 5 with BatchItem

use of org.zalando.nakadi.domain.BatchItem in project nakadi by zalando.

the class MetadataEnrichmentStrategyTest method whenFlowIsEmptyStringOverrideIt.

@Test
public void whenFlowIsEmptyStringOverrideIt() throws Exception {
    final EventType eventType = buildDefaultEventType();
    final JSONObject event = buildBusinessEvent();
    event.getJSONObject("metadata").put("flow_id", "");
    final BatchItem batch = createBatchItem(event);
    final String flowId = randomString();
    FlowIdUtils.push(flowId);
    strategy.enrich(batch, eventType);
    assertThat(batch.getEvent().getJSONObject("metadata").getString("flow_id"), equalTo(flowId));
}
Also used : JSONObject(org.json.JSONObject) EventType(org.zalando.nakadi.domain.EventType) TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) TestUtils.createBatchItem(org.zalando.nakadi.utils.TestUtils.createBatchItem) BatchItem(org.zalando.nakadi.domain.BatchItem) TestUtils.randomString(org.zalando.nakadi.utils.TestUtils.randomString) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Test(org.junit.Test)

Aggregations

BatchItem (org.zalando.nakadi.domain.BatchItem)23 Test (org.junit.Test)16 EventType (org.zalando.nakadi.domain.EventType)12 TestUtils.createBatchItem (org.zalando.nakadi.utils.TestUtils.createBatchItem)12 TestUtils.buildDefaultEventType (org.zalando.nakadi.utils.TestUtils.buildDefaultEventType)11 JSONObject (org.json.JSONObject)10 ArrayList (java.util.ArrayList)8 EventPublishingException (org.zalando.nakadi.exceptions.EventPublishingException)7 PartitionInfo (org.apache.kafka.common.PartitionInfo)6 Collections (java.util.Collections)3 List (java.util.List)3 Set (java.util.Set)3 TimeoutException (java.util.concurrent.TimeoutException)3 Collectors (java.util.stream.Collectors)3 Collectors.toList (java.util.stream.Collectors.toList)3 Consumer (org.apache.kafka.clients.consumer.Consumer)3 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)3 Node (org.apache.kafka.common.Node)3 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)3 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)3