Java Code Examples for org.codehaus.jettison.json.JSONObject#getJSONArray()
The following examples show how to use
org.codehaus.jettison.json.JSONObject#getJSONArray() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestRMWebServicesApps.java From hadoop with Apache License 2.0 | 6 votes |
@Test public void testAppsQueryFinalStatus() throws JSONException, Exception { rm.start(); MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048); RMApp app1 = rm.submitApp(CONTAINER_MB); amNodeManager.nodeHeartbeat(true); WebResource r = resource(); ClientResponse response = r.path("ws").path("v1").path("cluster") .path("apps").queryParam("finalStatus", FinalApplicationStatus.UNDEFINED.toString()) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); System.out.println(json.toString()); JSONObject apps = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, apps.length()); JSONArray array = apps.getJSONArray("app"); assertEquals("incorrect number of elements", 1, array.length()); verifyAppInfo(array.getJSONObject(0), app1); rm.stop(); }
Example 2
Source File: TestAMWebServicesTasks.java From big-c with Apache License 2.0 | 6 votes |
@Test public void testTasksDefault() throws JSONException, Exception { WebResource r = resource(); Map<JobId, Job> jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); ClientResponse response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks").get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject tasks = json.getJSONObject("tasks"); JSONArray arr = tasks.getJSONArray("task"); assertEquals("incorrect number of elements", 2, arr.length()); verifyAMTask(arr, jobsMap.get(id), null); } }
Example 3
Source File: TestHsWebServicesAttempts.java From big-c with Apache License 2.0 | 6 votes |
public void verifyHsJobTaskAttemptCounters(JSONObject info, TaskAttempt att) throws JSONException { assertEquals("incorrect number of elements", 2, info.length()); WebServicesTestUtils.checkStringMatch("id", MRApps.toString(att.getID()), info.getString("id")); // just do simple verification of fields - not data is correct // in the fields JSONArray counterGroups = info.getJSONArray("taskAttemptCounterGroup"); for (int i = 0; i < counterGroups.length(); i++) { JSONObject counterGroup = counterGroups.getJSONObject(i); String name = counterGroup.getString("counterGroupName"); assertTrue("name not set", (name != null && !name.isEmpty())); JSONArray counters = counterGroup.getJSONArray("counter"); for (int j = 0; j < counters.length(); j++) { JSONObject counter = counters.getJSONObject(j); String counterName = counter.getString("name"); assertTrue("name not set", (counterName != null && !counterName.isEmpty())); long value = counter.getLong("value"); assertTrue("value >= 0", value >= 0); } } }
Example 4
Source File: LivyRestExecutor.java From kylin-on-parquet-v2 with Apache License 2.0 | 6 votes |
private List<String> getLogs(JSONObject logInfo) { List<String> logs = Lists.newArrayList(); if (logInfo.has("log")) { try { JSONArray logArray = logInfo.getJSONArray("log"); for (int i=0; i<logArray.length(); i++) { logs.add(logArray.getString(i)); } } catch (JSONException e) { e.printStackTrace(); } } return logs; }
Example 5
Source File: TestAMWebServicesTasks.java From hadoop with Apache License 2.0 | 6 votes |
@Test public void testTasksQueryReduce() throws JSONException, Exception { WebResource r = resource(); Map<JobId, Job> jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); String type = "r"; ClientResponse response = r.path("ws").path("v1").path("mapreduce") .path("jobs").path(jobId).path("tasks").queryParam("type", type) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject tasks = json.getJSONObject("tasks"); JSONArray arr = tasks.getJSONArray("task"); assertEquals("incorrect number of elements", 1, arr.length()); verifyAMTask(arr, jobsMap.get(id), type); } }
Example 6
Source File: TestAMWebServicesJobConf.java From big-c with Apache License 2.0 | 6 votes |
public void verifyAMJobConf(JSONObject info, Job job) throws JSONException { assertEquals("incorrect number of elements", 2, info.length()); WebServicesTestUtils.checkStringMatch("path", job.getConfFile().toString(), info.getString("path")); // just do simple verification of fields - not data is correct // in the fields JSONArray properties = info.getJSONArray("property"); for (int i = 0; i < properties.length(); i++) { JSONObject prop = properties.getJSONObject(i); String name = prop.getString("name"); String value = prop.getString("value"); assertTrue("name not set", (name != null && !name.isEmpty())); assertTrue("value not set", (value != null && !value.isEmpty())); } }
Example 7
Source File: TestHsWebServicesJobs.java From hadoop with Apache License 2.0 | 6 votes |
@Test public void testJobsDefault() throws JSONException, Exception { WebResource r = resource(); ClientResponse response = r.path("ws").path("v1").path("history") .path("mapreduce").path("jobs").get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject jobs = json.getJSONObject("jobs"); JSONArray arr = jobs.getJSONArray("job"); assertEquals("incorrect number of elements", 1, arr.length()); JSONObject info = arr.getJSONObject(0); Job job = appContext.getPartialJob(MRApps.toJobID(info.getString("id"))); VerifyJobsUtils.verifyHsJobPartial(info, job); }
Example 8
Source File: TestAHSWebServices.java From big-c with Apache License 2.0 | 6 votes |
@Test public void testMultipleAttempts() throws Exception { ApplicationId appId = ApplicationId.newInstance(0, 1); WebResource r = resource(); ClientResponse response = r.path("ws").path("v1").path("applicationhistory").path("apps") .path(appId.toString()).path("appattempts") .queryParam("user.name", USERS[round]) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); if (round == 1) { assertEquals( Status.FORBIDDEN, response.getClientResponseStatus()); return; } assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject appAttempts = json.getJSONObject("appAttempts"); assertEquals("incorrect number of elements", 1, appAttempts.length()); JSONArray array = appAttempts.getJSONArray("appAttempt"); assertEquals("incorrect number of elements", 5, array.length()); }
Example 9
Source File: TestNMWebServicesApps.java From big-c with Apache License 2.0 | 6 votes |
@Test public void testNodeAppsState() throws JSONException, Exception { WebResource r = resource(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); MockApp app2 = new MockApp("foo", 1234, 2); nmContext.getApplications().put(app2.getAppId(), app2); HashMap<String, String> hash2 = addAppContainers(app2); app2.setState(ApplicationState.RUNNING); ClientResponse response = r.path("ws").path("v1").path("node").path("apps") .queryParam("state", ApplicationState.RUNNING.toString()) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); JSONObject info = json.getJSONObject("apps"); assertEquals("incorrect number of elements", 1, info.length()); JSONArray appInfo = info.getJSONArray("app"); assertEquals("incorrect number of elements", 1, appInfo.length()); verifyNodeAppInfo(appInfo.getJSONObject(0), app2, hash2); }
Example 10
Source File: TestRMWebServicesNodes.java From hadoop with Apache License 2.0 | 6 votes |
@Test public void testNodesQueryRunning() throws JSONException, Exception { WebResource r = resource(); MockNM nm1 = rm.registerNode("h1:1234", 5120); MockNM nm2 = rm.registerNode("h2:1235", 5121); rm.sendNodeStarted(nm1); rm.NMwaitForState(nm1.getNodeId(), NodeState.RUNNING); rm.NMwaitForState(nm2.getNodeId(), NodeState.NEW); ClientResponse response = r.path("ws").path("v1").path("cluster") .path("nodes").queryParam("states", "running") .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject nodes = json.getJSONObject("nodes"); assertEquals("incorrect number of elements", 1, nodes.length()); JSONArray nodeArray = nodes.getJSONArray("node"); assertEquals("incorrect number of elements", 1, nodeArray.length()); }
Example 11
Source File: TestHsWebServicesAttempts.java From hadoop with Apache License 2.0 | 6 votes |
public void verifyHsTaskAttempts(JSONObject json, Task task) throws JSONException { assertEquals("incorrect number of elements", 1, json.length()); JSONObject attempts = json.getJSONObject("taskAttempts"); assertEquals("incorrect number of elements", 1, json.length()); JSONArray arr = attempts.getJSONArray("taskAttempt"); for (TaskAttempt att : task.getAttempts().values()) { TaskAttemptId id = att.getID(); String attid = MRApps.toString(id); Boolean found = false; for (int i = 0; i < arr.length(); i++) { JSONObject info = arr.getJSONObject(i); if (attid.matches(info.getString("id"))) { found = true; verifyHsTaskAttempt(info, att, task.getType()); } } assertTrue("task attempt with id: " + attid + " not in web service output", found); } }
Example 12
Source File: TestHsWebServicesAttempts.java From hadoop with Apache License 2.0 | 6 votes |
public void verifyHsJobTaskAttemptCounters(JSONObject info, TaskAttempt att) throws JSONException { assertEquals("incorrect number of elements", 2, info.length()); WebServicesTestUtils.checkStringMatch("id", MRApps.toString(att.getID()), info.getString("id")); // just do simple verification of fields - not data is correct // in the fields JSONArray counterGroups = info.getJSONArray("taskAttemptCounterGroup"); for (int i = 0; i < counterGroups.length(); i++) { JSONObject counterGroup = counterGroups.getJSONObject(i); String name = counterGroup.getString("counterGroupName"); assertTrue("name not set", (name != null && !name.isEmpty())); JSONArray counters = counterGroup.getJSONArray("counter"); for (int j = 0; j < counters.length(); j++) { JSONObject counter = counters.getJSONObject(j); String counterName = counter.getString("name"); assertTrue("name not set", (counterName != null && !counterName.isEmpty())); long value = counter.getLong("value"); assertTrue("value >= 0", value >= 0); } } }
Example 13
Source File: TestAHSWebServices.java From hadoop with Apache License 2.0 | 6 votes |
@Test public void testMultipleAttempts() throws Exception { ApplicationId appId = ApplicationId.newInstance(0, 1); WebResource r = resource(); ClientResponse response = r.path("ws").path("v1").path("applicationhistory").path("apps") .path(appId.toString()).path("appattempts") .queryParam("user.name", USERS[round]) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); if (round == 1) { assertEquals( Status.FORBIDDEN, response.getClientResponseStatus()); return; } assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject appAttempts = json.getJSONObject("appAttempts"); assertEquals("incorrect number of elements", 1, appAttempts.length()); JSONArray array = appAttempts.getJSONArray("appAttempt"); assertEquals("incorrect number of elements", 5, array.length()); }
Example 14
Source File: TestNMWebServicesApps.java From hadoop with Apache License 2.0 | 5 votes |
public void verifyNodeAppInfo(JSONObject info, Application app, HashMap<String, String> hash) throws JSONException, Exception { assertEquals("incorrect number of elements", 4, info.length()); verifyNodeAppInfoGeneric(app, info.getString("id"), info.getString("state"), info.getString("user")); JSONArray containerids = info.getJSONArray("containerids"); for (int i = 0; i < containerids.length(); i++) { String id = containerids.getString(i); assertEquals("extra containerid: " + id, id, hash.remove(id)); } assertTrue("missing containerids", hash.isEmpty()); }
Example 15
Source File: TestRMWebServicesNodes.java From hadoop with Apache License 2.0 | 5 votes |
public void testNodesHelper(String path, String media) throws JSONException, Exception { WebResource r = resource(); MockNM nm1 = rm.registerNode("h1:1234", 5120); MockNM nm2 = rm.registerNode("h2:1235", 5121); rm.sendNodeStarted(nm1); rm.sendNodeStarted(nm2); rm.NMwaitForState(nm1.getNodeId(), NodeState.RUNNING); rm.NMwaitForState(nm2.getNodeId(), NodeState.RUNNING); ClientResponse response = r.path("ws").path("v1").path("cluster") .path(path).accept(media).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject nodes = json.getJSONObject("nodes"); assertEquals("incorrect number of elements", 1, nodes.length()); JSONArray nodeArray = nodes.getJSONArray("node"); assertEquals("incorrect number of elements", 2, nodeArray.length()); JSONObject info = nodeArray.getJSONObject(0); String id = info.get("id").toString(); if (id.matches("h1:1234")) { verifyNodeInfo(info, nm1); verifyNodeInfo(nodeArray.getJSONObject(1), nm2); } else { verifyNodeInfo(info, nm2); verifyNodeInfo(nodeArray.getJSONObject(1), nm1); } }
Example 16
Source File: ConvertSprintBacklogTest.java From ezScrum with GNU General Public License v2.0 | 5 votes |
@Test public void testGetSprintBacklogJsonString() throws Exception { AddStoryToSprint ASTS = new AddStoryToSprint(3, 5, mCS, mCP, "EST"); ASTS.exe(); AddTaskToStory ATTS = new AddTaskToStory(3, 5, ASTS, mCP); ATTS.exe(); long sprintId = 1; SprintObject sprint = SprintObject.get(sprintId); // give incorrect sprint id String response = ConvertSprintBacklog.getSprintBacklogJsonString(null); assertEquals("", response); // give correct sprint id response = ConvertSprintBacklog.getSprintBacklogJsonString(sprint); JSONObject responseJson = new JSONObject(response); assertEquals("true", responseJson.getString("success")); assertEquals(sprintId, responseJson.getLong("sprintId")); assertEquals(3, responseJson.getInt("Total")); JSONArray storiesJsonArray = responseJson.getJSONArray("Stories"); assertEquals(3, storiesJsonArray.length()); for (int i = 0; i <3; i++) { JSONObject storyJson = storiesJsonArray.getJSONObject(i); JSONArray tasksJsonArray = storyJson.getJSONArray("tasks"); assertEquals(3, tasksJsonArray.length()); } }
Example 17
Source File: TestHsWebServicesJobsQuery.java From hadoop with Apache License 2.0 | 5 votes |
@Test public void testJobsQueryQueue() throws JSONException, Exception { WebResource r = resource(); ClientResponse response = r.path("ws").path("v1").path("history") .path("mapreduce").path("jobs").queryParam("queue", "mockqueue") .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertEquals("incorrect number of elements", 1, json.length()); JSONObject jobs = json.getJSONObject("jobs"); JSONArray arr = jobs.getJSONArray("job"); assertEquals("incorrect number of elements", 3, arr.length()); }
Example 18
Source File: GraphBackedDiscoveryServiceTest.java From incubator-atlas with Apache License 2.0 | 4 votes |
@Test(dataProvider = "dslOrderByQueriesProvider") public void testSearchByDSLQueriesWithOrderBy(String dslQuery, Integer expectedNumRows, String orderBy, boolean ascending) throws Exception { System.out.println("Executing dslQuery = " + dslQuery); String jsonResults = searchByDSL(dslQuery); assertNotNull(jsonResults); JSONObject results = new JSONObject(jsonResults); assertEquals(results.length(), 3); Object query = results.get("query"); assertNotNull(query); JSONObject dataType = results.getJSONObject("dataType"); assertNotNull(dataType); String typeName = dataType.getString("typeName"); assertNotNull(typeName); JSONArray rows = results.getJSONArray("rows"); assertNotNull(rows); assertEquals(rows.length(), expectedNumRows.intValue()); // some queries may not have any results List<String> returnedList = new ArrayList<>(); for (int i = 0; i < rows.length(); i++) { JSONObject row = rows.getJSONObject(i); try { returnedList.add(row.get(orderBy).toString()); } catch(Exception ex) { System.out.println( " Exception occured " + ex.getMessage() + " found row: "+row); } } Iterator<String> iter = returnedList.iterator(); String _current = null, _prev = null; if (orderBy != null) { // Following code compares the results in rows and makes sure data // is sorted as expected while (iter.hasNext()) { _prev = _current; _current = iter.next().toLowerCase(); if (_prev != null && _prev.compareTo(_current) != 0) { if(ascending) { Assert.assertTrue(_prev.compareTo(_current) < 0, _prev + " is greater than " + _current); } else { Assert.assertTrue(_prev.compareTo(_current) > 0, _prev + " is less than " + _current); } } } } System.out.println("query [" + dslQuery + "] returned [" + rows.length() + "] rows"); }
Example 19
Source File: TranslationTest.java From ezScrum with GNU General Public License v2.0 | 4 votes |
@Test public void testTranslateBurndownChartDataToJson() throws JSONException { LinkedHashMap<Date, Double> dateToStoryIdealPoint = new LinkedHashMap<Date, Double>(); // Story的理想線 LinkedHashMap<Date, Double> dateToStoryPoint = new LinkedHashMap<Date, Double>(); // Story的真實線 Date date1 = DateUtil.dayFillter("2015/04/06-13:14:01", DateUtil._8DIGIT_DATE_1); Date date2 = DateUtil.dayFillter("2015/04/07-10:14:01", DateUtil._8DIGIT_DATE_1); Date date3 = DateUtil.dayFillter("2015/04/08-15:14:01", DateUtil._8DIGIT_DATE_1); Date date4 = DateUtil.dayFillter("2015/04/09-16:14:01", DateUtil._8DIGIT_DATE_1); Date date5 = DateUtil.dayFillter("2015/04/10-12:14:01", DateUtil._8DIGIT_DATE_1); dateToStoryIdealPoint.put(date1, 10d); dateToStoryIdealPoint.put(date2, 7.5d); dateToStoryIdealPoint.put(date3, 5d); dateToStoryIdealPoint.put(date4, 2.5d); dateToStoryIdealPoint.put(date5, 0d); dateToStoryPoint.put(date1, 10d); dateToStoryPoint.put(date2, 10d); dateToStoryPoint.put(date3, 10d); dateToStoryPoint.put(date4, 5d); dateToStoryPoint.put(date5, 5d); String result = Translation.translateBurndownChartDataToJson(dateToStoryIdealPoint, dateToStoryPoint); JSONObject actualJson = new JSONObject(result); assertTrue(actualJson.getBoolean("success")); JSONArray points = actualJson.getJSONArray("Points"); // day 1 JSONObject pointOfDay1 = points.getJSONObject(0); assertEquals("2015/04/06", pointOfDay1.getString("Date")); assertEquals(10d, pointOfDay1.getDouble("IdealPoint")); assertEquals(10d, pointOfDay1.getDouble("RealPoint")); // day 2 JSONObject pointOfDay2 = points.getJSONObject(1); assertEquals("2015/04/07", pointOfDay2.getString("Date")); assertEquals(7.5d, pointOfDay2.getDouble("IdealPoint")); assertEquals(10d, pointOfDay2.getDouble("RealPoint")); // day 3 JSONObject pointOfDay3 = points.getJSONObject(2); assertEquals("2015/04/08", pointOfDay3.getString("Date")); assertEquals(5d, pointOfDay3.getDouble("IdealPoint")); assertEquals(10d, pointOfDay3.getDouble("RealPoint")); // day 4 JSONObject pointOfDay4 = points.getJSONObject(3); assertEquals("2015/04/09", pointOfDay4.getString("Date")); assertEquals(2.5d, pointOfDay4.getDouble("IdealPoint")); assertEquals(5d, pointOfDay4.getDouble("RealPoint")); // day 5 JSONObject pointOfDay5 = points.getJSONObject(4); assertEquals("2015/04/10", pointOfDay5.getString("Date")); assertEquals(0d, pointOfDay5.getDouble("IdealPoint")); assertEquals(5d, pointOfDay5.getDouble("RealPoint")); }
Example 20
Source File: Compiler.java From appinventor-extensions with Apache License 2.0 | 4 votes |
private void loadJsonInfo(ConcurrentMap<String, Set<String>> infoMap, String targetInfo) throws IOException, JSONException { synchronized (infoMap) { if (!infoMap.isEmpty()) { return; } JSONArray buildInfo = new JSONArray( "[" + simpleCompsBuildInfo.join(",") + "," + extCompsBuildInfo.join(",") + "]"); for (int i = 0; i < buildInfo.length(); ++i) { JSONObject compJson = buildInfo.getJSONObject(i); JSONArray infoArray = null; String type = compJson.getString("type"); try { infoArray = compJson.getJSONArray(targetInfo); } catch (JSONException e) { // Older compiled extensions will not have a broadcastReceiver // defined. Rather then require them all to be recompiled, we // treat the missing attribute as empty. if (e.getMessage().contains("broadcastReceiver")) { LOG.log(Level.INFO, "Component \"" + type + "\" does not have a broadcast receiver."); continue; } else if (e.getMessage().contains(ComponentDescriptorConstants.ANDROIDMINSDK_TARGET)) { LOG.log(Level.INFO, "Component \"" + type + "\" does not specify a minimum SDK."); continue; } else { throw e; } } if (!simpleCompTypes.contains(type) && !extCompTypes.contains(type)) { continue; } Set<String> infoSet = Sets.newHashSet(); for (int j = 0; j < infoArray.length(); ++j) { String info = infoArray.getString(j); if (!info.isEmpty()) { infoSet.add(info); } } if (!infoSet.isEmpty()) { infoMap.put(type, infoSet); } processConditionalInfo(compJson, type, targetInfo); } } }