|
| 1 | +/* |
| 2 | + * Copyright 2026 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.internal.core.external.store.audit.domain; |
| 17 | + |
| 18 | +import io.flamingock.internal.common.core.targets.TargetSystemAuditMarkType; |
| 19 | +import org.junit.jupiter.api.DisplayName; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.params.ParameterizedTest; |
| 22 | +import org.junit.jupiter.params.provider.EnumSource; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.*; |
| 25 | + |
| 26 | +class OperationMappingTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + @DisplayName("EXECUTION should map to APPLIED") |
| 30 | + void executionMapsToApplied() { |
| 31 | + assertEquals(TargetSystemAuditMarkType.APPLIED, |
| 32 | + AuditContextBundle.Operation.EXECUTION.toOngoingStatusOperation()); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + @DisplayName("ROLLBACK should map to ROLLED_BACK") |
| 37 | + void rollbackMapsToRolledBack() { |
| 38 | + assertEquals(TargetSystemAuditMarkType.ROLLED_BACK, |
| 39 | + AuditContextBundle.Operation.ROLLBACK.toOngoingStatusOperation()); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + @DisplayName("APPLIED should map back to EXECUTION") |
| 44 | + void appliedMapsToExecution() { |
| 45 | + assertEquals(AuditContextBundle.Operation.EXECUTION, |
| 46 | + AuditContextBundle.Operation.fromOngoingStatusOperation(TargetSystemAuditMarkType.APPLIED)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @DisplayName("ROLLED_BACK should map back to ROLLBACK") |
| 51 | + void rolledBackMapsToRollback() { |
| 52 | + assertEquals(AuditContextBundle.Operation.ROLLBACK, |
| 53 | + AuditContextBundle.Operation.fromOngoingStatusOperation(TargetSystemAuditMarkType.ROLLED_BACK)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @DisplayName("START_EXECUTION should throw when mapped to TargetSystemAuditMarkType") |
| 58 | + void startExecutionHasNoMapping() { |
| 59 | + assertThrows(IllegalArgumentException.class, |
| 60 | + () -> AuditContextBundle.Operation.START_EXECUTION.toOngoingStatusOperation()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @DisplayName("NONE should throw when mapped to Operation") |
| 65 | + void noneHasNoMapping() { |
| 66 | + assertThrows(IllegalArgumentException.class, |
| 67 | + () -> AuditContextBundle.Operation.fromOngoingStatusOperation(TargetSystemAuditMarkType.NONE)); |
| 68 | + } |
| 69 | + |
| 70 | + @ParameterizedTest(name = "Every mappable Operation.{0} should round-trip through TargetSystemAuditMarkType") |
| 71 | + @EnumSource(value = AuditContextBundle.Operation.class, names = {"EXECUTION", "ROLLBACK"}) |
| 72 | + void operationRoundTrips(AuditContextBundle.Operation operation) { |
| 73 | + TargetSystemAuditMarkType markType = operation.toOngoingStatusOperation(); |
| 74 | + AuditContextBundle.Operation back = AuditContextBundle.Operation.fromOngoingStatusOperation(markType); |
| 75 | + assertEquals(operation, back); |
| 76 | + } |
| 77 | + |
| 78 | + @ParameterizedTest(name = "Every mappable TargetSystemAuditMarkType.{0} should round-trip through Operation") |
| 79 | + @EnumSource(value = TargetSystemAuditMarkType.class, names = {"APPLIED", "ROLLED_BACK"}) |
| 80 | + void markTypeRoundTrips(TargetSystemAuditMarkType markType) { |
| 81 | + AuditContextBundle.Operation operation = AuditContextBundle.Operation.fromOngoingStatusOperation(markType); |
| 82 | + TargetSystemAuditMarkType back = operation.toOngoingStatusOperation(); |
| 83 | + assertEquals(markType, back); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + @DisplayName("All mappable Operations must have a mapping — fails if a new Operation is added without updating the map") |
| 88 | + void allMappableOperationsCovered() { |
| 89 | + int expectedMappable = 2; // EXECUTION, ROLLBACK — START_EXECUTION is intentionally unmapped |
| 90 | + int actualMappable = 0; |
| 91 | + for (AuditContextBundle.Operation op : AuditContextBundle.Operation.values()) { |
| 92 | + try { |
| 93 | + op.toOngoingStatusOperation(); |
| 94 | + actualMappable++; |
| 95 | + } catch (IllegalArgumentException ignored) { |
| 96 | + } |
| 97 | + } |
| 98 | + assertEquals(expectedMappable, actualMappable, |
| 99 | + "A new Operation value was added without updating the mapping in Operation. " + |
| 100 | + "Update TO_MARK_TYPE/FROM_MARK_TYPE maps and this test."); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @DisplayName("All mappable TargetSystemAuditMarkTypes must have a mapping — fails if a new value is added without updating the map") |
| 105 | + void allMappableMarkTypesCovered() { |
| 106 | + int expectedMappable = 2; // APPLIED, ROLLED_BACK — NONE is intentionally unmapped |
| 107 | + int actualMappable = 0; |
| 108 | + for (TargetSystemAuditMarkType mt : TargetSystemAuditMarkType.values()) { |
| 109 | + try { |
| 110 | + AuditContextBundle.Operation.fromOngoingStatusOperation(mt); |
| 111 | + actualMappable++; |
| 112 | + } catch (IllegalArgumentException ignored) { |
| 113 | + } |
| 114 | + } |
| 115 | + assertEquals(expectedMappable, actualMappable, |
| 116 | + "A new TargetSystemAuditMarkType value was added without updating the mapping in Operation. " + |
| 117 | + "Update TO_MARK_TYPE/FROM_MARK_TYPE maps and this test."); |
| 118 | + } |
| 119 | +} |
0 commit comments