|
| 1 | +/* |
| 2 | + * Copyright 2025 Flamingock (https://www.flamingock.io) |
| 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 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.flamingock.support.validation.impl; |
| 17 | + |
| 18 | +import io.flamingock.internal.common.core.audit.AuditEntry; |
| 19 | +import io.flamingock.support.domain.AuditEntryDefinition; |
| 20 | +import io.flamingock.support.validation.error.CountMismatchError; |
| 21 | +import io.flamingock.support.validation.error.FieldMismatchError; |
| 22 | +import io.flamingock.support.validation.error.ValidationResult; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import java.time.LocalDateTime; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static io.flamingock.internal.common.core.audit.AuditEntry.ExecutionType.EXECUTION; |
| 32 | +import static io.flamingock.internal.common.core.audit.AuditEntry.Status.APPLIED; |
| 33 | +import static io.flamingock.internal.common.core.audit.AuditEntry.Status.FAILED; |
| 34 | +import static io.flamingock.support.domain.AuditEntryDefinition.APPLIED; |
| 35 | +import static org.junit.jupiter.api.Assertions.*; |
| 36 | + |
| 37 | +class AuditSequenceStrictValidatorTest { |
| 38 | + |
| 39 | + private List<AuditEntry> actualEntries; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + void setUp() { |
| 43 | + actualEntries = Arrays.asList( |
| 44 | + createAuditEntry("change-1", APPLIED), |
| 45 | + createAuditEntry("change-2", APPLIED), |
| 46 | + createAuditEntry("change-3", FAILED) |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void shouldPassValidation_whenEntriesMatchExactly() { |
| 52 | + List<AuditEntryDefinition> expectedDefinitions = Arrays.asList( |
| 53 | + APPLIED("change-1"), |
| 54 | + APPLIED("change-2"), |
| 55 | + AuditEntryDefinition.FAILED("change-3") |
| 56 | + ); |
| 57 | + |
| 58 | + AuditSequenceStrictValidator validator = new AuditSequenceStrictValidator(expectedDefinitions, actualEntries); |
| 59 | + ValidationResult result = validator.validate(); |
| 60 | + |
| 61 | + assertTrue(result.isSuccess()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void shouldFailValidation_whenCountMismatch() { |
| 66 | + List<AuditEntryDefinition> expectedDefinitions = Arrays.asList( |
| 67 | + APPLIED("change-1"), |
| 68 | + APPLIED("change-2") |
| 69 | + ); |
| 70 | + |
| 71 | + AuditSequenceStrictValidator validator = new AuditSequenceStrictValidator(expectedDefinitions, actualEntries); |
| 72 | + ValidationResult result = validator.validate(); |
| 73 | + |
| 74 | + assertFalse(result.isSuccess()); |
| 75 | + assertEquals(1, result.getErrors().size()); |
| 76 | + assertInstanceOf(CountMismatchError.class, result.getErrors().get(0)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void shouldFailValidation_whenStatusMismatch() { |
| 81 | + List<AuditEntryDefinition> expectedDefinitions = Arrays.asList( |
| 82 | + APPLIED("change-1"), |
| 83 | + APPLIED("change-2"), |
| 84 | + APPLIED("change-3") // Expected APPLIED but actual is FAILED |
| 85 | + ); |
| 86 | + |
| 87 | + AuditSequenceStrictValidator validator = new AuditSequenceStrictValidator(expectedDefinitions, actualEntries); |
| 88 | + ValidationResult result = validator.validate(); |
| 89 | + |
| 90 | + assertFalse(result.isSuccess()); |
| 91 | + assertEquals(1, result.getErrors().size()); |
| 92 | + assertInstanceOf(FieldMismatchError.class, result.getErrors().get(0)); |
| 93 | + |
| 94 | + FieldMismatchError error = (FieldMismatchError) result.getErrors().get(0); |
| 95 | + assertEquals("status", error.getFieldName()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void shouldFailValidation_whenChangeIdMismatch() { |
| 100 | + List<AuditEntryDefinition> expectedDefinitions = Arrays.asList( |
| 101 | + APPLIED("change-1"), |
| 102 | + APPLIED("wrong-id"), // Mismatch |
| 103 | + AuditEntryDefinition.FAILED("change-3") |
| 104 | + ); |
| 105 | + |
| 106 | + AuditSequenceStrictValidator validator = new AuditSequenceStrictValidator(expectedDefinitions, actualEntries); |
| 107 | + ValidationResult result = validator.validate(); |
| 108 | + |
| 109 | + assertFalse(result.isSuccess()); |
| 110 | + assertEquals(1, result.getErrors().size()); |
| 111 | + assertInstanceOf(FieldMismatchError.class, result.getErrors().get(0)); |
| 112 | + |
| 113 | + FieldMismatchError error = (FieldMismatchError) result.getErrors().get(0); |
| 114 | + assertEquals("changeId", error.getFieldName()); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void shouldFailValidation_whenMissingEntry() { |
| 119 | + List<AuditEntry> actualEntriesSubset = Arrays.asList( |
| 120 | + createAuditEntry("change-1", APPLIED), |
| 121 | + createAuditEntry("change-2", APPLIED) |
| 122 | + ); |
| 123 | + |
| 124 | + List<AuditEntryDefinition> expectedDefinitions = Arrays.asList( |
| 125 | + APPLIED("change-1"), |
| 126 | + APPLIED("change-2"), |
| 127 | + AuditEntryDefinition.FAILED("change-3") |
| 128 | + ); |
| 129 | + |
| 130 | + AuditSequenceStrictValidator validator = new AuditSequenceStrictValidator(expectedDefinitions, actualEntriesSubset); |
| 131 | + ValidationResult result = validator.validate(); |
| 132 | + |
| 133 | + assertFalse(result.isSuccess()); |
| 134 | + assertTrue(result.getErrors().stream().anyMatch(e -> e instanceof CountMismatchError)); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void shouldFailValidation_whenUnexpectedEntry() { |
| 139 | + List<AuditEntry> actualEntriesExtra = Arrays.asList( |
| 140 | + createAuditEntry("change-1", APPLIED), |
| 141 | + createAuditEntry("change-2", APPLIED), |
| 142 | + createAuditEntry("change-3", FAILED), |
| 143 | + createAuditEntry("change-4", APPLIED) |
| 144 | + ); |
| 145 | + |
| 146 | + List<AuditEntryDefinition> expectedDefinitions = Arrays.asList( |
| 147 | + APPLIED("change-1"), |
| 148 | + APPLIED("change-2"), |
| 149 | + AuditEntryDefinition.FAILED("change-3") |
| 150 | + ); |
| 151 | + |
| 152 | + AuditSequenceStrictValidator validator = new AuditSequenceStrictValidator(expectedDefinitions, actualEntriesExtra); |
| 153 | + ValidationResult result = validator.validate(); |
| 154 | + |
| 155 | + assertFalse(result.isSuccess()); |
| 156 | + assertTrue(result.getErrors().stream().anyMatch(e -> e instanceof CountMismatchError)); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void shouldPassValidation_whenOptionalFieldsMatch() { |
| 161 | + AuditEntry actualWithOptionalFields = new AuditEntry( |
| 162 | + "exec-1", |
| 163 | + "stage-1", |
| 164 | + "change-1", |
| 165 | + "author", |
| 166 | + LocalDateTime.now(), |
| 167 | + APPLIED, |
| 168 | + EXECUTION, |
| 169 | + "com.example.Change", |
| 170 | + "apply", |
| 171 | + 100L, |
| 172 | + "host", |
| 173 | + null, |
| 174 | + false, |
| 175 | + null, |
| 176 | + null, |
| 177 | + "target-1", |
| 178 | + "1", |
| 179 | + null, |
| 180 | + true |
| 181 | + ); |
| 182 | + |
| 183 | + AuditEntryDefinition expectedWithOptionalFields = APPLIED("change-1") |
| 184 | + .withAuthor("author") |
| 185 | + .withClassName("com.example.Change") |
| 186 | + .withMethodName("apply") |
| 187 | + .withTargetSystemId("target-1") |
| 188 | + .withOrder("1") |
| 189 | + .withTransactional(true); |
| 190 | + |
| 191 | + AuditSequenceStrictValidator validator = new AuditSequenceStrictValidator( |
| 192 | + Collections.singletonList(expectedWithOptionalFields), |
| 193 | + Collections.singletonList(actualWithOptionalFields) |
| 194 | + ); |
| 195 | + |
| 196 | + ValidationResult result = validator.validate(); |
| 197 | + |
| 198 | + assertTrue(result.isSuccess()); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + void shouldFailValidation_whenOptionalFieldMismatch() { |
| 203 | + AuditEntry actualEntry = new AuditEntry( |
| 204 | + "exec-1", |
| 205 | + "stage-1", |
| 206 | + "change-1", |
| 207 | + "author", |
| 208 | + LocalDateTime.now(), |
| 209 | + APPLIED, |
| 210 | + EXECUTION, |
| 211 | + "com.example.Change", |
| 212 | + "apply", |
| 213 | + 100L, |
| 214 | + "host", |
| 215 | + null, |
| 216 | + false, |
| 217 | + null, |
| 218 | + null, |
| 219 | + "target-1", |
| 220 | + null, |
| 221 | + null, |
| 222 | + null |
| 223 | + ); |
| 224 | + |
| 225 | + AuditEntryDefinition expectedWithDifferentOptional = APPLIED("change-1") |
| 226 | + .withTargetSystemId("different-target"); |
| 227 | + |
| 228 | + AuditSequenceStrictValidator validator = new AuditSequenceStrictValidator( |
| 229 | + Collections.singletonList(expectedWithDifferentOptional), |
| 230 | + Collections.singletonList(actualEntry) |
| 231 | + ); |
| 232 | + |
| 233 | + ValidationResult result = validator.validate(); |
| 234 | + |
| 235 | + assertFalse(result.isSuccess()); |
| 236 | + assertEquals(1, result.getErrors().size()); |
| 237 | + assertInstanceOf(FieldMismatchError.class, result.getErrors().get(0)); |
| 238 | + |
| 239 | + FieldMismatchError error = (FieldMismatchError) result.getErrors().get(0); |
| 240 | + assertEquals("targetSystemId", error.getFieldName()); |
| 241 | + } |
| 242 | + |
| 243 | + private AuditEntry createAuditEntry(String changeId, AuditEntry.Status status) { |
| 244 | + return new AuditEntry( |
| 245 | + "exec-id", |
| 246 | + "stage-id", |
| 247 | + changeId, |
| 248 | + "test-author", |
| 249 | + LocalDateTime.now(), |
| 250 | + status, |
| 251 | + EXECUTION, |
| 252 | + "com.example.TestChange", |
| 253 | + "apply", |
| 254 | + 100L, |
| 255 | + "localhost", |
| 256 | + null, |
| 257 | + false, |
| 258 | + null, |
| 259 | + null, |
| 260 | + null, |
| 261 | + null, |
| 262 | + null, |
| 263 | + null |
| 264 | + ); |
| 265 | + } |
| 266 | +} |
0 commit comments