private List<QualityProfile> loadAndOverrideIfNeeded(@Nullable String profileName, StringBuilder url) throws IOException { properties.organizationKey().ifPresent(k -> url.append("&organization=").append(encodeForUrl(k))); Map<String, QualityProfile> result = call(url.toString()); if (profileName != null) { StringBuilder urlForName = new StringBuilder(WS_URL + "?profileName="); urlForName.append(encodeForUrl(profileName)); properties.organizationKey().ifPresent(k -> urlForName.append("&organization=").append(encodeForUrl(k))); result.putAll(call(urlForName.toString())); } if (result.isEmpty()) { throw MessageException.of("No quality profiles have been found, you probably don't have any language plugin installed."); } return new ArrayList<>(result.values()); }
@Override public List<QualityProfile> loadDefault(@Nullable String profileName) { StringBuilder url = new StringBuilder(WS_URL + "?defaults=true"); return handleErrors(profileName, url, () -> "Failed to load the default quality profiles"); }
private List<QualityProfile> handleErrors(@Nullable String profileName, StringBuilder url, Supplier<String> errorMsg) { try { return loadAndOverrideIfNeeded(profileName, url); } catch (HttpException e) { if (e.code() == 404) { throw MessageException.of(errorMsg.get() + ": " + ScannerWsClient.createErrorMessage(e)); } throw new IllegalStateException(errorMsg.get() + ": " + ScannerWsClient.createErrorMessage(e)); } catch (MessageException e) { throw e; } catch (Exception e) { throw new IllegalStateException(errorMsg.get(), e); } }
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)); } }
@Test public void load_sets_organization_parameter_if_defined_in_settings() throws IOException { when(properties.organizationKey()).thenReturn(Optional.of("my-org")); prepareCallWithResults(); underTest.load("foo", null); verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo&organization=my-org"); }
@Test public void loadDefault_gets_all_default_profiles() throws IOException { prepareCallWithResults(); underTest.loadDefault(null); verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true"); }
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)); } }
@Test public void load_throws_MessageException_if_no_profiles_are_available_for_specified_project() throws IOException { prepareCallWithEmptyResults(); exception.expect(MessageException.class); exception.expectMessage("No quality profiles"); underTest.load("project", null); verifyNoMoreInteractions(wsClient); }
@Test public void loadDefault_sets_organization_parameter_if_defined_in_settings() throws IOException { when(properties.organizationKey()).thenReturn(Optional.of("my-org")); WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?defaults=true&organization=my-org", createStreamOfProfiles("qp")); WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?profileName=foo&organization=my-org", createStreamOfProfiles("qp")); underTest.loadDefault("foo"); verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true&organization=my-org"); verifyCalledPath("/api/qualityprofiles/search.protobuf?profileName=foo&organization=my-org"); }
@Override public List<QualityProfile> load(String projectKey, @Nullable String profileName) { StringBuilder url = new StringBuilder(WS_URL + "?projectKey=").append(encodeForUrl(projectKey)); return handleErrors(profileName, url, () -> String.format("Failed to load the quality profiles of project '%s'", projectKey)); }
private List<QualityProfile> loadAndOverrideIfNeeded(@Nullable String profileName, StringBuilder url) throws IOException { properties.organizationKey().ifPresent(k -> url.append("&organization=").append(encodeForUrl(k))); Map<String, QualityProfile> result = call(url.toString()); if (profileName != null) { StringBuilder urlForName = new StringBuilder(WS_URL + "?profileName="); urlForName.append(encodeForUrl(profileName)); properties.organizationKey().ifPresent(k -> urlForName.append("&organization=").append(encodeForUrl(k))); result.putAll(call(urlForName.toString())); } if (result.isEmpty()) { throw MessageException.of("No quality profiles have been found, you probably don't have any language plugin installed."); } return new ArrayList<>(result.values()); }
private List<QualityProfile> handleErrors(@Nullable String profileName, StringBuilder url, Supplier<String> errorMsg) { try { return loadAndOverrideIfNeeded(profileName, url); } catch (HttpException e) { if (e.code() == 404) { throw MessageException.of(errorMsg.get() + ": " + ScannerWsClient.createErrorMessage(e)); } throw new IllegalStateException(errorMsg.get() + ": " + ScannerWsClient.createErrorMessage(e)); } catch (MessageException e) { throw e; } catch (Exception e) { throw new IllegalStateException(errorMsg.get(), e); } }
@Test public void load_gets_all_profiles_for_specified_project() throws IOException { prepareCallWithResults(); underTest.load("foo", null); verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo"); }
@Test public void loadDefault_gets_profiles_with_specified_name() throws IOException { WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?defaults=true", createStreamOfProfiles("qp")); WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?profileName=foo", createStreamOfProfiles("qp")); underTest.loadDefault("foo"); verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true"); verifyCalledPath("/api/qualityprofiles/search.protobuf?profileName=foo"); }
@Override public List<QualityProfile> loadDefault(@Nullable String profileName) { StringBuilder url = new StringBuilder(WS_URL + "?defaults=true"); return handleErrors(profileName, url, () -> "Failed to load the default quality profiles"); }
@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); }
@Override public List<QualityProfile> load(String projectKey, @Nullable String profileName) { StringBuilder url = new StringBuilder(WS_URL + "?projectKey=").append(encodeForUrl(projectKey)); return handleErrors(profileName, url, () -> String.format("Failed to load the quality profiles of project '%s'", projectKey)); }
@Test public void load_gets_profiles_for_specified_project_and_profile_name() throws IOException { WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?projectKey=foo", createStreamOfProfiles("qp")); WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?profileName=bar", createStreamOfProfiles("qp")); underTest.load("foo", "bar"); verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo"); verifyCalledPath("/api/qualityprofiles/search.protobuf?profileName=bar"); }
@Test public void load_encodes_url_parameters() throws IOException { WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?projectKey=foo%232", createStreamOfProfiles("qp")); WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?profileName=bar%232", createStreamOfProfiles("qp")); underTest.load("foo#2", "bar#2"); verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo%232"); verifyCalledPath("/api/qualityprofiles/search.protobuf?profileName=bar%232"); }