]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8925 scanner sends param "organization" when requesting Quality profiles
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 22 Mar 2017 16:42:25 +0000 (17:42 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 23 Mar 2017 16:38:34 +0000 (17:38 +0100)
sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultQualityProfileLoader.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultQualityProfileLoaderTest.java
sonar-scanner-engine/src/test/resources/org/sonar/scanner/repository/DefaultQualityProfileLoaderTest/quality_profile_search_default [deleted file]

index 344f6c0ce7d636c3bdd60bf7f77c06954707c16d..4415c12e341a73db4a5e9383e21a0086d22683ad 100644 (file)
  */
 package org.sonar.scanner.repository;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+import java.util.Optional;
+import javax.annotation.Nullable;
+import org.apache.commons.io.IOUtils;
+import org.sonar.api.config.Settings;
 import org.sonar.api.utils.MessageException;
 import org.sonar.scanner.bootstrap.ScannerWsClient;
-import org.sonar.scanner.util.ScannerUtils;
 import org.sonarqube.ws.QualityProfiles.SearchWsResponse;
-import org.apache.commons.io.IOUtils;
 import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
 import org.sonarqube.ws.client.GetRequest;
-import javax.annotation.Nullable;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.List;
+import static org.sonar.scanner.util.ScannerUtils.encodeForUrl;
 
 public class DefaultQualityProfileLoader implements QualityProfileLoader {
   private static final String WS_URL = "/api/qualityprofiles/search.protobuf";
 
-  private ScannerWsClient wsClient;
+  private final Settings settings;
+  private final ScannerWsClient wsClient;
 
-  public DefaultQualityProfileLoader(ScannerWsClient wsClient) {
+  public DefaultQualityProfileLoader(Settings settings, ScannerWsClient wsClient) {
+    this.settings = settings;
     this.wsClient = wsClient;
   }
 
   @Override
   public List<QualityProfile> loadDefault(@Nullable String profileName) {
-    String url = WS_URL + "?defaults=true";
+    StringBuilder url = new StringBuilder(WS_URL + "?defaults=true");
     if (profileName != null) {
-      url += "&profileName=" + ScannerUtils.encodeForUrl(profileName);
+      url.append("&profileName=").append(encodeForUrl(profileName));
     }
-    return loadResource(url);
+    getOrganizationKey().ifPresent(k -> url.append("&organization=").append(encodeForUrl(k)));
+    return call(url.toString());
   }
 
   @Override
   public List<QualityProfile> load(String projectKey, @Nullable String profileName) {
-    String url = WS_URL + "?projectKey=" + ScannerUtils.encodeForUrl(projectKey);
+    StringBuilder url = new StringBuilder(WS_URL + "?projectKey=").append(encodeForUrl(projectKey));
     if (profileName != null) {
-      url += "&profileName=" + ScannerUtils.encodeForUrl(profileName);
+      url.append("&profileName=").append(encodeForUrl(profileName));
     }
-    return loadResource(url);
+    getOrganizationKey().ifPresent(k -> url.append("&organization=").append(encodeForUrl(k)));
+    return call(url.toString());
+  }
+
+  private Optional<String> getOrganizationKey() {
+    return Optional.ofNullable(settings.getString("sonar.organization"));
   }
 
-  private List<QualityProfile> loadResource(String url) {
+  private List<QualityProfile> call(String url) {
     GetRequest getRequest = new GetRequest(url);
     InputStream is = wsClient.call(getRequest).contentStream();
-    SearchWsResponse profiles = null;
+    SearchWsResponse profiles;
 
     try {
       profiles = SearchWsResponse.parseFrom(is);
index 9cbed2876df8630f15afdd71eca210fdcf3cf815..246425e592c7bcb9e957467fcc760bd650944344 100644 (file)
  */
 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
deleted file mode 100644 (file)
index 6780d73..0000000
Binary files a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/repository/DefaultQualityProfileLoaderTest/quality_profile_search_default and /dev/null differ