Skip to content

Commit a4eacf0

Browse files
author
yevhenii-nadtochii
committed
Adapt more tests
1 parent 1b3a5d9 commit a4eacf0

File tree

9 files changed

+31
-147
lines changed

9 files changed

+31
-147
lines changed

java-tests/runtime/src/test/kotlin/io/spine/validate/Assertions.kt

Lines changed: 0 additions & 38 deletions
This file was deleted.

java-tests/runtime/src/test/kotlin/io/spine/validate/EnclosedMessageValidationSpec.kt

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,15 @@ internal class EnclosedMessageValidationSpec : ValidationOfConstraintTest() {
9999

100100
val violation = singleViolation()
101101
violation.message.formatUnsafe() shouldContain "is invalid"
102-
assertFieldPathIs(
103-
violation,
104-
ENCLOSED_FIELD_NAME
105-
)
102+
violation.fieldName shouldBe ENCLOSED_FIELD_NAME
106103

107104
val innerViolations = violation.violationList
108105
innerViolations shouldHaveSize 1
109-
val innerViolation = innerViolations[0]
110-
innerViolation.message.format() shouldStartWith Diags.Regex.prefix
111-
112-
assertFieldPathIs(
113-
innerViolation,
114-
EMAIL
115-
)
116-
117-
innerViolation.violationList.shouldBeEmpty()
106+
with(innerViolations.first()) {
107+
message.format() shouldStartWith Diags.Regex.prefix
108+
fieldName shouldBe EMAIL
109+
violationList.shouldBeEmpty()
110+
}
118111
}
119112

120113
@Test

java-tests/runtime/src/test/kotlin/io/spine/validate/EntityIdSpec.kt

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -44,72 +44,10 @@ import org.junit.jupiter.api.assertDoesNotThrow
4444
@DisplayName(VALIDATION_SHOULD + "validate an entity ID")
4545
internal class EntityIdSpec : ValidationOfConstraintTest() {
4646

47-
@Nested inner class
48-
`in a command file and` {
49-
50-
@Test
51-
fun `find out that non-default message is valid`() {
52-
val state = assertDoesNotThrow {
53-
entityIdMsgFieldValue {
54-
value = newStringValue()
55-
}
56-
}
57-
assertValid(state)
58-
}
59-
60-
@Test
61-
fun `find out that default message is NOT valid`() {
62-
val msg = EntityIdMsgFieldValue.getDefaultInstance()
63-
assertNotValid(msg)
64-
}
65-
6647
@Test
6748
fun `find out that empty string is NOT valid`() {
6849
val msg = EntityIdStringFieldValue.getDefaultInstance()
6950
assertNotValid(msg)
7051
}
7152

72-
@Test
73-
fun `find out that non-empty string is valid`() {
74-
val state = assertDoesNotThrow {
75-
entityIdStringFieldValue {
76-
value = Identifier.newUuid()
77-
}
78-
}
79-
assertValid(state)
80-
}
81-
82-
@Test
83-
fun `provide a violation if is not valid`() {
84-
val msg = EntityIdMsgFieldValue.getDefaultInstance()
85-
assertSingleViolation(msg, MessageValidatorTestEnv.VALUE)
86-
}
87-
}
88-
89-
@Nested inner class
90-
`in a state and` {
91-
92-
@Test
93-
fun `consider it required by default`() {
94-
val stateWithDefaultId = AggregateState.getDefaultInstance()
95-
assertNotValid(stateWithDefaultId)
96-
}
97-
98-
@Test
99-
fun `match only the first field named 'id' or ending with '_id'`() {
100-
val onlyEntityIdSet = assertDoesNotThrow {
101-
// Only ID set.
102-
aggregateState {
103-
entityId = Identifier.newUuid()
104-
}
105-
}
106-
assertValid(onlyEntityIdSet)
107-
}
108-
109-
@Test
110-
fun `not consider it '(required)' if the option is set explicitly to false`() {
111-
val stateWithDefaultId = ProjectionState.getDefaultInstance()
112-
assertValid(stateWithDefaultId)
113-
}
114-
}
11553
}

java-tests/runtime/src/test/kotlin/io/spine/validate/ValidationOfConstraintTest.kt

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ import com.google.protobuf.Message
3131
import io.kotest.matchers.collections.shouldBeEmpty
3232
import io.kotest.matchers.collections.shouldHaveSize
3333
import io.kotest.matchers.shouldBe
34-
import io.kotest.matchers.string.shouldContain
3534
import io.kotest.matchers.string.shouldNotBeEmpty
36-
import io.kotest.matchers.string.shouldNotContain
3735
import io.spine.validate.Validate.violationsOf
3836
import org.junit.jupiter.api.Assertions.assertDoesNotThrow
3937
import org.junit.jupiter.api.assertThrows
@@ -96,17 +94,17 @@ abstract class ValidationOfConstraintTest {
9694
assertIsValid(false)
9795
}
9896

99-
protected fun assertNotValid(msg: Message, checkFieldPath: Boolean) {
97+
protected fun assertNotValid(msg: Message, checkFieldName: Boolean) {
10098
validate(msg)
101-
assertIsValid(false, checkFieldPath)
99+
assertIsValid(false, checkFieldName)
102100
}
103101

104102
@JvmOverloads
105-
protected fun assertIsValid(isValid: Boolean, checkFieldPath: Boolean = true) {
103+
protected fun assertIsValid(isValid: Boolean, checkFieldName: Boolean = true) {
106104
if (isValid) {
107105
assertThat(violations).isEmpty()
108106
} else {
109-
assertViolations(violations, checkFieldPath)
107+
assertViolations(violations, checkFieldName)
110108
}
111109
}
112110

@@ -125,42 +123,40 @@ abstract class ValidationOfConstraintTest {
125123
val violation = firstViolation()
126124
val actualErrorMessage = violation.message.format()
127125
actualErrorMessage shouldBe expectedErrMsg
128-
assertFieldPathIs(violation, invalidFieldName)
126+
violation.fieldName shouldBe invalidFieldName
129127
violation.violationList.shouldBeEmpty()
130128
}
131129

132130
protected fun assertSingleViolation(message: Message, invalidFieldName: String) {
133131
assertNotValid(message)
134132
assertThat(violations).hasSize(1)
135-
assertFieldPathIs(firstViolation(), invalidFieldName)
133+
firstViolation().fieldName shouldBe invalidFieldName
136134
}
137135

138136
companion object {
139137

140138
const val VALIDATION_SHOULD: String = "Validation should "
141139

142140
private fun assertViolations(
143-
violations: List<ConstraintViolation?>?,
144-
checkFieldPath: Boolean
141+
violations: List<ConstraintViolation>?,
142+
checkFieldName: Boolean
145143
) {
146144
assertThat(violations)
147145
.isNotEmpty()
148146
for (violation in violations!!) {
149147
assertHasCorrectFormat(violation)
150-
if (checkFieldPath) {
151-
assertHasFieldPath(violation)
148+
if (checkFieldName) {
149+
if (violation.fieldName.isEmpty()) {
150+
println(violation)
151+
}
152+
violation.fieldName.shouldNotBeEmpty()
152153
}
153154
}
154155
}
155156

156-
private fun assertHasCorrectFormat(violation: ConstraintViolation?) {
157-
val template = violation!!.message.withPlaceholders
157+
private fun assertHasCorrectFormat(violation: ConstraintViolation) {
158+
val template = violation.message.withPlaceholders
158159
template.shouldNotBeEmpty()
159160
}
160-
161-
private fun assertHasFieldPath(violation: ConstraintViolation?) {
162-
assertThat(violation!!.fieldPath.fieldNameList)
163-
.isNotEmpty()
164-
}
165161
}
166162
}

java-tests/validating/src/test/kotlin/io/spine/test/options/DistinctITest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ package io.spine.test.options
2929
import com.google.common.truth.extensions.proto.ProtoTruth.assertThat
3030
import io.kotest.matchers.optional.shouldBeEmpty
3131
import io.kotest.matchers.optional.shouldBePresent
32-
import io.spine.base.fieldPath
3332
import io.spine.protobuf.TypeConverter.toAny
3433
import io.spine.test.tools.validate.ProtoSet
3534
import io.spine.test.tools.validate.protoSet
@@ -54,7 +53,7 @@ internal class DistinctITest {
5453

5554
val violations = error.get().constraintViolationList
5655
val expected = constraintViolation {
57-
fieldPath = fieldPath { fieldName.add("element") }
56+
fieldName = "element"
5857
}
5958

6059
assertThat(violations)

java-tests/validating/src/test/kotlin/io/spine/test/options/ExternalConstraintITest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ internal class ExternalConstraintITest {
7171
val violations = error.get().constraintViolationList
7272

7373
violations.size shouldBe 1
74-
violations[0].fieldPath.fieldNameList[0] shouldBe "shipping_address"
74+
violations[0].fieldName shouldBe "shipping_address"
7575
}
7676

7777
@Test

java-tests/validating/src/test/kotlin/io/spine/test/options/ValidateConstraintTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ internal class ValidateConstraintTest {
6868
// See https://github.com/SpineEventEngine/validation/issues/148
6969
violations.size shouldBe 1
7070

71-
val receiverViolation = violations[0]
72-
receiverViolation!!.fieldPath.getFieldName(0) shouldBe "address"
71+
val receiverViolation = violations.first()
72+
receiverViolation.fieldName shouldBe "address"
7373

7474
val nestedViolations = receiverViolation.violationList
7575
nestedViolations.size shouldBe 2
76-
nestedViolations[0].fieldPath.getFieldName(0) shouldBe "first_line"
77-
nestedViolations[1].fieldPath.getFieldName(0) shouldBe "town"
76+
nestedViolations[0].fieldName shouldBe "first_line"
77+
nestedViolations[1].fieldName shouldBe "town"
7878
}
7979

8080
@Test
@@ -108,7 +108,7 @@ internal class ValidateConstraintTest {
108108

109109
for (violation in violations) {
110110
violation.violationList.size shouldBe 1
111-
violation.fieldPath.getFieldName(0) shouldBe "contact"
111+
violation.fieldName shouldBe "contact"
112112
}
113113
}
114114

java-tests/validating/src/testFixtures/kotlin/io/spine/validation/assertions/Assertions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fun assertInvalid(builder: Message.Builder): List<ConstraintViolation> {
7171

7272
private val fieldName: Correspondence<ConstraintViolation, String> =
7373
Correspondence.transforming(
74-
{ it.fieldPath.getFieldName(0) },
74+
{ it.fieldName },
7575
"field name"
7676
)
7777

@@ -97,7 +97,7 @@ fun assertViolation(
9797
* Obtains a violation for the field with the given name.
9898
*/
9999
fun List<ConstraintViolation>.atField(fieldName: String): ConstraintViolation {
100-
return find { it.fieldPath.fieldNameList[0] == fieldName }
100+
return find { it.fieldName == fieldName }
101101
?: fail("Cannot find a violation for the field `$fieldName`. Violations: `$this`.")
102102
}
103103

java-tests/vanilla/src/test/kotlin/io/spine/validation/java/GoesConstraintSpec.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import com.google.common.truth.Truth8.assertThat
3030
import com.google.common.truth.extensions.proto.ProtoTruth.assertThat
3131
import io.kotest.matchers.collections.shouldHaveSize
3232
import io.kotest.matchers.optional.shouldBePresent
33-
import io.spine.base.FieldPath
3433
import io.spine.base.Identifier
3534
import io.spine.base.Time.currentTime
3635
import io.spine.type.TypeName
@@ -65,10 +64,7 @@ internal class GoesConstraintSpec {
6564
.isEqualTo(
6665
ConstraintViolation.newBuilder()
6766
.setTypeName(TypeName.of(paper).toUrl().value())
68-
.setFieldPath(
69-
FieldPath.newBuilder()
70-
.addFieldName("when_archived")
71-
)
67+
.setFieldName("when_archived")
7268
.build()
7369
)
7470
}

0 commit comments

Comments
 (0)