Skip to content

Commit f29e0b0

Browse files
author
Willi Schönborn
authored
Merge pull request #144 from BenoitAverty/status-of-code
feat(Status): Add a method to create a Status instance from an integer
2 parents 1bab47c + 11773c3 commit f29e0b0

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

jackson-datatype-problem/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<developerConnection>scm:git:[email protected]:zalando/problem.git</developerConnection>
1616
</scm>
1717
<properties>
18-
<jackson.version>2.9.8</jackson.version>
18+
<jackson.version>2.9.9</jackson.version>
1919
</properties>
2020
<dependencies>
2121
<dependency>

problem/pom.xml

+6
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

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.apiguardian.api.API;
44

55
import javax.annotation.Nonnull;
6+
import java.util.Arrays;
67

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

@@ -268,6 +269,20 @@ public enum Status implements StatusType {
268269
this.reason = reasonPhrase;
269270
}
270271

272+
/**
273+
* Creates a Status instance from the given code.
274+
*
275+
* @param code the HTTP code as a number
276+
* @return the correct enum value for this status code.
277+
* @throws IllegalArgumentException if the given code does not correspond to a known HTTP status.
278+
*/
279+
public static Status ofCode(int code) {
280+
return Arrays.stream(Status.values())
281+
.filter(status -> status.getStatusCode() == code)
282+
.findFirst()
283+
.orElseThrow(() -> new IllegalArgumentException("There is no known status for this code (" + code + ")."));
284+
}
285+
271286
/**
272287
* Get the associated status code.
273288
*

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

+24
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)