File tree 4 files changed +46
-1
lines changed
main/java/org/zalando/problem
test/java/org/zalando/problem
4 files changed +46
-1
lines changed Original file line number Diff line number Diff line change 15
15
<
developerConnection >scm:git:
[email protected] :zalando/problem.git</
developerConnection >
16
16
</scm >
17
17
<properties >
18
- <jackson .version>2.9.8 </jackson .version>
18
+ <jackson .version>2.9.9 </jackson .version>
19
19
</properties >
20
20
<dependencies >
21
21
<dependency >
Original file line number Diff line number Diff line change 43
43
<version >${junit-jupiter.version} </version >
44
44
<scope >test</scope >
45
45
</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 >
46
52
<dependency >
47
53
<groupId >org.hamcrest</groupId >
48
54
<artifactId >java-hamcrest</artifactId >
Original file line number Diff line number Diff line change 3
3
import org .apiguardian .api .API ;
4
4
5
5
import javax .annotation .Nonnull ;
6
+ import java .util .Arrays ;
6
7
7
8
import static org .apiguardian .api .API .Status .MAINTAINED ;
8
9
@@ -268,6 +269,20 @@ public enum Status implements StatusType {
268
269
this .reason = reasonPhrase ;
269
270
}
270
271
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
+
271
286
/**
272
287
* Get the associated status code.
273
288
*
Original file line number Diff line number Diff line change 1
1
package org .zalando .problem ;
2
2
3
+ import org .junit .jupiter .api .Assertions ;
3
4
import org .junit .jupiter .api .Test ;
5
+ import org .junit .jupiter .params .ParameterizedTest ;
6
+ import org .junit .jupiter .params .provider .CsvSource ;
4
7
5
8
import java .util .stream .Stream ;
6
9
@@ -26,4 +29,25 @@ void shouldHaveMeaningfulToString() {
26
29
27
30
assertThat (notFound .toString (), equalTo ("404 Not Found" ));
28
31
}
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
+ }
29
53
}
You can’t perform that action at this time.
0 commit comments