You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Christian Ribeaud (JIRA)" <ji...@apache.org> on 2018/12/21 13:42:00 UTC

[jira] [Comment Edited] (CAMEL-13026) Header written out on each message when marshalling

    [ https://issues.apache.org/jira/browse/CAMEL-13026?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16726743#comment-16726743 ] 

Christian Ribeaud edited comment on CAMEL-13026 at 12/21/18 1:41 PM:
---------------------------------------------------------------------

See *Unit* test referenced by [this|https://github.com/apache/camel/pull/2683] pull request. We got _4_ lines (instead of _3_). On each append, the headers are written out. Test is currently green, demonstrating the current behavior. Following *Unit* test code:
{code:java}
package org.apache.camel.dataformat.csv;

import org.apache.camel.Exchange;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <b>Camel</b> based test cases for {@link org.apache.camel.dataformat.csv.CsvDataFormat}.
 */
public class CsvMarshalHeaderTest extends CamelTestSupport {

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Produce(uri = "direct:start")
    private ProducerTemplate producerTemplate;

    private File outputFile;

    @Override
    protected void doPreSetup() throws Exception {
        outputFile = new File(folder.newFolder(), "output.csv");
    }

    @Test
    public void testSendBody() throws IOException {
        Map<String, String> body = new LinkedHashMap<>();
        body.put("first_name", "John");
        body.put("last_name", "Doe");
        String fileName = outputFile.getName();
        assertEquals("output.csv", fileName);
        producerTemplate.sendBodyAndHeader(body, Exchange.FILE_NAME, fileName);
        body = new LinkedHashMap<>();
        body.put("first_name", "Max");
        body.put("last_name", "Mustermann");
        producerTemplate.sendBodyAndHeader(body, Exchange.FILE_NAME, fileName);
        List<String> lines = Files.lines(Paths.get(outputFile.toURI()))
                .filter(l -> l.trim().length() > 0).collect(Collectors.toList());
        // We got twice the headers... :(
        assertEquals(4, lines.size());
    }

    @Test
    public void testSendBodyWithList() throws IOException {
        List<List<String>> body = Collections.singletonList(Arrays.asList("John", "Doe"));
        String fileName = outputFile.getName();
        assertEquals("output.csv", fileName);
        producerTemplate.sendBodyAndHeader(body, Exchange.FILE_NAME, fileName);
        body = Collections.singletonList(Arrays.asList("Max", "Mustermann"));
        producerTemplate.sendBodyAndHeader(body, Exchange.FILE_NAME, fileName);
        List<String> lines = Files.lines(Paths.get(outputFile.toURI()))
                .filter(l -> l.trim().length() > 0).collect(Collectors.toList());
        // We got twice the headers... :(
        assertEquals(4, lines.size());
    }

    @Override
    protected RoutesBuilder createRouteBuilder() {
        return new RouteBuilder() {
            @Override
            public void configure() {
                String uri = String.format("file:%s?charset=utf-8&fileExist=Append", outputFile.getParentFile().getAbsolutePath());
                from("direct:start").marshal(createCsvDataFormat()).to(uri);
            }
        };
    }

    private static CsvDataFormat createCsvDataFormat() {
        CsvDataFormat dataFormat = new CsvDataFormat();
        dataFormat.setDelimiter('\t');
        dataFormat.setTrim(true);
        dataFormat.setIgnoreSurroundingSpaces(true);
        dataFormat.setHeader((String[]) Arrays.asList("first_name", "last_name").toArray());
        return dataFormat;
    }
}
{code}


was (Author: christianr):
See *Unit* test referenced by [this|https://github.com/apache/camel/pull/2683] pull request. We got _4_ lines (instead of _3_). On each append, the headers are written out. Test is currently green, demonstrating the current behavior.
Following the Unit test code:
{code:java}
package org.apache.camel.dataformat.csv;

import org.apache.camel.Exchange;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <b>Camel</b> based test cases for {@link org.apache.camel.dataformat.csv.CsvDataFormat}.
 */
public class CsvMarshalHeaderTest extends CamelTestSupport {

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Produce(uri = "direct:start")
    private ProducerTemplate producerTemplate;

    private File outputFile;

    @Override
    protected void doPreSetup() throws Exception {
        outputFile = new File(folder.newFolder(), "output.csv");
    }

    @Test
    public void testSendBody() throws IOException {
        Map<String, String> body = new LinkedHashMap<>();
        body.put("first_name", "John");
        body.put("last_name", "Doe");
        String fileName = outputFile.getName();
        assertEquals("output.csv", fileName);
        producerTemplate.sendBodyAndHeader(body, Exchange.FILE_NAME, fileName);
        body = new LinkedHashMap<>();
        body.put("first_name", "Max");
        body.put("last_name", "Mustermann");
        producerTemplate.sendBodyAndHeader(body, Exchange.FILE_NAME, fileName);
        List<String> lines = Files.lines(Paths.get(outputFile.toURI()))
                .filter(l -> l.trim().length() > 0).collect(Collectors.toList());
        // We got twice the headers... :(
        assertEquals(4, lines.size());
    }

    @Test
    public void testSendBodyWithList() throws IOException {
        List<List<String>> body = Collections.singletonList(Arrays.asList("John", "Doe"));
        String fileName = outputFile.getName();
        assertEquals("output.csv", fileName);
        producerTemplate.sendBodyAndHeader(body, Exchange.FILE_NAME, fileName);
        body = Collections.singletonList(Arrays.asList("Max", "Mustermann"));
        producerTemplate.sendBodyAndHeader(body, Exchange.FILE_NAME, fileName);
        List<String> lines = Files.lines(Paths.get(outputFile.toURI()))
                .filter(l -> l.trim().length() > 0).collect(Collectors.toList());
        // We got twice the headers... :(
        assertEquals(4, lines.size());
    }

    @Override
    protected RoutesBuilder createRouteBuilder() {
        return new RouteBuilder() {
            @Override
            public void configure() {
                String uri = String.format("file:%s?charset=utf-8&fileExist=Append", outputFile.getParentFile().getAbsolutePath());
                from("direct:start").marshal(createCsvDataFormat()).to(uri);
            }
        };
    }

    private static CsvDataFormat createCsvDataFormat() {
        CsvDataFormat dataFormat = new CsvDataFormat();
        dataFormat.setDelimiter('\t');
        dataFormat.setTrim(true);
        dataFormat.setIgnoreSurroundingSpaces(true);
        dataFormat.setHeader((String[]) Arrays.asList("first_name", "last_name").toArray());
        return dataFormat;
    }
}
{code}

> Header written out on each message when marshalling
> ---------------------------------------------------
>
>                 Key: CAMEL-13026
>                 URL: https://issues.apache.org/jira/browse/CAMEL-13026
>             Project: Camel
>          Issue Type: Improvement
>          Components: camel-csv
>            Reporter: Christian Ribeaud
>            Priority: Major
>
> When *CSV* marshalling, the header is written out on each message.
> Additionally, {{CsvMarshaller}} is so opaque, making it difficult for customization.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)