Java Code Examples for org.codehaus.jettison.json.JSONArray

The following are top voted examples for showing how to use org.codehaus.jettison.json.JSONArray. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: smaph   File: SmaphDebugger.java   Source Code and License 7 votes vote down vote up
private JSONArray getAnnotatedSnippetS3(String query, WikipediaInterface wikiApi) throws JSONException, IOException {
	JSONArray res = new JSONArray();
	if (this.annotatedSnippetsAndBoldsS3.containsKey(query))
		for (Triple<String, HashSet<Annotation>, HashSet<Mention>> p : this.annotatedSnippetsAndBoldsS3.get(query)) {
			JSONObject pairJs = new JSONObject();
			res.put(pairJs);
			pairJs.put("snippet", p.getLeft());
			JSONArray annotationsJs = new JSONArray();
			pairJs.put("parts", annotationsJs);
			int lastIdx = 0;
			for (Annotation a : SmaphUtils.sorted(p.getMiddle())) {
				annotationsJs.put(getTextPartJson(p.getLeft(), lastIdx, a.getPosition(), p.getRight()));

				JSONObject annotationJs = getTextPartJson(p.getLeft(), a.getPosition(), a.getPosition() + a.getLength(),
						p.getRight());
				annotationsJs.put(annotationJs);
				annotationJs.put("title", wikiApi.getTitlebyId(a.getConcept()));
				annotationJs.put("wid", a.getConcept());
				annotationJs.put("url", widToUrl(a.getConcept(), wikiApi));
				lastIdx = a.getPosition() + a.getLength();
			}
			annotationsJs.put(getTextPartJson(p.getLeft(), lastIdx, p.getLeft().length(), p.getRight()));
		}
	return res;
}
 
Example 2
Project: convertigo-engine   File: AbstractFullSyncListener.java   Source Code and License 7 votes vote down vote up
public void onBulkDocs(HttpServletRequest request, final JSONArray array) {
	if (isEnabled()) {
		final InternalHttpServletRequest internalRequest = new InternalHttpServletRequest(request);
		Engine.execute(new Runnable() {

			@Override
			public void run() {
				try {
					triggerSequence(internalRequest, array);
				} catch (Exception e) {
					Engine.logBeans.error("Unable to handle 'bulkDocs' event for \""+ getName() +"\" listener", e);
				}
			}
			
		});
	}
}
 
Example 3
Project: smaph   File: SmaphDebugger.java   Source Code and License 6 votes vote down vote up
private JSONArray getEntityFeaturesJson(
		HashMap<String, List<Triple<Integer, HashMap<String, Double>, Boolean>>> source,
		String query, WikipediaInterface wikiApi) throws JSONException,
		IOException {
	JSONArray res = new JSONArray();
	if (source.containsKey(query))
		for (Triple<Integer, HashMap<String, Double>, Boolean> p : source
				.get(query)) {
			JSONObject pairJs = new JSONObject();
			res.put(pairJs);
			pairJs.put("wid", p.getLeft());
			pairJs.put("title", wikiApi.getTitlebyId(p.getLeft()));
			pairJs.put("url", widToUrl(p.getLeft(), wikiApi));
			JSONObject features = new JSONObject();
			pairJs.put("features", features);
			for (String ftrName : SmaphUtils.sorted(p.getMiddle().keySet()))
				features.put(ftrName, p.getMiddle().get(ftrName));
			pairJs.put("accepted", p.getRight());
		}
	return res;
}
 
Example 4
Project: smaph   File: SmaphDebugger.java   Source Code and License 6 votes vote down vote up
private JSONArray getSourceSearchResultJson(
		HashMap<String, List<Triple<Integer, String, Integer>>> source,
		String query, WikipediaInterface wikiApi) throws JSONException, IOException {
	JSONArray res = new JSONArray();
	if (source.containsKey(query))
		for (Triple<Integer, String, Integer> t : source.get(query)) {
			JSONObject triple = new JSONObject();
			res.put(triple);
			triple.put("rank", t.getLeft());
			triple.put("wid", t.getRight());
			triple.put("title",
					t.getRight() >= 0 ? wikiApi.getTitlebyId(t.getRight())
							: "---not a wikipedia page---");
			triple.put("url", t.getMiddle());
		}
	return res;
}
 
Example 5
Project: smaph   File: SmaphDebugger.java   Source Code and License 6 votes vote down vote up
private JSONArray getResultsJson(String query, WikipediaInterface wikiApi)
		throws JSONException, IOException {
	JSONArray res = new JSONArray();
	if (result.containsKey(query))
		for (ScoredAnnotation a: result.get(query)) {
			JSONObject triple = new JSONObject();
			res.put(triple);
			triple.put("begin", a.getPosition());
			triple.put("end", a.getPosition() + a.getLength());
			triple.put("score", a.getScore());
			triple.put("wid", a.getConcept());
			triple.put("title", wikiApi.getTitlebyId(a.getConcept()));
			triple.put("url", widToUrl(a.getConcept(), wikiApi));
		}
	return res;
}
 
Example 6
Project: elastic-job-cloud   File: MesosStateService.java   Source Code and License 6 votes vote down vote up
/**
 * 获取沙箱信息.
 * 
 * @param appName 作业云配置App的名字
 * @return 沙箱信息
 * @throws JSONException 解析JSON格式异常
 */
public JsonArray sandbox(final String appName) throws JSONException {
    JSONObject state = fetch(stateUrl);
    JsonArray result = new JsonArray();
    for (JSONObject each : findExecutors(state.getJSONArray("frameworks"), appName)) {
        JSONArray slaves = state.getJSONArray("slaves");
        String slaveHost = null;
        for (int i = 0; i < slaves.length(); i++) {
            JSONObject slave = slaves.getJSONObject(i);
            if (each.getString("slave_id").equals(slave.getString("id"))) {
                slaveHost = slave.getString("pid").split("@")[1];
            }
        }
        Preconditions.checkNotNull(slaveHost);
        JSONObject slaveState = fetch(String.format("http://%s/state", slaveHost));
        String workDir = slaveState.getJSONObject("flags").getString("work_dir");
        Collection<JSONObject> executorsOnSlave = findExecutors(slaveState.getJSONArray("frameworks"), appName);
        for (JSONObject executorOnSlave : executorsOnSlave) {
            JsonObject r = new JsonObject();
            r.addProperty("hostname", slaveState.getString("hostname"));
            r.addProperty("path", executorOnSlave.getString("directory").replace(workDir, ""));
            result.add(r);
        }
    }
    return result;
}
 
Example 7
Project: elastic-job-cloud   File: MesosStateService.java   Source Code and License 6 votes vote down vote up
private Collection<JSONObject> findExecutors(final JSONArray frameworks, final String appName) throws JSONException {
    List<JSONObject> result = Lists.newArrayList();
    Optional<String> frameworkIDOptional = frameworkIDService.fetch();
    String frameworkID;
    if (frameworkIDOptional.isPresent()) {
        frameworkID = frameworkIDOptional.get();
    } else {
        return result;
    }
    for (int i = 0; i < frameworks.length(); i++) {
        JSONObject framework = frameworks.getJSONObject(i);
        if (!framework.getString("id").equals(frameworkID)) {
            continue;
        }
        JSONArray executors = framework.getJSONArray("executors");
        for (int j = 0; j < executors.length(); j++) {
            JSONObject executor = executors.getJSONObject(j);
            if (null == appName || appName.equals(getExecutorId(executor).split("@[email protected]")[0])) {
                result.add(executor);
            }
        }
    }
    return result;
}
 
Example 8
Project: convertigo-engine   File: MobileResourceHelper.java   Source Code and License 6 votes vote down vote up
public void listFiles(JSONObject response) throws JSONException, IOException {
	File canonicalDir = destDir.getCanonicalFile();
	int uriDirectoryLength = canonicalDir.toURI().toString().length();
	JSONArray jArray = new JSONArray();
	
	for (File f : FileUtils.listFiles(canonicalDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE)) {
		File canonnicalF = f.getCanonicalFile();
		JSONObject jObj = new JSONObject();
		jObj.put("uri", URLDecoder.decode(canonnicalF.toURI().toString().substring(uriDirectoryLength), "UTF-8"));
		jObj.put("date", canonnicalF.lastModified());
		jObj.put("size", canonnicalF.length());
		jArray.put(jObj);
	}
	response.put("files", jArray);
	response.put("date", destDir.lastModified());
}
 
Example 9
Project: convertigo-engine   File: AuthenticatedSessionManager.java   Source Code and License 6 votes vote down vote up
public JSONObject exportUsers(JSONArray usernames) throws EngineException {
	try {
		JSONObject users;
		JSONObject export = new JSONObject();
		
		synchronized (this) {
			users = load();
		}

		for (int i = 0; i < usernames.length(); i++) {
			String name = usernames.getJSONObject(i).getString("name");
			if (users.has(name)) {
				export.put(name, users.get(name));
			}
		}
		return export;
	} catch (Exception exception) {
		throw new EngineException("Failed to update Users", exception);
	}
}
 
Example 10
Project: convertigo-engine   File: IonProperty.java   Source Code and License 6 votes vote down vote up
public IonProperty() {
	try {
		jsonProperty = new JSONObject()
			.put(Key.name.name(), "property")
			.put(Key.attr.name(), "")
			.put(Key.label.name(), "label")
			.put(Key.editor.name(), "")
			.put(Key.hidden.name(), false)
			.put(Key.composite.name(), false)
			.put(Key.description.name(), "description")
			.put(Key.category.name(), "Attributes")
			.put(Key.mode.name(), "plain")
			.put(Key.type.name(), "string")
			.put(Key.value.name(), false)
			.put(Key.values.name(), new JSONArray().put(false).put(true));
	} catch (JSONException e) {
		e.printStackTrace();
	}
}
 
Example 11
Project: convertigo-engine   File: IonConfig.java   Source Code and License 6 votes vote down vote up
protected Map<String, String> getCfgPlugins(Key key, String keyId) {
	try {
		Map<String, String> map = new HashMap<String, String>();
		JSONArray ar = jsonConfig.getJSONArray(key.name());
		for (int i=0; i<ar.length(); i++) {
			Object ob = ar.get(i);
			if (ob instanceof JSONObject) {
				JSONObject jsonImport = (JSONObject)ob;
				String plugin = jsonImport.getString(keyId);
				if (!plugin.isEmpty() && !jsonImport.toString().isEmpty()) {
					map.put(plugin, jsonImport.toString());
				}
			}
		}
		return map;
	} catch (JSONException e) {
		return new HashMap<String, String>();
	}
}
 
Example 12
Project: convertigo-engine   File: IonConfig.java   Source Code and License 6 votes vote down vote up
protected Map<String, String> getCfgImports(Key key, String key1, String key2) {
	try {
		Map<String, String> map = new HashMap<String, String>();
		JSONArray ar = jsonConfig.getJSONArray(key.name());
		for (int i=0; i<ar.length(); i++) {
			Object ob = ar.get(i);
			if (ob instanceof JSONObject) {
				JSONObject jsonImport = (JSONObject)ob;
				String val1 = jsonImport.getString(key1);
				String val2 = jsonImport.getString(key2);
				if (!val1.isEmpty() && !val2.isEmpty()) {
					map.put(val1, val2);
				}
			}
		}
		return map;
	} catch (JSONException e) {
		return new HashMap<String, String>();
	}
}
 
Example 13
Project: fpm   File: TollReader.java   Source Code and License 6 votes vote down vote up
private Map<Long, Toll> read(String tollsFile) {
    try {
        Map<Long, Toll> tolls = newHashMap();

        File file = new File(tollsFile);

        if (file.exists()) {
            JSONArray array = new JSONArray(IOUtils.toString(file.toURI(), UTF_8));
            for (int i = 0; i < array.length(); i++) {
                JSONObject json = array.getJSONObject(i);
                tolls.put(json.getLong("tomtomId"), new Toll(json.getInt("id"), json.getString("name"), json.getString("tollcode1"), json.optString("tollcode2")));
            }

            log.info("Loaded {} tolls", tolls.size());

        } else {
            log.info("File not found : {}", file.getAbsolutePath());
        }

        return tolls;
    }
    catch (IOException|JSONException e) {
        throw propagate(e);
    }
}
 
Example 14
Project: hadoop   File: TestNMWebServicesApps.java   Source Code and License 6 votes vote down vote up
@Test
public void testNodeAppsUser() throws JSONException, Exception {
  WebResource r = resource();
  Application app = new MockApp(1);
  nmContext.getApplications().put(app.getAppId(), app);
  HashMap<String, String> hash = addAppContainers(app);
  Application app2 = new MockApp("foo", 1234, 2);
  nmContext.getApplications().put(app2.getAppId(), app2);
  addAppContainers(app2);

  ClientResponse response = r.path("ws").path("v1").path("node").path("apps")
      .queryParam("user", "mockUser").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), app, hash);
}
 
Example 15
Project: hadoop   File: TestNMWebServicesApps.java   Source Code and License 6 votes vote down vote up
@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 16
Project: hadoop   File: TestRMWebServicesCapacitySched.java   Source Code and License 6 votes vote down vote up
private void verifyClusterScheduler(JSONObject json) throws JSONException,
    Exception {
  assertEquals("incorrect number of elements", 1, json.length());
  JSONObject info = json.getJSONObject("scheduler");
  assertEquals("incorrect number of elements", 1, info.length());
  info = info.getJSONObject("schedulerInfo");
  assertEquals("incorrect number of elements", 6, info.length());
  verifyClusterSchedulerGeneric(info.getString("type"),
      (float) info.getDouble("usedCapacity"),
      (float) info.getDouble("capacity"),
      (float) info.getDouble("maxCapacity"), info.getString("queueName"));

  JSONArray arr = info.getJSONObject("queues").getJSONArray("queue");
  assertEquals("incorrect number of elements", 2, arr.length());

  // test subqueues
  for (int i = 0; i < arr.length(); i++) {
    JSONObject obj = arr.getJSONObject(i);
    String q = CapacitySchedulerConfiguration.ROOT + "." + obj.getString("queueName");
    verifySubQueue(obj, q, 100, 100);
  }
}
 
Example 17
Project: hadoop   File: TestRMWebServicesNodes.java   Source Code and License 6 votes vote down vote up
@Test
public void testNodesQueryNew() 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", NodeState.NEW.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());
  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());
  JSONObject info = nodeArray.getJSONObject(0);

  verifyNodeInfo(info, nm2);
}
 
Example 18
Project: hadoop   File: TestRMWebServicesNodes.java   Source Code and License 6 votes vote down vote up
@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 19
Project: hadoop   File: TestRMWebServicesApps.java   Source Code and License 6 votes vote down vote up
public void testAppsHelper(String path, RMApp app, String media)
    throws JSONException, Exception {
  WebResource r = resource();

  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 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), app);

}
 
Example 20
Project: hadoop   File: TestRMWebServicesApps.java   Source Code and License 6 votes vote down vote up
@Test
public void testAppsQueryState() 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("state", YarnApplicationState.ACCEPTED.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());
  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 21
Project: hadoop   File: TestRMWebServicesApps.java   Source Code and License 6 votes vote down vote up
@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 22
Project: hadoop   File: TestRMWebServicesApps.java   Source Code and License 6 votes vote down vote up
@Test
public void testAppsQueryQueue() throws JSONException, Exception {
  rm.start();
  MockNM amNodeManager = rm.registerNode("127.0.0.1:1234", 2048);
  rm.submitApp(CONTAINER_MB);
  rm.submitApp(CONTAINER_MB);

  amNodeManager.nodeHeartbeat(true);
  WebResource r = resource();

  ClientResponse response = r.path("ws").path("v1").path("cluster")
      .path("apps").queryParam("queue", "default")
      .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 apps = json.getJSONObject("apps");
  assertEquals("incorrect number of elements", 1, apps.length());
  JSONArray array = apps.getJSONArray("app");
  assertEquals("incorrect number of elements", 2, array.length());
  rm.stop();
}
 
Example 23
Project: hadoop   File: TestRMWebServicesApps.java   Source Code and License 6 votes vote down vote up
@Test
public void testAppsQueryLimit() throws JSONException, Exception {
  rm.start();
  rm.registerNode("127.0.0.1:1234", 2048);
  rm.submitApp(CONTAINER_MB);
  rm.submitApp(CONTAINER_MB);
  rm.submitApp(CONTAINER_MB);
  WebResource r = resource();
  ClientResponse response = r.path("ws").path("v1").path("cluster")
      .path("apps").queryParam("limit", "2")
      .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 apps = json.getJSONObject("apps");
  assertEquals("incorrect number of elements", 1, apps.length());
  JSONArray array = apps.getJSONArray("app");
  assertEquals("incorrect number of elements", 2, array.length());
  rm.stop();
}
 
Example 24
Project: hadoop   File: TestRMWebServicesApps.java   Source Code and License 6 votes vote down vote up
public void testAppAttemptsHelper(String path, RMApp app, String media)
    throws JSONException, Exception {
  WebResource r = resource();
  ClientResponse response = r.path("ws").path("v1").path("cluster")
      .path("apps").path(path).path("appattempts").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 jsonAppAttempts = json.getJSONObject("appAttempts");
  assertEquals("incorrect number of elements", 1, jsonAppAttempts.length());
  JSONArray jsonArray = jsonAppAttempts.getJSONArray("appAttempt");

  Collection<RMAppAttempt> attempts = app.getAppAttempts().values();
  assertEquals("incorrect number of elements", attempts.size(),
      jsonArray.length());

  // Verify these parallel arrays are the same
  int i = 0;
  for (RMAppAttempt attempt : attempts) {
    verifyAppAttemptsInfo(jsonArray.getJSONObject(i), attempt, app.getUser());
    ++i;
  }
}
 
Example 25
Project: hadoop   File: TestAHSWebServices.java   Source Code and License 6 votes vote down vote up
@Test
public void testAppsQuery() throws Exception {
  WebResource r = resource();
  ClientResponse response =
      r.path("ws").path("v1").path("applicationhistory").path("apps")
        .queryParam("state", YarnApplicationState.FINISHED.toString())
        .queryParam("user.name", USERS[round])
        .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 apps = json.getJSONObject("apps");
  assertEquals("incorrect number of elements", 1, apps.length());
  JSONArray array = apps.getJSONArray("app");
  assertEquals("incorrect number of elements", 5, array.length());
}
 
Example 26
Project: hadoop   File: TestAHSWebServices.java   Source Code and License 6 votes vote down vote up
@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 27
Project: hadoop   File: TestAHSWebServices.java   Source Code and License 6 votes vote down vote up
@Test
public void testMultipleContainers() throws Exception {
  ApplicationId appId = ApplicationId.newInstance(0, 1);
  ApplicationAttemptId appAttemptId =
      ApplicationAttemptId.newInstance(appId, 1);
  WebResource r = resource();
  ClientResponse response =
      r.path("ws").path("v1").path("applicationhistory").path("apps")
        .path(appId.toString()).path("appattempts")
        .path(appAttemptId.toString()).path("containers")
        .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 containers = json.getJSONObject("containers");
  assertEquals("incorrect number of elements", 1, containers.length());
  JSONArray array = containers.getJSONArray("container");
  assertEquals("incorrect number of elements", 5, array.length());
}
 
Example 28
Project: hadoop   File: TestAMWebServicesTasks.java   Source Code and License 6 votes vote down vote up
@Test
public void testTasks() 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")
        .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", 2, arr.length());

    verifyAMTask(arr, jobsMap.get(id), null);
  }
}
 
Example 29
Project: hadoop   File: TestAMWebServicesTasks.java   Source Code and License 6 votes vote down vote up
@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 30
Project: hadoop   File: TestAMWebServicesTasks.java   Source Code and License 6 votes vote down vote up
@Test
public void testTasksSlash() 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/")
        .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", 2, arr.length());

    verifyAMTask(arr, jobsMap.get(id), null);
  }
}
 
Example 31
Project: hadoop   File: TestAMWebServicesTasks.java   Source Code and License 6 votes vote down vote up
@Test
public void testTasksQueryMap() throws JSONException, Exception {
  WebResource r = resource();
  Map<JobId, Job> jobsMap = appContext.getAllJobs();
  for (JobId id : jobsMap.keySet()) {
    String jobId = MRApps.toString(id);
    String type = "m";
    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 32
Project: hadoop   File: TestAMWebServicesTasks.java   Source Code and License 6 votes vote down vote up
@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 33
Project: hadoop   File: TestAMWebServicesTasks.java   Source Code and License 6 votes vote down vote up
public void verifyAMTask(JSONArray arr, Job job, String type)
    throws JSONException {
  for (Task task : job.getTasks().values()) {
    TaskId id = task.getID();
    String tid = MRApps.toString(id);
    Boolean found = false;
    if (type != null && task.getType() == MRApps.taskType(type)) {

      for (int i = 0; i < arr.length(); i++) {
        JSONObject info = arr.getJSONObject(i);
        if (tid.matches(info.getString("id"))) {
          found = true;
          verifyAMSingleTask(info, task);
        }
      }
      assertTrue("task with id: " + tid + " not in web service output", found);
    }
  }
}
 
Example 34
Project: hadoop   File: TestAMWebServicesTasks.java   Source Code and License 6 votes vote down vote up
public void verifyAMJobTaskCounters(JSONObject info, Task task)
    throws JSONException {

  assertEquals("incorrect number of elements", 2, info.length());

  WebServicesTestUtils.checkStringMatch("id", MRApps.toString(task.getID()),
      info.getString("id"));
  // just do simple verification of fields - not data is correct
  // in the fields
  JSONArray counterGroups = info.getJSONArray("taskCounterGroup");
  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 35
Project: hadoop   File: TestAMWebServicesJobConf.java   Source Code and License 6 votes vote down vote up
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 36
Project: mirrorgate-jira-stories-collector   File: JiraIssueUtils.java   Source Code and License 6 votes vote down vote up
public List<String> objectToStringList(Object o){
    if(o == null) {
        return null;
    }

    List<String> stringList = null;

    if(o instanceof JSONArray) {
        JSONArray array = (JSONArray) o;
        stringList = new ArrayList<>(array.length());

        for (int i = 0; i < array.length(); i++) {
            try {
                stringList.add((String) array.get(i));
            } catch (JSONException e) {
                LOGGER.error("Error parsing sprint field", e);
            }
        }
    } else if(o instanceof List) {
        stringList = (List<String>) o;
    }

    return stringList;
}
 
Example 37
Project: hadoop   File: TestAMWebServicesJobs.java   Source Code and License 6 votes vote down vote up
@Test
public void testJobsSlash() throws JSONException, Exception {
  WebResource r = resource();
  ClientResponse response = r.path("ws").path("v1").path("mapreduce")
      .path("jobs/").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");
  JSONObject info = arr.getJSONObject(0);
  Job job = appContext.getJob(MRApps.toJobID(info.getString("id")));
  verifyAMJob(info, job);

}
 
Example 38
Project: hadoop   File: TestAMWebServicesAttempts.java   Source Code and License 6 votes vote down vote up
public void verifyAMTaskAttempts(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;
        verifyAMTaskAttempt(info, att, task.getType());
      }
    }
    assertTrue("task attempt with id: " + attid
        + " not in web service output", found);
  }
}
 
Example 39
Project: hadoop   File: TestAMWebServicesAttempts.java   Source Code and License 6 votes vote down vote up
public void verifyAMJobTaskAttemptCounters(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 40
Project: hadoop   File: TestHsWebServicesJobConf.java   Source Code and License 6 votes vote down vote up
public void verifyHsJobConf(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()));
    }
  }