public WebServiceFilter(WebServiceEngine webServiceEngine, SonarRuntime runtime) { this.webServiceEngine = webServiceEngine; this.includeUrls = concat( Stream.of("/api/*"), webServiceEngine.controllers().stream() .flatMap(controller -> controller.actions().stream()) .map(toPath())) .collect(MoreCollectors.toSet()); this.excludeUrls = concat(concat( Stream.of("/" + CONTROLLER_PROPERTIES + "*"), MOVED_WEB_SERVICES.stream()), webServiceEngine.controllers().stream() .flatMap(controller -> controller.actions().stream()) .filter(action -> action.handler() instanceof ServletFilterHandler) .map(toPath())) .collect(MoreCollectors.toSet()); this.runtime = runtime; }
@Test public void define_app_action() { WebService.Action action = ws.getDef(); assertThat(action).isNotNull(); assertThat(action.isInternal()).isTrue(); assertThat(action.isPost()).isFalse(); assertThat(action.handler()).isNotNull(); assertThat(action.params()).hasSize(4); }
@Test public void handle_request() throws Exception { MetricWs metricWs = new MetricWs(); metricWs.define(context); assertThat(metricWs.showCalled).isFalse(); assertThat(metricWs.createCalled).isFalse(); context.controller("api/metric").action("show").handler().handle(mock(Request.class), mock(Response.class)); assertThat(metricWs.showCalled).isTrue(); assertThat(metricWs.createCalled).isFalse(); context.controller("api/metric").action("create").handler().handle(mock(Request.class), mock(Response.class)); assertThat(metricWs.createCalled).isTrue(); }
@Test public void verify_definition() { WebService.Action action = wsTester.getDef(); assertThat(action.key()).isEqualTo("set_root"); assertThat(action.isInternal()).isTrue(); assertThat(action.isPost()).isTrue(); assertThat(action.since()).isEqualTo("6.2"); assertThat(action.description()).isEqualTo("Make the specified user root.<br/>" + "Requires to be root."); assertThat(action.responseExample()).isNull(); assertThat(action.deprecatedKey()).isNull(); assertThat(action.deprecatedSince()).isNull(); assertThat(action.handler()).isSameAs(underTest); assertThat(action.params()).hasSize(1); WebService.Param param = action.param("login"); assertThat(param.isRequired()).isTrue(); assertThat(param.description()).isEqualTo("A user login"); assertThat(param.defaultValue()).isNull(); assertThat(param.deprecatedSince()).isNull(); assertThat(param.deprecatedKey()).isNull(); assertThat(param.exampleValue()).isEqualTo("admin"); }
private void initWebServiceEngine(WsUrl... wsUrls) { List<WebService.Controller> controllers = new ArrayList<>(); for (WsUrl wsUrl : wsUrls) { String controller = wsUrl.getController(); WebService.Controller wsController = mock(WebService.Controller.class); when(wsController.path()).thenReturn(controller); List<WebService.Action> actions = new ArrayList<>(); for (String action : wsUrl.getActions()) { WebService.Action wsAction = mock(WebService.Action.class); when(wsAction.path()).thenReturn(controller + "/" + action); when(wsAction.key()).thenReturn(action); when(wsAction.handler()).thenReturn(wsUrl.getRequestHandler()); actions.add(wsAction); } when(wsController.actions()).thenReturn(actions); controllers.add(wsController); } when(webServiceEngine.controllers()).thenReturn(controllers); underTest = new WebServiceFilter(webServiceEngine, runtime); }
@Test public void should_be_well_defined() { Controller controller = tester.controller(CONTROLLER_LANGUAGES); assertThat(controller).isNotNull(); assertThat(controller.description()).isNotEmpty(); assertThat(controller.isInternal()).isFalse(); assertThat(controller.path()).isEqualTo(CONTROLLER_LANGUAGES); assertThat(controller.since()).isEqualTo("5.1"); assertThat(controller.actions()).hasSize(1); Action list = controller.action(ACTION_LIST); assertThat(list).isNotNull(); assertThat(list.description()).isNotEmpty(); assertThat(list.handler()).isInstanceOf(ListAction.class); assertThat(list.isInternal()).isFalse(); assertThat(list.isPost()).isFalse(); assertThat(list.responseExampleAsString()).isNotEmpty(); assertThat(list.params()).hasSize(2); }
@Test public void definition() { WebService.Action action = wsTester.getDef(); assertThat(action.key()).isEqualTo("delete"); assertThat(action.isPost()).isTrue(); assertThat(action.description()).isEqualTo("Delete an organization.<br/>" + "Require 'Administer System' permission on the specified organization. Organization support must be enabled."); assertThat(action.isInternal()).isTrue(); assertThat(action.since()).isEqualTo("6.2"); assertThat(action.handler()).isNotNull(); assertThat(action.params()).hasSize(1); assertThat(action.responseExample()).isNull(); assertThat(action.param("organization")) .matches(param -> param.isRequired()) .matches(param -> "foo-company".equals(param.exampleValue())) .matches(param -> "Organization key".equals(param.description())); }
@Test public void verify_define() { WebService.Action action = wsTester.getDef(); assertThat(action.key()).isEqualTo(ACTION); assertThat(action.isPost()).isTrue(); assertThat(action.description()).isNotEmpty(); assertThat(action.isInternal()).isTrue(); assertThat(action.since()).isEqualTo("6.4"); assertThat(action.handler()).isEqualTo(underTest); assertThat(action.changelog()) .extracting(Change::getVersion, Change::getDescription) .contains(tuple("7.3", "This WS used to be located at /api/organizations/update_project_visibility")); WebService.Param projectVisibility = action.param(PARAM_PROJECT_VISIBILITY); assertThat(projectVisibility.isRequired()).isTrue(); assertThat(projectVisibility.possibleValues()).containsExactlyInAnyOrder("private", "public"); assertThat(projectVisibility.description()).isEqualTo("Default visibility for projects"); } }
@Test public void define_ws() { WsTester tester = new WsTester(underTest); WebService.Controller controller = tester.controller("api/webservices"); assertThat(controller).isNotNull(); assertThat(controller.path()).isEqualTo("api/webservices"); assertThat(controller.since()).isEqualTo("4.2"); assertThat(controller.description()).isNotEmpty(); assertThat(controller.actions()).hasSize(2); WebService.Action index = controller.action("list"); assertThat(index).isNotNull(); assertThat(index.key()).isEqualTo("list"); assertThat(index.handler()).isNotNull(); assertThat(index.since()).isEqualTo("4.2"); assertThat(index.isPost()).isFalse(); assertThat(index.isInternal()).isFalse(); assertThat(controller.action("response_example")).isNotNull(); }
@Test public void verify_definition() { WebService.Action action = wsTester.getDef(); assertThat(action.key()).isEqualTo("search"); assertThat(action.isInternal()).isTrue(); assertThat(action.isPost()).isFalse(); assertThat(action.since()).isEqualTo("6.2"); assertThat(action.description()).isEqualTo("Search for root users.<br/>" + "Requires to be root."); assertThat(action.responseExample()).isNotNull(); assertThat(action.deprecatedKey()).isNull(); assertThat(action.deprecatedSince()).isNull(); assertThat(action.handler()).isSameAs(underTest); assertThat(action.params()).isEmpty(); }
@Test public void define_list_action() { WebService.Controller controller = controller(); WebService.Action listProfiles = controller.action("list"); assertThat(listProfiles).isNotNull(); assertThat(listProfiles.handler()).isInstanceOf(RemovedWebServiceHandler.class); assertThat(listProfiles.responseExampleAsString()).isNotEmpty(); assertThat(listProfiles.params()).isEmpty(); }
@Test public void define_search_action() { WebService.Action action = ws.getDef(); assertThat(action).isNotNull(); assertThat(action.isPost()).isFalse(); assertThat(action.handler()).isNotNull(); assertThat(action.responseExampleAsString()).isNotEmpty(); assertThat(action.params()).hasSize(2); }
@Test public void define_index_action() { WebService.Controller controller = ws.controller("api/profiles"); WebService.Action restoreProfiles = controller.action("index"); assertThat(restoreProfiles).isNotNull(); assertThat(restoreProfiles.handler()).isInstanceOf(RemovedWebServiceHandler.class); assertThat(restoreProfiles.responseExampleAsString()).isNotEmpty(); assertThat(restoreProfiles.params()).isEmpty(); }
@Test public void define_delete_action() { WebService.Action action = ws.getDef(); assertThat(action).isNotNull(); assertThat(action.isPost()).isTrue(); assertThat(action.handler()).isNotNull(); assertThat(action.responseExample()).isNull(); assertThat(action.params()).hasSize(1); }
@Test public void define_create_action() { WebService.Action action = ws.getDef(); assertThat(action).isNotNull(); assertThat(action.isPost()).isTrue(); assertThat(action.handler()).isNotNull(); assertThat(action.responseExampleAsString()).isNotEmpty(); assertThat(action.params()).hasSize(4); }
@Test public void define_index_action() { WebService.Action action = controller.action("index"); assertThat(action).isNotNull(); assertThat(action.handler()).isInstanceOf(RemovedWebServiceHandler.class); assertThat(action.responseExampleAsString()).isNotEmpty(); assertThat(action.params()).isEmpty(); }
@Test public void define_upload_action() { WebService.Controller controller = tester.controller("api/updatecenter"); WebService.Action action = controller.action("upload"); assertThat(action).isNotNull(); assertThat(action.handler()).isNotNull(); assertThat(action.isInternal()).isTrue(); assertThat(action.isPost()).isTrue(); assertThat(action.params()).hasSize(1); } }
public Result execute() throws Exception { TestResponse response = new TestResponse(); verifyRequest(action(), this); action().handler().handle(this, response); return new Result(response); } }
public TestResponse execute() { try { DumbResponse response = new DumbResponse(); action().handler().handle(this, response); return new TestResponse(response); } catch (Exception e) { throw Throwables.propagate(e); } }
@Test public void define_ws() { WebService.Action show = ws.getDef(); assertThat(show).isNotNull(); assertThat(show.handler()).isNotNull(); assertThat(show.since()).isEqualTo("4.4"); assertThat(show.isInternal()).isFalse(); assertThat(show.responseExampleAsString()).isNotEmpty(); assertThat(show.params()).hasSize(4); }