Java Code Examples for org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody#writeTo()

The following examples show how to use org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody#writeTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EventStreamControllerTest.java    From nakadi with MIT License 6 votes vote down vote up
@Test
public void whenNoCursorsThenLatestOffsetsAreUsed() throws IOException, InvalidCursorException {
    when(eventTypeCache.getEventType(TEST_EVENT_TYPE_NAME)).thenReturn(EVENT_TYPE);
    final List<PartitionStatistics> tps2 = ImmutableList.of(
            new KafkaPartitionStatistics(timeline, 0, 0, 87),
            new KafkaPartitionStatistics(timeline, 1, 0, 34));
    when(timelineService.getActiveTimeline(any(EventType.class))).thenReturn(timeline);
    when(topicRepositoryMock.loadTopicStatistics(eq(Collections.singletonList(timeline))))
            .thenReturn(tps2);

    final ArgumentCaptor<EventStreamConfig> configCaptor = ArgumentCaptor.forClass(EventStreamConfig.class);
    final EventStream eventStreamMock = mock(EventStream.class);
    when(eventStreamFactoryMock.createEventStream(any(), any(), configCaptor.capture(), any()))
            .thenReturn(eventStreamMock);

    final StreamingResponseBody responseBody = createStreamingResponseBody(1, 0, 1, 1, 0, null);
    responseBody.writeTo(new ByteArrayOutputStream());

    final EventStreamConfig streamConfig = configCaptor.getValue();
    assertThat(
            streamConfig.getCursors(),
            equalTo(tps2.stream().map(PartitionStatistics::getLast).collect(Collectors.toList())));
}
 
Example 2
Source File: TransformationService.java    From data-prep with Apache License 2.0 6 votes vote down vote up
/**
 * Add the following preparation in cache.
 *
 * @param preparation the preparation to cache.
 * @param stepId the preparation step id.
 */
private void addPreparationInCache(PreparationDTO preparation, String stepId) {
    final ExportParameters exportParameters = new ExportParameters();
    exportParameters.setPreparationId(preparation.getId());
    exportParameters.setExportType("JSON");
    exportParameters.setStepId(stepId);
    exportParameters.setDatasetId(preparation.getDataSetId());

    final StreamingResponseBody streamingResponseBody = executeSampleExportStrategy(exportParameters);
    try {
        // the result is not important here as it will be cached !
        streamingResponseBody.writeTo(new NullOutputStream());
    } catch (IOException e) {
        throw new TDPException(UNEXPECTED_EXCEPTION, e);
    }
}
 
Example 3
Source File: PreparationExportStrategyTest.java    From data-prep with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUsedVersionedPreparation() throws IOException {
    // Given
    final ExportParameters parameters = new ExportParameters();
    parameters.setExportType("JSON");
    parameters.setPreparationId("prep-1234");
    parameters.setStepId("step-1234");

    final PreparationDTO preparation = new PreparationDTO();
    preparation.setId("prep-1234");
    preparation.setHeadId("step-1234");
    configurePreparation(preparation, "prep-1234", "step-1234");

    // When
    final StreamingResponseBody body = strategy.execute(parameters);
    body.writeTo(new NullOutputStream());

    // Then
    final ArgumentCaptor<Configuration> captor = ArgumentCaptor.forClass(Configuration.class);
    verify(transformer).buildExecutable(any(), captor.capture());
    assertEquals("prep-1234", captor.getValue().getPreparationId());
    assertEquals("step-1234", captor.getValue().getPreparation().getHeadId());
}
 
Example 4
Source File: PreparationExportStrategyTest.java    From data-prep with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUsedHeadPreparation() throws IOException {
    // Given
    final ExportParameters parameters = new ExportParameters();
    parameters.setExportType("JSON");
    parameters.setPreparationId("prep-1234");
    parameters.setStepId("head");

    final PreparationDTO preparation = new PreparationDTO();
    preparation.getSteps().add(Step.ROOT_STEP.id());
    preparation.setId("prep-1234");
    preparation.setHeadId("head");
    configurePreparation(preparation, "prep-1234", "head");

    // When
    final StreamingResponseBody body = strategy.execute(parameters);
    body.writeTo(new NullOutputStream());

    // Then
    final ArgumentCaptor<Configuration> captor = ArgumentCaptor.forClass(Configuration.class);
    verify(transformer).buildExecutable(any(), captor.capture());
    assertEquals("prep-1234", captor.getValue().getPreparationId());
    assertEquals("head", captor.getValue().getPreparation().getHeadId());
}
 
Example 5
Source File: CommandHelperTest.java    From data-prep with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommandToStreamingWithNoHeader() throws Exception {
    HystrixCommand<InputStream> command = new CommandHelperTestCommand();

    final StreamingResponseBody responseBody = CommandHelper.toStreaming(command);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    responseBody.writeTo(outputStream);
    assertEquals("test", new String(outputStream.toByteArray()));
}
 
Example 6
Source File: EventStreamControllerTest.java    From nakadi with MIT License 4 votes vote down vote up
@Test
public void whenNormalCaseThenParametersArePassedToConfigAndStreamStarted() throws Exception {
    final EventConsumer eventConsumerMock = mock(EventConsumer.class);
    when(eventTypeCache.getEventType(TEST_EVENT_TYPE_NAME)).thenReturn(EVENT_TYPE);
    when(timelineService.createEventConsumer(
            eq(KAFKA_CLIENT_ID), eq(ImmutableList.of(NakadiCursor.of(timeline, "0", "000000000000000000")))))
            .thenReturn(eventConsumerMock);
    when(timelineService.getActiveTimeline(eq(EVENT_TYPE))).thenReturn(timeline);

    final ArgumentCaptor<Integer> statusCaptor = getStatusCaptor();
    final ArgumentCaptor<String> contentTypeCaptor = getContentTypeCaptor();

    final ArgumentCaptor<EventStreamConfig> configCaptor = ArgumentCaptor.forClass(EventStreamConfig.class);
    final EventStream eventStreamMock = mock(EventStream.class);
    when(eventStreamFactoryMock.createEventStream(any(), any(), configCaptor.capture(), any()))
            .thenReturn(eventStreamMock);

    final StreamingResponseBody responseBody = createStreamingResponseBody(1, 2, 3, 4, 5,
            "[{\"partition\":\"0\",\"offset\":\"000000000000000000\"}]");
    final OutputStream outputStream = mock(OutputStream.class);
    responseBody.writeTo(outputStream);

    final EventStreamConfig streamConfig = configCaptor.getValue();
    assertThat(
            streamConfig,
            equalTo(EventStreamConfig
                    .builder()
                    .withCursors(ImmutableList.of(
                            NakadiCursor.of(timeline, "0", "000000000000000000")))
                    .withBatchLimit(1)
                    .withStreamLimit(2)
                    .withBatchTimeout(3)
                    .withStreamTimeout(4)
                    .withStreamKeepAliveLimit(5)
                    .build()
            ));

    assertThat(statusCaptor.getValue(), equalTo(HttpStatus.OK.value()));
    assertThat(contentTypeCaptor.getValue(), equalTo("application/x-json-stream"));

    verify(timelineService, times(1)).createEventConsumer(eq(KAFKA_CLIENT_ID),
            eq(ImmutableList.of(NakadiCursor.of(timeline, "0", "000000000000000000"))));
    verify(eventStreamFactoryMock, times(1)).createEventStream(eq(outputStream),
            eq(eventConsumerMock), eq(streamConfig), any());
    verify(eventStreamMock, times(1)).streamEvents(any(), any());
    verify(outputStream, times(2)).flush();
    verify(outputStream, times(1)).close();
}
 
Example 7
Source File: EventStreamControllerTest.java    From nakadi with MIT License 4 votes vote down vote up
private void writeStream() throws Exception {
    final StreamingResponseBody responseBody = createStreamingResponseBody(new NakadiClient("clientId", ""));
    final OutputStream outputStream = mock(OutputStream.class);
    responseBody.writeTo(outputStream);
}
 
Example 8
Source File: EventStreamControllerTest.java    From nakadi with MIT License 4 votes vote down vote up
protected String responseToString(final StreamingResponseBody responseBody) throws IOException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    responseBody.writeTo(out);
    return out.toString();
}