|
15 | 15 | */ |
16 | 16 | package io.flamingock.support.validation.impl; |
17 | 17 |
|
| 18 | +import io.flamingock.internal.common.core.audit.AuditEntry; |
18 | 19 | import io.flamingock.internal.common.core.audit.AuditReader; |
19 | 20 | import io.flamingock.support.domain.AuditEntryDefinition; |
20 | 21 | import io.flamingock.support.validation.SimpleValidator; |
21 | | -import io.flamingock.support.validation.error.ValidationResult; |
| 22 | +import io.flamingock.support.validation.error.*; |
22 | 23 |
|
| 24 | +import java.util.ArrayList; |
23 | 25 | import java.util.Arrays; |
24 | 26 | import java.util.List; |
25 | 27 | import java.util.stream.Collectors; |
26 | 28 |
|
| 29 | +/** |
| 30 | + * Validator that performs strict sequence validation of audit entries. |
| 31 | + * |
| 32 | + * <p>This validator verifies that the actual audit entries match the expected |
| 33 | + * sequence exactly, both in count and in field values. Checking:</p> |
| 34 | + * <ul> |
| 35 | + * <li>Exact count match between expected and actual entries</li> |
| 36 | + * <li>Strict field-by-field validation for each entry at each index</li> |
| 37 | + * <li>Order preservation (expected[0] must match actual[0], etc.)</li> |
| 38 | + * </ul> |
| 39 | + */ |
27 | 40 | public class AuditSequenceStrictValidator implements SimpleValidator { |
28 | 41 |
|
29 | 42 | private static final String VALIDATOR_NAME = "Audit Sequence (Strict)"; |
30 | 43 |
|
31 | 44 | private final AuditReader auditReader; |
32 | 45 | private final List<AuditEntryExpectation> expectations; |
33 | | - |
| 46 | + private final List<AuditEntry> actualEntries; |
34 | 47 |
|
35 | 48 | public AuditSequenceStrictValidator(AuditReader auditReader, AuditEntryDefinition... definitions) { |
36 | 49 | this.auditReader = auditReader; |
37 | 50 | this.expectations = Arrays.stream(definitions) |
38 | 51 | .map(AuditEntryExpectation::new) |
39 | 52 | .collect(Collectors.toList()); |
| 53 | + this.actualEntries = auditReader.getAuditHistory(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Internal constructor for direct list initialization (used by tests). |
| 58 | + */ |
| 59 | + AuditSequenceStrictValidator(List<AuditEntryDefinition> expectedDefinitions, List<AuditEntry> actualEntries, AuditReader auditReader) { |
| 60 | + this.expectations = expectedDefinitions != null |
| 61 | + ? expectedDefinitions.stream() |
| 62 | + .map(AuditEntryExpectation::new) |
| 63 | + .collect(Collectors.toList()) |
| 64 | + : new ArrayList<>(); |
| 65 | + this.auditReader = auditReader; |
| 66 | + this.actualEntries = actualEntries != null ? actualEntries : new ArrayList<>(); |
40 | 67 | } |
41 | 68 |
|
42 | 69 | @Override |
43 | 70 | public ValidationResult validate() { |
44 | | - // TODO: Implement actual validation logic |
45 | | - return ValidationResult.success(VALIDATOR_NAME); |
| 71 | + List<ValidationError> allErrors = new ArrayList<>(); |
| 72 | + |
| 73 | + int expectedSize = expectations.size(); |
| 74 | + int actualSize = actualEntries.size(); |
| 75 | + |
| 76 | + if (expectedSize != actualSize) { |
| 77 | + allErrors.add(new CountMismatchError(getExpectedChangeIds(), getActualChangeIds())); |
| 78 | + } |
| 79 | + |
| 80 | + allErrors.addAll(getValidationErrors(expectations, actualEntries)); |
| 81 | + |
| 82 | + if (allErrors.isEmpty()) { |
| 83 | + return ValidationResult.success(VALIDATOR_NAME); |
| 84 | + } |
| 85 | + |
| 86 | + return ValidationResult.failure(VALIDATOR_NAME, allErrors.toArray(new ValidationError[0])); |
| 87 | + } |
| 88 | + |
| 89 | + private static List<ValidationError> getValidationErrors(List<AuditEntryExpectation> expectedEntries, List<AuditEntry> actualEntries) { |
| 90 | + List<ValidationError> allErrors = new ArrayList<>(); |
| 91 | + if (expectedEntries.isEmpty()) { |
| 92 | + return allErrors; |
| 93 | + } |
| 94 | + int actualSize = actualEntries.size(); |
| 95 | + int limit = Math.max(expectedEntries.size(), actualSize); |
| 96 | + |
| 97 | + for (int i = 0; i < limit; i++) { |
| 98 | + AuditEntryExpectation expected = i < expectedEntries.size() ? expectedEntries.get(i) : null; |
| 99 | + AuditEntry actual = i < actualEntries.size() ? actualEntries.get(i) : null; |
| 100 | + if( expected != null && actual != null) { |
| 101 | + allErrors.addAll(expected.compareWith(actual)); |
| 102 | + } else if( expected != null) { |
| 103 | + AuditEntryDefinition def = expected.getDefinition(); |
| 104 | + allErrors.add(new MissingEntryError(i, def.getChangeId(), def.getState())); |
| 105 | + } else { |
| 106 | + assert actual != null; |
| 107 | + allErrors.add(new UnexpectedEntryError(i, actual.getTaskId(), actual.getState())); |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + return allErrors; |
| 112 | + } |
| 113 | + |
| 114 | + private List<String> getExpectedChangeIds() { |
| 115 | + return expectations.stream() |
| 116 | + .map(exp -> exp.getDefinition().getChangeId()) |
| 117 | + .collect(Collectors.toList()); |
| 118 | + } |
| 119 | + |
| 120 | + private List<String> getActualChangeIds() { |
| 121 | + return actualEntries.stream() |
| 122 | + .map(AuditEntry::getTaskId) |
| 123 | + .collect(Collectors.toList()); |
46 | 124 | } |
47 | 125 | } |
0 commit comments