// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.visualization.datasource.render;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.visualization.datasource.DataSourceRequest;
import com.google.visualization.datasource.base.DataSourceException;
import com.google.visualization.datasource.base.DataSourceParameters;
import com.google.visualization.datasource.base.ReasonType;
import com.google.visualization.datasource.base.ResponseStatus;
import com.google.visualization.datasource.base.StatusType;
import com.google.visualization.datasource.base.Warning;
import com.google.visualization.datasource.datatable.ColumnDescription;
import com.google.visualization.datasource.datatable.DataTable;
import com.google.visualization.datasource.datatable.TableCell;
import com.google.visualization.datasource.datatable.TableRow;
import com.google.visualization.datasource.datatable.value.DateTimeValue;
import com.google.visualization.datasource.datatable.value.DateValue;
import com.google.visualization.datasource.datatable.value.NumberValue;
import com.google.visualization.datasource.datatable.value.TextValue;
import com.google.visualization.datasource.datatable.value.TimeOfDayValue;
import com.google.visualization.datasource.datatable.value.Value;
import com.google.visualization.datasource.datatable.value.ValueType;
import junit.framework.TestCase;
import static org.easymock.EasyMock.*;
/**
* Tests for JsonpRenderer.
*
* @author Nimrod T.
*/
public class JsonpRendererTest extends TestCase {
public void testJsonpResponseContentType() {
final String jsonpContentType = "text/javascript; charset=UTF-8";
HttpServletResponse mockHttpServletResponse = createMock(HttpServletResponse.class);
mockHttpServletResponse.setContentType(eq(jsonpContentType));
expectLastCall();
replay(mockHttpServletResponse);
JsonRenderer r = new JsonpRenderer();
r.setHeaders(new DataSourceRequest(), mockHttpServletResponse);
verify(mockHttpServletResponse);
}
public void testEmptyDataTableToJson() {
DataTable dataTable = new DataTable();
JsonRenderer r = new JsonpRenderer();
assertEquals("", r.renderDataTable(dataTable, false, false));
assertEquals("", r.renderDataTable(dataTable, false, true));
assertEquals("", r.renderDataTable(dataTable, true, false));
assertEquals("", r.renderDataTable(dataTable, true, true));
}
public void testAppendCellJson() {
TableCell dateCell = new TableCell(new DateValue(2009, 1, 12));
TableCell timeofdayCell = new TableCell(new TimeOfDayValue(12, 13, 14, 15));
TableCell datetimeCell = new TableCell(new DateTimeValue(2009, 1, 12, 12, 13, 14, 15));
TableCell booleanCell = new TableCell(true);
TableCell numberCell = new TableCell(12.3);
TableCell textCell = new TableCell("aba");
JsonRenderer r = new JsonpRenderer();
assertEquals("{\"v\":new Date(2009,1,12)}",
r.appendCellJson(dateCell, new StringBuilder(),
true, false).toString());
assertEquals("{\"v\":[12,13,14,15]}",
r.appendCellJson(timeofdayCell, new StringBuilder(),
true, false).toString());
assertEquals("{\"v\":new Date(2009,1,12,12,13,14)}", //no milliseconds passed
r.appendCellJson(datetimeCell, new StringBuilder(),
true, false).toString());
assertEquals("{\"v\":true}",
r.appendCellJson(booleanCell, new StringBuilder(),
true, false).toString());
assertEquals("{\"v\":12.3}",
r.appendCellJson(numberCell, new StringBuilder(),
true, false).toString());
assertEquals("{\"v\":\"aba\"}",
r.appendCellJson(textCell, new StringBuilder(),
true, false).toString());
// No formatting still stays the same when there is no formatted value
assertEquals("{\"v\":12.3}",
r.appendCellJson(numberCell, new StringBuilder(),
false, false).toString());
dateCell = new TableCell(new DateValue(2009, 1, 12), "2009-2-12");
// With formatting
assertEquals("{\"v\":new Date(2009,1,12),\"f\":\"2009-2-12\"}",
r.appendCellJson(dateCell, new StringBuilder(),
true, false).toString());
// Without formatting
assertEquals("{\"v\":new Date(2009,1,12)}",
r.appendCellJson(dateCell, new StringBuilder(),
false, false).toString());
TableCell nullCell = new TableCell(Value.getNullValueFromValueType(ValueType.NUMBER));
// Null value
assertEquals("",
r.appendCellJson(nullCell, new StringBuilder(),
true, false).toString());
// isLast = true
assertEquals("{\"v\":null}",
r.appendCellJson(nullCell, new StringBuilder(),
true, true).toString());
}
public void testAppendColumnDescriptionJson() {
JsonRenderer r = new JsonpRenderer();
ColumnDescription columnDescription = new ColumnDescription("ID", ValueType.BOOLEAN, "LABEL");
assertEquals("{\"id\":\"ID\",\"label\":\"LABEL\",\"type\":\"boolean\",\"pattern\":\"\"}",
r.appendColumnDescriptionJson(
columnDescription, new StringBuilder()).toString());
columnDescription.setPattern("%%%.@@");
assertEquals("{\"id\":\"ID\",\"label\":\"LABEL\",\"type\":\"boolean\",\"pattern\":\"%%%.@@\"}",
r.appendColumnDescriptionJson(
columnDescription, new StringBuilder()).toString());
}
public void testSimpleDataTableToJson() throws DataSourceException {
DataTable testData;
List<TableRow> rows;
testData = new DataTable();
ColumnDescription c0 = new ColumnDescription("A", ValueType.TEXT, "col0");
ColumnDescription c1 = new ColumnDescription("B", ValueType.NUMBER, "col1");
ColumnDescription c2 = new ColumnDescription("C", ValueType.BOOLEAN, "col2");
testData.addColumn(c0);
testData.addColumn(c1);
testData.addColumn(c2);
rows = Lists.newArrayList();
TableRow row = new TableRow();
row.addCell(new TableCell("aaa"));
row.addCell(new TableCell(new NumberValue(222), "222"));
row.addCell(new TableCell(false));
rows.add(row);
row = new TableRow();
row.addCell(new TableCell(""));
row.addCell(new TableCell(NumberValue.getNullValue()));
row.addCell(new TableCell(true));
rows.add(row);
row = new TableRow();
row.addCell(new TableCell(new TextValue("bbb"), "bbb"));
row.addCell(new TableCell(333));
row.addCell(new TableCell(true));
rows.add(row);
row = new TableRow();
row.addCell(new TableCell("d'dd"));
row.addCell(new TableCell(222));
row.addCell(new TableCell(false));
rows.add(row);
testData.addRows(rows);
JsonRenderer r = new JsonpRenderer();
assertEquals(
"{\"cols\":[{\"id\":\"A\",\"label\":\"col0\",\"type\":\"string\",\"pattern\":\"\"},"
+ "{\"id\":\"B\",\"label\":\"col1\",\"type\":\"number\",\"pattern\":\"\"},"
+ "{\"id\":\"C\",\"label\":\"col2\",\"type\":\"boolean\",\"pattern\":\"\"}],"
+ "\"rows\":[{\"c\":[{\"v\":\"aaa\"},{\"v\":222.0,\"f\":\"222\"},{\"v\":false}]},"
+ "{\"c\":[{\"v\":\"\"},,{\"v\":true}]},"
+ "{\"c\":[{\"v\":\"bbb\"},{\"v\":333.0},{\"v\":true}]},"
+ "{\"c\":[{\"v\":\"d\\u0027dd\"},{\"v\":222.0},{\"v\":false}]}]}",
r.renderDataTable(testData, true, true).toString());
// With non default pattern (the ' will be escaped)
testData.getColumnDescription(1).setPattern("00'\"<script>##");
assertEquals(
"{\"cols\":[{\"id\":\"A\",\"label\":\"col0\",\"type\":\"string\",\"pattern\":\"\"},"
+ "{\"id\":\"B\",\"label\":\"col1\",\"type\":\"number\",\"pattern\":"
+ "\"00\\u0027\\u0022\\u003cscript\\u003e##\"},"
+ "{\"id\":\"C\",\"label\":\"col2\",\"type\":\"boolean\",\"pattern\":\"\"}],"
+ "\"rows\":[{\"c\":[{\"v\":\"aaa\"},{\"v\":222.0,\"f\":\"222\"},{\"v\":false}]},"
+ "{\"c\":[{\"v\":\"\"},,{\"v\":true}]},"
+ "{\"c\":[{\"v\":\"bbb\"},{\"v\":333.0},{\"v\":true}]},"
+ "{\"c\":[{\"v\":\"d\\u0027dd\"},{\"v\":222.0},{\"v\":false}]}]}",
r.renderDataTable(testData, true, true).toString());
}
public void testSimpleDataTableWithDatesInJson() throws DataSourceException {
DataTable testData;
List<TableRow> rows;
testData = new DataTable();
ColumnDescription c0 = new ColumnDescription("DateA", ValueType.DATE, "col0");
ColumnDescription c1 = new ColumnDescription("DateTimeA", ValueType.DATETIME, "col1");
ColumnDescription c2 = new ColumnDescription("ValueA", ValueType.NUMBER, "col2");
testData.addColumn(c0);
testData.addColumn(c1);
testData.addColumn(c2);
rows = Lists.newArrayList();
TableRow row = new TableRow();
row.addCell(new TableCell(new DateValue(2011, 1, 1), "1/1/2011"));
row.addCell(new TableCell(new DateTimeValue(2011, 1, 1, 0, 0, 0, 0), "1/1/2011 00:00:00"));
row.addCell(new TableCell(new NumberValue(222), "222"));
rows.add(row);
row = new TableRow();
row.addCell(new TableCell(new DateValue(2011, 1, 2), "1/2/2011"));
row.addCell(new TableCell(new DateTimeValue(2011, 1, 2, 3, 15, 0, 0)));
row.addCell(new TableCell(NumberValue.getNullValue()));
rows.add(row);
row = new TableRow();
row.addCell(new TableCell(new DateValue(2011, 1, 3), "1/3/2011"));
row.addCell(new TableCell(new DateTimeValue(2011, 1, 3, 3, 15, 0, 0), "1/1/2011 03:15:00"));
row.addCell(new TableCell(333));
rows.add(row);
row = new TableRow();
row.addCell(new TableCell(new DateValue(2011, 1, 4)));
row.addCell(new TableCell(new DateTimeValue(2011, 1, 4, 0, 0, 0, 0)));
row.addCell(new TableCell(222));
rows.add(row);
testData.addRows(rows);
JsonpRenderer r = new JsonpRenderer();
assertEquals(
"{\"cols\":[{\"id\":\"DateA\",\"label\":\"col0\",\"type\":\"date\",\"pattern\":\"\"},"
+ "{\"id\":\"DateTimeA\",\"label\":\"col1\",\"type\":\"datetime\",\"pattern\":\"\"},"
+ "{\"id\":\"ValueA\",\"label\":\"col2\",\"type\":\"number\",\"pattern\":\"\"}],"
+ "\"rows\":[{\"c\":[{\"v\":new Date(2011,1,1),\"f\":\"1/1/2011\"},"
+ "{\"v\":new Date(2011,1,1,0,0,0),\"f\":\"1/1/2011 00:00:00\"},"
+ "{\"v\":222.0,\"f\":\"222\"}]},"
+ "{\"c\":[{\"v\":new Date(2011,1,2),\"f\":\"1/2/2011\"},"
+ "{\"v\":new Date(2011,1,2,3,15,0)},{\"v\":null}]},"
+ "{\"c\":[{\"v\":new Date(2011,1,3),\"f\":\"1/3/2011\"},"
+ "{\"v\":new Date(2011,1,3,3,15,0),\"f\":\"1/1/2011 03:15:00\"},{\"v\":333.0}]},"
+ "{\"c\":[{\"v\":new Date(2011,1,4)},"
+ "{\"v\":new Date(2011,1,4,0,0,0)},{\"v\":222.0}]}]}",
r.renderDataTable(testData,
true, true /* renderDateConstructor */).toString());
}
public void testEntireResponseWithWarnings() throws DataSourceException {
DataTable testData;
List<TableRow> rows;
testData = new DataTable();
ColumnDescription c0 = new ColumnDescription("A", ValueType.TEXT, "col0");
ColumnDescription c1 = new ColumnDescription("B", ValueType.NUMBER, "col1");
testData.addColumn(c0);
testData.addColumn(c1);
rows = Lists.newArrayList();
TableRow row = new TableRow();
row.addCell(new TableCell("aaa"));
row.addCell(new TableCell(new NumberValue(222), "$222"));
rows.add(row);
row = new TableRow();
row.addCell(new TableCell("bbb"));
row.addCell(new TableCell(new NumberValue(333)));
rows.add(row);
testData.addRows(rows);
testData.addWarning(new Warning(ReasonType.DATA_TRUNCATED, "Sorry, data truncated"));
testData.addWarning(new Warning(ReasonType.NOT_SUPPORTED, "foobar"));
JsonpRenderer r = new JsonpRenderer();
DataSourceRequest request = new DataSourceRequest(new DataSourceParameters("reqId:7"));
assertEquals(
"google.visualization.Query.setResponse({\"version\":\"0.6\","
+ "\"reqId\":\"7\",\"status\":\"warning\","
+ "\"warnings\":[{\"reason\":\"data_truncated\",\"message\":"
+ "\"Retrieved data was truncated\",\"detailed_message\":"
+ "\"Sorry, data truncated\"},{\"reason\":\"not_supported\",\"message\":"
+ "\"Operation not supported\",\"detailed_message\":\"foobar\"}],"
+ "\"sig\":\"121655538\",\"table\":"
+ "{\"cols\":[{\"id\":\"A\",\"label\":\"col0\",\"type\":\"string\",\"pattern\":\"\"},"
+ "{\"id\":\"B\",\"label\":\"col1\",\"type\":\"number\",\"pattern\":\"\"}],"
+ "\"rows\":[{\"c\":[{\"v\":\"aaa\"},{\"v\":222.0,\"f\":\"$222\"}]},"
+ "{\"c\":[{\"v\":\"bbb\"},{\"v\":333.0}]}]}});",
r.render(request, testData).toString());
}
public void testCustomPropertiesToJson() throws DataSourceException {
DataTable testData;
List<TableRow> rows;
testData = new DataTable();
ColumnDescription c0 = new ColumnDescription("A", ValueType.TEXT, "col0");
ColumnDescription c1 = new ColumnDescription("B", ValueType.NUMBER, "col1");
c1.setCustomProperty("arak", "elit");
testData.addColumn(c0);
testData.addColumn(c1);
rows = Lists.newArrayList();
TableRow row = new TableRow();
row.addCell(new TableCell("aaa"));
row.addCell(new TableCell(new NumberValue(222), "222"));
rows.add(row);
row = new TableRow();
row.addCell(new TableCell(""));
row.addCell(new TableCell(NumberValue.getNullValue()));
rows.add(row);
row.setCustomProperty("sensi", "puff");
testData.addRows(rows);
testData.getRow(0).getCell(0).setCustomProperty("a", "b");
JsonpRenderer r = new JsonpRenderer();
assertEquals(
"{\"cols\":[{\"id\":\"A\",\"label\":\"col0\",\"type\":\"string\",\"pattern\":\"\"},"
+ "{\"id\":\"B\",\"label\":\"col1\",\"type\":\"number\",\"pattern\":\"\""
+ ",\"p\":{\"arak\":\"elit\"}}],"
+ "\"rows\":[{\"c\":[{\"v\":\"aaa\",\"p\":{\"a\":\"b\"}},"
+ "{\"v\":222.0,\"f\":\"222\"}]},"
+ "{\"c\":[{\"v\":\"\"},{\"v\":null}],\"p\":{\"sensi\":\"puff\"}}]}",
r.renderDataTable(testData, true, true).toString());
testData.setCustomProperty("brandy", "cognac");
assertEquals(
"{\"cols\":[{\"id\":\"A\",\"label\":\"col0\",\"type\":\"string\",\"pattern\":\"\"},"
+ "{\"id\":\"B\",\"label\":\"col1\",\"type\":\"number\",\"pattern\":\"\""
+ ",\"p\":{\"arak\":\"elit\"}}],"
+ "\"rows\":[{\"c\":[{\"v\":\"aaa\",\"p\":{\"a\":\"b\"}},"
+ "{\"v\":222.0,\"f\":\"222\"}]},"
+ "{\"c\":[{\"v\":\"\"},{\"v\":null}],\"p\":{\"sensi\":\"puff\"}}]"
+ ",\"p\":{\"brandy\":\"cognac\"}}",
r.renderDataTable(testData, true, true).toString());
}
private DataTable getTestDataTable() throws DataSourceException {
DataTable dataTable = new DataTable();
ColumnDescription c0 = new ColumnDescription("A", ValueType.TEXT, "col0");
ColumnDescription c1 = new ColumnDescription("B", ValueType.NUMBER, "col1");
ColumnDescription c2 = new ColumnDescription("C", ValueType.BOOLEAN, "col2");
dataTable.addColumn(c0);
dataTable.addColumn(c1);
dataTable.addColumn(c2);
List<TableRow> rows = Lists.newArrayList();
TableRow row = new TableRow();
row.addCell(new TableCell("aaa"));
row.addCell(new TableCell(new NumberValue(222), "222"));
row.addCell(new TableCell(false));
rows.add(row);
row = new TableRow();
row.addCell(new TableCell(""));
row.addCell(new TableCell(111));
row.addCell(new TableCell(true));
rows.add(row);
row = new TableRow();
row.addCell(new TableCell((new TextValue("bbb")), "bbb"));
row.addCell(new TableCell(333));
row.addCell(new TableCell(true));
rows.add(row);
row = new TableRow();
row.addCell(new TableCell("ddd"));
row.addCell(new TableCell(222));
row.addCell(new TableCell(false));
rows.add(row);
dataTable.addRows(rows);
return dataTable;
}
public void testSetServletResponseJson() throws DataSourceException {
// Basic test 1.
String expected = "{\"version\":\"0.6\",\"status\":\"ok\","
+ "\"sig\":\"2087475733\",\"table\":"
+ "{\"cols\":[{\"id\":\"A\",\"label\":\"col0\",\"type\":\"string\",\"pattern\":\"\"},"
+ "{\"id\":\"B\",\"label\":\"col1\",\"type\":\"number\",\"pattern\":\"\"},"
+ "{\"id\":\"C\",\"label\":\"col2\",\"type\":\"boolean\",\"pattern\":\"\"}],"
+ "\"rows\":[{\"c\":[{\"v\":\"aaa\"},{\"v\":222.0,\"f\":\"222\"},{\"v\":false}]},"
+ "{\"c\":[{\"v\":\"\"},{\"v\":111.0},{\"v\":true}]},"
+ "{\"c\":[{\"v\":\"bbb\"},{\"v\":333.0},{\"v\":true}]},"
+ "{\"c\":[{\"v\":\"ddd\"},{\"v\":222.0},{\"v\":false}]}]}}";
assertEquals("babylon(" + expected + ");", new JsonpRenderer().render(new DataSourceRequest(
new DataSourceParameters("responseHandler:babylon;out:jsonp")
), this.getTestDataTable()).toString());
// Basic test 2.
expected = "{\"version\":\"0.6\",\"reqId\":\"90210\",\"status\":\"ok\","
+ "\"sig\":\"2087475733\",\"table\":"
+ "{\"cols\":[{\"id\":\"A\",\"label\":\"col0\",\"type\":\"string\",\"pattern\":\"\"},"
+ "{\"id\":\"B\",\"label\":\"col1\",\"type\":\"number\",\"pattern\":\"\"},"
+ "{\"id\":\"C\",\"label\":\"col2\",\"type\":\"boolean\",\"pattern\":\"\"}],"
+ "\"rows\":[{\"c\":[{\"v\":\"aaa\"},{\"v\":222.0,\"f\":\"222\"},{\"v\":false}]},"
+ "{\"c\":[{\"v\":\"\"},{\"v\":111.0},{\"v\":true}]},"
+ "{\"c\":[{\"v\":\"bbb\"},{\"v\":333.0},{\"v\":true}]},"
+ "{\"c\":[{\"v\":\"ddd\"},{\"v\":222.0},{\"v\":false}]}]}}";
assertEquals("babylon(" + expected + ");", new JsonpRenderer().render(new DataSourceRequest(
new DataSourceParameters("reqId:90210;responseHandler:babylon;out:jsonp")
), this.getTestDataTable()).toString());
}
public void testGenerateJsonResponseError() throws DataSourceException {
ResponseStatus responseStatus = new ResponseStatus(
StatusType.ERROR,
ReasonType.INTERNAL_ERROR,
"this is me not you why it is that not knowing me cave man");
String expected = "{\"version\":\"0.6\",\"reqId\":\"90210\",\"status\":\"error\",\"errors\":"
+ "[{\"reason\":\"internal_error\",\"message\":\"Internal error\",\"detailed_message\":"
+ "\"this is me not you why it is that not knowing me cave man\"}]}";
DataSourceParameters parameters =
new DataSourceParameters("reqId:90210;responseHandler:babylon;out:jsonp");
assertEquals(
"babylon(" + expected + ");",
new JsonpRenderer().error(new DataSourceRequest(parameters), responseStatus).toString());
}
}