Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonarsource.scanner.lib.internal.endpoint;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -31,7 +32,8 @@

public enum OfficialSonarQubeCloudInstance {
GLOBAL("https://sonarcloud.io", "https://api.sonarcloud.io", null),
US("https://sonarqube.us", "https://api.sonarqube.us", "US");
US("https://sonarqube.us", "https://api.sonarqube.us", "US"),
AU("https://sonarqube.au", "https://api.sonarqube.au", "AU");

private final ScannerEndpoint endpoint;

Expand All @@ -40,7 +42,7 @@ public enum OfficialSonarQubeCloudInstance {
}

public static Set<String> getRegionCodesWithoutGlobal() {
return getRegionsWithoutGlobal().map(Enum::name).map(s -> s.toLowerCase(Locale.ENGLISH)).collect(Collectors.toSet());
return getRegionsWithoutGlobal().map(Enum::name).map(s -> s.toLowerCase(Locale.ENGLISH)).collect(Collectors.toCollection(LinkedHashSet::new));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ void should_use_new_bootstrapping_on_sonarqube_cloud_us() throws Exception {
}
}

@Test
void should_use_new_bootstrapping_on_sonarqube_cloud_au() throws Exception {
try (var bootstrapResult = underTest.setBootstrapProperty(ScannerProperties.SONAR_REGION, "au").bootstrap()) {
verify(scannerEngineLauncherFactory).createLauncher(eq(scannerHttpClient), any(DownloadCache.class), anyMap());
assertThat(bootstrapResult.getEngineFacade().isSonarQubeCloud()).isTrue();
assertThat(bootstrapResult.getEngineFacade().getBootstrapProperties()).contains(
entry(ScannerProperties.HOST_URL, "https://sonarqube.au"),
entry(ScannerProperties.API_BASE_URL, "https://api.sonarqube.au"),
entry(ScannerProperties.SONARQUBE_CLOUD_URL, "https://sonarqube.au"));
assertThat(logTester.logs(Level.INFO)).contains("Communicating with SonarQube Cloud (AU region)");
}
}

@Test
void should_use_cleaned_urls() throws Exception {
try (var bootstrapResult = underTest.setBootstrapProperty(ScannerProperties.HOST_URL, "http://www.sonarcloud.io/").bootstrap()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ class OfficialSonarQubeCloudInstanceTest {

@Test
void shouldListRegionsCodes() {
assertThat(OfficialSonarQubeCloudInstance.getRegionCodesWithoutGlobal()).containsExactlyInAnyOrder("us");
assertThat(OfficialSonarQubeCloudInstance.getRegionCodesWithoutGlobal()).containsExactly("us", "au");
Comment thread
antoine-vinot-sonarsource marked this conversation as resolved.
Resolved
}

@Test
void shouldGetInstanceFromRegionCodeIgnoringCase() {
assertThat(OfficialSonarQubeCloudInstance.fromRegionCode("US")).contains(OfficialSonarQubeCloudInstance.US);
assertThat(OfficialSonarQubeCloudInstance.fromRegionCode("us")).contains(OfficialSonarQubeCloudInstance.US);
assertThat(OfficialSonarQubeCloudInstance.fromRegionCode("AU")).contains(OfficialSonarQubeCloudInstance.AU);
assertThat(OfficialSonarQubeCloudInstance.fromRegionCode("au")).contains(OfficialSonarQubeCloudInstance.AU);
assertThat(OfficialSonarQubeCloudInstance.fromRegionCode("")).contains(OfficialSonarQubeCloudInstance.GLOBAL);
assertThat(OfficialSonarQubeCloudInstance.fromRegionCode(null)).contains(OfficialSonarQubeCloudInstance.GLOBAL);
assertThat(OfficialSonarQubeCloudInstance.fromRegionCode("foo")).isEmpty();
Expand All @@ -45,6 +47,7 @@ void shouldGetInstanceFromRegionCodeIgnoringCase() {
void shouldGetInstanceFromWebEndpoint() {
assertThat(OfficialSonarQubeCloudInstance.fromWebEndpoint("https://sonarcloud.io")).contains(OfficialSonarQubeCloudInstance.GLOBAL);
assertThat(OfficialSonarQubeCloudInstance.fromWebEndpoint("https://sonarqube.us")).contains(OfficialSonarQubeCloudInstance.US);
assertThat(OfficialSonarQubeCloudInstance.fromWebEndpoint("https://sonarqube.au")).contains(OfficialSonarQubeCloudInstance.AU);
assertThat(OfficialSonarQubeCloudInstance.fromWebEndpoint("https://sonarqube.fr")).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void should_fail_if_invalid_region() {

assertThatThrownBy(() -> ScannerEndpointResolver.resolveEndpoint(props))
.isInstanceOf(MessageException.class)
.hasMessage("Invalid region 'fr'. Valid regions are: 'us'. Please check the 'sonar.region' property or the 'SONAR_REGION' environment variable.");
.hasMessage("Invalid region 'fr'. Valid regions are: 'us', 'au'. Please check the 'sonar.region' property or the 'SONAR_REGION' environment variable.");
}

@Test
Expand All @@ -197,6 +197,41 @@ void should_support_us_region() {
assertThat(endpoint.getApiEndpoint()).isEqualTo("https://api.sonarqube.us");
}

@Test
void should_support_au_region() {
var props = Map.of(ScannerProperties.SONAR_REGION, "au");

var endpoint = ScannerEndpointResolver.resolveEndpoint(props);

assertThat(endpoint.isSonarQubeCloud()).isTrue();
assertThat(endpoint.getWebEndpoint()).isEqualTo("https://sonarqube.au");
assertThat(endpoint.getApiEndpoint()).isEqualTo("https://api.sonarqube.au");
}

@ParameterizedTest
@ValueSource(strings = {"https://sonarqube.au", "https://sonarqube.au/", " https://sonarqube.au ", "https://www.sonarqube.au/"})
Comment thread
antoine-vinot-sonarsource marked this conversation as resolved.
Resolved
void should_recognize_sonarqube_cloud_au_endpoint_passed_through_host_url(String hostUrl) {
var props = Map.of(ScannerProperties.HOST_URL, hostUrl);

var endpoint = ScannerEndpointResolver.resolveEndpoint(props);

assertThat(endpoint.isSonarQubeCloud()).isTrue();
assertThat(endpoint.getWebEndpoint()).isEqualTo("https://sonarqube.au");
assertThat(endpoint.getApiEndpoint()).isEqualTo("https://api.sonarqube.au");
}

@Test
void should_fail_if_region_and_host_url_point_to_different_regions() {
var props = Map.of(
ScannerProperties.HOST_URL, "https://sonarqube.au",
ScannerProperties.SONAR_REGION, "us"
);

assertThatThrownBy(() -> ScannerEndpointResolver.resolveEndpoint(props))
.isInstanceOf(MessageException.class)
.hasMessage("Inconsistent values for properties 'sonar.region' and 'sonar.host.url'. Please only specify one of the two properties.");
}

@Test
void should_throw_if_custom_sq_cloud_url_is_set_but_not_api_url() {
var props = Map.of(ScannerProperties.SONARQUBE_CLOUD_URL, "https://preprod.sonarcloud.io");
Expand Down
Loading