diff --git a/its/src/test/java/com/sonar/maven/it/suite/AbstractMavenTest.java b/its/src/test/java/com/sonar/maven/it/suite/AbstractMavenTest.java index 6faba933..c9c7d574 100644 --- a/its/src/test/java/com/sonar/maven/it/suite/AbstractMavenTest.java +++ b/its/src/test/java/com/sonar/maven/it/suite/AbstractMavenTest.java @@ -81,7 +81,6 @@ public abstract class AbstractMavenTest { .addPlugin(FileLocation.of("../property-dump-plugin/target/property-dump-plugin-1-SNAPSHOT.jar")) .build(); - protected HttpConnector wsConnector; protected WsClient wsClient; @BeforeAll @@ -125,11 +124,11 @@ protected static String[] cleanPackageSonarGoal() { @BeforeEach public void setUpWsClient() { - wsConnector = HttpConnector.newBuilder() + wsClient = WsClientFactories.getDefault() + .newClient(HttpConnector.newBuilder() .url(ORCHESTRATOR.getServer().getUrl()) .credentials(Server.ADMIN_LOGIN, Server.ADMIN_PASSWORD) - .build(); - wsClient = WsClientFactories.getDefault().newClient(wsConnector); + .build()); } protected static Version mojoVersion() { @@ -152,8 +151,8 @@ protected static Version mojoVersion() { } @CheckForNull - static Measure getMeasure(String componentKey, String metricKey) { - Measures.ComponentWsResponse response = newWsClient().measures().component(new ComponentRequest() + Measure getMeasure(String componentKey, String metricKey) { + Measures.ComponentWsResponse response = wsClient.measures().component(new ComponentRequest() .setComponent(componentKey) .setMetricKeys(singletonList(metricKey))); List measures = response.getComponent().getMeasuresList(); @@ -161,15 +160,15 @@ static Measure getMeasure(String componentKey, String metricKey) { } @CheckForNull - static Integer getMeasureAsInteger(String componentKey, String metricKey) { + Integer getMeasureAsInteger(String componentKey, String metricKey) { Measure measure = getMeasure(componentKey, metricKey); return (measure == null) ? null : Integer.parseInt(measure.getValue()); } @CheckForNull - static Component getComponent(String componentKey) { + Component getComponent(String componentKey) { try { - return newWsClient().components().show(new ShowRequest().setComponent(componentKey)).getComponent(); + return wsClient.components().show(new ShowRequest().setComponent(componentKey)).getComponent(); } catch (HttpException e) { if (e.code() == 404) { return null; @@ -178,19 +177,6 @@ static Component getComponent(String componentKey) { } } - static WsClient newWsClient() { - return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder() - .url(ORCHESTRATOR.getServer().getUrl()) - .build()); - } - - static WsClient newAuthenticatedWsClient() { - return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder() - .url(ORCHESTRATOR.getServer().getUrl()) - .credentials(Server.ADMIN_LOGIN, Server.ADMIN_PASSWORD) - .build()); - } - Version mavenVersion = null; protected Version getMavenVersion() { @@ -222,7 +208,7 @@ public BuildResult executeBuildAndValidateWithCE(Build build) { return validateBuildWithCE(ORCHESTRATOR.executeBuild(build)); } - public static BuildResult validateBuildWithCE(BuildResult result) { + public BuildResult validateBuildWithCE(BuildResult result) { assertBuildResultStatuses(result, 0); List ceTaskIds = extractCETaskIds(result); if (ceTaskIds.isEmpty()) { @@ -272,10 +258,9 @@ public static List extractCETaskIds(BuildResult result) { private static final long POLLING_TIME = 500; // 0.5 second private static final long MAX_WAIT_TIME = 20_000; // 20 seconds - private static void waitForCeTaskToBeFinished(String ceTaskId) { + private void waitForCeTaskToBeFinished(String ceTaskId) { LOG.info("Waiting for CE task {} to be finished", ceTaskId); try { - WsClient wsClient = newAuthenticatedWsClient(); long start = System.currentTimeMillis(); while (true) { TaskStatus status = wsClient.ce().task(new TaskRequest().setId(ceTaskId)).getTask().getStatus(); diff --git a/its/src/test/java/com/sonar/maven/it/suite/JavaTest.java b/its/src/test/java/com/sonar/maven/it/suite/JavaTest.java index 31c4b583..4d8812ef 100644 --- a/its/src/test/java/com/sonar/maven/it/suite/JavaTest.java +++ b/its/src/test/java/com/sonar/maven/it/suite/JavaTest.java @@ -24,15 +24,11 @@ import com.sonar.orchestrator.version.Version; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Path; import java.util.Properties; - -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import org.sonarqube.ws.client.settings.SetRequest; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.data.MapEntry.entry; @@ -43,11 +39,6 @@ class JavaTest extends AbstractMavenTest { @TempDir public Path temp; - @AfterEach - public void cleanup() { - wsClient.settings().set(new SetRequest().setKey("sonar.forceAuthentication").setValue("false")); - } - // MSONAR-83 @Test void shouldPopulateLibraries() throws IOException { @@ -203,8 +194,7 @@ void takeFirstToolchainIfMultipleExecutions() throws IOException { assertThat(props).contains(entry("sonar.java.jdkHome", "fake_jdk_9")); } - private Properties getProps(File outputProps) - throws FileNotFoundException, IOException { + private Properties getProps(File outputProps) throws IOException { Properties props = new Properties(); try (FileInputStream fis = new FileInputStream(outputProps)) { props.load(fis); diff --git a/its/src/test/java/com/sonar/maven/it/suite/LinksTest.java b/its/src/test/java/com/sonar/maven/it/suite/LinksTest.java index f1006104..c7460c60 100644 --- a/its/src/test/java/com/sonar/maven/it/suite/LinksTest.java +++ b/its/src/test/java/com/sonar/maven/it/suite/LinksTest.java @@ -22,13 +22,9 @@ import com.sonar.maven.it.ItUtils; import com.sonar.orchestrator.build.MavenBuild; import com.sonar.orchestrator.container.Server; - import org.junit.jupiter.api.Test; import org.sonarqube.ws.ProjectLinks; import org.sonarqube.ws.ProjectLinks.SearchWsResponse; -import org.sonarqube.ws.client.HttpConnector; -import org.sonarqube.ws.client.WsClient; -import org.sonarqube.ws.client.WsClientFactories; import org.sonarqube.ws.client.projectlinks.SearchRequest; import static org.assertj.core.api.Assertions.assertThat; @@ -51,8 +47,7 @@ void shouldUseLinkPropertiesOverPomLinksInMaven() { private void checkLinks() { Server server = ORCHESTRATOR.getServer(); - WsClient client = newAuthenticatedWsClient(); - SearchWsResponse response = client.projectLinks().search(new SearchRequest().setProjectKey("com.sonarsource.it.samples:simple-sample")); + SearchWsResponse response = wsClient.projectLinks().search(new SearchRequest().setProjectKey("com.sonarsource.it.samples:simple-sample")); if (server.version().isGreaterThanOrEquals(7, 1)) { // SONAR-10299 assertThat(response.getLinksList()) diff --git a/its/src/test/java/com/sonar/maven/it/suite/MavenTest.java b/its/src/test/java/com/sonar/maven/it/suite/MavenTest.java index 573092ff..2f96d98e 100644 --- a/its/src/test/java/com/sonar/maven/it/suite/MavenTest.java +++ b/its/src/test/java/com/sonar/maven/it/suite/MavenTest.java @@ -24,50 +24,40 @@ import com.sonar.orchestrator.build.BuildRunner; import com.sonar.orchestrator.build.MavenBuild; import java.io.File; +import java.nio.file.Files; import java.nio.file.Path; import java.util.Map; -import org.apache.commons.io.FileUtils; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.sonarqube.ws.Components; import org.sonarqube.ws.client.components.ComponentsService; import org.sonarqube.ws.client.components.ShowRequest; -import org.sonarqube.ws.client.settings.SetRequest; import org.sonarqube.ws.client.users.CreateRequest; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; class MavenTest extends AbstractMavenTest { private static final String MODULE_START = "------------- Run sensors on module "; - @TempDir - public Path temp; - - @AfterEach - public void cleanup() { - wsClient.settings().set(new SetRequest().setKey("sonar.forceAuthentication").setValue("false")); - } - /** * See MSONAR-129 */ @Test - void useUserPropertiesGlobalConfig() throws Exception { + void useUserPropertiesGlobalConfig(@TempDir Path temp) throws Exception { BuildRunner runner = new BuildRunner(ORCHESTRATOR.getConfiguration()); MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/maven-only-test-dir")) .setGoals(cleanSonarGoal()); - File settingsXml = temp.resolve("settings.xml").toFile(); - settingsXml.createNewFile(); + Path settingsXml = temp.resolve("settings.xml").toAbsolutePath(); Map props = ORCHESTRATOR.getDatabase().getSonarProperties(); props.put("sonar.host.url", ORCHESTRATOR.getServer().getUrl()); props.put("sonar.login", ORCHESTRATOR.getDefaultAdminToken()); - FileUtils.write(settingsXml, ItUtils.createSettingsXml(props)); + Files.write(settingsXml, ItUtils.createSettingsXml(props).getBytes(UTF_8)); - build.addArgument("--settings=" + settingsXml.getAbsolutePath()); + build.addArgument("--settings=" + settingsXml); build.addArgument("-Psonar"); // we build without sonarqube server settings, it will need to fetch it from the profile defined in the settings xml file BuildResult result = runner.run(null, build); @@ -420,9 +410,8 @@ void shouldSkipWithEnvVar() { * MSONAR-141 */ @Test - void supportMavenEncryption() throws Exception { + void supportMavenEncryption() { Assertions.assertDoesNotThrow(() -> { - wsClient.settings().set(new SetRequest().setKey("sonar.forceAuthentication").setValue("true")); wsClient.users().create(new CreateRequest().setLogin("julien").setName("Julien").setPassword("123abc")); MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/maven-only-test-dir")) @@ -441,10 +430,9 @@ void supportMavenEncryption() throws Exception { } @Test - void supportMavenEncryptionWithDefaultSecuritySettings() throws Exception { + void supportMavenEncryptionWithDefaultSecuritySettings() { // Should fail because settings-security.xml is missing Assertions.assertThrows(Exception.class, () -> { - wsClient.settings().set(new SetRequest().setKey("sonar.forceAuthentication").setValue("true")); wsClient.users().create(new CreateRequest().setLogin("julien3").setName("Julien3").setPassword("123abc")); MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/maven-only-test-dir")) @@ -460,7 +448,6 @@ void supportMavenEncryptionWithDefaultSecuritySettings() throws Exception { }); Assertions.assertDoesNotThrow(() -> { - wsClient.settings().set(new SetRequest().setKey("sonar.forceAuthentication").setValue("true")); wsClient.users().create(new CreateRequest().setLogin("julien2").setName("Julien2").setPassword("123abc")); MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/maven-only-test-dir")) diff --git a/its/src/test/java/com/sonar/maven/it/suite/ProxyTest.java b/its/src/test/java/com/sonar/maven/it/suite/ProxyTest.java index dbc838e6..bc0f52e9 100644 --- a/its/src/test/java/com/sonar/maven/it/suite/ProxyTest.java +++ b/its/src/test/java/com/sonar/maven/it/suite/ProxyTest.java @@ -19,8 +19,10 @@ */ package com.sonar.maven.it.suite; -import static org.assertj.core.api.Assertions.assertThat; - +import com.sonar.maven.it.ItUtils; +import com.sonar.maven.it.Proxy; +import com.sonar.orchestrator.build.BuildResult; +import com.sonar.orchestrator.build.MavenBuild; import java.io.IOException; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; @@ -29,20 +31,15 @@ import java.nio.file.Paths; import java.util.List; import java.util.stream.Collectors; - -import com.sonar.maven.it.ItUtils; -import com.sonar.maven.it.Proxy; -import com.sonar.orchestrator.build.BuildResult; -import com.sonar.orchestrator.build.MavenBuild; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import static org.assertj.core.api.Assertions.assertThat; + class ProxyTest extends AbstractMavenTest { private Proxy proxy; - @TempDir - public Path temp; @BeforeEach public void prepare() throws Exception { @@ -58,7 +55,7 @@ public void after() throws Exception { } @Test - void useActiveProxyInSettings() throws IOException, URISyntaxException, InterruptedException { + void useActiveProxyInSettings(@TempDir Path temp) throws IOException, URISyntaxException, InterruptedException { Thread.sleep(2000); Path proxyXml = Paths.get(this.getClass().getResource("/proxy-settings.xml").toURI()); Path proxyXmlPatched = temp.resolve("settings.xml");