StramWebServices { @GET @Produces(MediaType.APPLICATION_JSON) public JSONObject get() throws Exception { return getAppInfo(); } @Inject  StramWebServices(StramAppContext context); @GET @Produces(MediaType.APPLICATION_JSON) JSONObject get(); @GET @Path(PATH_INFO) @Produces(MediaType.APPLICATION_JSON) JSONObject getAppInfo(); @GET @Path(PATH_PHYSICAL_PLAN) @Produces(MediaType.APPLICATION_JSON) JSONObject getPhysicalPlan(); @GET @Path(PATH_PHYSICAL_PLAN_OPERATORS) @Produces(MediaType.APPLICATION_JSON) JSONObject getOperatorsInfo(); @GET @Path(PATH_PHYSICAL_PLAN_STREAMS) @Produces(MediaType.APPLICATION_JSON) JSONObject getStreamsInfo(); @GET @Path(PATH_PHYSICAL_PLAN_OPERATORS + "/{operatorId:\\d+}") @Produces(MediaType.APPLICATION_JSON) JSONObject getOperatorInfo(@PathParam("operatorId") int operatorId); @GET @Path(PATH_PHYSICAL_PLAN_OPERATORS + "/{operatorId:\\d+}/ports") @Produces(MediaType.APPLICATION_JSON) JSONObject getPortsInfo(@PathParam("operatorId") int operatorId); @GET @Path(PATH_PHYSICAL_PLAN_OPERATORS + "/{operatorId:\\d+}/ports/{portName}") @Produces(MediaType.APPLICATION_JSON) JSONObject getPortsInfo(@PathParam("operatorId") int operatorId, @PathParam("portName") String portName); @GET @Path(PATH_OPERATOR_CLASSES) @Produces(MediaType.APPLICATION_JSON) JSONObject getOperatorClasses(@QueryParam("q") String searchTerm, @QueryParam("parent") String parent); @GET @Path(PATH_OPERATOR_CLASSES + "/{className}") @Produces(MediaType.APPLICATION_JSON) @SuppressWarnings("unchecked") JSONObject describeOperator(@PathParam("className") String className); @POST // not supported by WebAppProxyServlet, can only be called directly @Path(PATH_SHUTDOWN) @Produces(MediaType.APPLICATION_JSON) JSONObject shutdown(); @POST @Path(PATH_PHYSICAL_PLAN_OPERATORS + "/{opId:\\d+}/" + PATH_RECORDINGS_START) @Produces(MediaType.APPLICATION_JSON) JSONObject startRecording(@PathParam("opId") int opId, String content); @POST @Path(PATH_PHYSICAL_PLAN_OPERATORS + "/{opId:\\d+}/ports/{portName}/" + PATH_RECORDINGS_START) @Produces(MediaType.APPLICATION_JSON) JSONObject startRecording(@PathParam("opId") int opId, @PathParam("portName") String portName, String content); @POST @Path(PATH_PHYSICAL_PLAN_OPERATORS + "/{opId:\\d+}/" + PATH_RECORDINGS_STOP) @Produces(MediaType.APPLICATION_JSON) JSONObject stopRecording(@PathParam("opId") int opId); @POST @Path(PATH_PHYSICAL_PLAN_OPERATORS + "/{opId:\\d+}/ports/{portName}/" + PATH_RECORDINGS_STOP) @Produces(MediaType.APPLICATION_JSON) JSONObject stopRecording(@PathParam("opId") int opId, @PathParam("portName") String portName); @GET @Path(PATH_PHYSICAL_PLAN_CONTAINERS) @Produces(MediaType.APPLICATION_JSON) JSONObject listContainers(@QueryParam("states") String states); @GET @Path(PATH_PHYSICAL_PLAN_CONTAINERS + "/{containerId}") @Produces(MediaType.APPLICATION_JSON) JSONObject getContainer(@PathParam("containerId") String containerId); @GET @Path(PATH_PHYSICAL_PLAN_CONTAINERS + "/{containerId}/" + PATH_STACKTRACE) @Produces(MediaType.APPLICATION_JSON) JSONObject getContainerStackTrace(@PathParam("containerId") String containerId); @POST // not supported by WebAppProxyServlet, can only be called directly @Path(PATH_PHYSICAL_PLAN_CONTAINERS + "/{containerId}/kill") @Produces(MediaType.APPLICATION_JSON) JSONObject killContainer(@PathParam("containerId") String containerId); @GET @Path(PATH_LOGICAL_PLAN_OPERATORS) @Produces(MediaType.APPLICATION_JSON) JSONObject getLogicalOperators(); @GET @Path(PATH_LOGICAL_PLAN_OPERATORS + "/{operatorName}") @Produces(MediaType.APPLICATION_JSON) JSONObject getLogicalOperator(@PathParam("operatorName") String operatorName); @GET @Path(PATH_LOGICAL_PLAN_OPERATORS + "/{operatorName}/aggregation") @Produces(MediaType.APPLICATION_JSON) JSONObject getOperatorAggregation(@PathParam("operatorName") String operatorName); @POST // not supported by WebAppProxyServlet, can only be called directly @Path(PATH_LOGICAL_PLAN_OPERATORS + "/{operatorName}/properties") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) JSONObject setOperatorProperties(JSONObject request, @PathParam("operatorName") String operatorName); @POST // not supported by WebAppProxyServlet, can only be called directly @Path(PATH_PHYSICAL_PLAN_OPERATORS + "/{operatorId:\\d+}/properties") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) JSONObject setPhysicalOperatorProperties(JSONObject request, @PathParam("operatorId") int operatorId); @GET @Path(PATH_LOGICAL_PLAN_OPERATORS + "/{operatorName}/attributes") @Produces(MediaType.APPLICATION_JSON) JSONObject getOperatorAttributes(@PathParam("operatorName") String operatorName, @QueryParam("attributeName") String attributeName); @GET @Path(PATH_LOGICAL_PLAN + "/attributes") @Produces(MediaType.APPLICATION_JSON) JSONObject getApplicationAttributes(@QueryParam("attributeName") String attributeName); @GET @Path(PATH_LOGICAL_PLAN_OPERATORS + "/{operatorName}/ports") @Produces(MediaType.APPLICATION_JSON) JSONObject getPorts(@PathParam("operatorName") String operatorName); @GET @Path(PATH_LOGICAL_PLAN_OPERATORS + "/{operatorName}/ports/{portName}") @Produces(MediaType.APPLICATION_JSON) JSONObject getPort(@PathParam("operatorName") String operatorName, @PathParam("portName") String portName); @GET @Path(PATH_LOGICAL_PLAN_OPERATORS + "/{operatorName}/ports/{portName}/attributes") @Produces(MediaType.APPLICATION_JSON) JSONObject getPortAttributes(@PathParam("operatorName") String operatorName, @PathParam("portName") String portName, @QueryParam("attributeName") String attributeName); @GET @Path(PATH_LOGICAL_PLAN_OPERATORS + "/{operatorName}/properties") @Produces(MediaType.APPLICATION_JSON) JSONObject getOperatorProperties(@PathParam("operatorName") String operatorName, @QueryParam("propertyName") String propertyName); @GET @Path(PATH_PHYSICAL_PLAN_OPERATORS + "/{operatorId:\\d+}/properties") @Produces(MediaType.APPLICATION_JSON) JSONObject getPhysicalOperatorProperties(@PathParam("operatorId") int operatorId, @QueryParam("propertyName") String propertyName, @QueryParam("waitTime") long waitTime); @GET @Path(PATH_LOGICAL_PLAN) @Produces(MediaType.APPLICATION_JSON) JSONObject getLogicalPlan(@QueryParam("includeModules") String includeModules); @POST // not supported by WebAppProxyServlet, can only be called directly @Path(PATH_LOGICAL_PLAN) @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) JSONObject logicalPlanModification(JSONObject request); @POST @Path(PATH_LOGGERS) @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) JSONObject setLoggersLevel(JSONObject request); @GET @Path(PATH_LOGGERS + "/search") @Produces(MediaType.APPLICATION_JSON) JSONObject searchLoggersLevel(@QueryParam("pattern") String pattern); @GET @Path(PATH_LOGGERS) @Produces(MediaType.APPLICATION_JSON) JSONObject getLoggerLevels(); static final String PATH; static final String PATH_INFO; static final String PATH_PHYSICAL_PLAN; static final String PATH_PHYSICAL_PLAN_OPERATORS; static final String PATH_PHYSICAL_PLAN_STREAMS; static final String PATH_PHYSICAL_PLAN_CONTAINERS; static final String PATH_SHUTDOWN; static final String PATH_RECORDINGS; static final String PATH_RECORDINGS_START; static final String PATH_RECORDINGS_STOP; static final String PATH_LOGICAL_PLAN; static final String PATH_LOGICAL_PLAN_OPERATORS; static final String PATH_OPERATOR_CLASSES; static final String PATH_ALERTS; static final String PATH_LOGGERS; static final String PATH_STACKTRACE; static final long WAIT_TIME; static final long STACK_TRACE_WAIT_TIME; static final long STACK_TRACE_ATTEMPTS; }
@Test public void testAM() throws JSONException, Exception { WebResource r = resource(); ClientResponse response = r.path(StramWebServices.PATH) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertTrue("Too few elements", json.length() > 1); verifyAMInfo(json, appContext); }
@Test public void testAMSlash() throws JSONException, Exception { WebResource r = resource(); ClientResponse response = r.path(StramWebServices.PATH + "/") .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertTrue("Too few elements", json.length() > 1); verifyAMInfo(json, appContext); }
@Test public void testAMDefault() throws JSONException, Exception { WebResource r = resource(); ClientResponse response = r.path(StramWebServices.PATH + "/") .get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertTrue("Too few elements", json.length() > 1); verifyAMInfo(json, appContext); }
@Ignore @Test public void testAMXML() throws JSONException, Exception { WebResource r = resource(); ClientResponse response = r.path(StramWebServices.PATH) .accept(MediaType.APPLICATION_XML).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType()); String xml = response.getEntity(String.class); verifyAMInfoXML(xml, appContext); }
@Test public void testInfo() throws JSONException, Exception { WebResource r = resource(); ClientResponse response = r.path(StramWebServices.PATH) .path(StramWebServices.PATH_INFO).accept(MediaType.APPLICATION_JSON) .get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertTrue("Too few elements", json.length() > 1); verifyAMInfo(json, appContext); }
@Test public void testInfoSlash() throws JSONException, Exception { WebResource r = resource(); ClientResponse response = r.path(StramWebServices.PATH) .path(StramWebServices.PATH_INFO + "/").accept(MediaType.APPLICATION_JSON) .get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertTrue("Too few elements", json.length() > 1); verifyAMInfo(json, appContext); }
@Test public void testInfoDefault() throws JSONException, Exception { WebResource r = resource(); ClientResponse response = r.path(StramWebServices.PATH) .path(StramWebServices.PATH_INFO + "/").get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); assertTrue("Too few elements", json.length() > 1); verifyAMInfo(json, appContext); }
@Ignore @Test public void testInfoXML() throws JSONException, Exception { WebResource r = resource(); ClientResponse response = r.path(StramWebServices.PATH) .path(StramWebServices.PATH_INFO + "/").accept(MediaType.APPLICATION_XML) .get(ClientResponse.class); assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType()); String xml = response.getEntity(String.class); verifyAMInfoXML(xml, appContext); }
@Test @SuppressWarnings("UnusedAssignment") public void testInvalidUri() throws JSONException, Exception { WebResource r = resource(); String responseStr = ""; try { responseStr = r.path(StramWebServices.PATH).path("bogus") .accept(MediaType.APPLICATION_JSON).get(String.class); fail("should have thrown exception on invalid uri"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.NOT_FOUND, response.getClientResponseStatus()); StramTestSupport.checkStringMatch( "error string exists and shouldn't", "", responseStr); } }
@Test @SuppressWarnings("UnusedAssignment") public void testInvalidUri2() throws JSONException, Exception { WebResource r = resource(); String responseStr = ""; try { responseStr = r.path("ws").path("v2").path("invalid") .accept(MediaType.APPLICATION_JSON).get(String.class); fail("should have thrown exception on invalid uri"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.NOT_FOUND, response.getClientResponseStatus()); StramTestSupport.checkStringMatch( "error string exists and shouldn't", "", responseStr); } }
@Test @SuppressWarnings("UnusedAssignment") public void testInvalidAccept() throws JSONException, Exception { java.util.logging.Logger.getLogger("org.glassfish.grizzly.servlet.ServletHandler").setLevel(Level.OFF); java.util.logging.Logger.getLogger("com.sun.jersey.spi.container.ContainerResponse").setLevel(Level.OFF); WebResource r = resource(); String responseStr = ""; try { responseStr = r.path(StramWebServices.PATH) .accept(MediaType.TEXT_PLAIN).get(String.class); fail("should have thrown exception on invalid accept"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.INTERNAL_SERVER_ERROR, response.getClientResponseStatus()); StramTestSupport.checkStringMatch( "error string exists and shouldn't", "", responseStr); } }
@Test public void testAttributes() throws Exception { WebResource r = resource(); ClientResponse response = r.path(StramWebServices.PATH + "/") .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject json = response.getEntity(JSONObject.class); JSONObject attrs = json.getJSONObject("attributes"); Assert.assertEquals(AutoMetricBuiltInTransport.class.getName() + ":xyz", attrs.getString(Context.DAGContext.METRICS_TRANSPORT.getSimpleName())); }
@Test public void testSubmitLogicalPlanChange() throws JSONException, Exception { List<LogicalPlanRequest> requests = new ArrayList<>(); WebResource r = resource(); CreateOperatorRequest request1 = new CreateOperatorRequest(); request1.setOperatorName("operatorName"); request1.setOperatorFQCN("className"); requests.add(request1); SetOperatorPropertyRequest request2 = new SetOperatorPropertyRequest(); request2.setOperatorName("operatorName"); request2.setPropertyName("propertyName"); request2.setPropertyValue("propertyValue"); requests.add(request2); ObjectMapper mapper = new ObjectMapper(); final Map<String, Object> m = new HashMap<>(); m.put("requests", requests); final JSONObject jsonRequest = new JSONObject(mapper.writeValueAsString(m)); ClientResponse response = r.path(StramWebServices.PATH) .path(StramWebServices.PATH_LOGICAL_PLAN).accept(MediaType.APPLICATION_JSON) .post(ClientResponse.class, jsonRequest); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); assertEquals(DummyStreamingContainerManager.lastRequests.size(), 2); LogicalPlanRequest request = DummyStreamingContainerManager.lastRequests.get(0); assertTrue(request instanceof CreateOperatorRequest); request1 = (CreateOperatorRequest)request; assertEquals(request1.getOperatorName(), "operatorName"); assertEquals(request1.getOperatorFQCN(), "className"); request = DummyStreamingContainerManager.lastRequests.get(1); assertTrue(request instanceof SetOperatorPropertyRequest); request2 = (SetOperatorPropertyRequest)request; assertEquals(request2.getOperatorName(), "operatorName"); assertEquals(request2.getPropertyName(), "propertyName"); assertEquals(request2.getPropertyValue(), "propertyValue"); }