Skip to content

Commit

Permalink
feat(Status): Add a method to create a Status instance from an intege…
Browse files Browse the repository at this point in the history
…r code
  • Loading branch information
BenoitAverty committed May 27, 2019
1 parent 1bab47c commit 0195fc0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
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
28 changes: 28 additions & 0 deletions problem/src/main/java/org/zalando/problem/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import javax.annotation.Nonnull;

import java.util.HashMap;
import java.util.Map;

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

/**
Expand Down Expand Up @@ -263,11 +266,36 @@ public enum Status implements StatusType {
private final int code;
private final String reason;

// Build a HashMap of all status keyed by their codes.
// Used in the `ofCode` factory method
private static Map<Integer, Status> allStatusByCode = new HashMap<>();
static {
for (Status s : Status.values()) {
allStatusByCode.put(s.getStatusCode(), s);
}
}

Status(final int statusCode, final String reasonPhrase) {
this.code = statusCode;
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.
* @throws IllegalArgumentException if the given code does not correspond to a known HTTP status.
*/
public static Status ofCode(Integer code) {
if (allStatusByCode.containsKey(code)) {
return allStatusByCode.get(code);
}
else {
throw new IllegalArgumentException("There is no known status for this code (" + code.toString() + ").");
}
}

/**
* 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);
});
}
}

0 comments on commit 0195fc0

Please sign in to comment.