Search in sources :

Example 11 with Row

use of org.apache.geode.management.internal.cli.result.TableBuilder.Row in project geode by apache.

the class TableBuilderJUnitTest method testExceptionTooSmallWidth.

@Test(expected = TableBuilderHelper.TooManyColumnsException.class)
public void testExceptionTooSmallWidth() throws Exception {
    when(TableBuilderHelper.class, "getScreenWidth").thenReturn(7);
    assertTrue(TableBuilderHelper.shouldTrimColumns());
    Table table = createTableStructure(3, "|", "A", "A", "A");
    RowGroup rowGroup = table.getLastRowGroup();
    Row row1 = rowGroup.newRow();
    row1.newLeftCol("12").newLeftCol("12").newLeftCol("12");
    // This should throw an exception
    List<String> result = validateTable(table, true);
}
Also used : Table(org.apache.geode.management.internal.cli.result.TableBuilder.Table) TableBuilderHelper(org.apache.geode.management.internal.cli.result.TableBuilderHelper) RowGroup(org.apache.geode.management.internal.cli.result.TableBuilder.RowGroup) Row(org.apache.geode.management.internal.cli.result.TableBuilder.Row) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 12 with Row

use of org.apache.geode.management.internal.cli.result.TableBuilder.Row in project geode by apache.

the class TableBuilderJUnitTest method testTooLittleSpaceOnNextToLastColumn.

@Test
public void testTooLittleSpaceOnNextToLastColumn() throws Exception {
    assertTrue(TableBuilderHelper.shouldTrimColumns());
    Table table = createTableStructure(4, "|");
    RowGroup rowGroup = table.getLastRowGroup();
    Row row1 = rowGroup.newRow();
    row1.newLeftCol("1").newLeftCol("123456789-").newLeftCol("123456789-123456789-123456789-").newLeftCol("123456789-123456789-12345");
    List<String> result = validateTable(table, true);
    // Check the last line
    assertEquals("1     |123456789-|123456789..|1234567..", result.get(3));
}
Also used : Table(org.apache.geode.management.internal.cli.result.TableBuilder.Table) RowGroup(org.apache.geode.management.internal.cli.result.TableBuilder.RowGroup) Row(org.apache.geode.management.internal.cli.result.TableBuilder.Row) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 13 with Row

use of org.apache.geode.management.internal.cli.result.TableBuilder.Row in project geode by apache.

the class CommandResult method buildSection.

private void buildSection(Table table, RowGroup parentRowGroup, GfJsonObject section, int depth) throws GfJsonException {
    Iterator<String> keys = section.keys();
    RowGroup rowGroup = null;
    if (parentRowGroup != null) {
        rowGroup = parentRowGroup;
    } else {
        rowGroup = table.newRowGroup();
    }
    addHeaderInRowGroup(rowGroup, section);
    while (keys.hasNext()) {
        String key = keys.next();
        Object object = section.get(key);
        // System.out.println(key +" : " + object);
        if (key.startsWith(CompositeResultData.TABLE_DATA_ACCESSOR)) {
            GfJsonObject tableObject = section.getJSONObject(key);
            addHeaderInTable(table, tableObject);
            RowGroup rowGroupForTable = table.newRowGroup();
            buildTable(rowGroupForTable, tableObject.getJSONObject(ResultData.RESULT_CONTENT));
            addFooterInTable(table, tableObject);
        } else if (key.startsWith(CompositeResultData.SECTION_DATA_ACCESSOR)) {
            GfJsonObject subSection = section.getJSONObject(key);
            buildSection(table, rowGroup, subSection, depth + 1);
        } else if (key.equals(CompositeResultData.SEPARATOR)) {
            String separatorString = section.getString(key);
            rowGroup.newRowSeparator(separatorString.charAt(0), true);
        } else if (key.equals(ResultData.RESULT_HEADER) || key.equals(ResultData.RESULT_FOOTER)) {
            // skip header & footer
            continue;
        } else {
            Row newRow = rowGroup.newRow();
            String prefix = "";
            for (int i = 0; i < depth; i++) {
                prefix += " . ";
            }
            String[] value = getValuesSeparatedByLines(object);
            if (value.length == 1) {
                newRow.newLeftCol(prefix + key).newLeftCol(value[0]);
            } else {
                if (value.length != 0) {
                    // possible when object == CliConstants.LINE_SEPARATOR
                    newRow.newLeftCol(prefix + key).newLeftCol(value[0]);
                    for (int i = 1; i < value.length; i++) {
                        newRow = rowGroup.newRow();
                        newRow.setColumnSeparator("   ");
                        newRow.newLeftCol("").newLeftCol(value[i]);
                    }
                } else {
                    newRow.newLeftCol(prefix + key).newLeftCol("");
                }
            }
        // System.out.println(key+" : "+object);
        }
    }
    addFooterInRowGroup(rowGroup, section);
}
Also used : GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) RowGroup(org.apache.geode.management.internal.cli.result.TableBuilder.RowGroup) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) Row(org.apache.geode.management.internal.cli.result.TableBuilder.Row)

Example 14 with Row

use of org.apache.geode.management.internal.cli.result.TableBuilder.Row in project geode by apache.

the class CommandResult method buildTable.

/**
   * @param rowGroup
   * @param content
   * @throws GfJsonException
   */
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 (AbstractResultData.BYTE_DATA_ACCESSOR.equals(object)) {
            // skip file data if any //TODO - make response format better
            continue;
        }
        headerRow.newCenterCol((String) 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 (AbstractResultData.BYTE_DATA_ACCESSOR.equals(object)) {
            // skip file data if any //TODO - make response format better
            continue;
        }
        GfJsonArray accumulatedData = content.getJSONArray((String) object);
        dataRows = buildRows(rowGroup, dataRows, accumulatedData);
    }
}
Also used : GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) Row(org.apache.geode.management.internal.cli.result.TableBuilder.Row)

Example 15 with Row

use of org.apache.geode.management.internal.cli.result.TableBuilder.Row in project geode by apache.

the class CommandResult method buildObjectSection.

private void buildObjectSection(Table table, RowGroup parentRowGroup, GfJsonObject object, int depth) throws GfJsonException {
    Iterator<String> keys = object.keys();
    RowGroup rowGroup = null;
    if (parentRowGroup != null) {
        rowGroup = parentRowGroup;
    } else {
        rowGroup = table.newRowGroup();
    }
    GfJsonArray nestedCollection = null;
    GfJsonObject nestedObject = null;
    GfJsonObject fieldDisplayNames = object.getJSONObject(CliJsonSerializable.FIELDS_TO_DISPLAYNAME_MAPPING);
    List<String> fieldsToSkipOnUI = null;
    if (object.has(CliJsonSerializable.FIELDS_TO_SKIP_ON_UI)) {
        GfJsonArray jsonArray = object.getJSONArray(CliJsonSerializable.FIELDS_TO_SKIP_ON_UI);
        ;
        fieldsToSkipOnUI = new ArrayList<String>();
        for (int i = 0; i < jsonArray.size(); i++) {
            fieldsToSkipOnUI.add(String.valueOf(jsonArray.get(i)));
        }
    }
    while (keys.hasNext()) {
        String key = keys.next();
        if (CliJsonSerializable.FIELDS_TO_SKIP.contains(key) || (fieldsToSkipOnUI != null && fieldsToSkipOnUI.contains(key))) {
            continue;
        }
        try {
            nestedCollection = object.getJSONArray(key);
        } catch (GfJsonException e) {
        /* next check if it's a nested object */
        }
        Object field = null;
        if (nestedCollection == null) {
            field = object.get(key);
            if (!isPrimitiveOrStringOrWrapper(field)) {
                nestedObject = object.getJSONObject(key);
            }
        }
        if (nestedCollection != null && isPrimitiveOrStringOrWrapperArray(nestedCollection)) {
            String str = nestedCollection.toString();
            field = str.substring(1, str.length() - 1);
            nestedCollection = null;
        }
        Row newRow = rowGroup.newRow();
        String prefix = "";
        /* if (nestedCollection != null) */
        {
            for (int i = 0; i < depth; i++) {
                prefix += " . ";
            }
        }
        String fieldNameToDisplay = fieldDisplayNames.getString(key);
        if (nestedCollection == null) {
            newRow.newLeftCol(prefix + fieldNameToDisplay);
        }
        if (nestedCollection != null) {
            Map<String, Integer> columnsMap = new HashMap<String, Integer>();
            GfJsonArray rowsArray = nestedCollection;
            RowGroup newRowGroup = table.newRowGroup();
            newRowGroup.setColumnSeparator(" | ");
            newRowGroup.newBlankRow();
            newRowGroup.newRow().newLeftCol(fieldNameToDisplay);
            Row headerRow = newRowGroup.newRow();
            int numOfRows = rowsArray.size();
            List<String> tableFieldsToSkipOnUI = null;
            for (int j = 0; j < numOfRows; j++) {
                GfJsonObject content = rowsArray.getJSONObject(j);
                if (content.has(CliJsonSerializable.FIELDS_TO_SKIP_ON_UI)) {
                    GfJsonArray jsonArray = content.getJSONArray(CliJsonSerializable.FIELDS_TO_SKIP_ON_UI);
                    tableFieldsToSkipOnUI = new ArrayList<String>();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        tableFieldsToSkipOnUI.add(String.valueOf(jsonArray.get(i)));
                    }
                }
                GfJsonArray columnNames = content.names();
                int numOfColumns = columnNames.size();
                if (headerRow.isEmpty()) {
                    GfJsonObject innerFieldDisplayNames = content.getJSONObject(CliJsonSerializable.FIELDS_TO_DISPLAYNAME_MAPPING);
                    for (int i = 0; i < numOfColumns; i++) {
                        Object columnName = columnNames.get(i);
                        if (CliJsonSerializable.FIELDS_TO_SKIP.contains((String) columnName) || (tableFieldsToSkipOnUI != null && tableFieldsToSkipOnUI.contains(columnName))) {
                            // skip file data if any //TODO - make response format better
                            continue;
                        }
                        headerRow.newCenterCol(innerFieldDisplayNames.getString((String) columnName));
                        columnsMap.put((String) columnName, i);
                    }
                    newRowGroup.newRowSeparator('-', false);
                }
                newRow = newRowGroup.newRow();
                for (int i = 0; i < numOfColumns; i++) {
                    Object columnName = columnNames.get(i);
                    if (CliJsonSerializable.FIELDS_TO_SKIP.contains((String) columnName) || (tableFieldsToSkipOnUI != null && tableFieldsToSkipOnUI.contains(columnName))) {
                        // skip file data if any //TODO - make response format better
                        continue;
                    }
                    newRow.newLeftCol(String.valueOf(content.get((String) columnName)));
                }
            }
        } else if (nestedObject != null) {
            buildObjectSection(table, rowGroup, nestedObject, depth + 1);
        } else {
            // Row newRow = rowGroup.newRow();
            // String prefix = "";
            // for (int i = 0; i < depth; i++) {
            // prefix += " . ";
            // }
            // newRow.newLeftCol(prefix+fieldDisplayNames.getString(key)).newLeftCol(field);
            Object value = field;
            /*
         * if (isPrimitiveOrStringOrWrapperArray(value)) { value = Arrays.toString((String[])value);
         * }
         */
            newRow.newLeftCol(value);
        }
        nestedCollection = null;
        nestedObject = null;
    }
}
Also used : GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) HashMap(java.util.HashMap) RowGroup(org.apache.geode.management.internal.cli.result.TableBuilder.RowGroup) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) Row(org.apache.geode.management.internal.cli.result.TableBuilder.Row)

Aggregations

Row (org.apache.geode.management.internal.cli.result.TableBuilder.Row)15 RowGroup (org.apache.geode.management.internal.cli.result.TableBuilder.RowGroup)14 Table (org.apache.geode.management.internal.cli.result.TableBuilder.Table)12 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)11 Test (org.junit.Test)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)11 GfJsonObject (org.apache.geode.management.internal.cli.json.GfJsonObject)3 TableBuilderHelper (org.apache.geode.management.internal.cli.result.TableBuilderHelper)3 GfJsonArray (org.apache.geode.management.internal.cli.json.GfJsonArray)2 HashMap (java.util.HashMap)1 GfJsonException (org.apache.geode.management.internal.cli.json.GfJsonException)1