Search in sources :

Example 16 with GfJsonException

use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.

the class DataCommandResult method pageResult.

/**
   * This method returns a "Page" as dictated by arguments startCount and endCount. Returned result
   * is not standard CommandResult and its consumed by Display Step
   */
@SuppressWarnings({ "rawtypes", "unchecked" })
public Result pageResult(int startCount, int endCount, String step) {
    List<String> fields = new ArrayList<>();
    List values = new ArrayList<String>();
    fields.add(RESULT_FLAG);
    values.add(operationCompletedSuccessfully);
    fields.add(QUERY_PAGE_START);
    values.add(startCount);
    fields.add(QUERY_PAGE_END);
    values.add(endCount);
    if (errorString != null) {
        fields.add("Message");
        values.add(errorString);
        return createBannerResult(fields, values, step);
    } else {
        if (infoString != null) {
            fields.add("Message");
            values.add(infoString);
        }
        if (selectResult != null) {
            try {
                TabularResultData table = ResultBuilder.createTabularResultData();
                String[] headers;
                Object[][] rows;
                int rowCount = buildTable(table, startCount, endCount);
                GfJsonArray array = table.getHeaders();
                headers = new String[array.size()];
                rows = new Object[rowCount][array.size()];
                for (int i = 0; i < array.size(); i++) {
                    headers[i] = (String) array.get(i);
                    List<String> list = table.retrieveAllValues(headers[i]);
                    for (int j = 0; j < list.size(); j++) {
                        rows[j][i] = list.get(j);
                    }
                }
                fields.add(NUM_ROWS);
                values.add((selectResult == null) ? 0 : selectResult.size());
                if (queryTraceString != null) {
                    fields.add(QUERY_TRACE);
                    values.add(queryTraceString);
                }
                return createPageResult(fields, values, step, headers, rows);
            } catch (GfJsonException e) {
                String[] headers = new String[] { "Error" };
                Object[][] rows = { { e.getMessage() } };
                String[] fieldsArray = { QUERY_PAGE_START, QUERY_PAGE_END };
                Object[] valuesArray = { startCount, endCount };
                return createPageResult(fieldsArray, valuesArray, step, headers, rows);
            }
        } else {
            return createBannerResult(fields, values, step);
        }
    }
}
Also used : GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) ArrayList(java.util.ArrayList) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) ArrayList(java.util.ArrayList) List(java.util.List) JSONObject(org.json.JSONObject) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject)

Example 17 with GfJsonException

use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.

the class TypedJsonJUnitTest method checkResult.

public void checkResult(TypedJson tjson) {
    GfJsonObject gfJsonObj;
    try {
        gfJsonObj = new GfJsonObject(tjson.toString());
        System.out.println(gfJsonObj);
        assertNotNull(gfJsonObj.get(RESULT));
    } catch (GfJsonException e) {
        fail("Result could not be found");
    }
}
Also used : GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException)

Example 18 with GfJsonException

use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.

the class FunctionCommandsDUnitTest method testDestroyOnGroups.

@Test
public void testDestroyOnGroups() {
    Properties localProps = new Properties();
    localProps.setProperty(NAME, "Manager");
    localProps.setProperty(GROUPS, "Group0");
    setUpJmxManagerOnVm0ThenConnect(localProps);
    Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
    FunctionService.registerFunction(function);
    VM vm1 = Host.getHost(0).getVM(1);
    VM vm2 = Host.getHost(0).getVM(2);
    String vm1id = (String) vm1.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Properties localProps = new Properties();
            localProps.setProperty(GROUPS, "Group1");
            getSystem(localProps);
            Cache cache = getCache();
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
            FunctionService.registerFunction(function);
            return cache.getDistributedSystem().getDistributedMember().getId();
        }
    });
    String vm2id = (String) vm2.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Properties localProps = new Properties();
            localProps.setProperty(GROUPS, "Group2");
            getSystem(localProps);
            Cache cache = getCache();
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
            FunctionService.registerFunction(function);
            return cache.getDistributedSystem().getDistributedMember().getId();
        }
    });
    Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {

        public void run() {
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
            FunctionService.registerFunction(function);
        }
    });
    String command = "destroy function --id=" + TestFunction.TEST_FUNCTION1 + " --groups=Group1,Group2";
    getLogWriter().info("testDestroyOnGroups command=" + command);
    CommandResult cmdResult = executeCommand(command);
    getLogWriter().info("testDestroyOnGroups cmdResult=" + cmdResult);
    assertEquals(Result.Status.OK, cmdResult.getStatus());
    String content = null;
    try {
        content = cmdResult.getContent().get("message").toString();
        getLogWriter().info("testDestroyOnGroups content = " + content);
    } catch (GfJsonException e) {
        fail("testDestroyOnGroups exception=" + e);
    }
    assertNotNull(content);
    assertTrue(content.equals("[\"Destroyed " + TestFunction.TEST_FUNCTION1 + " Successfully on " + vm1id + "," + vm2id + "\"]") || content.equals("[\"Destroyed " + TestFunction.TEST_FUNCTION1 + " Successfully on " + vm2id + "," + vm1id + "\"]"));
}
Also used : Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) Cache(org.apache.geode.cache.Cache) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 19 with GfJsonException

use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.

the class GemfireDataCommandsDUnitTest method validateSelectResult.

private void validateSelectResult(CommandResult cmdResult, boolean expectedFlag, int expectedRows, String[] cols) {
    if (ResultData.TYPE_COMPOSITE.equals(cmdResult.getType())) {
        CompositeResultData rd = (CompositeResultData) cmdResult.getResultData();
        SectionResultData section = rd.retrieveSectionByIndex(0);
        boolean result = (Boolean) section.retrieveObject("Result");
        assertEquals(expectedFlag, result);
        if (expectedFlag && expectedRows != -1) {
            int rowsReturned = (Integer) section.retrieveObject("Rows");
            assertEquals(expectedRows, rowsReturned);
            if (rowsReturned > 0 && cols != null) {
                try {
                    TabularResultData table = section.retrieveTableByIndex(0);
                    GfJsonArray array = table.getHeaders();
                    assertEquals(cols.length, array.size());
                    for (String col : cols) {
                        boolean found = false;
                        getLogWriter().info("Validating column " + col);
                        for (int i = 0; i < array.size(); i++) {
                            String header = (String) array.get(i);
                            if (col.equals(header))
                                found = true;
                        }
                        assertEquals(true, found);
                    }
                } catch (GfJsonException e) {
                    fail("Error accessing table data", e);
                }
            }
        }
    } else
        fail("Expected CompositeResult Returned Result Type " + cmdResult.getType());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) CompositeResultData(org.apache.geode.management.internal.cli.result.CompositeResultData) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) SectionResultData(org.apache.geode.management.internal.cli.result.CompositeResultData.SectionResultData)

Example 20 with GfJsonException

use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.

the class DataCommandFunction method select_NonSelectResults.

private void select_NonSelectResults(Object results, List<SelectResultRow> list) {
    if (logger.isDebugEnabled()) {
        logger.debug("BeanResults : Bean Results class is {}", results.getClass());
    }
    String str = toJson(results);
    GfJsonObject jsonBean;
    try {
        jsonBean = new GfJsonObject(str);
    } catch (GfJsonException e) {
        logger.info("Exception occurred:", e);
        jsonBean = new GfJsonObject();
        try {
            jsonBean.put("msg", e.getMessage());
        } catch (GfJsonException e1) {
            logger.warn("Ignored GfJsonException:", e1);
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("BeanResults : Adding bean json string : {}", jsonBean);
    }
    list.add(new SelectResultRow(DataCommandResult.ROW_TYPE_BEAN, jsonBean.toString()));
}
Also used : GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) SelectResultRow(org.apache.geode.management.internal.cli.domain.DataCommandResult.SelectResultRow) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException)

Aggregations

GfJsonException (org.apache.geode.management.internal.cli.json.GfJsonException)34 GfJsonObject (org.apache.geode.management.internal.cli.json.GfJsonObject)27 GfJsonArray (org.apache.geode.management.internal.cli.json.GfJsonArray)14 JSONObject (org.json.JSONObject)9 ArrayList (java.util.ArrayList)6 ResultDataException (org.apache.geode.management.internal.cli.result.ResultDataException)4 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)4 JSONArray (org.json.JSONArray)4 JSONException (org.json.JSONException)4 HashMap (java.util.HashMap)3 SelectResultRow (org.apache.geode.management.internal.cli.domain.DataCommandResult.SelectResultRow)3 CompositeResultData (org.apache.geode.management.internal.cli.result.CompositeResultData)3 SectionResultData (org.apache.geode.management.internal.cli.result.CompositeResultData.SectionResultData)3 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 CliJsonSerializable (org.apache.geode.management.internal.cli.result.CliJsonSerializable)2 Table (org.apache.geode.management.internal.cli.result.TableBuilder.Table)2