Skip to content
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

feat(Status): Add a method to create a Status instance from an integer #144

Merged
merged 1 commit into from
May 27, 2019
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
2 changes: 1 addition & 1 deletion jackson-datatype-problem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<developerConnection>scm:git:[email protected]:zalando/problem.git</developerConnection>
</scm>
<properties>
<jackson.version>2.9.8</jackson.version>
<jackson.version>2.9.9</jackson.version>
</properties>
<dependencies>
<dependency>
Expand Down
6 changes: 6 additions & 0 deletions problem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
Expand Down
15 changes: 15 additions & 0 deletions problem/src/main/java/org/zalando/problem/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.apiguardian.api.API;

import javax.annotation.Nonnull;
import java.util.Arrays;

import static org.apiguardian.api.API.Status.MAINTAINED;

Expand Down Expand Up @@ -268,6 +269,20 @@ public enum Status implements StatusType {
this.reason = reasonPhrase;
}

/**
* Creates a Status instance from the given code.
*
* @param code the HTTP code as a number
* @return the correct enum value for this status code.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should mention a @throws section, imo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I'll add it.

* @throws IllegalArgumentException if the given code does not correspond to a known HTTP status.
*/
public static Status ofCode(int code) {
return Arrays.stream(Status.values())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Personally I'm not a big fan of linear search. What about constructing a map in a static initializer which we just use here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not used to do that but I can see why it'd be better in most cases. I'll make the change.

.filter(status -> status.getStatusCode() == code)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("There is no known status for this code (" + code + ")."));
}

/**
* Get the associated status code.
*
Expand Down
24 changes: 24 additions & 0 deletions problem/src/test/java/org/zalando/problem/StatusTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.zalando.problem;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.util.stream.Stream;

Expand All @@ -26,4 +29,25 @@ void shouldHaveMeaningfulToString() {

assertThat(notFound.toString(), equalTo("404 Not Found"));
}

@ParameterizedTest
@CsvSource({
"409, Conflict",
"404, Not Found",
"200, OK",
"500, Internal Server Error"
})
void shouldHaveCorrectValueFromCode(int code, String line) {
Status statusFromCode = Status.ofCode(code);

assertThat(statusFromCode.getStatusCode(), equalTo(code));
assertThat(statusFromCode.getReasonPhrase(), equalTo(line));
}

@Test
void shouldThrowOnNonExistingCode() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
Status.ofCode(111);
});
}
}