Skip to content

Commit 77b8be4

Browse files
committed
Adding validation to Region class
1 parent 646939a commit 77b8be4

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

core/src/main/java/software/amazon/awssdk/core/regions/Region.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020
import java.util.List;
2121
import software.amazon.awssdk.utils.AbstractEnum;
22+
import software.amazon.awssdk.utils.Validate;
2223

2324
/**
2425
* An Amazon Web Services region that hosts a set of Amazon services.
@@ -120,10 +121,11 @@ private Region(String value) {
120121
* For example, the following conditions will always evaluated to true:
121122
* {@code Region.of("us-east-1") == Region.of("us-east-1")}, {@code Region.US_EAST_1 == Region.of("us-east-1")}.</p>
122123
*
123-
* @param value The name of the region to load.
124+
* @param value The name of the region to load. Can't be null, empty or blank
124125
* @return The region associated with the provided name.
125126
*/
126127
public static Region of(String value) {
128+
Validate.paramNotBlank(value, "region");
127129
return AbstractEnum.value(value, Region.class, Region::new);
128130
}
129131

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package software.amazon.awssdk.core.regions;
16+
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
19+
import org.junit.Test;
20+
21+
public class RegionTest {
22+
23+
@Test(expected = NullPointerException.class)
24+
public void of_ThrowsNullPointerException_WhenNullValue() {
25+
Region.of(null);
26+
}
27+
28+
@Test(expected = IllegalArgumentException.class)
29+
public void of_ThrowsIllegalArgumentException_WhenEmptyString() {
30+
Region.of("");
31+
}
32+
33+
@Test(expected = IllegalArgumentException.class)
34+
public void of_ThrowsIllegalArgumentException_WhenBlankString() {
35+
Region.of(" ");
36+
}
37+
38+
@Test
39+
public void of_ReturnsRegion_WhenValidString() {
40+
Region region = Region.of("us-east-1");
41+
assertThat(region.value()).isEqualTo("us-east-1");
42+
}
43+
}

utils/src/main/java/software/amazon/awssdk/utils/Validate.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,30 @@ public static <T> T paramNotNull(final T object, final String paramName) {
140140
return object;
141141
}
142142

143+
/**
144+
* <p>Validate that the specified char sequence is neither
145+
* {@code null}, a length of zero (no characters), empty nor
146+
* whitespace; otherwise throwing an exception with the specified
147+
* message.
148+
*
149+
* <pre>Validate.paramNotBlank(myCharSequence, "myCharSequence");</pre>
150+
*
151+
* @param <T> the char sequence type
152+
* @param chars the character sequence to check
153+
* @param paramName The name of the param or field being checked.
154+
* @return the validated char sequence (never {@code null} for method chaining)
155+
* @throws NullPointerException if the char sequence is {@code null}
156+
*/
157+
public static <T extends CharSequence> T paramNotBlank(final T chars, final String paramName) {
158+
if (chars == null) {
159+
throw new NullPointerException(String.format("%s must not be null.", paramName));
160+
}
161+
if (StringUtils.isBlank(chars)) {
162+
throw new IllegalArgumentException(String.format("%s must not be blank or empty.", paramName));
163+
}
164+
return chars;
165+
}
166+
143167
/**
144168
* <p>Validate the stateful predicate is true for the given object and return the object;
145169
* otherwise throw an exception with the specified message.</p>

0 commit comments

Comments
 (0)