Package com.google.visualization.datasource.render

Source Code of com.google.visualization.datasource.render.JsonRendererTest

// 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.Value;
import com.google.visualization.datasource.datatable.value.ValueType;
import junit.framework.TestCase;

import static org.easymock.EasyMock.*;

/**
* Tests for JsonRenderer.
*
* @author Nimrod T.
*/
public class JsonRendererTest extends TestCase {

    public void testJsonResponseContentType() {
        final String jsonContentType = "application/json; charset=UTF-8";
        HttpServletResponse mockHttpServletResponse = createMock(HttpServletResponse.class);
        mockHttpServletResponse.setContentType(eq(jsonContentType));
        expectLastCall();

        replay(mockHttpServletResponse);
        JsonRenderer r = new JsonRenderer();
        r.setHeaders(new DataSourceRequest(), mockHttpServletResponse);
        verify(mockHttpServletResponse);
    }

    public void testEmptyDataTableToJson() {
        DataTable dataTable = new DataTable();
        JsonRenderer r = new JsonRenderer();
        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 datetimeCell = new TableCell(new DateTimeValue(2009, 1, 12, 12, 13, 14, 15));
        TableCell numberCell = new TableCell(12.3);
        TableCell textCell = new TableCell("aba");

        JsonRenderer r = new JsonRenderer();

        assertEquals("{\"v\":\"Date(2009,1,12)\"}",
                r.appendCellJson(dateCell, new StringBuilder(),
                        true, false).toString());
        assertEquals("{\"v\":\"Date(2009,1,12,12,13,14)\"}", //no milliseconds passed
                r.appendCellJson(datetimeCell, 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());

        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 JsonRenderer();
        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);

        JsonRenderer r = new JsonRenderer();

        testData.addRows(rows);
        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);

        JsonRenderer r = new JsonRenderer();
        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\":\"Date(2011,1,1)\",\"f\":\"1/1/2011\"},"
                        + "{\"v\":\"Date(2011,1,1,0,0,0)\",\"f\":\"1/1/2011 00:00:00\"},"
                        + "{\"v\":222.0,\"f\":\"222\"}]},"
                        + "{\"c\":[{\"v\":\"Date(2011,1,2)\",\"f\":\"1/2/2011\"},"
                        + "{\"v\":\"Date(2011,1,2,3,15,0)\"},{\"v\":null}]},"
                        + "{\"c\":[{\"v\":\"Date(2011,1,3)\",\"f\":\"1/3/2011\"},"
                        + "{\"v\":\"Date(2011,1,3,3,15,0)\",\"f\":\"1/1/2011 03:15:00\"},{\"v\":333.0}]},"
                        + "{\"c\":[{\"v\":\"Date(2011,1,4)\"},"
                        + "{\"v\":\"Date(2011,1,4,0,0,0)\"},{\"v\":222.0}]}]}",
                r.renderDataTable(testData, true, true).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"));

        JsonRenderer r = new JsonRenderer();
        DataSourceRequest request = new DataSourceRequest(new DataSourceParameters("reqId:7"));
        assertEquals(
                "{\"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");

        JsonRenderer r = new JsonRenderer();
        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(expected, new JsonRenderer().render(new DataSourceRequest(
                new DataSourceParameters("responseHandler:babylon;out:json")
        ), 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(expected, new JsonRenderer().render(new DataSourceRequest(
                new DataSourceParameters("reqId:90210;responseHandler:babylon;")
        ), this.getTestDataTable()).toString());
    }

    public void testGenerateJsonResponseError() throws DataSourceException {
        DataSourceParameters parameters =
                new DataSourceParameters("reqId:90210;responseHandler:babylon;");
        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\"}]}";
        assertEquals(
                expected,
                new JsonRenderer().error(new DataSourceRequest(parameters), responseStatus).toString());
    }
}
TOP

Related Classes of com.google.visualization.datasource.render.JsonRendererTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.