RestRules { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response add(String json) { RestListener listener = RestManager.getListener(); Response response; try { log.info("Add request with json: {}", json); listener.add(parseMap(json)); response = Response.ok().build(); } catch (RestInvalidException e) { log.info("Add request was invalid: {}", e.getMessage()); response = Response.status(Response.Status.BAD_REQUEST) .entity(toMap(e, Response.Status.BAD_REQUEST)) .build(); } catch (Exception e) { e.printStackTrace(); response = Response.serverError() .entity(toMap(e, Response.Status.INTERNAL_SERVER_ERROR)) .build(); } return response; }  @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response add(String json); @DELETE @Path("/{id}") @Produces(MediaType.APPLICATION_JSON) Response remove(@PathParam("id") String id); @POST @Path("/synchronize") @Consumes (MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response synchronize(String json); @GET @Produces(MediaType.APPLICATION_JSON) Response list();  }
@Test public void add() throws Exception { Map<String, Object> elementMap = new HashMap<>(); Map<String, String> outputTopics = new HashMap<>(); outputTopics.put("outputStream", "outputTopic"); elementMap.put("id", "testID"); elementMap.put("input", Arrays.asList("test")); elementMap.put("output", outputTopics); elementMap.put("executionPlan", "from test select a, b insert into outputStream;"); String json = objectMapper.writeValueAsString(elementMap); Entity<String> entity = Entity.json(json); Response response = target("/v1/").request(MediaType.APPLICATION_JSON_TYPE).post(entity); int statusCode = response.getStatus(); assertEquals(Response.Status.OK.getStatusCode(), statusCode); }
@Test public void synchronizeInvalid() throws Exception { List<Map<String, Object>> listOfRules = new ArrayList<>(); Map<String, Object> elementMap = new HashMap<>(); Map<String, String> outputTopics = new HashMap<>(); outputTopics.put("outputStream", "outputTopic"); elementMap.put("id", "rule_list_one"); elementMap.put("input", Arrays.asList("test")); elementMap.put("output", outputTopics); elementMap.put("executionPlan", "from test select a, b insert into outputStream;"); listOfRules.add(elementMap); Map<String, Object> elementMap2 = new HashMap<>(); Map<String, String> outputTopics2 = new HashMap<>(); outputTopics2.put("outputStream", "outputTopic"); elementMap2.put("input", Arrays.asList("test")); elementMap2.put("output", outputTopics2); elementMap2.put("executionPlan", "from test select a, b insert into outputStream;"); listOfRules.add(elementMap2); String json = objectMapper.writeValueAsString(listOfRules); Entity<String> entity = Entity.json(json); Response response = target("/v1/synchronize").request(MediaType.APPLICATION_JSON_TYPE).post(entity); int statusCode = response.getStatus(); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), statusCode); }
@Test public void synchronizeInvalidType() throws Exception { List<Map<String, Object>> listOfRules = new ArrayList<>(); Map<String, Object> elementMap = new HashMap<>(); Map<String, String> outputTopics = new HashMap<>(); outputTopics.put("outputStream", "outputTopic"); elementMap.put("id", "rule_list_one"); elementMap.put("input", 2); elementMap.put("output", outputTopics); elementMap.put("executionPlan", "from test select a, b insert into outputStream;"); listOfRules.add(elementMap); Map<String, Object> elementMap2 = new HashMap<>(); Map<String, String> outputTopics2 = new HashMap<>(); outputTopics2.put("outputStream", "outputTopic"); elementMap2.put("input", Arrays.asList("test")); elementMap2.put("output", outputTopics2); elementMap2.put("executionPlan", "from test select a, b insert into outputStream;"); listOfRules.add(elementMap2); String json = objectMapper.writeValueAsString(listOfRules); Entity<String> entity = Entity.json(json); Response response = target("/v1/synchronize").request(MediaType.APPLICATION_JSON_TYPE).post(entity); int statusCode = response.getStatus(); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), statusCode); }
@Test public void synchronizeWithSameID() throws Exception { List<Map<String, Object>> listOfRules = new ArrayList<>(); Map<String, Object> elementMap = new HashMap<>(); Map<String, String> outputTopics = new HashMap<>(); outputTopics.put("outputStream", "outputTopic"); elementMap.put("id", "rule_list_one"); elementMap.put("input", Arrays.asList("test")); elementMap.put("output", outputTopics); elementMap.put("executionPlan", "from test select a, b insert into outputStream;"); listOfRules.add(elementMap); Map<String, Object> elementMap2 = new HashMap<>(); Map<String, String> outputTopics2 = new HashMap<>(); outputTopics2.put("outputStream", "outputTopic"); elementMap2.put("id", "rule_list_one"); elementMap2.put("input", Arrays.asList("test")); elementMap2.put("output", outputTopics2); elementMap2.put("executionPlan", "from test select a, b insert into outputStream;"); listOfRules.add(elementMap2); String json = objectMapper.writeValueAsString(listOfRules); Entity<String> entity = Entity.json(json); Response response = target("/v1/synchronize").request(MediaType.APPLICATION_JSON_TYPE).post(entity); int statusCode = response.getStatus(); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), statusCode); }