|
| 1 | +package com.retailsvc.http.internal; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 5 | + |
| 6 | +import com.retailsvc.http.ValidationException; |
| 7 | +import com.retailsvc.http.spec.schema.BooleanSchema; |
| 8 | +import com.retailsvc.http.spec.schema.IntegerSchema; |
| 9 | +import com.retailsvc.http.spec.schema.NumberSchema; |
| 10 | +import com.retailsvc.http.spec.schema.Schema; |
| 11 | +import com.retailsvc.http.spec.schema.StringSchema; |
| 12 | +import com.retailsvc.http.spec.schema.TypeName; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Set; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +class ValueCoercionTest { |
| 18 | + |
| 19 | + private final Schema intSchema = anIntegerSchema(); |
| 20 | + private final Schema numSchema = aNumberSchema(); |
| 21 | + private final Schema boolSchema = aBooleanSchema(); |
| 22 | + private final Schema strSchema = aStringSchema(); |
| 23 | + |
| 24 | + @Test |
| 25 | + void coercesIntegerString() { |
| 26 | + assertThat(ValueCoercion.coerce("42", intSchema, "/a")).isEqualTo(42L); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void coercesNumberString() { |
| 31 | + assertThat(ValueCoercion.coerce("3.14", numSchema, "/a")).isEqualTo(3.14); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void coercesBooleanTrue() { |
| 36 | + assertThat(ValueCoercion.coerce("true", boolSchema, "/a")).isEqualTo(Boolean.TRUE); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void coercesBooleanFalse() { |
| 41 | + assertThat(ValueCoercion.coerce("false", boolSchema, "/a")).isEqualTo(Boolean.FALSE); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void leavesStringSchemaUntouched() { |
| 46 | + assertThat(ValueCoercion.coerce("hello", strSchema, "/a")).isEqualTo("hello"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void integerCoercionFailureThrowsValidationException() { |
| 51 | + assertThatThrownBy(() -> ValueCoercion.coerce("abc", intSchema, "/a")) |
| 52 | + .isInstanceOf(ValidationException.class) |
| 53 | + .extracting("error.pointer", "error.keyword") |
| 54 | + .containsExactly("/a", "type"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void numberCoercionFailureThrowsValidationException() { |
| 59 | + assertThatThrownBy(() -> ValueCoercion.coerce("not-a-number", numSchema, "/x")) |
| 60 | + .isInstanceOf(ValidationException.class); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void booleanCoercionFailureThrowsValidationException() { |
| 65 | + assertThatThrownBy(() -> ValueCoercion.coerce("yes", boolSchema, "/b")) |
| 66 | + .isInstanceOf(ValidationException.class); |
| 67 | + } |
| 68 | + |
| 69 | + private static IntegerSchema anIntegerSchema() { |
| 70 | + return new IntegerSchema( |
| 71 | + Set.of(TypeName.INTEGER), null, null, null, null, null, null, Map.of()); |
| 72 | + } |
| 73 | + |
| 74 | + private static NumberSchema aNumberSchema() { |
| 75 | + return new NumberSchema(Set.of(TypeName.NUMBER), null, null, null, null, null, null, Map.of()); |
| 76 | + } |
| 77 | + |
| 78 | + private static BooleanSchema aBooleanSchema() { |
| 79 | + return new BooleanSchema(Set.of(TypeName.BOOLEAN), Map.of()); |
| 80 | + } |
| 81 | + |
| 82 | + private static StringSchema aStringSchema() { |
| 83 | + return new StringSchema(Set.of(TypeName.STRING), null, null, null, null, null, Map.of()); |
| 84 | + } |
| 85 | +} |
0 commit comments