|
1 | 1 | package io.github.malczuuu.problem4j.core; |
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
4 | 5 |
|
5 | 6 | import java.net.URI; |
6 | 7 | import java.util.Arrays; |
@@ -33,6 +34,51 @@ void givenNullProblemStatus_shouldNotSetTitleOrStatus() { |
33 | 34 | assertThat(problem.getTitle()).isNull(); |
34 | 35 | } |
35 | 36 |
|
| 37 | + @Test |
| 38 | + void givenProblemStatus_shouldSetNumericStatusAndTitle() { |
| 39 | + Problem problem = Problem.builder().status(ProblemStatus.BAD_REQUEST).build(); |
| 40 | + |
| 41 | + assertThat(problem.getStatus()).isEqualTo(ProblemStatus.BAD_REQUEST.getStatus()); |
| 42 | + assertThat(problem.getTitle()).isEqualTo(ProblemStatus.BAD_REQUEST.getTitle()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void givenProblemStatus_shouldPreferExplicitStatusValueWhenSetEarlier() { |
| 47 | + Problem problem = Problem.builder().status(405).status(ProblemStatus.I_AM_A_TEAPOT).build(); |
| 48 | + |
| 49 | + assertThat(problem.getStatus()).isEqualTo(ProblemStatus.I_AM_A_TEAPOT.getStatus()); |
| 50 | + assertThat(problem.getTitle()).isEqualTo(ProblemStatus.I_AM_A_TEAPOT.getTitle()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void givenExplicitTitle_thenStatusProblemStatus_shouldNotOverrideTitle() { |
| 55 | + Problem problem = |
| 56 | + Problem.builder().title("Custom Title").status(ProblemStatus.BAD_REQUEST).build(); |
| 57 | + |
| 58 | + assertThat(problem.getStatus()).isEqualTo(ProblemStatus.BAD_REQUEST.getStatus()); |
| 59 | + assertThat(problem.getTitle()).isEqualTo("Custom Title"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void givenStatusProblemStatus_thenExplicitTitle_shouldOverrideDerivedTitle() { |
| 64 | + Problem problem = Problem.builder().status(ProblemStatus.NOT_FOUND).title("My Title").build(); |
| 65 | + |
| 66 | + assertThat(problem.getStatus()).isEqualTo(ProblemStatus.NOT_FOUND.getStatus()); |
| 67 | + assertThat(problem.getTitle()).isEqualTo("My Title"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void givenInvalidTypeString_shouldThrowIllegalArgumentException() { |
| 72 | + assertThatThrownBy(() -> Problem.builder().type("ht tp://not a uri")) |
| 73 | + .isInstanceOf(IllegalArgumentException.class); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void givenInvalidInstanceString_shouldThrowIllegalArgumentException() { |
| 78 | + assertThatThrownBy(() -> Problem.builder().instance("::://invalid")) |
| 79 | + .isInstanceOf(IllegalArgumentException.class); |
| 80 | + } |
| 81 | + |
36 | 82 | @Test |
37 | 83 | void givenNullURIInstance_shouldNotSetIt() { |
38 | 84 | Problem problem = Problem.builder().instance((URI) null).build(); |
@@ -107,4 +153,58 @@ void givenCollectionWithNullElement_shouldIgnoreNullElement() { |
107 | 153 |
|
108 | 154 | assertThat(problem.getExtensions()).containsExactlyInAnyOrder("x", "y"); |
109 | 155 | } |
| 156 | + |
| 157 | + @Test |
| 158 | + void numericStatus_shouldDeriveTitleWhenKnown() { |
| 159 | + Problem problem = Problem.builder().status(ProblemStatus.MULTI_STATUS.getStatus()).build(); |
| 160 | + |
| 161 | + assertThat(problem.getStatus()).isEqualTo(ProblemStatus.MULTI_STATUS.getStatus()); |
| 162 | + assertThat(problem.getTitle()).isEqualTo(ProblemStatus.MULTI_STATUS.getTitle()); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + void unknownNumericStatus_shouldNotDeriveTitle() { |
| 167 | + Problem problem = Problem.builder().status(999).build(); |
| 168 | + |
| 169 | + assertThat(problem.getStatus()).isEqualTo(999); |
| 170 | + assertThat(problem.getTitle()).isNull(); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + void typeAndInstance_stringOverloads_shouldAcceptValidUris() { |
| 175 | + String t = "http://example.org/type"; |
| 176 | + String i = "http://example.org/instance"; |
| 177 | + |
| 178 | + Problem problem = Problem.builder().type(t).instance(i).build(); |
| 179 | + |
| 180 | + assertThat(problem.getType()).isEqualTo(URI.create(t)); |
| 181 | + assertThat(problem.getInstance()).isEqualTo(URI.create(i)); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + void emptyMapExtension_shouldBeIgnored() { |
| 186 | + Map<String, Object> m = new HashMap<>(); |
| 187 | + Problem problem = Problem.builder().extension(m).build(); |
| 188 | + |
| 189 | + assertThat(problem.getExtensions()).isEmpty(); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + void nullValuedExtension_isPresentButOmittedFromToString() { |
| 194 | + Problem problem = Problem.builder().extension("ext", null).build(); |
| 195 | + |
| 196 | + assertThat(problem.getExtensions()).containsExactly("ext"); |
| 197 | + assertThat(problem.getExtensionValue("ext")).isNull(); |
| 198 | + |
| 199 | + String s = problem.toString(); |
| 200 | + assertThat(s).doesNotContain("\"ext\""); |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + void laterExtensions_shouldOverwriteEarlierValues() { |
| 205 | + Problem problem = |
| 206 | + Problem.builder().extension("k", "v1").extension(Problem.extension("k", "v2")).build(); |
| 207 | + |
| 208 | + assertThat(problem.getExtensionValue("k")).isEqualTo("v2"); |
| 209 | + } |
110 | 210 | } |
0 commit comments