private Map<String, QualityProfile> call(String url) throws IOException { GetRequest getRequest = new GetRequest(url); try (InputStream is = wsClient.call(getRequest).contentStream()) { SearchWsResponse profiles = SearchWsResponse.parseFrom(is); List<QualityProfile> profilesList = profiles.getProfilesList(); return profilesList.stream().collect(toMap(QualityProfile::getLanguage, identity(), throwingMerger(), LinkedHashMap::new)); } }
public static void mockException(ScannerWsClient mock, Exception e) { when(mock.call(any(WsRequest.class))).thenThrow(e); }
public static void mockStream(ScannerWsClient mock, InputStream is) { WsResponse response = mock(WsResponse.class); when(response.contentStream()).thenReturn(is); when(mock.call(any(WsRequest.class))).thenReturn(response); }
/** * Gets information about the plugins installed on server (filename, checksum) */ private InstalledPlugin[] listInstalledPlugins() { Profiler profiler = Profiler.create(LOG).startInfo("Load plugins index"); GetRequest getRequest = new GetRequest(PLUGINS_WS_URL); InstalledPlugins installedPlugins; try (Reader reader = wsClient.call(getRequest).contentReader()) { installedPlugins = new Gson().fromJson(reader, InstalledPlugins.class); } catch (Exception e) { throw new IllegalStateException("Fail to parse response of " + PLUGINS_WS_URL, e); } profiler.stopInfo(); return installedPlugins.plugins; }
public static void mockReader(ScannerWsClient mock, Reader reader) { WsResponse response = mock(WsResponse.class); when(response.contentReader()).thenReturn(reader); when(mock.call(any(WsRequest.class))).thenReturn(response); }
@Override public ProjectRepositories load(String projectKey, boolean issuesMode, @Nullable String branchBase) { GetRequest request = new GetRequest(getUrl(projectKey, issuesMode, branchBase)); try (WsResponse response = wsClient.call(request)) { try (InputStream is = response.contentStream()) { return processStream(is); } catch (IOException e) { throw new IllegalStateException("Couldn't load project repository for " + projectKey, e); } } catch (RuntimeException e) { if (shouldThrow(e)) { throw e; } LOG.debug("Project repository not available - continuing without it"); return new SingleProjectRepository(); } }
@Override public List<Rule> load() { GetRequest getRequest = new GetRequest(RULES_SEARCH_URL); ListResponse list = loadFromStream(wsClient.call(getRequest).contentStream()); return list.getRulesList(); }
public static void mockReader(ScannerWsClient mock, String path, Reader reader, Reader... others) { WsResponse response = mock(WsResponse.class); when(response.contentReader()).thenReturn(reader); WsResponse[] otherResponses = new WsResponse[others.length]; for (int i = 0; i < others.length; i++) { WsResponse otherResponse = mock(WsResponse.class); when(otherResponse.contentReader()).thenReturn(others[i]); otherResponses[i] = otherResponse; } when(mock.call(argThat(new RequestMatcher(path)))).thenReturn(response, otherResponses); }
@Override public void load(String componentKey, Consumer<ServerIssue> consumer) { GetRequest getRequest = new GetRequest("/batch/issues.protobuf?key=" + ScannerUtils.encodeForUrl(componentKey)); InputStream is = wsClient.call(getRequest).contentStream(); parseIssues(is, consumer); }
public static void mockStream(ScannerWsClient mock, String path, InputStream is) { WsResponse response = mock(WsResponse.class); when(response.contentStream()).thenReturn(is); when(mock.call(argThat(new RequestMatcher(path)))).thenReturn(response); }
@Test public void continueOnError() { when(wsClient.call(any(WsRequest.class))).thenThrow(IllegalStateException.class); ProjectRepositories proj = loader.load(PROJECT_KEY, false, null); assertThat(proj.exists()).isEqualTo(false); }
public static void mockException(ScannerWsClient mock, String path, Exception e) { when(mock.call(argThat(new RequestMatcher(path)))).thenThrow(e); }
private String loadHashesFromWs(String fileKey) { Profiler profiler = Profiler.createIfDebug(Loggers.get(getClass())) .addContext("file", fileKey) .startDebug("Load line hashes"); GetRequest getRequest = new GetRequest("/api/sources/hash?key=" + ScannerUtils.encodeForUrl(fileKey)); Reader reader = wsClient.call(getRequest).contentReader(); try { return IOUtils.toString(reader); } catch (IOException e) { throw new IllegalStateException(e); } finally { profiler.stopDebug(); } } }
public static void verifyCall(ScannerWsClient mock, String path) { verify(mock).call(argThat(new RequestMatcher(path))); }
@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 public void log_and_profile_request_if_debug_level() { WsRequest request = newRequest(); WsResponse response = newResponse().setRequestUrl("https://local/api/issues/search"); when(wsClient.wsConnector().call(request)).thenReturn(response); logTester.setLevel(LoggerLevel.DEBUG); ScannerWsClient underTest = new ScannerWsClient(wsClient, false, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))); WsResponse result = underTest.call(request); // do not fail the execution -> interceptor returns the response assertThat(result).isSameAs(response); // check logs List<String> debugLogs = logTester.logs(LoggerLevel.DEBUG); assertThat(debugLogs).hasSize(1); assertThat(debugLogs.get(0)).contains("GET 200 https://local/api/issues/search | time="); }
@Test public void fail_if_requires_credentials() { expectedException.expect(MessageException.class); expectedException .expectMessage("Not authorized. Analyzing this project requires to be authenticated. Please provide the values of the properties sonar.login and sonar.password."); WsRequest request = newRequest(); WsResponse response = newResponse().setCode(401); when(wsClient.wsConnector().call(request)).thenReturn(response); new ScannerWsClient(wsClient, false, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request); }
@Test public void fail_if_credentials_are_not_valid() { expectedException.expect(MessageException.class); expectedException.expectMessage("Not authorized. Please check the properties sonar.login and sonar.password."); WsRequest request = newRequest(); WsResponse response = newResponse().setCode(401); when(wsClient.wsConnector().call(request)).thenReturn(response); new ScannerWsClient(wsClient, /* credentials are configured */true, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request); }
@Test public void fail_if_requires_permission() { expectedException.expect(MessageException.class); expectedException.expectMessage("You're not authorized to run analysis. Please contact the project administrator."); WsRequest request = newRequest(); WsResponse response = newResponse() .setCode(403); when(wsClient.wsConnector().call(request)).thenReturn(response); new ScannerWsClient(wsClient, true, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request); }
@Test public void fail_if_bad_request() { expectedException.expect(MessageException.class); expectedException.expectMessage("Boo! bad request! bad!"); WsRequest request = newRequest(); WsResponse response = newResponse() .setCode(400) .setContent("{\"errors\":[{\"msg\":\"Boo! bad request! bad!\"}]}"); when(wsClient.wsConnector().call(request)).thenReturn(response); new ScannerWsClient(wsClient, true, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request); }