|
| 1 | +/* |
| 2 | + * Copyright 2025, TeamDev. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Redistribution and use in source and/or binary forms, with or without |
| 11 | + * modification, must retain the above copyright notice and the following |
| 12 | + * disclaimer. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 15 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 16 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 17 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 18 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +package io.spine.test |
| 28 | + |
| 29 | +import io.kotest.matchers.collections.shouldHaveSize |
| 30 | +import io.kotest.matchers.maps.shouldContainExactly |
| 31 | +import io.kotest.matchers.shouldBe |
| 32 | +import io.spine.test.tools.validate.RequiredPlaceholders |
| 33 | +import io.spine.validate.ValidationException |
| 34 | +import org.junit.jupiter.api.DisplayName |
| 35 | +import org.junit.jupiter.api.Nested |
| 36 | +import org.junit.jupiter.api.Test |
| 37 | +import org.junit.jupiter.api.assertThrows |
| 38 | + |
| 39 | +@DisplayName("Error message placeholders should have values") |
| 40 | +internal class PlaceholdersSomeTest { |
| 41 | + |
| 42 | + @Nested |
| 43 | + inner class `for '(required)' option` { |
| 44 | + |
| 45 | + @Test |
| 46 | + fun `with the default error message`() { |
| 47 | + val exception = assertThrows<ValidationException> { |
| 48 | + RequiredPlaceholders.newBuilder() |
| 49 | + .setValueCustom("something") |
| 50 | + .build() |
| 51 | + } |
| 52 | + exception.constraintViolations shouldHaveSize 1 |
| 53 | + val template = exception.constraintViolations[0].message |
| 54 | + template.withPlaceholders shouldBe "The field `\${parent.type}.\${field.path}` of the type `\${field.type}` must have a value." |
| 55 | + template.placeholderValueMap shouldContainExactly mapOf( |
| 56 | + "field.path" to "value_default", |
| 57 | + "field.type" to "string", |
| 58 | + "parent.type" to "spine.test.tools.validate.RequiredPlaceholders" |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun `with the custom error message`() { |
| 64 | + val exception = assertThrows<ValidationException> { |
| 65 | + RequiredPlaceholders.newBuilder() |
| 66 | + .setValueDefault("something") |
| 67 | + .build() |
| 68 | + } |
| 69 | + exception.constraintViolations shouldHaveSize 1 |
| 70 | + val template = exception.constraintViolations[0].message |
| 71 | + template.withPlaceholders shouldBe "Custom for (required)." |
| 72 | + template.placeholderValueMap shouldContainExactly mapOf( |
| 73 | + "field.path" to "value_default", |
| 74 | + "field.type" to "string", |
| 75 | + "parent.type" to "spine.test.tools.validate.RequiredPlaceholders" |
| 76 | + ) |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments