]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5417 Add back preview parameter to check permission
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 26 Aug 2014 08:57:50 +0000 (10:57 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Tue, 26 Aug 2014 09:13:12 +0000 (11:13 +0200)
sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java
sonar-batch/src/test/java/org/sonar/batch/referential/DefaultProjectReferentialsLoaderTest.java [new file with mode: 0644]

index 700b477d1d623dc292fbca85d6dca5690b32d9e9..4c8f33bdfebef54daf76fc84eae684ab6f1ad164 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.batch.referential;
 import com.google.common.base.Charsets;
 import com.google.common.io.InputSupplier;
 import org.sonar.api.batch.bootstrap.ProjectReactor;
+import org.sonar.batch.bootstrap.AnalysisMode;
 import org.sonar.batch.bootstrap.ServerClient;
 import org.sonar.batch.bootstrap.TaskProperties;
 import org.sonar.batch.protocol.input.ProjectReferentials;
@@ -30,23 +31,32 @@ import org.sonar.batch.rule.ModuleQProfiles;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
 
 public class DefaultProjectReferentialsLoader implements ProjectReferentialsLoader {
 
   private static final String BATCH_PROJECT_URL = "/batch/project";
 
   private final ServerClient serverClient;
+  private final AnalysisMode analysisMode;
 
-  public DefaultProjectReferentialsLoader(ServerClient serverClient) {
+  public DefaultProjectReferentialsLoader(ServerClient serverClient, AnalysisMode analysisMode) {
     this.serverClient = serverClient;
+    this.analysisMode = analysisMode;
   }
 
   @Override
   public ProjectReferentials load(ProjectReactor reactor, TaskProperties taskProperties) {
     String url = BATCH_PROJECT_URL + "?key=" + reactor.getRoot().getKeyWithBranch();
     if (taskProperties.properties().containsKey(ModuleQProfiles.SONAR_PROFILE_PROP)) {
-      url += "&profile=" + taskProperties.properties().get(ModuleQProfiles.SONAR_PROFILE_PROP);
+      try {
+        url += "&profile=" + URLEncoder.encode(taskProperties.properties().get(ModuleQProfiles.SONAR_PROFILE_PROP), "UTF-8");
+      } catch (UnsupportedEncodingException e) {
+        throw new IllegalStateException("Unable to encode URL", e);
+      }
     }
+    url += "&preview=" + analysisMode.isPreview();
     InputSupplier<InputStream> jsonStream = serverClient.doRequest(url, null);
     try {
       return ProjectReferentials.fromJson(new InputStreamReader(jsonStream.getInput(), Charsets.UTF_8));
diff --git a/sonar-batch/src/test/java/org/sonar/batch/referential/DefaultProjectReferentialsLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/referential/DefaultProjectReferentialsLoaderTest.java
new file mode 100644 (file)
index 0000000..2dd3887
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.batch.referential;
+
+import com.google.common.collect.Maps;
+import com.google.common.io.InputSupplier;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.batch.bootstrap.ProjectReactor;
+import org.sonar.batch.bootstrap.AnalysisMode;
+import org.sonar.batch.bootstrap.ServerClient;
+import org.sonar.batch.bootstrap.TaskProperties;
+import org.sonar.batch.rule.ModuleQProfiles;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class DefaultProjectReferentialsLoaderTest {
+
+  private DefaultProjectReferentialsLoader loader;
+  private ServerClient serverClient;
+  private AnalysisMode analysisMode;
+  private ProjectReactor reactor;
+  private TaskProperties taskProperties;
+
+  @Before
+  public void prepare() {
+    serverClient = mock(ServerClient.class);
+    analysisMode = mock(AnalysisMode.class);
+    loader = new DefaultProjectReferentialsLoader(serverClient, analysisMode);
+    when(serverClient.doRequest(anyString(), anyInt())).thenReturn(new InputSupplier<InputStream>() {
+
+      @Override
+      public InputStream getInput() throws IOException {
+        return new ByteArrayInputStream(new byte[0]);
+      }
+    });
+    reactor = new ProjectReactor(ProjectDefinition.create().setKey("foo"));
+    taskProperties = new TaskProperties(Maps.<String, String>newHashMap(), "");
+  }
+
+  @Test
+  public void passPreviewParameter() {
+    when(analysisMode.isPreview()).thenReturn(false);
+    loader.load(reactor, taskProperties);
+    verify(serverClient).doRequest("/batch/project?key=foo&preview=false", null);
+
+    when(analysisMode.isPreview()).thenReturn(true);
+    loader.load(reactor, taskProperties);
+    verify(serverClient).doRequest("/batch/project?key=foo&preview=true", null);
+  }
+
+  @Test
+  public void passProfileParameter() {
+    taskProperties.properties().put(ModuleQProfiles.SONAR_PROFILE_PROP, "my-profile");
+    loader.load(reactor, taskProperties);
+    verify(serverClient).doRequest("/batch/project?key=foo&profile=my-profile&preview=false", null);
+  }
+
+}