@Test public void shouldRetrieveRootNodeForValidRepository() throws Exception { URL postUrl = new URL(SERVER_URL + "/mode%3arepository/default/items/"); HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); JSONObject body = new JSONObject(getResponseFor(connection)); assertThat(body.length(), is(2)); JSONObject properties = body.getJSONObject("properties"); assertThat(properties, is(notNullValue())); assertThat(properties.length(), is(2)); assertThat(properties.getString("jcr:primaryType"), is("mode:root")); assertThat(properties.get("jcr:uuid"), is(notNullValue())); JSONArray children = body.getJSONArray("children"); assertThat(children.length(), is(1)); assertThat(children.getString(0), is("jcr:system")); assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_OK)); connection.disconnect(); }
private void addTagsToProperties(MethodInfo mi, JSONObject propJ) throws JSONException { // create description object. description tag enables the visual tools to display description of // keys/values // of a map property, items of a list, properties within a complex type. JSONObject descriptionObj = new JSONObject(); if (mi.comment != null) { descriptionObj.put("$", mi.comment); } for (Map.Entry<String, String> descEntry : mi.descriptions.entrySet()) { descriptionObj.put(descEntry.getKey(), descEntry.getValue()); } if (descriptionObj.length() > 0) { propJ.put("descriptions", descriptionObj); } // create useSchema object. useSchema tag is added to enable visual tools to be able to render a // text field // as a dropdown with choices populated from the schema attached to the port. JSONObject useSchemaObj = new JSONObject(); for (Map.Entry<String, String> useSchemaEntry : mi.useSchemas.entrySet()) { useSchemaObj.put(useSchemaEntry.getKey(), useSchemaEntry.getValue()); } if (useSchemaObj.length() > 0) { propJ.put("useSchema", useSchemaObj); } }
@Test public void shouldPostNodeToValidPathWithPrimaryType() throws Exception { URL postUrl = new URL(SERVER_URL + "/mode%3arepository/default/items/nodeA"); HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); String payload = "{ \"properties\": {\"jcr:primaryType\": \"nt:unstructured\", \"testProperty\": \"testValue\", \"multiValuedProperty\": [\"value1\", \"value2\"]}}"; connection.getOutputStream().write(payload.getBytes()); JSONObject body = new JSONObject(getResponseFor(connection)); assertThat(body.length(), is(1)); JSONObject properties = body.getJSONObject("properties"); assertThat(properties, is(notNullValue())); assertThat(properties.length(), is(3)); assertThat(properties.getString("jcr:primaryType"), is("nt:unstructured")); assertThat(properties.getString("testProperty"), is("testValue")); assertThat(properties.get("multiValuedProperty"), instanceOf(JSONArray.class)); JSONArray values = properties.getJSONArray("multiValuedProperty"); assertThat(values, is(notNullValue())); assertThat(values.length(), is(2)); assertThat(values.getString(0), is("value1")); assertThat(values.getString(1), is("value2")); assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_CREATED)); connection.disconnect(); }
@Test public void shouldPostNodeToValidPathWithMixinTypes() throws Exception { URL postUrl = new URL(SERVER_URL + "/mode%3arepository/default/items/withMixinType"); HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); String payload = "{ \"properties\": {\"jcr:mixinTypes\": \"mix:referenceable\"}}"; connection.getOutputStream().write(payload.getBytes()); JSONObject body = new JSONObject(getResponseFor(connection)); assertThat(body.length(), is(1)); JSONObject properties = body.getJSONObject("properties"); assertThat(properties, is(notNullValue())); assertThat(properties.length(), is(3)); assertThat(properties.getString("jcr:primaryType"), is("nt:unstructured")); assertThat(properties.getString("jcr:uuid"), is(notNullValue())); JSONArray values = properties.getJSONArray("jcr:mixinTypes"); assertThat(values, is(notNullValue())); assertThat(values.length(), is(1)); assertThat(values.getString(0), is("mix:referenceable")); assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_CREATED)); connection.disconnect(); postUrl = new URL(SERVER_URL + "/mode%3arepository/default/items/withMixinType"); connection = (HttpURLConnection) postUrl.openConnection(); // Make sure that we can retrieve the node with a GET connection.setDoOutput(true); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); body = new JSONObject(getResponseFor(connection)); assertThat(body.length(), is(1)); properties = body.getJSONObject("properties"); assertThat(properties, is(notNullValue())); assertThat(properties.length(), is(3)); assertThat(properties.getString("jcr:primaryType"), is("nt:unstructured")); assertThat(properties.getString("jcr:uuid"), is(notNullValue())); values = properties.getJSONArray("jcr:mixinTypes"); assertThat(values, is(notNullValue())); assertThat(values.length(), is(1)); assertThat(values.getString(0), is("mix:referenceable")); assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_OK)); connection.disconnect(); }
@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("history") .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()); verifyHsTask(arr, jobsMap.get(id), type); } }
public void verifyClusterInfo(JSONObject json) throws JSONException, Exception { assertEquals("incorrect number of elements", 1, json.length()); JSONObject info = json.getJSONObject("clusterInfo"); assertEquals("incorrect number of elements", 10, info.length()); verifyClusterGeneric( info.getLong("id"), info.getLong("startedOn"), info.getString("state"), info.getString("haState"), info.getString("hadoopVersionBuiltOn"), info.getString("hadoopBuildVersion"), info.getString("hadoopVersion"), info.getString("resourceManagerVersionBuiltOn"), info.getString("resourceManagerBuildVersion"), info.getString("resourceManagerVersion")); }
public void testNodeHelper(String path, String media) 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(2); nmContext.getApplications().put(app2.getAppId(), app2); HashMap<String, String> hash2 = addAppContainers(app2); ClientResponse response = r.path("ws").path("v1").path("node").path(path).accept(media).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", 2, appInfo.length()); String id = appInfo.getJSONObject(0).getString("id"); if (id.matches(app.getAppId().toString())) { verifyNodeAppInfo(appInfo.getJSONObject(0), app, hash); verifyNodeAppInfo(appInfo.getJSONObject(1), app2, hash2); } else { verifyNodeAppInfo(appInfo.getJSONObject(0), app2, hash2); verifyNodeAppInfo(appInfo.getJSONObject(1), app, hash); } }
@Test public void testTaskIdCountersDefault() throws JSONException, Exception { WebResource r = resource(); Map<JobId, Job> jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); for (Task task : jobsMap.get(id).getTasks().values()) { String tid = MRApps.toString(task.getID()); ClientResponse response = r.path("ws") .path("v1") .path("history") .path("mapreduce") .path("jobs") .path(jobId) .path("tasks") .path(tid) .path("counters") .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 info = json.getJSONObject("jobTaskCounters"); verifyHsJobTaskCounters(info, task); } } }
@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); }
// verify the exception object default format is JSON @Test public void testNodeAppsStateInvalidDefault() throws JSONException, Exception { WebResource r = resource(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); Application app2 = new MockApp("foo", 1234, 2); nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); try { r.path("ws") .path("v1") .path("node") .path("apps") .queryParam("state", "FOO_STATE") .get(JSONObject.class); fail("should have thrown exception on invalid user query"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus()); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject msg = response.getEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); String type = exception.getString("exception"); String classname = exception.getString("javaClassName"); verifyStateInvalidException(message, type, classname); } }
/** * This is a helper method to initialize the schema. * * @throws JSONException This exception is thrown if there is an error parsing the JSON which * specified this schema. */ private void initialize() throws JSONException { schema = new JSONObject(schemaJSON); Preconditions.checkState( schema.length() == NUM_KEYS_FIRST_LEVEL, "Expected " + NUM_KEYS_FIRST_LEVEL + " keys in the first level but found " + schema.length()); if (schemaKeys != null) { schema.put(Schema.FIELD_SCHEMA_KEYS, SchemaUtils.createJSONObject(schemaKeys)); } valueToType = Maps.newHashMap(); JSONArray values = schema.getJSONArray(FIELD_VALUES); Preconditions.checkState(values.length() > 0, "The schema does not specify any values."); for (int index = 0; index < values.length(); index++) { JSONObject value = values.getJSONObject(index); String name = value.getString(FIELD_VALUES_NAME); String typeName = value.getString(FIELD_VALUES_TYPE); Type type = Type.NAME_TO_TYPE.get(typeName); valueToType.put(name, type); Preconditions.checkArgument(type != null, typeName + " is not a valid type."); } valueToType = Collections.unmodifiableMap(valueToType); valuesDescriptor = new FieldsDescriptor(valueToType); try { schema.put(FIELD_SCHEMA_TYPE, SCHEMA_TYPE); schema.put(FIELD_SCHEMA_VERSION, SCHEMA_VERSION); } catch (JSONException e) { throw new RuntimeException(e); } schemaJSON = this.schema.toString(); }
public void verifyClusterMetricsJSON(JSONObject json) throws JSONException, Exception { assertEquals("incorrect number of elements", 1, json.length()); JSONObject clusterinfo = json.getJSONObject("clusterMetrics"); assertEquals("incorrect number of elements", 19, clusterinfo.length()); verifyClusterMetrics( clusterinfo.getInt("appsSubmitted"), clusterinfo.getInt("appsCompleted"), clusterinfo.getInt("reservedMB"), clusterinfo.getInt("availableMB"), clusterinfo.getInt("allocatedMB"), clusterinfo.getInt("containersAllocated"), clusterinfo.getInt("totalMB"), clusterinfo.getInt("totalNodes"), clusterinfo.getInt("lostNodes"), clusterinfo.getInt("unhealthyNodes"), clusterinfo.getInt("decommissionedNodes"), clusterinfo.getInt("rebootedNodes"), clusterinfo.getInt("activeNodes")); }
public void verifyClusterSchedulerFifo(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", 11, info.length()); verifyClusterSchedulerFifoGeneric( info.getString("type"), info.getString("qstate"), (float) info.getDouble("capacity"), (float) info.getDouble("usedCapacity"), info.getInt("minQueueMemoryCapacity"), info.getInt("maxQueueMemoryCapacity"), info.getInt("numNodes"), info.getInt("usedNodeCapacity"), info.getInt("availNodeCapacity"), info.getInt("totalNodeCapacity"), info.getInt("numContainers")); }
private Collection<Field> parseFields(JSONObject json) throws JSONException { ArrayList<Field> res = new ArrayList<Field>(json.length()); final Iterator<String> iterator = getStringKeys(json); while (iterator.hasNext()) { final String key = iterator.next(); if (SPECIAL_FIELDS.contains(key)) { continue; } res.add(fieldParser.parse(json.getJSONObject(key), key)); } return res; }
@Test public void shouldBeAbleToPutValueToProperty() throws Exception { URL postUrl = new URL(SERVER_URL + "/mode%3arepository/default/items/nodeForPutProperty"); HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); String payload = "{ \"properties\": {\"jcr:primaryType\": \"nt:unstructured\", \"testProperty\": \"testValue\" }}"; connection.getOutputStream().write(payload.getBytes()); assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_CREATED)); connection.disconnect(); postUrl = new URL(SERVER_URL + "/mode%3arepository/default/items/nodeForPutProperty/testProperty"); connection = (HttpURLConnection) postUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); payload = "{\"testProperty\":\"someOtherValue\"}"; connection.getOutputStream().write(payload.getBytes()); JSONObject body = new JSONObject(getResponseFor(connection)); assertThat(body.length(), is(1)); JSONObject properties = body.getJSONObject("properties"); assertThat(properties, is(notNullValue())); assertThat(properties.length(), is(2)); assertThat(properties.getString("jcr:primaryType"), is("nt:unstructured")); assertThat(properties.getString("testProperty"), is("someOtherValue")); assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_OK)); connection.disconnect(); }
public void verifyHsSingleTask(JSONObject info, Task task) throws JSONException { assertEquals("incorrect number of elements", 9, info.length()); verifyTaskGeneric( task, info.getString("id"), info.getString("state"), info.getString("type"), info.getString("successfulAttempt"), info.getLong("startTime"), info.getLong("finishTime"), info.getLong("elapsedTime"), (float) info.getDouble("progress")); }
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()); }
@Test public void shouldRetrieveNodeAndChildrenWhenDepthSet() throws Exception { URL postUrl = new URL(SERVER_URL + "/mode%3arepository/default/items/jcr:system?mode:depth=1"); HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); JSONObject body = new JSONObject(getResponseFor(connection)); assertThat(body.length(), is(2)); JSONObject properties = body.getJSONObject("properties"); assertThat(properties, is(notNullValue())); assertThat(properties.length(), is(1)); assertThat(properties.getString("jcr:primaryType"), is("mode:system")); JSONObject children = body.getJSONObject("children"); assertThat(children.length(), is(4)); JSONObject namespaces = children.getJSONObject("mode:namespaces"); assertThat(namespaces.length(), is(2)); JSONObject locks = children.getJSONObject("mode:locks"); assertThat(locks.length(), is(1)); JSONObject versionStorage = children.getJSONObject("jcr:versionStorage"); assertThat(versionStorage.length(), is(1)); properties = namespaces.getJSONObject("properties"); assertThat(properties.length(), is(1)); assertThat(properties.getString("jcr:primaryType"), is("mode:namespaces")); JSONArray namespace = namespaces.getJSONArray("children"); assertThat(namespace.length(), is(10)); Set<String> prefixes = new HashSet<String>(namespace.length()); for (int i = 0; i < namespace.length(); i++) { prefixes.add(namespace.getString(i)); } String[] expectedNamespaces = new String[] {"mode", "jcr", "nt", "mix", "sv", "xml", "modeint", "xmlns", "xsi", "xs"}; for (int i = 0; i < expectedNamespaces.length; i++) { assertTrue(prefixes.contains(expectedNamespaces[i])); } assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_OK)); connection.disconnect(); }
@Test public void testTaskIdInvalid() throws JSONException, Exception { WebResource r = resource(); Map<JobId, Job> jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); String tid = "task_0_0000_d_000000"; try { r.path("ws") .path("v1") .path("history") .path("mapreduce") .path("jobs") .path(jobId) .path("tasks") .path(tid) .get(JSONObject.class); fail("should have thrown exception on invalid uri"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.NOT_FOUND, response.getClientResponseStatus()); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject msg = response.getEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); String type = exception.getString("exception"); String classname = exception.getString("javaClassName"); WebServicesTestUtils.checkStringMatch( "exception message", "java.lang.Exception: Bad TaskType identifier. TaskId string : " + "task_0_0000_d_000000 is not properly formed.", message); WebServicesTestUtils.checkStringMatch("exception type", "NotFoundException", type); WebServicesTestUtils.checkStringMatch( "exception classname", "org.apache.hadoop.yarn.webapp.NotFoundException", classname); } } }
@Test public void testNodeAppsUserEmpty() throws JSONException, Exception { WebResource r = resource(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); Application app2 = new MockApp("foo", 1234, 2); nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); try { r.path("ws") .path("v1") .path("node") .path("apps") .queryParam("user", "") .accept(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid user query"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus()); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject msg = response.getEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); String type = exception.getString("exception"); String classname = exception.getString("javaClassName"); WebServicesTestUtils.checkStringMatch( "exception message", "java.lang.Exception: Error: You must specify a non-empty string for the user", message); WebServicesTestUtils.checkStringMatch("exception type", "BadRequestException", type); WebServicesTestUtils.checkStringMatch( "exception classname", "org.apache.hadoop.yarn.webapp.BadRequestException", classname); } }
@Test public void testTasksQueryInvalid() throws JSONException, Exception { WebResource r = resource(); Map<JobId, Job> jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); // tasktype must be exactly either "m" or "r" String tasktype = "reduce"; try { r.path("ws") .path("v1") .path("history") .path("mapreduce") .path("jobs") .path(jobId) .path("tasks") .queryParam("type", tasktype) .accept(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid uri"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus()); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject msg = response.getEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); String type = exception.getString("exception"); String classname = exception.getString("javaClassName"); WebServicesTestUtils.checkStringMatch( "exception message", "java.lang.Exception: tasktype must be either m or r", message); WebServicesTestUtils.checkStringMatch("exception type", "BadRequestException", type); WebServicesTestUtils.checkStringMatch( "exception classname", "org.apache.hadoop.yarn.webapp.BadRequestException", classname); } } }
@Test public void testNodeSingleAppsMissing() throws JSONException, Exception { WebResource r = resource(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); Application app2 = new MockApp(2); nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); try { r.path("ws") .path("v1") .path("node") .path("apps") .path("application_1234_0009") .accept(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid user query"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.NOT_FOUND, response.getClientResponseStatus()); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject msg = response.getEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); String type = exception.getString("exception"); String classname = exception.getString("javaClassName"); WebServicesTestUtils.checkStringMatch( "exception message", "java.lang.Exception: app with id application_1234_0009 not found", message); WebServicesTestUtils.checkStringMatch("exception type", "NotFoundException", type); WebServicesTestUtils.checkStringMatch( "exception classname", "org.apache.hadoop.yarn.webapp.NotFoundException", classname); } }
public void verifyHsJobTaskCounters(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); } } }
private Collection<Field> parseFieldsJira5x0(JSONObject issueJson) throws JSONException { final JSONObject names = (this.names == null ? issueJson.optJSONObject(NAMES_SECTION) : this.names); final Map<String, String> namesMap = parseNames(names); final JSONObject types = (this.types == null ? issueJson.optJSONObject(SCHEMA_SECTION) : this.types); final Map<String, String> typesMap = parseSchema(types); final JSONObject json = issueJson.getJSONObject(FIELDS); final ArrayList<Field> res = new ArrayList<Field>(json.length()); @SuppressWarnings("unchecked") final Iterator<String> iterator = json.keys(); while (iterator.hasNext()) { final String key = iterator.next(); try { if (SPECIAL_FIELDS.contains(key)) { continue; } final Object value = json.opt(key); res.add( new Field( key, namesMap.get(key), typesMap.get("key"), value != JSONObject.NULL ? value : null)); } catch (final Exception e) { throw new JSONException("Error while parsing [" + key + "] field: " + e.getMessage()) { @Override public Throwable getCause() { return e; } }; } } return res; }
@Test public void testNodeSingleAppsInvalid() throws JSONException, Exception { WebResource r = resource(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); Application app2 = new MockApp(2); nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); try { r.path("ws") .path("v1") .path("node") .path("apps") .path("app_foo_0000") .accept(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid user query"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus()); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject msg = response.getEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); String type = exception.getString("exception"); String classname = exception.getString("javaClassName"); WebServicesTestUtils.checkStringMatch( "exception message", "For input string: \"foo\"", message); WebServicesTestUtils.checkStringMatch("exception type", "NumberFormatException", type); WebServicesTestUtils.checkStringMatch( "exception classname", "java.lang.NumberFormatException", classname); } }
/** * Verify the web service deployment and lifecycle functionality * * @throws Exception */ @Ignore // disabled due to web service init delay issue @Test public void testWebService() throws Exception { // single container topology of inline input and module Properties props = new Properties(); props.put( StreamingApplication.DT_PREFIX + "stream.input.classname", TestGeneratorInputOperator.class.getName()); props.put(StreamingApplication.DT_PREFIX + "stream.input.outputNode", "module1"); props.put( StreamingApplication.DT_PREFIX + "module.module1.classname", GenericTestOperator.class.getName()); LOG.info("Initializing Client"); LogicalPlanConfiguration tb = new LogicalPlanConfiguration(new Configuration(false)); tb.addFromProperties(props, null); StramClient client = new StramClient(new Configuration(yarnCluster.getConfig()), createDAG(tb)); if (StringUtils.isBlank(System.getenv("JAVA_HOME"))) { client.javaCmd = "java"; // JAVA_HOME not set in the yarn mini cluster } try { client.start(); client.startApplication(); // attempt web service connection ApplicationReport appReport = client.getApplicationReport(); Thread.sleep(5000); // delay to give web service time to fully initialize Client wsClient = Client.create(); wsClient.setFollowRedirects(true); WebResource r = wsClient .resource("http://" + appReport.getTrackingUrl()) .path(StramWebServices.PATH) .path(StramWebServices.PATH_INFO); LOG.info("Requesting: " + r.getURI()); ClientResponse response = r.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); LOG.info("Got response: " + json.toString()); assertEquals("incorrect number of elements", 1, json.length()); assertEquals("appId", appReport.getApplicationId().toString(), json.get("id")); r = wsClient .resource("http://" + appReport.getTrackingUrl()) .path(StramWebServices.PATH) .path(StramWebServices.PATH_PHYSICAL_PLAN_OPERATORS); LOG.info("Requesting: " + r.getURI()); response = r.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); json = response.getEntity(JSONObject.class); LOG.info("Got response: " + json.toString()); } finally { // LOG.info("waiting..."); // synchronized (this) { // this.wait(); // } // boolean result = client.monitorApplication(); client.killApplication(); client.stop(); } }
@Test public void shouldBeAbleToAddAndRemoveMixinTypes() throws Exception { URL postUrl = new URL(SERVER_URL + "/mode%3arepository/default/items/nodeWithNoMixins"); HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); String payload = "{ \"properties\": {\"jcr:primaryType\": \"nt:unstructured\" }}"; connection.getOutputStream().write(payload.getBytes()); assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_CREATED)); connection.disconnect(); connection = (HttpURLConnection) postUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); payload = "{\"jcr:mixinTypes\": \"mix:referenceable\"}"; connection.getOutputStream().write(payload.getBytes()); JSONObject body = new JSONObject(getResponseFor(connection)); assertThat(body.length(), is(1)); JSONObject properties = body.getJSONObject("properties"); assertThat(properties, is(notNullValue())); assertThat(properties.length(), is(3)); assertThat(properties.getString("jcr:primaryType"), is("nt:unstructured")); JSONArray mixinTypes = properties.getJSONArray("jcr:mixinTypes"); assertThat(mixinTypes, is(notNullValue())); assertThat(mixinTypes.length(), is(1)); assertThat(mixinTypes.getString(0), is("mix:referenceable")); assertThat(properties.getString("jcr:uuid"), is(notNullValue())); assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_OK)); connection.disconnect(); connection = (HttpURLConnection) postUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); payload = "{\"jcr:mixinTypes\": []}"; connection.getOutputStream().write(payload.getBytes()); body = new JSONObject(getResponseFor(connection)); assertThat(body.length(), is(1)); properties = body.getJSONObject("properties"); assertThat(properties, is(notNullValue())); assertThat(properties.length(), is(2)); assertThat(properties.getString("jcr:primaryType"), is("nt:unstructured")); // removeMixin doesn't currently null out this value mixinTypes = properties.getJSONArray("jcr:mixinTypes"); assertThat(mixinTypes, is(notNullValue())); assertThat(mixinTypes.length(), is(0)); assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_OK)); connection.disconnect(); }
@Test public void shouldBeAbleToPutBinaryValueToProperty() throws Exception { URL postUrl = new URL(SERVER_URL + "/mode%3arepository/default/items/nodeForPutBinaryProperty"); HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); // Base64-encode a value ... String encodedValue = Base64.encodeBytes("propertyValue".getBytes("UTF-8")); String payload = "{ \"properties\": {\"jcr:primaryType\": \"nt:unstructured\", \"testProperty/base64/\": \"" + encodedValue + "\" }}"; connection.getOutputStream().write(payload.getBytes()); assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_CREATED)); connection.disconnect(); URL putUrl = new URL( SERVER_URL + "/mode%3arepository/default/items/nodeForPutBinaryProperty/testProperty"); connection = (HttpURLConnection) putUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); String otherValue = "someOtherValue"; payload = "{\"testProperty/base64/\":\"" + Base64.encodeBytes(otherValue.getBytes("UTF-8")) + "\"}"; connection.getOutputStream().write(payload.getBytes()); JSONObject body = new JSONObject(getResponseFor(connection)); assertThat(body.length(), is(1)); JSONObject properties = body.getJSONObject("properties"); assertThat(properties, is(notNullValue())); assertThat(properties.length(), is(2)); assertThat(properties.getString("jcr:primaryType"), is("nt:unstructured")); String responseEncodedValue = properties.getString("testProperty/base64/"); String decodedValue = new String(Base64.decode(responseEncodedValue), "UTF-8"); assertThat(decodedValue, is(otherValue)); assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_OK)); connection.disconnect(); // Try putting a non-binary value ... connection = (HttpURLConnection) putUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); String anotherValue = "yetAnotherValue"; payload = "{\"testProperty\":\"" + anotherValue + "\"}"; connection.getOutputStream().write(payload.getBytes()); body = new JSONObject(getResponseFor(connection)); assertThat(body.length(), is(1)); properties = body.getJSONObject("properties"); assertThat(properties, is(notNullValue())); assertThat(properties.length(), is(2)); assertThat(properties.getString("jcr:primaryType"), is("nt:unstructured")); assertThat(properties.getString("testProperty"), is(anotherValue)); assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_OK)); connection.disconnect(); }
@Test public void shouldPostNodeHierarchy() throws Exception { URL postUrl = new URL(SERVER_URL + "/mode%3arepository/default/items/nestedPost"); HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); String payload = "{ \"properties\": {\"jcr:primaryType\": \"nt:unstructured\", \"testProperty\": \"testValue\", \"multiValuedProperty\": [\"value1\", \"value2\"]}," + " \"children\": { \"childNode\" : { \"properties\": {\"nestedProperty\": \"nestedValue\"}}}}"; connection.getOutputStream().write(payload.getBytes()); assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_CREATED)); connection.disconnect(); postUrl = new URL(SERVER_URL + "/mode%3arepository/default/items/nestedPost?mode:depth=1"); connection = (HttpURLConnection) postUrl.openConnection(); // Make sure that we can retrieve the node with a GET connection.setDoOutput(true); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON); JSONObject body = new JSONObject(getResponseFor(connection)); assertThat(body.length(), is(2)); JSONObject properties = body.getJSONObject("properties"); assertThat(properties, is(notNullValue())); assertThat(properties.length(), is(3)); assertThat(properties.getString("jcr:primaryType"), is("nt:unstructured")); assertThat(properties.getString("testProperty"), is("testValue")); assertThat(properties.get("multiValuedProperty"), instanceOf(JSONArray.class)); JSONArray values = properties.getJSONArray("multiValuedProperty"); assertThat(values, is(notNullValue())); assertThat(values.length(), is(2)); assertThat(values.getString(0), is("value1")); assertThat(values.getString(1), is("value2")); JSONObject children = body.getJSONObject("children"); assertThat(children, is(notNullValue())); assertThat(children.length(), is(1)); JSONObject child = children.getJSONObject("childNode"); assertThat(child, is(notNullValue())); assertThat(child.length(), is(1)); properties = child.getJSONObject("properties"); assertThat(properties, is(notNullValue())); assertThat(properties.length(), is(2)); // Parent primary type is nt:unstructured, so this should default to nt:unstructured primary // type assertThat(properties.getString("jcr:primaryType"), is("nt:unstructured")); assertThat(properties.getString("nestedProperty"), is("nestedValue")); assertThat(connection.getResponseCode(), is(HttpURLConnection.HTTP_OK)); connection.disconnect(); }