|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2019, Leftshift One |
| 3 | + * __________________ |
| 4 | + * [2019] Leftshift One |
| 5 | + * All Rights Reserved. |
| 6 | + * NOTICE: All information contained herein is, and remains |
| 7 | + * the property of Leftshift One and its suppliers, |
| 8 | + * if any. The intellectual and technical concepts contained |
| 9 | + * herein are proprietary to Leftshift One |
| 10 | + * and its suppliers and may be covered by Patents, |
| 11 | + * patents in process, and are protected by trade secret or copyright law. |
| 12 | + * Dissemination of this information or reproduction of this material |
| 13 | + * is strictly forbidden unless prior written permission is obtained |
| 14 | + * from Leftshift One. |
| 15 | + */ |
| 16 | + |
| 17 | +package implicit.validation.validator |
| 18 | + |
| 19 | +import implicit.Implicit |
| 20 | +import implicit.annotation.validation.Max |
| 21 | +import implicit.annotation.validation.Min |
| 22 | +import implicit.exception.ImplicitValidationException |
| 23 | +import implicit.exception.ImplicitViolations |
| 24 | +import org.assertj.core.api.Assertions |
| 25 | +import org.assertj.core.api.Assertions.assertThat |
| 26 | +import org.junit.jupiter.api.Test |
| 27 | + |
| 28 | +class CombinedLimitsValidatorTest { |
| 29 | + |
| 30 | + |
| 31 | + @Test |
| 32 | + fun instantiatingAnEntityWhichFieldsDoNotMatchTheLimits() { |
| 33 | + val factory = Implicit { "${this.javaClass.name.toLowerCase()}.${it.simpleName}" } |
| 34 | + |
| 35 | + val map = mapOf<String, Any?>("floatValue" to 4.0f, "integerValue" to 0f) |
| 36 | + try { |
| 37 | + factory.instantiate(IPojo::class.java, map) |
| 38 | + Assertions.fail<String>("Validation must fail because values do not respect the limits") |
| 39 | + } catch (ex: ImplicitViolations) { |
| 40 | + assertThat(ex.violations).hasSize(2) |
| 41 | + assertThat(ex.violations.get(0)).extracting{ it.message}.isEqualTo("value of field 'setFloatValue' is > 3.5") |
| 42 | + assertThat(ex.violations.get(1)).extracting{ it.message}.isEqualTo("value of field 'setIntegerValue' is < 1.0") |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun settingAValueWhichDoesNotMatchTheLimits() { |
| 48 | + val factory = Implicit { "${this.javaClass.name.toLowerCase()}.${it.simpleName}" } |
| 49 | + val supplier = factory.getSupplier(IPojo::class.java) |
| 50 | + val pojo = supplier.get() |
| 51 | + |
| 52 | + try { |
| 53 | + pojo.setFloatValue(6f) |
| 54 | + Assertions.fail<String>("Validation must fail because values do not respect the limits") |
| 55 | + } catch (ex: ImplicitValidationException) { |
| 56 | + assertThat(ex.message).isEqualTo("value of field 'setFloatValue' is > 3.5") |
| 57 | + } |
| 58 | + try { |
| 59 | + pojo.setIntegerValue(10) |
| 60 | + Assertions.fail<String>("Validation must fail because values do not respect the limits") |
| 61 | + } catch (ex: ImplicitValidationException) { |
| 62 | + assertThat(ex.message).isEqualTo("value of field 'setIntegerValue' is > 4.0") |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + try { |
| 67 | + pojo.setFloatValue(1f) |
| 68 | + Assertions.fail<String>("Validation must fail because values do not respect the limits") |
| 69 | + } catch (ex: ImplicitValidationException) { |
| 70 | + assertThat(ex.message).isEqualTo("value of field 'setFloatValue' is < 1.5") |
| 71 | + } |
| 72 | + try { |
| 73 | + pojo.setIntegerValue(0) |
| 74 | + Assertions.fail<String>("Validation must fail because values do not respect the limits") |
| 75 | + } catch (ex: ImplicitValidationException) { |
| 76 | + assertThat(ex.message).isEqualTo("value of field 'setIntegerValue' is < 1.0") |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + interface IPojo { |
| 83 | + |
| 84 | + fun getFloatValue() : Float |
| 85 | + fun setFloatValue(@Min(1.5f) @Max(3.5f) number:Float) |
| 86 | + fun getIntegerValue() : Int |
| 87 | + fun setIntegerValue(@Min(1f) @Max(4f) number:Int) |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments