Skip to content

Commit 34fdaf3

Browse files
authored
basic validation on the preferred regions. (Azure#17176)
* basic preferred regions validation * add missing test file
1 parent 1d4f79d commit 34fdaf3

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/CosmosClientBuilder.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99
import com.azure.cosmos.implementation.ConnectionPolicy;
1010
import com.azure.cosmos.implementation.CosmosAuthorizationTokenResolver;
1111
import com.azure.cosmos.implementation.apachecommons.lang.StringUtils;
12+
import com.azure.cosmos.implementation.guava25.base.Preconditions;
13+
import com.azure.cosmos.implementation.routing.LocationHelper;
1214
import com.azure.cosmos.models.CosmosPermissionProperties;
1315

16+
import java.net.URI;
17+
import java.net.URISyntaxException;
1418
import java.util.Collections;
1519
import java.util.List;
20+
import java.util.Locale;
1621
import java.util.Objects;
1722

1823
/**
@@ -736,6 +741,24 @@ private void buildConnectionPolicy() {
736741
}
737742

738743
private void validateConfig() {
744+
URI uri;
745+
try {
746+
uri = new URI(serviceEndpoint);
747+
} catch (URISyntaxException e) {
748+
throw new IllegalArgumentException("invalid serviceEndpoint", e);
749+
}
750+
751+
if (preferredRegions != null) {
752+
// validate preferredRegions
753+
preferredRegions.stream().forEach(
754+
preferredRegion -> {
755+
Preconditions.checkArgument(StringUtils.trimToNull(preferredRegion) != null, "preferredRegion can't be empty");
756+
String trimmedPreferredRegion = preferredRegion.toLowerCase(Locale.ROOT).replace(" ", "");
757+
LocationHelper.getLocationEndpoint(uri, trimmedPreferredRegion);
758+
}
759+
);
760+
}
761+
739762
ifThrowIllegalArgException(this.serviceEndpoint == null,
740763
"cannot buildAsyncClient client without service endpoint");
741764
ifThrowIllegalArgException(

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/LocationHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.azure.cosmos.implementation.routing;
55

66
import com.azure.cosmos.implementation.apachecommons.lang.StringUtils;
7+
import com.azure.cosmos.implementation.guava27.Strings;
78

89
import java.net.URI;
910

@@ -40,7 +41,7 @@ public static URI getLocationEndpoint(URI serviceEndpoint, String location) {
4041
serviceEndpoint.getPath(), serviceEndpoint.getQuery(),
4142
serviceEndpoint.getFragment());
4243
} catch (Exception e) {
43-
throw new RuntimeException(e);
44+
throw new RuntimeException(Strings.lenientFormat("invalid location [%s] or serviceEndpoint [%s]", location, serviceEndpoint.toString()), e);
4445
}
4546
}
4647

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package com.azure.cosmos;
4+
5+
import com.azure.cosmos.implementation.TestConfigurations;
6+
import com.google.common.collect.ImmutableList;
7+
import org.testng.annotations.Test;
8+
9+
import java.net.URISyntaxException;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
public class CosmosClientBuilderTest {
14+
15+
String hostName = "https://sample-account.documents.azure.com:443/";
16+
17+
@Test(groups = "unit")
18+
public void validateBadPreferredRegions1() {
19+
try {
20+
CosmosAsyncClient client = new CosmosClientBuilder()
21+
.key(TestConfigurations.MASTER_KEY)
22+
.endpoint(hostName)
23+
.preferredRegions(ImmutableList.of("westus1,eastus1"))
24+
.buildAsyncClient();
25+
client.close();
26+
} catch (Exception e) {
27+
assertThat(e).isInstanceOf(RuntimeException.class);
28+
assertThat(e).hasCauseExactlyInstanceOf(URISyntaxException.class);
29+
assertThat(e.getMessage()).isEqualTo("invalid location [westus1,eastus1] or serviceEndpoint [https://sample-account.documents.azure.com:443/]");
30+
}
31+
}
32+
33+
@Test(groups = "unit")
34+
public void validateBadPreferredRegions2() {
35+
try {
36+
CosmosAsyncClient client = new CosmosClientBuilder()
37+
.key(TestConfigurations.MASTER_KEY)
38+
.endpoint(hostName)
39+
.preferredRegions(ImmutableList.of(" "))
40+
.buildAsyncClient();
41+
client.close();
42+
} catch (Exception e) {
43+
assertThat(e).isInstanceOf(RuntimeException.class);
44+
assertThat(e.getMessage()).isEqualTo("preferredRegion can't be empty");
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)