ToCsvHandler implements OutputOperationHandler<ToCsv, Iterable<? extends String>> { @Override public Iterable<? extends String> doOperation(final ToCsv operation, final Context context, final Store store) throws OperationException { if (null == operation.getInput()) { return null; } if (null == operation.getElementGenerator()) { throw new IllegalArgumentException("ToCsv operation requires a generator"); } final Iterable<? extends String> csv = operation.getElementGenerator().apply(operation.getInput()); if (operation.isIncludeHeader()) { return new ChainedIterable<>(Collections.singletonList(operation.getElementGenerator().getHeader()), csv); } return csv; }  @Override Iterable<? extends String> doOperation(final ToCsv operation, final Context context, final Store store);  }
@Test public void shouldConvertToCsv() throws OperationException { final List<Element> elements = Lists.newArrayList( makeEntity("vertex1", "count", 1), makeEntity("vertex2"), makeEdge("source1", "count", 1), makeEdge("source2") ); final ToCsv operation = new ToCsv.Builder() .input(elements) .generator(new CsvGenerator.Builder() .group("Group Label") .vertex("Vertex Label") .source("Source Label") .property("count", "Count Label") .constant("A Constant", "Some constant value") .quoted(false) .build()) .includeHeader(false) .build(); final ToCsvHandler handler = new ToCsvHandler(); final Iterable<? extends String> results = handler.doOperation(operation, new Context(), null); final List<String> resultList = Lists.newArrayList(results); final List<String> expected = Arrays.asList( "Foo,vertex1,,1,A Constant", "Foo,vertex2,,,A Constant", "Bar,,source1,1,A Constant", "Bar,,source2,,A Constant" ); assertEquals(expected, resultList); }
@Test public void shouldConvertToQuotedCsv() throws OperationException { final List<Element> elements = Lists.newArrayList( makeEntity("vertex1", "count", 1), makeEntity("vertex2"), makeEdge("source1", "count", 1), makeEdge("source2") ); final ToCsv operation = new ToCsv.Builder() .input(elements) .generator(new CsvGenerator.Builder() .group("Group Label") .vertex("Vertex Label") .source("Source Label") .property("count", "Count Label") .constant("A Constant", "Some constant value") .quoted(true) .build()) .includeHeader(false) .build(); final ToCsvHandler handler = new ToCsvHandler(); final Iterable<? extends String> results = handler.doOperation(operation, new Context(), null); final List<String> resultList = Lists.newArrayList(results); final List<String> expected = Arrays.asList( "\"Foo\",\"vertex1\",,\"1\",\"A Constant\"", "\"Foo\",\"vertex2\",,,\"A Constant\"", "\"Bar\",,\"source1\",\"1\",\"A Constant\"", "\"Bar\",,\"source2\",,\"A Constant\"" ); assertEquals(expected, resultList); }
@Test public void shouldConvertToCsvWithCommaReplacement() throws OperationException { final List<Element> elements = Lists.newArrayList( makeEntity("vertex1,with comma", "count", 1), makeEntity("vertex2"), makeEdge("source1,with comma", "count", 1), makeEdge("source2") ); final ToCsv operation = new ToCsv.Builder() .input(elements) .generator(new CsvGenerator.Builder() .group("Group Label") .vertex("Vertex Label") .source("Source Label") .property("count", "Count Label") .constant("A Constant", "Some constant value") .quoted(false) .commaReplacement("-") .build()) .includeHeader(false) .build(); final ToCsvHandler handler = new ToCsvHandler(); final Iterable<? extends String> results = handler.doOperation(operation, new Context(), null); final List<String> resultList = Lists.newArrayList(results); final List<String> expected = Arrays.asList( "Foo,vertex1-with comma,,1,A Constant", "Foo,vertex2,,,A Constant", "Bar,,source1-with comma,1,A Constant", "Bar,,source2,,A Constant" ); assertEquals(expected, resultList); }
@Test public void shouldConvertToCsvWithHeader() throws OperationException { final List<Element> elements = Lists.newArrayList( makeEntity("vertex1", "count", 1), makeEntity("vertex2"), makeEdge("source1", "count", 1), makeEdge("source2") ); final ToCsv operation = new ToCsv.Builder() .input(elements) .generator(new CsvGenerator.Builder() .group("Group Label") .vertex("Vertex Label") .source("Source Label") .property("count", "Count Label") .constant("A Constant", "Some constant value") .quoted(false) .build()) .includeHeader(true) .build(); final ToCsvHandler handler = new ToCsvHandler(); final Iterable<? extends String> results = handler.doOperation(operation, new Context(), null); final List<String> resultList = Lists.newArrayList(results); final List<String> expected = Arrays.asList( "Group Label,Vertex Label,Source Label,Count Label,Some constant value", "Foo,vertex1,,1,A Constant", "Foo,vertex2,,,A Constant", "Bar,,source1,1,A Constant", "Bar,,source2,,A Constant" ); assertEquals(expected, resultList); }