Skip to content

Commit 0195fc0

Browse files
committed
feat(Status): Add a method to create a Status instance from an integer code
1 parent 1bab47c commit 0195fc0

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

problem/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
<version>${junit-jupiter.version}</version>
4444
<scope>test</scope>
4545
</dependency>
46+
<dependency>
47+
<groupId>org.junit.jupiter</groupId>
48+
<artifactId>junit-jupiter-params</artifactId>
49+
<version>${junit-jupiter.version}</version>
50+
<scope>test</scope>
51+
</dependency>
4652
<dependency>
4753
<groupId>org.hamcrest</groupId>
4854
<artifactId>java-hamcrest</artifactId>

problem/src/main/java/org/zalando/problem/Status.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import javax.annotation.Nonnull;
66

7+
import java.util.HashMap;
8+
import java.util.Map;
9+
710
import static org.apiguardian.api.API.Status.MAINTAINED;
811

912
/**
@@ -263,11 +266,36 @@ public enum Status implements StatusType {
263266
private final int code;
264267
private final String reason;
265268

269+
// Build a HashMap of all status keyed by their codes.
270+
// Used in the `ofCode` factory method
271+
private static Map<Integer, Status> allStatusByCode = new HashMap<>();
272+
static {
273+
for (Status s : Status.values()) {
274+
allStatusByCode.put(s.getStatusCode(), s);
275+
}
276+
}
277+
266278
Status(final int statusCode, final String reasonPhrase) {
267279
this.code = statusCode;
268280
this.reason = reasonPhrase;
269281
}
270282

283+
/**
284+
* Creates a Status instance from the given code.
285+
*
286+
* @param code the HTTP code as a number
287+
* @return the correct enum value for this status code.
288+
* @throws IllegalArgumentException if the given code does not correspond to a known HTTP status.
289+
*/
290+
public static Status ofCode(Integer code) {
291+
if (allStatusByCode.containsKey(code)) {
292+
return allStatusByCode.get(code);
293+
}
294+
else {
295+
throw new IllegalArgumentException("There is no known status for this code (" + code.toString() + ").");
296+
}
297+
}
298+
271299
/**
272300
* Get the associated status code.
273301
*

problem/src/test/java/org/zalando/problem/StatusTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.zalando.problem;
22

3+
import org.junit.jupiter.api.Assertions;
34
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
47

58
import java.util.stream.Stream;
69

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

2730
assertThat(notFound.toString(), equalTo("404 Not Found"));
2831
}
32+
33+
@ParameterizedTest
34+
@CsvSource({
35+
"409, Conflict",
36+
"404, Not Found",
37+
"200, OK",
38+
"500, Internal Server Error"
39+
})
40+
void shouldHaveCorrectValueFromCode(int code, String line) {
41+
Status statusFromCode = Status.ofCode(code);
42+
43+
assertThat(statusFromCode.getStatusCode(), equalTo(code));
44+
assertThat(statusFromCode.getReasonPhrase(), equalTo(line));
45+
}
46+
47+
@Test
48+
void shouldThrowOnNonExistingCode() {
49+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
50+
Status.ofCode(111);
51+
});
52+
}
2953
}

0 commit comments

Comments
 (0)