Search in sources :

Example 1 with FileMarker

use of org.springframework.integration.file.splitter.FileSplitter.FileMarker in project spring-integration by spring-projects.

the class FileSplitterTests method testFileSplitterReactive.

@Test
public void testFileSplitterReactive() {
    FluxMessageChannel outputChannel = new FluxMessageChannel();
    FileSplitter splitter = new FileSplitter(true, true);
    splitter.setApplySequence(true);
    splitter.setOutputChannel(outputChannel);
    splitter.handleMessage(new GenericMessage<>(file));
    StepVerifier.create(outputChannel).assertNext(m -> {
        assertThat(m, hasHeaderKey(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE));
        assertThat(m, hasHeader(FileHeaders.MARKER, "START"));
        assertThat(m, hasPayload(instanceOf(FileSplitter.FileMarker.class)));
        FileMarker fileMarker = (FileSplitter.FileMarker) m.getPayload();
        assertEquals(FileMarker.Mark.START, fileMarker.getMark());
        assertEquals(file.getAbsolutePath(), fileMarker.getFilePath());
    }).expectNextCount(2).assertNext(m -> {
        assertThat(m, hasHeader(FileHeaders.MARKER, "END"));
        assertThat(m, hasPayload(instanceOf(FileSplitter.FileMarker.class)));
        FileMarker fileMarker = (FileSplitter.FileMarker) m.getPayload();
        assertEquals(FileMarker.Mark.END, fileMarker.getMark());
        assertEquals(file.getAbsolutePath(), fileMarker.getFilePath());
        assertEquals(2, fileMarker.getLineCount());
    }).then(() -> ((Subscriber<?>) TestUtils.getPropertyValue(outputChannel, "subscribers", List.class).get(0)).onComplete()).verifyComplete();
}
Also used : Subscriber(org.reactivestreams.Subscriber) FileMarker(org.springframework.integration.file.splitter.FileSplitter.FileMarker) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) Test(org.junit.Test)

Example 2 with FileMarker

use of org.springframework.integration.file.splitter.FileSplitter.FileMarker in project spring-integration by spring-projects.

the class FileSplitterTests method testFirstLineAsHeaderOnlyHeader.

@Test
public void testFirstLineAsHeaderOnlyHeader() throws IOException {
    QueueChannel outputChannel = new QueueChannel();
    FileSplitter splitter = new FileSplitter(true, true);
    splitter.setFirstLineAsHeader("firstLine");
    splitter.setOutputChannel(outputChannel);
    File file = File.createTempFile("empty", ".txt");
    splitter.handleMessage(new GenericMessage<>(file));
    Message<?> received = outputChannel.receive(0);
    assertNotNull(received);
    assertNull(received.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE));
    assertNull(received.getHeaders().get("firstLine"));
    assertEquals("START", received.getHeaders().get(FileHeaders.MARKER));
    assertThat(received.getPayload(), instanceOf(FileSplitter.FileMarker.class));
    FileMarker fileMarker = (FileSplitter.FileMarker) received.getPayload();
    assertEquals(FileSplitter.FileMarker.Mark.START, fileMarker.getMark());
    assertEquals(file.getAbsolutePath(), fileMarker.getFilePath());
    received = outputChannel.receive(0);
    assertNotNull(received);
    assertEquals("END", received.getHeaders().get(FileHeaders.MARKER));
    assertNull(received.getHeaders().get("firstLine"));
    assertThat(received.getPayload(), instanceOf(FileSplitter.FileMarker.class));
    fileMarker = (FileSplitter.FileMarker) received.getPayload();
    assertEquals(FileSplitter.FileMarker.Mark.END, fileMarker.getMark());
    assertEquals(file.getAbsolutePath(), fileMarker.getFilePath());
    assertEquals(0, fileMarker.getLineCount());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) FileMarker(org.springframework.integration.file.splitter.FileSplitter.FileMarker) File(java.io.File) Test(org.junit.Test)

Example 3 with FileMarker

use of org.springframework.integration.file.splitter.FileSplitter.FileMarker in project spring-integration by spring-projects.

the class FileSplitterTests method testMarkers.

@Test
public void testMarkers() {
    QueueChannel outputChannel = new QueueChannel();
    FileSplitter splitter = new FileSplitter(true, true);
    splitter.setOutputChannel(outputChannel);
    splitter.handleMessage(new GenericMessage<File>(file));
    Message<?> received = outputChannel.receive(0);
    assertNotNull(received);
    assertNull(received.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE));
    assertEquals("START", received.getHeaders().get(FileHeaders.MARKER));
    assertThat(received.getPayload(), instanceOf(FileSplitter.FileMarker.class));
    FileMarker fileMarker = (FileSplitter.FileMarker) received.getPayload();
    assertEquals(FileSplitter.FileMarker.Mark.START, fileMarker.getMark());
    assertEquals(file.getAbsolutePath(), fileMarker.getFilePath());
    assertNotNull(outputChannel.receive(0));
    assertNotNull(outputChannel.receive(0));
    received = outputChannel.receive(0);
    assertNotNull(received);
    assertEquals("END", received.getHeaders().get(FileHeaders.MARKER));
    assertThat(received.getPayload(), instanceOf(FileSplitter.FileMarker.class));
    fileMarker = (FileSplitter.FileMarker) received.getPayload();
    assertEquals(FileSplitter.FileMarker.Mark.END, fileMarker.getMark());
    assertEquals(file.getAbsolutePath(), fileMarker.getFilePath());
    assertEquals(2, fileMarker.getLineCount());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) FileMarker(org.springframework.integration.file.splitter.FileSplitter.FileMarker) File(java.io.File) Test(org.junit.Test)

Example 4 with FileMarker

use of org.springframework.integration.file.splitter.FileSplitter.FileMarker in project spring-integration by spring-projects.

the class FileSplitterTests method testMarkersEmptyFile.

@Test
public void testMarkersEmptyFile() throws IOException {
    QueueChannel outputChannel = new QueueChannel();
    FileSplitter splitter = new FileSplitter(true, true);
    splitter.setOutputChannel(outputChannel);
    File file = File.createTempFile("empty", ".txt");
    splitter.handleMessage(new GenericMessage<File>(file));
    Message<?> received = outputChannel.receive(0);
    assertNotNull(received);
    assertNull(received.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE));
    assertEquals("START", received.getHeaders().get(FileHeaders.MARKER));
    assertThat(received.getPayload(), instanceOf(FileSplitter.FileMarker.class));
    FileMarker fileMarker = (FileSplitter.FileMarker) received.getPayload();
    assertEquals(FileMarker.Mark.START, fileMarker.getMark());
    assertEquals(file.getAbsolutePath(), fileMarker.getFilePath());
    assertEquals(0, fileMarker.getLineCount());
    received = outputChannel.receive(0);
    assertNotNull(received);
    assertEquals("END", received.getHeaders().get(FileHeaders.MARKER));
    assertThat(received.getPayload(), instanceOf(FileSplitter.FileMarker.class));
    fileMarker = (FileSplitter.FileMarker) received.getPayload();
    assertEquals(FileMarker.Mark.END, fileMarker.getMark());
    assertEquals(file.getAbsolutePath(), fileMarker.getFilePath());
    assertEquals(0, fileMarker.getLineCount());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) FileMarker(org.springframework.integration.file.splitter.FileSplitter.FileMarker) File(java.io.File) Test(org.junit.Test)

Example 5 with FileMarker

use of org.springframework.integration.file.splitter.FileSplitter.FileMarker in project spring-integration by spring-projects.

the class FileSplitterTests method testMarkersJson.

@Test
public void testMarkersJson() throws Exception {
    JsonObjectMapper<?, ?> objectMapper = JsonObjectMapperProvider.newInstance();
    QueueChannel outputChannel = new QueueChannel();
    FileSplitter splitter = new FileSplitter(true, true, true);
    splitter.setOutputChannel(outputChannel);
    splitter.handleMessage(new GenericMessage<File>(file));
    Message<?> received = outputChannel.receive(0);
    assertNotNull(received);
    assertNull(received.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE));
    assertEquals("START", received.getHeaders().get(FileHeaders.MARKER));
    assertThat(received.getPayload(), instanceOf(String.class));
    String payload = (String) received.getPayload();
    assertThat(payload, containsString("\"mark\":\"START\",\"lineCount\":0"));
    FileMarker fileMarker = objectMapper.fromJson(payload, FileSplitter.FileMarker.class);
    assertEquals(FileSplitter.FileMarker.Mark.START, fileMarker.getMark());
    assertEquals(file.getAbsolutePath(), fileMarker.getFilePath());
    assertNotNull(outputChannel.receive(0));
    assertNotNull(outputChannel.receive(0));
    received = outputChannel.receive(0);
    assertNotNull(received);
    assertEquals("END", received.getHeaders().get(FileHeaders.MARKER));
    assertThat(received.getPayload(), instanceOf(String.class));
    fileMarker = objectMapper.fromJson((String) received.getPayload(), FileSplitter.FileMarker.class);
    assertEquals(FileSplitter.FileMarker.Mark.END, fileMarker.getMark());
    assertEquals(file.getAbsolutePath(), fileMarker.getFilePath());
    assertEquals(2, fileMarker.getLineCount());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) FileMarker(org.springframework.integration.file.splitter.FileSplitter.FileMarker) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)6 FileMarker (org.springframework.integration.file.splitter.FileSplitter.FileMarker)6 QueueChannel (org.springframework.integration.channel.QueueChannel)5 File (java.io.File)4 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Subscriber (org.reactivestreams.Subscriber)1 FluxMessageChannel (org.springframework.integration.channel.FluxMessageChannel)1