|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat; |
4 | 4 |
|
| 5 | +import com.retailsvc.http.spec.schema.ArraySchema; |
| 6 | +import com.retailsvc.http.spec.schema.IntegerSchema; |
| 7 | +import com.retailsvc.http.spec.schema.ObjectSchema; |
| 8 | +import com.retailsvc.http.spec.schema.Schema; |
| 9 | +import com.retailsvc.http.spec.schema.StringSchema; |
| 10 | +import com.retailsvc.http.spec.schema.TypeName; |
5 | 11 | import java.nio.charset.StandardCharsets; |
6 | 12 | import java.util.List; |
7 | 13 | import java.util.Map; |
| 14 | +import java.util.Set; |
8 | 15 | import org.junit.jupiter.api.Test; |
9 | 16 |
|
10 | 17 | class FormUrlEncodedParserTest { |
@@ -70,4 +77,78 @@ void charsetFromHeader() { |
70 | 77 | assertThat(parser.parse(iso, "application/x-www-form-urlencoded; charset=iso-8859-1")) |
71 | 78 | .containsExactly(Map.entry("x", "räka")); |
72 | 79 | } |
| 80 | + |
| 81 | + @Test |
| 82 | + void coercesIntegerProperty() { |
| 83 | + IntegerSchema intSchema = anIntegerSchema(); |
| 84 | + ObjectSchema bodySchema = anObjectSchema(Map.of("age", intSchema)); |
| 85 | + |
| 86 | + Map<String, Object> out = |
| 87 | + parser.parseAndCoerce("age=30".getBytes(StandardCharsets.UTF_8), null, bodySchema); |
| 88 | + |
| 89 | + assertThat(out).containsExactly(Map.entry("age", 30L)); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void coercesArrayOfIntegersProperty() { |
| 94 | + IntegerSchema intItems = anIntegerSchema(); |
| 95 | + ArraySchema arrSchema = anArraySchemaOf(intItems); |
| 96 | + ObjectSchema bodySchema = anObjectSchema(Map.of("ids", arrSchema)); |
| 97 | + |
| 98 | + Map<String, Object> out = |
| 99 | + parser.parseAndCoerce("ids=1&ids=2".getBytes(StandardCharsets.UTF_8), null, bodySchema); |
| 100 | + |
| 101 | + assertThat(out).containsExactly(Map.entry("ids", List.of(1L, 2L))); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void coercionFailureThrowsValidationExceptionAtPropertyPointer() { |
| 106 | + IntegerSchema intSchema = anIntegerSchema(); |
| 107 | + ObjectSchema bodySchema = anObjectSchema(Map.of("age", intSchema)); |
| 108 | + |
| 109 | + org.assertj.core.api.Assertions.assertThatThrownBy( |
| 110 | + () -> |
| 111 | + parser.parseAndCoerce("age=abc".getBytes(StandardCharsets.UTF_8), null, bodySchema)) |
| 112 | + .isInstanceOf(com.retailsvc.http.ValidationException.class) |
| 113 | + .extracting("error.pointer", "error.keyword") |
| 114 | + .containsExactly("/age", "type"); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void unknownPropertyPassesThroughUnchanged() { |
| 119 | + ObjectSchema bodySchema = anObjectSchema(Map.of()); |
| 120 | + |
| 121 | + Map<String, Object> out = |
| 122 | + parser.parseAndCoerce("anything=v".getBytes(StandardCharsets.UTF_8), null, bodySchema); |
| 123 | + |
| 124 | + assertThat(out).containsExactly(Map.entry("anything", "v")); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void nonObjectSchemaReturnsRawMap() { |
| 129 | + StringSchema strSchema = aStringSchema(); |
| 130 | + |
| 131 | + Map<String, Object> out = |
| 132 | + parser.parseAndCoerce("a=1".getBytes(StandardCharsets.UTF_8), null, strSchema); |
| 133 | + |
| 134 | + assertThat(out).containsExactly(Map.entry("a", "1")); |
| 135 | + } |
| 136 | + |
| 137 | + private static IntegerSchema anIntegerSchema() { |
| 138 | + return new IntegerSchema( |
| 139 | + Set.of(TypeName.INTEGER), null, null, null, null, null, null, Map.of()); |
| 140 | + } |
| 141 | + |
| 142 | + private static StringSchema aStringSchema() { |
| 143 | + return new StringSchema(Set.of(TypeName.STRING), null, null, null, null, null, Map.of()); |
| 144 | + } |
| 145 | + |
| 146 | + private static ArraySchema anArraySchemaOf(Schema items) { |
| 147 | + return new ArraySchema(Set.of(TypeName.ARRAY), items, null, null, false, Map.of()); |
| 148 | + } |
| 149 | + |
| 150 | + private static ObjectSchema anObjectSchema(Map<String, Schema> properties) { |
| 151 | + return new ObjectSchema( |
| 152 | + Set.of(TypeName.OBJECT), properties, List.of(), null, null, null, Map.of()); |
| 153 | + } |
73 | 154 | } |
0 commit comments