Skip to content

Adding validation to Region class #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2017
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 @@ -19,6 +19,7 @@
import java.util.Collections;
import java.util.List;
import software.amazon.awssdk.utils.AbstractEnum;
import software.amazon.awssdk.utils.Validate;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.awssdk.core.regions;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

public class RegionTest {

@Test(expected = NullPointerException.class)
public void of_ThrowsNullPointerException_WhenNullValue() {
Region.of(null);
}

@Test(expected = IllegalArgumentException.class)
public void of_ThrowsIllegalArgumentException_WhenEmptyString() {
Region.of("");
}

@Test(expected = IllegalArgumentException.class)
public void of_ThrowsIllegalArgumentException_WhenBlankString() {
Region.of(" ");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy day test?


@Test
public void of_ReturnsRegion_WhenValidString() {
Region region = Region.of("us-east-1");
assertThat(region.value()).isEqualTo("us-east-1");
}
}
24 changes: 24 additions & 0 deletions utils/src/main/java/software/amazon/awssdk/utils/Validate.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,30 @@ public static <T> T paramNotNull(final T object, final String paramName) {
return object;
}

/**
* <p>Validate that the specified char sequence is neither
* {@code null}, a length of zero (no characters), empty nor
* whitespace; otherwise throwing an exception with the specified
* message.
*
* <pre>Validate.paramNotBlank(myCharSequence, "myCharSequence");</pre>
*
* @param <T> the char sequence type
* @param chars the character sequence to check
* @param paramName The name of the param or field being checked.
* @return the validated char sequence (never {@code null} for method chaining)
* @throws NullPointerException if the char sequence is {@code null}
*/
public static <T extends CharSequence> T paramNotBlank(final T chars, final String paramName) {
if (chars == null) {
throw new NullPointerException(String.format("%s must not be null.", paramName));
}
if (StringUtils.isBlank(chars)) {
throw new IllegalArgumentException(String.format("%s must not be blank or empty.", paramName));
}
return chars;
}

/**
* <p>Validate the stateful predicate is true for the given object and return the object;
* otherwise throw an exception with the specified message.</p>
Expand Down