diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-03-22 17:42:25 +0100 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-03-23 17:38:34 +0100 |
commit | f2ffddfe321416578d97ff7d100ff85e940ba1b4 (patch) | |
tree | c2e3a2fd737d311006f0c62223c0da77e2bdbbcf /sonar-scanner-engine/src/test | |
parent | 333d9bce74015255e99c7bce8b4664c5bb9ddff4 (diff) | |
download | sonarqube-f2ffddfe321416578d97ff7d100ff85e940ba1b4.tar.gz sonarqube-f2ffddfe321416578d97ff7d100ff85e940ba1b4.zip |
SONAR-8925 scanner sends param "organization" when requesting Quality profiles
Diffstat (limited to 'sonar-scanner-engine/src/test')
2 files changed, 78 insertions, 51 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultQualityProfileLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultQualityProfileLoaderTest.java index 9cbed2876df..246425e592c 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultQualityProfileLoaderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultQualityProfileLoaderTest.java @@ -19,87 +19,114 @@ */ package org.sonar.scanner.repository; -import org.sonar.api.utils.MessageException; - -import org.sonarqube.ws.QualityProfiles; -import com.google.common.io.Resources; -import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile; -import org.sonar.scanner.WsTestUtil; -import org.sonar.scanner.bootstrap.ScannerWsClient; -import org.sonar.scanner.repository.DefaultQualityProfileLoader; -import org.junit.Rule; -import org.junit.rules.ExpectedException; -import org.junit.Before; -import org.junit.Test; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.util.List; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.config.MapSettings; +import org.sonar.api.config.Settings; +import org.sonar.api.utils.MessageException; +import org.sonar.scanner.WsTestUtil; +import org.sonar.scanner.bootstrap.ScannerWsClient; +import org.sonarqube.ws.QualityProfiles; +import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.verifyNoMoreInteractions; public class DefaultQualityProfileLoaderTest { @Rule public ExpectedException exception = ExpectedException.none(); - private DefaultQualityProfileLoader qpLoader; - private ScannerWsClient wsClient; - private InputStream is; - - @Before - public void setUp() throws IOException { - wsClient = mock(ScannerWsClient.class); - is = mock(InputStream.class); - when(is.read()).thenReturn(-1); - WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?projectKey=foo%232&profileName=my-profile%232", is); - qpLoader = new DefaultQualityProfileLoader(wsClient); + private ScannerWsClient wsClient = mock(ScannerWsClient.class); + private Settings settings = new MapSettings(); + private DefaultQualityProfileLoader underTest = new DefaultQualityProfileLoader(settings, wsClient); + + @Test + public void load_gets_profiles_for_specified_project_and_profile_name() throws IOException { + prepareCallWithResults(); + underTest.load("foo", "bar"); + verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo&profileName=bar"); } @Test - public void testEncoding() throws IOException { - InputStream is = createEncodedQP("qp"); - WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?projectKey=foo%232&profileName=my-profile%232", is); + public void load_gets_all_profiles_for_specified_project() throws IOException { + prepareCallWithResults(); + underTest.load("foo", null); + verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo"); + } - List<QualityProfile> loaded = qpLoader.load("foo#2", "my-profile#2"); - WsTestUtil.verifyCall(wsClient, "/api/qualityprofiles/search.protobuf?projectKey=foo%232&profileName=my-profile%232"); - verifyNoMoreInteractions(wsClient); - assertThat(loaded).hasSize(1); + @Test + public void load_encodes_url_parameters() throws IOException { + prepareCallWithResults(); + underTest.load("foo#2", "bar#2"); + verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo%232&profileName=bar%232"); } @Test - public void testNoProfile() throws IOException { - InputStream is = createEncodedQP(); - WsTestUtil.mockStream(wsClient, is); + public void load_sets_organization_parameter_if_defined_in_settings() throws IOException { + settings.setProperty("sonar.organization", "my-org"); + prepareCallWithResults(); + underTest.load("foo", null); + verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo&organization=my-org"); + } + + @Test + public void loadDefault_gets_profiles_with_specified_name() throws IOException { + prepareCallWithResults(); + underTest.loadDefault("foo"); + verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true&profileName=foo"); + } + + @Test + public void loadDefault_gets_all_default_profiles() throws IOException { + prepareCallWithResults(); + underTest.loadDefault(null); + verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true"); + } + + @Test + public void loadDefault_encodes_url_parameters() throws IOException { + prepareCallWithResults(); + underTest.loadDefault("foo#2"); + verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true&profileName=foo%232"); + } + + @Test + public void loadDefault_sets_organization_parameter_if_defined_in_settings() throws IOException { + settings.setProperty("sonar.organization", "my-org"); + prepareCallWithResults(); + underTest.loadDefault("foo"); + verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true&profileName=foo&organization=my-org"); + } + + @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"); - qpLoader.load("project", null); + underTest.load("project", null); verifyNoMoreInteractions(wsClient); } - @Test - public void use_real_response() throws IOException { - InputStream is = getTestResource("quality_profile_search_default"); - WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?defaults=true", is); + private void verifyCalledPath(String expectedPath) { + WsTestUtil.verifyCall(wsClient, expectedPath); + } - List<QualityProfile> loaded = qpLoader.loadDefault(null); - WsTestUtil.verifyCall(wsClient, "/api/qualityprofiles/search.protobuf?defaults=true"); - verifyNoMoreInteractions(wsClient); - assertThat(loaded).hasSize(1); + private void prepareCallWithResults() throws IOException { + WsTestUtil.mockStream(wsClient, createStreamOfProfiles("qp")); } - private InputStream getTestResource(String name) throws IOException { - return Resources.asByteSource(this.getClass().getResource(this.getClass().getSimpleName() + "/" + name)) - .openBufferedStream(); + private void prepareCallWithEmptyResults() throws IOException { + WsTestUtil.mockStream(wsClient, createStreamOfProfiles()); } - private static InputStream createEncodedQP(String... names) throws IOException { + private static InputStream createStreamOfProfiles(String... names) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); QualityProfiles.SearchWsResponse.Builder responseBuilder = QualityProfiles.SearchWsResponse.newBuilder(); diff --git a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/repository/DefaultQualityProfileLoaderTest/quality_profile_search_default b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/repository/DefaultQualityProfileLoaderTest/quality_profile_search_default Binary files differdeleted file mode 100644 index 6780d7338a1..00000000000 --- a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/repository/DefaultQualityProfileLoaderTest/quality_profile_search_default +++ /dev/null |