diff --git a/java-tests/runtime/src/test/kotlin/io/spine/validate/Assertions.kt b/java-tests/runtime/src/test/kotlin/io/spine/validate/Assertions.kt deleted file mode 100644 index 2e9395db..00000000 --- a/java-tests/runtime/src/test/kotlin/io/spine/validate/Assertions.kt +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2024, TeamDev. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Redistribution and use in source and/or binary forms, with or without - * modification, must retain the above copyright notice and the following - * disclaimer. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package io.spine.validate - -import io.kotest.matchers.collections.shouldContainExactly - -/** - * Asserts that the field names in the given [violation] are exactly as listed by [expectedFields]. - */ -fun assertFieldPathIs(violation: ConstraintViolation, vararg expectedFields: String) { - val path = violation.fieldPath - val actualFields = path.fieldNameList - actualFields.toList() shouldContainExactly expectedFields.asList() -} diff --git a/java-tests/runtime/src/test/kotlin/io/spine/validate/EnclosedMessageValidationSpec.kt b/java-tests/runtime/src/test/kotlin/io/spine/validate/EnclosedMessageValidationSpec.kt index 343d40f1..42834657 100644 --- a/java-tests/runtime/src/test/kotlin/io/spine/validate/EnclosedMessageValidationSpec.kt +++ b/java-tests/runtime/src/test/kotlin/io/spine/validate/EnclosedMessageValidationSpec.kt @@ -99,22 +99,15 @@ internal class EnclosedMessageValidationSpec : ValidationOfConstraintTest() { val violation = singleViolation() violation.message.formatUnsafe() shouldContain "is invalid" - assertFieldPathIs( - violation, - ENCLOSED_FIELD_NAME - ) + violation.fieldName shouldBe ENCLOSED_FIELD_NAME val innerViolations = violation.violationList innerViolations shouldHaveSize 1 - val innerViolation = innerViolations[0] - innerViolation.message.format() shouldStartWith Diags.Regex.prefix - - assertFieldPathIs( - innerViolation, - EMAIL - ) - - innerViolation.violationList.shouldBeEmpty() + with(innerViolations.first()) { + message.format() shouldStartWith Diags.Regex.prefix + fieldName shouldBe EMAIL + violationList.shouldBeEmpty() + } } @Test diff --git a/java-tests/runtime/src/test/kotlin/io/spine/validate/EntityIdSpec.kt b/java-tests/runtime/src/test/kotlin/io/spine/validate/EntityIdSpec.kt index 5124b3f2..bfe8fc57 100644 --- a/java-tests/runtime/src/test/kotlin/io/spine/validate/EntityIdSpec.kt +++ b/java-tests/runtime/src/test/kotlin/io/spine/validate/EntityIdSpec.kt @@ -44,72 +44,10 @@ import org.junit.jupiter.api.assertDoesNotThrow @DisplayName(VALIDATION_SHOULD + "validate an entity ID") internal class EntityIdSpec : ValidationOfConstraintTest() { - @Nested inner class - `in a command file and` { - - @Test - fun `find out that non-default message is valid`() { - val state = assertDoesNotThrow { - entityIdMsgFieldValue { - value = newStringValue() - } - } - assertValid(state) - } - - @Test - fun `find out that default message is NOT valid`() { - val msg = EntityIdMsgFieldValue.getDefaultInstance() - assertNotValid(msg) - } - @Test fun `find out that empty string is NOT valid`() { val msg = EntityIdStringFieldValue.getDefaultInstance() assertNotValid(msg) } - @Test - fun `find out that non-empty string is valid`() { - val state = assertDoesNotThrow { - entityIdStringFieldValue { - value = Identifier.newUuid() - } - } - assertValid(state) - } - - @Test - fun `provide a violation if is not valid`() { - val msg = EntityIdMsgFieldValue.getDefaultInstance() - assertSingleViolation(msg, MessageValidatorTestEnv.VALUE) - } - } - - @Nested inner class - `in a state and` { - - @Test - fun `consider it required by default`() { - val stateWithDefaultId = AggregateState.getDefaultInstance() - assertNotValid(stateWithDefaultId) - } - - @Test - fun `match only the first field named 'id' or ending with '_id'`() { - val onlyEntityIdSet = assertDoesNotThrow { - // Only ID set. - aggregateState { - entityId = Identifier.newUuid() - } - } - assertValid(onlyEntityIdSet) - } - - @Test - fun `not consider it '(required)' if the option is set explicitly to false`() { - val stateWithDefaultId = ProjectionState.getDefaultInstance() - assertValid(stateWithDefaultId) - } - } } diff --git a/java-tests/runtime/src/test/kotlin/io/spine/validate/ValidationOfConstraintTest.kt b/java-tests/runtime/src/test/kotlin/io/spine/validate/ValidationOfConstraintTest.kt index ed6abbb5..0a41db93 100644 --- a/java-tests/runtime/src/test/kotlin/io/spine/validate/ValidationOfConstraintTest.kt +++ b/java-tests/runtime/src/test/kotlin/io/spine/validate/ValidationOfConstraintTest.kt @@ -31,9 +31,7 @@ import com.google.protobuf.Message import io.kotest.matchers.collections.shouldBeEmpty import io.kotest.matchers.collections.shouldHaveSize import io.kotest.matchers.shouldBe -import io.kotest.matchers.string.shouldContain import io.kotest.matchers.string.shouldNotBeEmpty -import io.kotest.matchers.string.shouldNotContain import io.spine.validate.Validate.violationsOf import org.junit.jupiter.api.Assertions.assertDoesNotThrow import org.junit.jupiter.api.assertThrows @@ -96,17 +94,17 @@ abstract class ValidationOfConstraintTest { assertIsValid(false) } - protected fun assertNotValid(msg: Message, checkFieldPath: Boolean) { + protected fun assertNotValid(msg: Message, checkFieldName: Boolean) { validate(msg) - assertIsValid(false, checkFieldPath) + assertIsValid(false, checkFieldName) } @JvmOverloads - protected fun assertIsValid(isValid: Boolean, checkFieldPath: Boolean = true) { + protected fun assertIsValid(isValid: Boolean, checkFieldName: Boolean = true) { if (isValid) { assertThat(violations).isEmpty() } else { - assertViolations(violations, checkFieldPath) + assertViolations(violations, checkFieldName) } } @@ -125,14 +123,14 @@ abstract class ValidationOfConstraintTest { val violation = firstViolation() val actualErrorMessage = violation.message.format() actualErrorMessage shouldBe expectedErrMsg - assertFieldPathIs(violation, invalidFieldName) + violation.fieldName shouldBe invalidFieldName violation.violationList.shouldBeEmpty() } protected fun assertSingleViolation(message: Message, invalidFieldName: String) { assertNotValid(message) assertThat(violations).hasSize(1) - assertFieldPathIs(firstViolation(), invalidFieldName) + firstViolation().fieldName shouldBe invalidFieldName } companion object { @@ -140,27 +138,25 @@ abstract class ValidationOfConstraintTest { const val VALIDATION_SHOULD: String = "Validation should " private fun assertViolations( - violations: List?, - checkFieldPath: Boolean + violations: List?, + checkFieldName: Boolean ) { assertThat(violations) .isNotEmpty() for (violation in violations!!) { assertHasCorrectFormat(violation) - if (checkFieldPath) { - assertHasFieldPath(violation) + if (checkFieldName) { + if (violation.fieldName.isEmpty()) { + println(violation) + } + violation.fieldName.shouldNotBeEmpty() } } } - private fun assertHasCorrectFormat(violation: ConstraintViolation?) { - val template = violation!!.message.withPlaceholders + private fun assertHasCorrectFormat(violation: ConstraintViolation) { + val template = violation.message.withPlaceholders template.shouldNotBeEmpty() } - - private fun assertHasFieldPath(violation: ConstraintViolation?) { - assertThat(violation!!.fieldPath.fieldNameList) - .isNotEmpty() - } } } diff --git a/java-tests/validating/src/test/kotlin/io/spine/test/options/DistinctITest.kt b/java-tests/validating/src/test/kotlin/io/spine/test/options/DistinctITest.kt index 41685cf5..762fc5d2 100644 --- a/java-tests/validating/src/test/kotlin/io/spine/test/options/DistinctITest.kt +++ b/java-tests/validating/src/test/kotlin/io/spine/test/options/DistinctITest.kt @@ -29,7 +29,6 @@ package io.spine.test.options import com.google.common.truth.extensions.proto.ProtoTruth.assertThat import io.kotest.matchers.optional.shouldBeEmpty import io.kotest.matchers.optional.shouldBePresent -import io.spine.base.fieldPath import io.spine.protobuf.TypeConverter.toAny import io.spine.test.tools.validate.ProtoSet import io.spine.test.tools.validate.protoSet @@ -54,7 +53,7 @@ internal class DistinctITest { val violations = error.get().constraintViolationList val expected = constraintViolation { - fieldPath = fieldPath { fieldName.add("element") } + fieldName = "element" } assertThat(violations) diff --git a/java-tests/validating/src/test/kotlin/io/spine/test/options/ExternalConstraintITest.kt b/java-tests/validating/src/test/kotlin/io/spine/test/options/ExternalConstraintITest.kt index c37148c1..a5390e5c 100644 --- a/java-tests/validating/src/test/kotlin/io/spine/test/options/ExternalConstraintITest.kt +++ b/java-tests/validating/src/test/kotlin/io/spine/test/options/ExternalConstraintITest.kt @@ -71,7 +71,7 @@ internal class ExternalConstraintITest { val violations = error.get().constraintViolationList violations.size shouldBe 1 - violations[0].fieldPath.fieldNameList[0] shouldBe "shipping_address" + violations[0].fieldName shouldBe "shipping_address" } @Test diff --git a/java-tests/validating/src/test/kotlin/io/spine/test/options/ValidateConstraintTest.kt b/java-tests/validating/src/test/kotlin/io/spine/test/options/ValidateConstraintTest.kt index 2ecba766..399b6b86 100644 --- a/java-tests/validating/src/test/kotlin/io/spine/test/options/ValidateConstraintTest.kt +++ b/java-tests/validating/src/test/kotlin/io/spine/test/options/ValidateConstraintTest.kt @@ -68,13 +68,13 @@ internal class ValidateConstraintTest { // See https://github.com/SpineEventEngine/validation/issues/148 violations.size shouldBe 1 - val receiverViolation = violations[0] - receiverViolation!!.fieldPath.getFieldName(0) shouldBe "address" + val receiverViolation = violations.first() + receiverViolation.fieldName shouldBe "address" val nestedViolations = receiverViolation.violationList nestedViolations.size shouldBe 2 - nestedViolations[0].fieldPath.getFieldName(0) shouldBe "first_line" - nestedViolations[1].fieldPath.getFieldName(0) shouldBe "town" + nestedViolations[0].fieldName shouldBe "first_line" + nestedViolations[1].fieldName shouldBe "town" } @Test @@ -108,7 +108,7 @@ internal class ValidateConstraintTest { for (violation in violations) { violation.violationList.size shouldBe 1 - violation.fieldPath.getFieldName(0) shouldBe "contact" + violation.fieldName shouldBe "contact" } } diff --git a/java-tests/validating/src/testFixtures/kotlin/io/spine/validation/assertions/Assertions.kt b/java-tests/validating/src/testFixtures/kotlin/io/spine/validation/assertions/Assertions.kt index 939cbdde..ce78d5bc 100644 --- a/java-tests/validating/src/testFixtures/kotlin/io/spine/validation/assertions/Assertions.kt +++ b/java-tests/validating/src/testFixtures/kotlin/io/spine/validation/assertions/Assertions.kt @@ -71,7 +71,7 @@ fun assertInvalid(builder: Message.Builder): List { private val fieldName: Correspondence = Correspondence.transforming( - { it.fieldPath.getFieldName(0) }, + { it.fieldName }, "field name" ) @@ -97,7 +97,7 @@ fun assertViolation( * Obtains a violation for the field with the given name. */ fun List.atField(fieldName: String): ConstraintViolation { - return find { it.fieldPath.fieldNameList[0] == fieldName } + return find { it.fieldName == fieldName } ?: fail("Cannot find a violation for the field `$fieldName`. Violations: `$this`.") } diff --git a/java-tests/vanilla/src/test/kotlin/io/spine/validation/java/GoesConstraintSpec.kt b/java-tests/vanilla/src/test/kotlin/io/spine/validation/java/GoesConstraintSpec.kt index 87a31b31..06885731 100644 --- a/java-tests/vanilla/src/test/kotlin/io/spine/validation/java/GoesConstraintSpec.kt +++ b/java-tests/vanilla/src/test/kotlin/io/spine/validation/java/GoesConstraintSpec.kt @@ -30,7 +30,6 @@ import com.google.common.truth.Truth8.assertThat import com.google.common.truth.extensions.proto.ProtoTruth.assertThat import io.kotest.matchers.collections.shouldHaveSize import io.kotest.matchers.optional.shouldBePresent -import io.spine.base.FieldPath import io.spine.base.Identifier import io.spine.base.Time.currentTime import io.spine.type.TypeName @@ -65,10 +64,7 @@ internal class GoesConstraintSpec { .isEqualTo( ConstraintViolation.newBuilder() .setTypeName(TypeName.of(paper).toUrl().value()) - .setFieldPath( - FieldPath.newBuilder() - .addFieldName("when_archived") - ) + .setFieldName("when_archived") .build() ) }