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);
}
}
}
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");
}
}
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 + "\"]"));
}
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());
}
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()));
}
Aggregations