|
| 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.targetsystem.couchbase; |
| 17 | + |
| 18 | +import com.couchbase.client.core.io.CollectionIdentifier; |
| 19 | +import com.couchbase.client.java.Bucket; |
| 20 | +import com.couchbase.client.java.Cluster; |
| 21 | +import com.couchbase.client.java.transactions.TransactionAttemptContext; |
| 22 | +import io.flamingock.internal.common.core.targets.TargetSystemAuditMarkType; |
| 23 | +import io.flamingock.internal.core.external.targets.mark.TargetSystemAuditMark; |
| 24 | +import io.flamingock.internal.core.transaction.TransactionManager; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.Assertions; |
| 27 | +import org.junit.jupiter.api.BeforeAll; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.DisplayName; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.testcontainers.couchbase.BucketDefinition; |
| 32 | +import org.testcontainers.couchbase.CouchbaseContainer; |
| 33 | +import org.testcontainers.junit.jupiter.Container; |
| 34 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 35 | + |
| 36 | +import java.time.Duration; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.Set; |
| 39 | +import java.util.stream.Collectors; |
| 40 | + |
| 41 | +/** |
| 42 | + * Direct write-verification for {@link CouchbaseTargetSystemAuditMarker}. The existing |
| 43 | + * {@code CouchbaseTargetSystemTest} only asserts the end-state marker count is zero, which is |
| 44 | + * also satisfied when {@code mark()} is a no-op. This test exercises {@code mark()}, |
| 45 | + * {@code listAll()} and {@code clearMark()} through their production code paths, proving the |
| 46 | + * write actually persists. |
| 47 | + * |
| 48 | + * <p>Couchbase's marker {@code mark()} requires a live {@link TransactionAttemptContext} from |
| 49 | + * the {@link TransactionManager}. Each call is therefore wrapped in a real Couchbase transaction |
| 50 | + * (`cluster.transactions().run(...)`), registering the attempt context under the changeId before |
| 51 | + * invoking the marker and unregistering after. |
| 52 | + */ |
| 53 | +@Testcontainers |
| 54 | +public class CouchbaseTargetSystemAuditMarkerTest { |
| 55 | + |
| 56 | + private static final String BUCKET_NAME = "test"; |
| 57 | + private static final String SCOPE_NAME = CollectionIdentifier.DEFAULT_SCOPE; |
| 58 | + private static final String MARKER_COLLECTION = "flamingockAuditMarkerTest"; |
| 59 | + |
| 60 | + @Container |
| 61 | + public static final CouchbaseContainer couchbaseContainer = new CouchbaseContainer("couchbase/server:7.2.4") |
| 62 | + .withBucket(new BucketDefinition(BUCKET_NAME)); |
| 63 | + |
| 64 | + private static Cluster cluster; |
| 65 | + private static Bucket bucket; |
| 66 | + |
| 67 | + private TransactionManager<TransactionAttemptContext> txManager; |
| 68 | + private CouchbaseTargetSystemAuditMarker marker; |
| 69 | + |
| 70 | + @BeforeAll |
| 71 | + static void beforeAll() { |
| 72 | + couchbaseContainer.start(); |
| 73 | + cluster = Cluster.connect( |
| 74 | + couchbaseContainer.getConnectionString(), |
| 75 | + couchbaseContainer.getUsername(), |
| 76 | + couchbaseContainer.getPassword()); |
| 77 | + |
| 78 | + bucket = cluster.bucket(BUCKET_NAME); |
| 79 | + bucket.waitUntilReady(Duration.ofSeconds(10)); |
| 80 | + } |
| 81 | + |
| 82 | + @BeforeEach |
| 83 | + void beforeEach() { |
| 84 | + txManager = new TransactionManager<>(() -> { |
| 85 | + throw new UnsupportedOperationException( |
| 86 | + "Supplier is unused: Couchbase tests register the TransactionAttemptContext explicitly via startSession(id, ctx)"); |
| 87 | + }); |
| 88 | + |
| 89 | + marker = CouchbaseTargetSystemAuditMarker.builder(cluster, bucket, txManager) |
| 90 | + .withScopeName(SCOPE_NAME) |
| 91 | + .withCollectionName(MARKER_COLLECTION) |
| 92 | + .withAutoCreate(true) |
| 93 | + .build(); |
| 94 | + |
| 95 | + // Start each test from an empty marker collection. |
| 96 | + clearAll(); |
| 97 | + } |
| 98 | + |
| 99 | + @AfterEach |
| 100 | + void afterEach() { |
| 101 | + clearAll(); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + @DisplayName("mark() persists each mark and listAll() returns them with the right contents") |
| 106 | + void markPersistsAndIsReadableViaListAll() { |
| 107 | + String changeId1 = "change-1"; |
| 108 | + String changeId2 = "change-2"; |
| 109 | + |
| 110 | + markInTransaction(changeId1, TargetSystemAuditMarkType.APPLIED); |
| 111 | + markInTransaction(changeId2, TargetSystemAuditMarkType.ROLLED_BACK); |
| 112 | + |
| 113 | + Set<TargetSystemAuditMark> marks = marker.listAll(); |
| 114 | + Assertions.assertEquals(2, marks.size()); |
| 115 | + |
| 116 | + Map<String, TargetSystemAuditMarkType> byId = marks.stream() |
| 117 | + .collect(Collectors.toMap(TargetSystemAuditMark::getChangeId, |
| 118 | + TargetSystemAuditMark::getOperation)); |
| 119 | + Assertions.assertEquals(TargetSystemAuditMarkType.APPLIED, byId.get(changeId1)); |
| 120 | + Assertions.assertEquals(TargetSystemAuditMarkType.ROLLED_BACK, byId.get(changeId2)); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + @DisplayName("clearMark() removes only the targeted mark") |
| 125 | + void clearMarkRemovesOnlyTheTargetedMark() { |
| 126 | + String changeId1 = "change-1"; |
| 127 | + String changeId2 = "change-2"; |
| 128 | + markInTransaction(changeId1, TargetSystemAuditMarkType.APPLIED); |
| 129 | + markInTransaction(changeId2, TargetSystemAuditMarkType.APPLIED); |
| 130 | + |
| 131 | + marker.clearMark(changeId1); |
| 132 | + |
| 133 | + Set<TargetSystemAuditMark> marks = marker.listAll(); |
| 134 | + Assertions.assertEquals(1, marks.size()); |
| 135 | + Assertions.assertEquals(changeId2, marks.iterator().next().getChangeId()); |
| 136 | + } |
| 137 | + |
| 138 | + private void markInTransaction(String changeId, TargetSystemAuditMarkType operation) { |
| 139 | + cluster.transactions().run(ctx -> { |
| 140 | + txManager.startSession(changeId, ctx); |
| 141 | + marker.mark(new TargetSystemAuditMark(changeId, operation)); |
| 142 | + txManager.closeSession(changeId); |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + private void clearAll() { |
| 147 | + for (TargetSystemAuditMark mark : marker.listAll()) { |
| 148 | + marker.clearMark(mark.getChangeId()); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments