ViewportResultsJsonCsvWriter { public String getJson(ViewportResults viewportResults) { List<ResultsCell> viewportCells = viewportResults.getResults(); List<Object> results = Lists.newArrayListWithCapacity(viewportCells.size()); for (ResultsCell cell : viewportCells) { Object cellValue = cell.getValue(); ValueSpecification cellValueSpec = cell.getValueSpecification(); Object formattedValue = _formatter.format(cellValue, cellValueSpec, cell.getFormat(), cell.getInlineKey()); Collection<Object> history = cell.getHistory(); Class<?> columnType = cell.getType(); DataType columnFormat = _formatter.getDataType(columnType); Map<String, Object> valueMap = Maps.newHashMap(); AggregatedExecutionLog executionLog = cell.getExecutionLog(); LogLevel logLevel = maxLogLevel(executionLog); valueMap.put(VALUE, formattedValue); if (columnFormat == UNKNOWN) { valueMap.put(TYPE, _formatter.getDataTypeForValue(cellValue, cellValueSpec).name()); } if (history != null) { List<Object> formattedHistoy = formatHistory(cellValueSpec, cell.getInlineKey(), history); if (formattedHistoy != null) { valueMap.put(HISTORY, formattedHistoy); } } if (cell.isError() || isError(formattedValue)) { valueMap.put(ERROR, true); } if (logLevel != null) { valueMap.put(LOG_LEVEL, logLevel); } if (hasLogOutput(executionLog)) { valueMap.put(LOG_OUTPUT, formatLogOutput(executionLog)); } results.add(valueMap); } String duration = _durationFormatter.format(new BigDecimal(viewportResults.getCalculationDuration().toMillis())); ImmutableMap<String, Object> resultsMap = ImmutableMap.of(VERSION, viewportResults.getVersion(), CALCULATION_DURATION, duration, DATA, results); return new JSONObject(resultsMap).toString(); } ViewportResultsJsonCsvWriter(ResultsFormatter formatter); String getCsv(ViewportResults viewportResults); String getJson(ViewportResults viewportResults);  }
@Test public void valueWithNoHistory() throws JSONException { List<ResultsCell> results = createResults("val", null, String.class); ViewportResults viewportResults = new ViewportResults(results, _viewportDefinition, createColumns(String.class), DURATION, CALC_TIME); String json = _writer.getJson(viewportResults); String expectedJson = "{\"version\":0, \"calculationDuration\":\"1,234\", \"data\":[{\"v\":\"val\"}]}"; assertTrue(JsonTestUtils.equal(new JSONObject(expectedJson), new JSONObject(json))); }
@Test public void valueWithHistory() throws JSONException { List<ResultsCell> results = createResults(3d, ImmutableList.<Object>of(1d, 2d, 3d), Double.class); ViewportResults viewportResults = new ViewportResults(results, _viewportDefinition, createColumns(Double.class), DURATION, CALC_TIME); String json = _writer.getJson(viewportResults); String expectedJson = "{\"version\":0, \"calculationDuration\":\"1,234\", \"data\":[{\"v\":\"3.0\",\"h\":[1,2,3]}]}"; assertTrue(JsonTestUtils.equal(new JSONObject(expectedJson), new JSONObject(json))); }
@Test public void valueWithUnknownType() throws JSONException { List<ResultsCell> results = createResults(3d, null, null); ViewportResults viewportResults = new ViewportResults(results, _viewportDefinition, createColumns(null), DURATION, CALC_TIME); String json = _writer.getJson(viewportResults); String expectedJson = "{\"version\":0, \"calculationDuration\":\"1,234\", \"data\":[{\"v\":\"3.0\",\"t\":\"DOUBLE\"}]}"; assertTrue(JsonTestUtils.equal(new JSONObject(expectedJson), new JSONObject(json))); }
@Test public void nullValueWithUnknownType() throws JSONException { List<ResultsCell> results = createResults(null, null, null); ViewportResults viewportResults = new ViewportResults(results, _viewportDefinition, createColumns(null), DURATION, CALC_TIME); String json = _writer.getJson(viewportResults); String expectedJson = "{\"version\":0, \"calculationDuration\":\"1,234\", \"data\":[{\"v\":\"\",\"t\":\"STRING\"}]}"; assertTrue(JsonTestUtils.equal(new JSONObject(expectedJson), new JSONObject(json))); }
@Test public void valueWithUnknownTypeAndHistory() throws JSONException { List<ResultsCell> results = createResults(3d, ImmutableList.<Object>of(1d, 2d, 3d), null); ViewportResults viewportResults = new ViewportResults(results, _viewportDefinition, createColumns(null), DURATION, CALC_TIME); String json = _writer.getJson(viewportResults); String expectedJson = "{\"version\":0, \"calculationDuration\":\"1,234\", \"data\":[{\"v\":\"3.0\",\"t\":\"DOUBLE\",\"h\":[1,2,3]}]}"; assertTrue(JsonTestUtils.equal(new JSONObject(expectedJson), new JSONObject(json))); }
@Test public void errorValueNoHistory() throws JSONException { List<ResultsCell> results = createResults(MissingOutput.EVALUATION_ERROR, null, String.class); ViewportResults viewportResults = new ViewportResults(results, _viewportDefinition, createColumns(String.class), DURATION, CALC_TIME); String json = _writer.getJson(viewportResults); String expectedJson = "{\"version\":0, \"calculationDuration\":\"1,234\", \"data\":[{\"v\":\"Evaluation error\", \"error\":true}]}"; assertTrue(JsonTestUtils.equal(new JSONObject(expectedJson), new JSONObject(json))); }
@Test public void errorValueWithHistory() throws JSONException { ImmutableList<Object> history = ImmutableList.<Object>of(1d, 2d, MissingOutput.EVALUATION_ERROR); List<ResultsCell> results = createResults(MissingOutput.EVALUATION_ERROR, history, Double.class); ViewportResults viewportResults = new ViewportResults(results, _viewportDefinition, createColumns(Double.class), DURATION, CALC_TIME); String json = _writer.getJson(viewportResults); String expectedJson = "{\"version\":0, \"calculationDuration\":\"1,234\", \"data\":[{\"v\":\"Evaluation error\", \"h\":[1,2,null], \"error\":true}]}"; assertTrue(JsonTestUtils.equal(new JSONObject(expectedJson), new JSONObject(json))); }
@Test public void errorValueInHistory() throws JSONException { ImmutableList<Object> history = ImmutableList.<Object>of(1d, MissingOutput.EVALUATION_ERROR, 3d); List<ResultsCell> results = createResults(3d, history, Double.class); ViewportResults viewportResults = new ViewportResults(results, _viewportDefinition, createColumns(Double.class), DURATION, CALC_TIME); String json = _writer.getJson(viewportResults); String expectedJson = "{\"version\":0, \"calculationDuration\":\"1,234\", \"data\":[{\"v\":\"3.0\",\"h\":[1,null,3]}]}"; assertTrue(JsonTestUtils.equal(new JSONObject(expectedJson), new JSONObject(json))); }