/** * Build a Result object from the given ResultData * * @param resultData data to use to build Result */ public static CommandResult buildResult(ResultData resultData) { return new LegacyCommandResult(resultData); }
private void buildData() { try { if (ResultData.TYPE_COMPOSITE.equals(resultData.getType())) { buildComposite(); } else { GfJsonObject content = getContent(); if (content != null) { Table resultTable = TableBuilder.newTable(); addHeaderInTable(resultTable, getGfJsonObject()); RowGroup rowGroup = resultTable.newRowGroup(); if (ResultData.TYPE_TABULAR.equals(resultData.getType())) { resultTable.setColumnSeparator(" "); resultTable.setTabularResult(true); buildTable(rowGroup, content); } else { buildInfoErrorData(rowGroup, content); } addFooterInTable(resultTable, getGfJsonObject()); resultLines.addAll(resultTable.buildTableList()); } } } catch (GfJsonException e) { resultLines .add("Error occurred while processing Command Result. Internal Error - Invalid Result."); } finally { isDataBuilt = true; } }
/** * @throws ArrayIndexOutOfBoundsException if this method is called more number of times than the * data items it contains */ @Override public String nextLine() { if (!isDataBuilt) { buildData(); } return resultLines.get(index++); }
rowGroup = table.newRowGroup(); addHeaderInRowGroup(rowGroup, section); while (keys.hasNext()) { String key = keys.next(); GfJsonObject tableObject = section.getJSONObject(key); addHeaderInTable(table, tableObject); buildTable(rowGroupForTable, tableObject.getJSONObject(ResultData.RESULT_CONTENT)); addFooterInTable(table, tableObject); } else if (key.equals(CompositeResultData.SEPARATOR)) { String separatorString = section.getString(key); prefix += " . "; String[] value = getValuesSeparatedByLines(object); if (value.length == 1) { newRow.newLeftCol(prefix + key).newLeftCol(value[0]); addFooterInRowGroup(rowGroup, section);
private void buildComposite() { try { GfJsonObject content = getContent(); if (content != null) { Table resultTable = TableBuilder.newTable(); resultTable.setColumnSeparator(" : "); addHeaderInTable(resultTable, getGfJsonObject()); for (Iterator<String> it = content.keys(); it.hasNext();) { String key = it.next(); if (key.startsWith(ResultData.SECTION_DATA_ACCESSOR)) { GfJsonObject subSection = content.getJSONObject(key); buildSection(resultTable, null, subSection, 0); } else if (key.equals(CompositeResultData.SEPARATOR)) { String separatorString = content.getString(key); resultTable.newRowGroup().newRowSeparator(separatorString.charAt(0), true); } } addFooterInTable(resultTable, getGfJsonObject()); resultLines.addAll(resultTable.buildTableList()); } } catch (GfJsonException e) { resultLines .add("Error occurred while processing Command Result. Internal Error - Invalid Result."); logger.info( "Error occurred while processing Command Result. Internal Error - Invalid Result.", e); } finally { isDataBuilt = true; } }
private void buildInfoErrorData(RowGroup rowGroup, GfJsonObject content) throws GfJsonException { GfJsonArray accumulatedData = content.getJSONArray(InfoResultData.RESULT_CONTENT_MESSAGE); if (accumulatedData != null) { buildRows(rowGroup, null, accumulatedData); } }
private void buildTable(RowGroup rowGroup, GfJsonObject content) throws GfJsonException { GfJsonArray columnNames = content.names(); int numOfColumns = columnNames.size(); Row headerRow = rowGroup.newRow(); rowGroup.setColumnSeparator(" | "); rowGroup.newRowSeparator('-', false); // build Table Header first for (int i = 0; i < numOfColumns; i++) { Object object = columnNames.get(i); if (ResultData.BYTE_DATA_ACCESSOR.equals(object)) { // skip file data if any continue; } headerRow.newCenterCol(object); } // Build remaining rows by extracting data column-wise from JSON object Row[] dataRows = null; for (int i = 0; i < numOfColumns; i++) { Object object = columnNames.get(i); if (ResultData.BYTE_DATA_ACCESSOR.equals(object)) { // skip file data if any continue; } GfJsonArray accumulatedData = content.getJSONArray((String) object); dataRows = buildRows(rowGroup, dataRows, accumulatedData); } }
@Test public void resultWithFileDoesHaveFileToDownload() { Path fileToDownload = Paths.get(".").toAbsolutePath(); CommandResult commandResult = new LegacyCommandResult(fileToDownload); assertThat(commandResult.hasFileToDownload()).isTrue(); assertThat(commandResult.nextLine()).isEqualTo(fileToDownload.toString() + LINE_SEPARATOR); assertThat(commandResult.getFileToDownload()).isEqualTo(fileToDownload); } }
@Override public boolean hasNextLine() { if (!isDataBuilt) { buildData(); } return index < resultLines.size(); }
@Test public void emptyResultHasOneEmptyLine() { CommandResult commandResult = new LegacyCommandResult(new InfoResultData()); Assertions.assertThat(commandResult.nextLine()).isEqualTo(""); Assertions.assertThat(commandResult.hasNextLine()).isFalse(); }
@Test public void resultWithOneLineHasOneLine() { CommandResult commandResult = new LegacyCommandResult(new InfoResultData("oneLine")); assertThat(commandResult.nextLine()).isEqualTo("oneLine" + LINE_SEPARATOR); assertThat(commandResult.hasNextLine()).isFalse(); }
@Test public void emptyResultDoesNotHaveFileToDownload() { CommandResult commandResult = new LegacyCommandResult(new InfoResultData()); Assertions.assertThat(commandResult.hasFileToDownload()).isFalse(); }
@Test public void resultWithTwoLinesHasTwoLines() { InfoResultData resultData = new InfoResultData(); resultData.addLine("lineOne"); resultData.addLine("lineTwo"); CommandResult commandResult = new LegacyCommandResult(resultData); assertThat(commandResult.nextLine()) .isEqualTo("lineOne" + LINE_SEPARATOR + "lineTwo" + LINE_SEPARATOR); assertThat(commandResult.hasNextLine()).isFalse(); }
FileUtils.deleteDirectory(tempDir.toFile()); result = new LegacyCommandResult(exportedLogsZipFile); } finally { ExportLogsFunction.destroyExportLogsRegion(cache);