Skip to content

Commit

Permalink
csv: fix NULL handling
Browse files Browse the repository at this point in the history
Work around FasterXML/jackson-dataformats-text#10.

Signed-off-by: Pierre-Alexandre Meyer <[email protected]>
  • Loading branch information
pierre committed Jun 23, 2021
1 parent 4f37465 commit 4a844ec
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
21 changes: 20 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
7.2.2
Bug fix for missing refreshes (https://github.com/killbill/killbill-analytics-plugin/issues/118)
Fix NULL handling in CSV generation

7.2.1
Allow date filters for all report types

7.2.0
Add support for Trino based queries
Change configuration format to YAML
Note: DDL change in analytics_reports

7.1.1
Update to killbill-oss-parent 0.144.49

7.1.0
Be resilient if a payment plugin doesn't return info on deleted payment methods
Update to killbill-oss-parent 0.144.18

7.0.8
Update parent pom.xml

7.0.6
refresh payment rows when CBA is distributed
Refresh payment rows when CBA is distributed

7.0.5
Updates for Kill Bill 0.21.5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright 2010-2014 Ning, Inc.
* Copyright 2014-2020 Groupon, Inc
* Copyright 2020-2020 Equinix, Inc
* Copyright 2014-2020 The Billing Project, LLC
* Copyright 2020-2021 Equinix, Inc
* Copyright 2014-2021 The Billing Project, LLC
*
* The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
Expand Down Expand Up @@ -68,6 +68,8 @@

import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.Lists;

@Singleton
// Handle /plugins/killbill-analytics/reports
Expand Down Expand Up @@ -114,7 +116,14 @@ static void writeAsCSV(final Iterable<Chart> charts, final OutputStream out) thr
final TableDataSeries tableDataSeries = (TableDataSeries) marker;
out.write(csvMapper.writeValueAsBytes(tableDataSeries.getHeader()));
for (final List<Object> row : tableDataSeries.getValues()) {
out.write(csvMapper.writeValueAsBytes(row));
// Workaround for https://github.com/FasterXML/jackson-dataformats-text/issues/10
final List<Object> withoutNulls = Lists.<Object, Object>transform(row, new Function<Object, Object>() {
@Override
public Object apply(final Object input) {
return (input == null) ? "" : input;
}
});
out.write(csvMapper.writeValueAsBytes(withoutNulls));
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright 2010-2014 Ning, Inc.
* Copyright 2014-2020 Groupon, Inc
* Copyright 2020-2020 Equinix, Inc
* Copyright 2014-2020 The Billing Project, LLC
* Copyright 2020-2021 Equinix, Inc
* Copyright 2014-2021 The Billing Project, LLC
*
* The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
Expand All @@ -22,9 +22,9 @@
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -33,14 +33,15 @@
import org.killbill.billing.plugin.analytics.json.Chart;
import org.killbill.billing.plugin.analytics.json.DataMarker;
import org.killbill.billing.plugin.analytics.json.NamedXYTimeSeries;
import org.killbill.billing.plugin.analytics.json.TableDataSeries;
import org.killbill.billing.plugin.analytics.json.XY;
import org.killbill.billing.plugin.analytics.reports.configuration.ReportsConfigurationModelDao.ReportType;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;

public class TestReportsServlet extends AnalyticsTestSuiteNoDB {

Expand Down Expand Up @@ -90,6 +91,37 @@ public void testSimpleSerialization() throws Exception {
);
}

@Test(groups = "fast")
public void testSimpleSerializationWithNULL() throws Exception {
final List<DataMarker> res = new ArrayList<DataMarker>();

final List<List<Object>> values = new ArrayList<List<Object>>();
values.add(Arrays.asList("2013-01-01", null, 1.0));
values.add(Arrays.asList("2013-01-01", " ", 7.0));
values.add(Arrays.asList("2013-01-01", " ", null));
values.add(Arrays.asList("2013-01-01", " ", ""));
values.add(Arrays.asList("2013-01-01", "something", null));
values.add(Arrays.asList("2013-01-01", "something", ""));
values.add(Arrays.asList("2013-01-01", "something", " "));
final DataMarker serie1 = new TableDataSeries("serie1",
ImmutableList.<String>of("c1", "c2", "c3"),
values);
res.add(serie1);

final ByteArrayOutputStream out = new ByteArrayOutputStream();

ReportsResource.writeAsCSV(Collections.singletonList(new Chart(ReportType.TABLE, "foo", res)), out);
Assert.assertEquals(out.toString(StandardCharsets.UTF_8.name()),
"c1,c2,c3\n" +
"2013-01-01,,1.0\n" +
"2013-01-01,\" \",7.0\n" +
"2013-01-01,\" \",\n" +
"2013-01-01,\" \",\n" +
"2013-01-01,something,\n" +
"2013-01-01,something,\n" +
"2013-01-01,something,\" \"\n"
);
}

@Test(groups = "fast")
public void testDeserializationReserialization() throws Exception {
Expand Down

0 comments on commit 4a844ec

Please sign in to comment.