File tree Expand file tree Collapse file tree 3 files changed +58
-0
lines changed
main/java/org/zalando/problem
test/java/org/zalando/problem Expand file tree Collapse file tree 3 files changed +58
-0
lines changed 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 4
4
5
5
import javax .annotation .Nonnull ;
6
6
7
+ import java .util .HashMap ;
8
+ import java .util .Map ;
9
+
7
10
import static org .apiguardian .api .API .Status .MAINTAINED ;
8
11
9
12
/**
@@ -263,11 +266,36 @@ public enum Status implements StatusType {
263
266
private final int code ;
264
267
private final String reason ;
265
268
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
+
266
278
Status (final int statusCode , final String reasonPhrase ) {
267
279
this .code = statusCode ;
268
280
this .reason = reasonPhrase ;
269
281
}
270
282
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
+
271
299
/**
272
300
* Get the associated status code.
273
301
*
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