From 223299c1192266db9c4813faa37b5d10fba6f65f Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Tue, 26 Aug 2014 10:57:50 +0200 Subject: [PATCH] SONAR-5417 Add back preview parameter to check permission --- .../DefaultProjectReferentialsLoader.java | 14 ++- .../DefaultProjectReferentialsLoaderTest.java | 85 +++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 sonar-batch/src/test/java/org/sonar/batch/referential/DefaultProjectReferentialsLoaderTest.java diff --git a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java index 700b477d1d6..4c8f33bdfeb 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java @@ -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 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 index 00000000000..2dd3887c6de --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/referential/DefaultProjectReferentialsLoaderTest.java @@ -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() { + + @Override + public InputStream getInput() throws IOException { + return new ByteArrayInputStream(new byte[0]); + } + }); + reactor = new ProjectReactor(ProjectDefinition.create().setKey("foo")); + taskProperties = new TaskProperties(Maps.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); + } + +} -- 2.39.5