Skip to content

Commit 92644e6

Browse files
authored
refactor: add auditEntryExpectation fields: order, recovery and transactional (#757)
1 parent 4cea3ac commit 92644e6

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

core/flamingock-test-support/src/main/java/io/flamingock/support/domain/AuditEntryExpectation.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
*/
1616
package io.flamingock.support.domain;
1717

18+
import io.flamingock.api.RecoveryStrategy;
1819
import io.flamingock.api.annotations.Apply;
1920
import io.flamingock.api.annotations.Change;
21+
import io.flamingock.api.annotations.Recovery;
2022
import io.flamingock.api.annotations.Rollback;
2123
import io.flamingock.api.annotations.TargetSystem;
2224
import io.flamingock.internal.common.core.audit.AuditEntry;
@@ -90,6 +92,9 @@
9092
*/
9193
public class AuditEntryExpectation {
9294

95+
private static final String ORDER_PATTERN_PREFIX = "_";
96+
private static final String ORDER_PATTERN_SEPARATOR = "__";
97+
9398
private final String expectedChangeId;
9499
private final AuditEntry.Status expectedState;
95100

@@ -104,6 +109,9 @@ public class AuditEntryExpectation {
104109
private String expectedExecutionHostname;
105110
private String expectedErrorTrace;
106111
private String expectedTargetSystemId;
112+
private RecoveryStrategy expectedRecoveryStrategy;
113+
private String expectedOrder;
114+
private Boolean expectedTransactional;
107115

108116
// Time range for flexible timestamp verification
109117
private LocalDateTime timestampAfter;
@@ -279,6 +287,15 @@ private static AuditEntryExpectation fromChangeClass(Class<?> changeClass, Audit
279287
expectation.expectedTargetSystemId = targetSystem.id();
280288
}
281289

290+
Recovery recovery = changeClass.getAnnotation(Recovery.class);
291+
expectation.expectedRecoveryStrategy = (recovery != null)
292+
? recovery.strategy()
293+
: RecoveryStrategy.MANUAL_INTERVENTION;
294+
295+
expectation.expectedOrder = extractOrderFromClassName(changeClass.getSimpleName());
296+
297+
expectation.expectedTransactional = changeAnnotation.transactional();
298+
282299
return expectation;
283300
}
284301

@@ -298,6 +315,17 @@ private static String findMethodName(Class<?> changeClass, AuditEntry.Status sta
298315
annotationClass.getSimpleName()));
299316
}
300317

318+
private static String extractOrderFromClassName(String className) {
319+
if (className == null || !className.startsWith(ORDER_PATTERN_PREFIX)) {
320+
return null;
321+
}
322+
int separatorIndex = className.indexOf(ORDER_PATTERN_SEPARATOR);
323+
if (separatorIndex <= 1) {
324+
return null;
325+
}
326+
return className.substring(1, separatorIndex);
327+
}
328+
301329
/**
302330
* Sets the expected execution ID for verification.
303331
*
@@ -456,6 +484,46 @@ public AuditEntryExpectation withTargetSystemId(String targetSystemId) {
456484
return this;
457485
}
458486

487+
/**
488+
* Sets the expected recovery strategy for verification.
489+
*
490+
* <p>Enables verification of the recovery strategy field.</p>
491+
*
492+
* @param recoveryStrategy the expected recovery strategy
493+
* @return this builder for method chaining
494+
*/
495+
public AuditEntryExpectation withRecoveryStrategy(RecoveryStrategy recoveryStrategy) {
496+
this.expectedRecoveryStrategy = recoveryStrategy;
497+
return this;
498+
}
499+
500+
/**
501+
* Sets the expected order value for verification.
502+
*
503+
* <p>The order is typically extracted from the class name pattern {@code _ORDER__CHANGE-NAME},
504+
* but can be overridden using this method.</p>
505+
*
506+
* @param order the expected order value
507+
* @return this builder for method chaining
508+
*/
509+
public AuditEntryExpectation withOrder(String order) {
510+
this.expectedOrder = order;
511+
return this;
512+
}
513+
514+
/**
515+
* Sets the expected transactional flag for verification.
516+
*
517+
* <p>Indicates whether the change should run within a transaction.</p>
518+
*
519+
* @param transactional the expected transactional flag
520+
* @return this builder for method chaining
521+
*/
522+
public AuditEntryExpectation withTransactional(boolean transactional) {
523+
this.expectedTransactional = transactional;
524+
return this;
525+
}
526+
459527
/**
460528
* Compares this expectation against an actual audit entry.
461529
*
@@ -531,6 +599,22 @@ public List<FieldMismatchError> compareWith(AuditEntry actual) {
531599
errors.add(new FieldMismatchError("targetSystemId", expectedTargetSystemId, actual.getTargetSystemId()));
532600
}
533601

602+
if (expectedRecoveryStrategy != null && expectedRecoveryStrategy != actual.getRecoveryStrategy()) {
603+
errors.add(new FieldMismatchError("recoveryStrategy",
604+
expectedRecoveryStrategy.name(),
605+
actual.getRecoveryStrategy() != null ? actual.getRecoveryStrategy().name() : null));
606+
}
607+
608+
if (expectedOrder != null && !expectedOrder.equals(actual.getOrder())) {
609+
errors.add(new FieldMismatchError("order", expectedOrder, actual.getOrder()));
610+
}
611+
612+
if (expectedTransactional != null && !expectedTransactional.equals(actual.getTransactionFlag())) {
613+
errors.add(new FieldMismatchError("transactional",
614+
String.valueOf(expectedTransactional),
615+
String.valueOf(actual.getTransactionFlag())));
616+
}
617+
534618
errors.addAll(compareTimestamp(actual));
535619

536620
return errors;

0 commit comments

Comments
 (0)