@Test public void create_error_msg_from_json() { String content = "{\"errors\":[{\"msg\":\"missing scan permission\"}, {\"msg\":\"missing another permission\"}]}"; assertThat(ScannerWsClient.createErrorMessage(new HttpException("url", 400, content))).isEqualTo("missing scan permission, missing another permission"); }
@Test public void create_error_msg_from_html() { String content = "<!DOCTYPE html><html>something</html>"; assertThat(ScannerWsClient.createErrorMessage(new HttpException("url", 400, content))).isEqualTo("HTTP code 400"); }
@Test public void test_exception() throws Exception { HttpException exception = new HttpException("http://localhost:9000/api/search", 500, "error"); assertThat(exception.code()).isEqualTo(500); assertThat(exception.url()).isEqualTo("http://localhost:9000/api/search"); assertThat(exception.getMessage()).isEqualTo("Error 500 on http://localhost:9000/api/search : error"); } }
@Override public WsResponse failIfNotSuccessful() { if (!isSuccessful()) { String content = content(); close(); throw new HttpException(requestUrl(), code(), content); } return this; }
@Test public void create_error_msg_from_long_content() { String content = StringUtils.repeat("mystring", 1000); assertThat(ScannerWsClient.createErrorMessage(new HttpException("url", 400, content))).hasSize(15 + 128); }
@Test public void parse_upload_error_message() throws IOException { HttpException ex = new HttpException("url", 404, "{\"errors\":[{\"msg\":\"Organization with key 'MyOrg' does not exist\"}]}"); WsResponse response = mock(WsResponse.class); when(response.failIfNotSuccessful()).thenThrow(ex); when(wsClient.call(any(WsRequest.class))).thenReturn(response); exception.expect(MessageException.class); exception.expectMessage("Failed to upload report - Organization with key 'MyOrg' does not exist"); underTest.upload(temp.newFile()); }
@Test(expected = IllegalStateException.class) public void failFastHttpError() { HttpException http = new HttpException("url", 403, null); IllegalStateException e = new IllegalStateException("http error", http); WsTestUtil.mockException(wsClient, e); loader.load(PROJECT_KEY, false, null); }
@Test public void load_throws_MessageException_if_organization_is_not_found() throws IOException { HttpException e = new HttpException("", 404, "{\"errors\":[{\"msg\":\"No organization with key 'myorg'\"}]}"); WsTestUtil.mockException(wsClient, e); exception.expect(MessageException.class); exception.expectMessage("Failed to load the quality profiles of project 'project': No organization with key 'myorg'"); underTest.load("project", null); verifyNoMoreInteractions(wsClient); }
@Test public void failFastHttpErrorMessageException() { thrown.expect(MessageException.class); thrown.expectMessage("http error"); HttpException http = new HttpException("uri", 403, null); MessageException e = MessageException.of("http error", http); WsTestUtil.mockException(wsClient, e); loader.load(PROJECT_KEY, false, null); }
@Override public WsResponse failIfNotSuccessful() { if (!isSuccessful()) { String content = content(); close(); throw new HttpException(requestUrl(), code(), content); } return this; }
private Rules.ShowResponse showRule(String ruleKey) { GetRequest getRequest = new GetRequest("api/rules/show").setParam("key", ruleKey).setMediaType(MediaTypes.PROTOBUF); WsResponse wsResponse = wsClient.wsConnector().call(getRequest); if (wsResponse.code() != 200) { throw new HttpException(wsClient.wsConnector().baseUrl() + toString(getRequest), wsResponse.code(), wsResponse.content()); } try { return Rules.ShowResponse.parseFrom(wsResponse.contentStream()); } catch (IOException e) { throw new IllegalStateException(e.getMessage(), e); } }