|
| 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.support.mongock; |
| 17 | + |
| 18 | +import io.flamingock.internal.common.core.audit.AuditEntry; |
| 19 | +import io.flamingock.internal.common.core.audit.AuditPersistenceFactory; |
| 20 | +import io.flamingock.internal.common.core.audit.AuditHistoryReader; |
| 21 | +import io.flamingock.internal.common.core.error.FlamingockException; |
| 22 | +import io.flamingock.internal.common.core.pipeline.PipelineDescriptor; |
| 23 | +import io.flamingock.internal.core.external.store.audit.community.CommunityAuditPersistence; |
| 24 | +import io.flamingock.internal.core.external.targets.TargetSystemManager; |
| 25 | +import io.flamingock.internal.core.external.targets.operations.TransactionalTargetSystemOps; |
| 26 | +import io.flamingock.internal.util.Result; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.Optional; |
| 31 | + |
| 32 | +import static io.flamingock.internal.common.core.audit.AuditReaderType.MONGOCK; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 36 | +import static org.mockito.ArgumentMatchers.any; |
| 37 | +import static org.mockito.Mockito.mock; |
| 38 | +import static org.mockito.Mockito.when; |
| 39 | + |
| 40 | +class MongockImportChangeTest { |
| 41 | + @Test |
| 42 | + void failedAppendIncludesEntryIdentityAndOriginalCause() { |
| 43 | + TargetSystemManager manager = mock(TargetSystemManager.class); |
| 44 | + TransactionalTargetSystemOps ops = mock(TransactionalTargetSystemOps.class); |
| 45 | + AuditHistoryReader reader = mock(AuditHistoryReader.class); |
| 46 | + PipelineDescriptor pipeline = mock(PipelineDescriptor.class); |
| 47 | + CommunityAuditPersistence persistence = mock(CommunityAuditPersistence.class); |
| 48 | + AuditPersistenceFactory<CommunityAuditPersistence> factory = stageId -> { |
| 49 | + assertEquals("flamingock-legacy-stage", stageId); |
| 50 | + return persistence; |
| 51 | + }; |
| 52 | + AuditEntry entry = mock(AuditEntry.class); |
| 53 | + RuntimeException cause = new RuntimeException("write failed"); |
| 54 | + when(manager.getTargetSystem("mongo")).thenReturn(ops); |
| 55 | + when(ops.getAuditAuditReader(MONGOCK)).thenReturn(Optional.of(reader)); |
| 56 | + when(reader.getAuditHistory()).thenReturn(Collections.singletonList(entry)); |
| 57 | + when(entry.getChangeId()).thenReturn("change-1"); |
| 58 | + when(entry.copyWithNewIdAndStageId("change-1", "stage-1")).thenReturn(entry); |
| 59 | + when(pipeline.getStageByChange("change-1")).thenReturn(Optional.of("stage-1")); |
| 60 | + when(persistence.append(any(AuditEntry.class))).thenReturn(new Result.Error(cause)); |
| 61 | + |
| 62 | + FlamingockException failure = assertThrows(FlamingockException.class, |
| 63 | + () -> new MongockImportChange().importHistory("mongo", manager, factory, pipeline, null, null, null)); |
| 64 | + assertSame(cause, failure.getCause()); |
| 65 | + assertEquals("Error importing audit entry with changeId[change-1] into stage[stage-1] for targetSystem[mongo].", |
| 66 | + failure.getMessage()); |
| 67 | + } |
| 68 | +} |
0 commit comments