Skip to content

Commit 301af7e

Browse files
committed
test: mongock couchbase importer test
1 parent 51cc187 commit 301af7e

File tree

7 files changed

+256
-11
lines changed

7 files changed

+256
-11
lines changed

core/importer/flamingock-importer-couchbase-tests/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ repositories {
1111

1212
dependencies {
1313
compileOnly("com.couchbase.client:java-client:3.6.0")
14+
15+
1416
testImplementation(project(":core:importer:flamingock-importer"))
1517
testAnnotationProcessor(project(":core:flamingock-processor"))
1618
testImplementation(project(":community:flamingock-auditstore-couchbase"))

legacy/mongock-importer-couchbase/build.gradle.kts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,41 @@ dependencies {
44

55
//General
66
compileOnly("com.couchbase.client:java-client:3.6.0")
7+
8+
9+
testAnnotationProcessor(project(":core:flamingock-processor"))
10+
testAnnotationProcessor(project(":legacy:mongock-support"))
11+
testImplementation(project(":legacy:mongock-support"))
12+
testImplementation(project(":core:target-systems:couchbase-target-system"))
13+
14+
testImplementation(project(":community:flamingock-auditstore-couchbase"))
15+
testImplementation(project(":utils:couchbase-util"))
16+
testImplementation(project(":utils:test-util"))
17+
18+
testImplementation("org.testcontainers:couchbase:1.21.3")
19+
testImplementation("org.testcontainers:junit-jupiter:1.21.3")
20+
testImplementation("org.mockito:mockito-inline:4.11.0")
721
}
822

923

10-
description = "A MongoDB migration utility that imports Mongock’s execution history into Flamingock-Community’s audit store for smooth project upgrades."
24+
description = "A Couchbase migration utility that imports Mongock’s execution history into Flamingock-Community’s audit store for smooth project upgrades."
1125

1226

1327
java {
1428
toolchain {
1529
languageVersion.set(JavaLanguageVersion.of(8))
1630
}
1731
}
32+
33+
34+
tasks.withType<JavaCompile>().configureEach {
35+
if (name.contains("Test", ignoreCase = true)) {
36+
options.compilerArgs.addAll(listOf(
37+
"-Asources=${projectDir}/src/test/java",
38+
"-Aresources=${projectDir}/src/test/resources"
39+
))
40+
}
41+
}
42+
configurations.testImplementation {
43+
extendsFrom(configurations.compileOnly.get())
44+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
* Copyright 2023 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.importer.mongock.couchbase;
17+
18+
import com.couchbase.client.core.io.CollectionIdentifier;
19+
import com.couchbase.client.java.Cluster;
20+
import com.couchbase.client.java.Collection;
21+
import com.couchbase.client.java.json.JsonObject;
22+
import com.couchbase.client.java.manager.bucket.BucketManager;
23+
import com.couchbase.client.java.manager.bucket.BucketSettings;
24+
import io.flamingock.api.annotations.EnableFlamingock;
25+
import io.flamingock.api.annotations.Stage;
26+
import io.flamingock.community.couchbase.driver.CouchbaseAuditStore;
27+
import io.flamingock.internal.common.couchbase.CouchbaseCollectionHelper;
28+
import io.flamingock.internal.core.builder.FlamingockFactory;
29+
import io.flamingock.internal.core.runner.Runner;
30+
import io.flamingock.internal.util.constants.CommunityPersistenceConstants;
31+
import org.junit.jupiter.api.AfterEach;
32+
import org.junit.jupiter.api.BeforeAll;
33+
import org.junit.jupiter.api.Disabled;
34+
import org.junit.jupiter.api.Test;
35+
import org.testcontainers.couchbase.CouchbaseContainer;
36+
import org.testcontainers.junit.jupiter.Container;
37+
import org.testcontainers.junit.jupiter.Testcontainers;
38+
39+
import java.time.Duration;
40+
import java.time.Instant;
41+
import java.util.List;
42+
43+
import static io.flamingock.api.StageType.LEGACY;
44+
import static io.flamingock.api.StageType.SYSTEM;
45+
import static org.junit.jupiter.api.Assertions.assertEquals;
46+
import static org.junit.jupiter.api.Assertions.assertFalse;
47+
import static org.junit.jupiter.api.Assertions.assertTrue;
48+
49+
@EnableFlamingock(
50+
stages = {
51+
@Stage(location = "io.flamingock.importer.couchbase.system", type = SYSTEM),
52+
@Stage(location = "io.flamingock.importer.couchbase.legacy", type = LEGACY),
53+
@Stage(location = "io.flamingock.importer.couchbase.couchbase")
54+
}
55+
)
56+
@Testcontainers
57+
public class CouchbaseImporterTest {
58+
59+
public static final String FLAMINGOCK_BUCKET_NAME = "test";
60+
public static final String FLAMINGOCK_SCOPE_NAME = "flamingock";
61+
public static final String FLAMINGOCK_COLLECTION_NAME = CommunityPersistenceConstants.DEFAULT_AUDIT_STORE_NAME;
62+
63+
public static final String MONGOCK_BUCKET_NAME = "test";
64+
public static final String MONGOCK_SCOPE_NAME = CollectionIdentifier.DEFAULT_SCOPE;
65+
public static final String MONGOCK_COLLECTION_NAME = CollectionIdentifier.DEFAULT_COLLECTION;
66+
67+
68+
@Container
69+
static final CouchbaseContainer couchbaseContainer = new CouchbaseContainer("couchbase/server:7.2.4")
70+
.withBucket(new org.testcontainers.couchbase.BucketDefinition(FLAMINGOCK_BUCKET_NAME));
71+
72+
private static Cluster cluster;
73+
74+
@BeforeAll
75+
static void setupAll() {
76+
couchbaseContainer.start();
77+
cluster = Cluster.connect(
78+
couchbaseContainer.getConnectionString(),
79+
couchbaseContainer.getUsername(),
80+
couchbaseContainer.getPassword()
81+
);
82+
cluster.bucket(FLAMINGOCK_BUCKET_NAME).waitUntilReady(Duration.ofSeconds(10));
83+
84+
// Setup Mongock Bucket, Scope and Collection
85+
BucketManager bucketManager = cluster.buckets();
86+
87+
int ramQuotaMB = 100;
88+
89+
if (!bucketManager.getAllBuckets().containsKey(MONGOCK_BUCKET_NAME)) {
90+
bucketManager.createBucket(BucketSettings.create(MONGOCK_BUCKET_NAME).ramQuotaMB(ramQuotaMB));
91+
cluster.bucket(MONGOCK_BUCKET_NAME).waitUntilReady(Duration.ofSeconds(10));
92+
}
93+
94+
CouchbaseCollectionHelper.createScopeIfNotExists(cluster, MONGOCK_BUCKET_NAME, MONGOCK_SCOPE_NAME);
95+
CouchbaseCollectionHelper.createCollectionIfNotExists(cluster, MONGOCK_BUCKET_NAME, MONGOCK_SCOPE_NAME, MONGOCK_COLLECTION_NAME);
96+
CouchbaseCollectionHelper.createPrimaryIndexIfNotExists(cluster, MONGOCK_BUCKET_NAME, MONGOCK_SCOPE_NAME, MONGOCK_COLLECTION_NAME);
97+
}
98+
99+
@AfterEach
100+
void cleanUp() {
101+
CouchbaseCollectionHelper.deleteAllDocuments(cluster, FLAMINGOCK_BUCKET_NAME, FLAMINGOCK_SCOPE_NAME, FLAMINGOCK_COLLECTION_NAME);
102+
CouchbaseCollectionHelper.deleteAllDocuments(cluster, MONGOCK_BUCKET_NAME, MONGOCK_SCOPE_NAME, MONGOCK_COLLECTION_NAME);
103+
}
104+
105+
@Test
106+
@Disabled("restore when https://trello.com/c/4gEQ8Wb4/458-mongock-legacy-targetsystem done")
107+
void testImporterIntegration() {
108+
Collection originCollection = cluster.bucket(MONGOCK_BUCKET_NAME).scope(MONGOCK_SCOPE_NAME).collection(MONGOCK_COLLECTION_NAME);
109+
JsonObject doc = JsonObject.create()
110+
.put("executionId", "exec-1")
111+
.put("changeId", "change-1")
112+
.put("author", "author1")
113+
.put("timestamp", String.valueOf(Instant.now().toEpochMilli()))
114+
.put("state", "EXECUTED")
115+
.put("type", "EXECUTION")
116+
.put("changeLogClass", "io.flamingock.changelog.Class1")
117+
.put("changeSetMethod", "method1")
118+
.putNull("metadata")
119+
.put("executionMillis", 123L)
120+
.put("executionHostName", "host1")
121+
.putNull("errorTrace")
122+
.put("systemChange", true)
123+
.put("_doctype", "mongockChangeEntry");
124+
originCollection.upsert("change-1", doc);
125+
126+
Runner flamingock = FlamingockFactory.getCommunityBuilder()
127+
.setAuditStore(new CouchbaseAuditStore(cluster, FLAMINGOCK_BUCKET_NAME)
128+
.withScopeName(FLAMINGOCK_SCOPE_NAME)
129+
.withAuditRepositoryName(FLAMINGOCK_COLLECTION_NAME))
130+
.build();
131+
132+
flamingock.run();
133+
134+
List<JsonObject> auditLog = CouchbaseCollectionHelper.selectAllDocuments(cluster, FLAMINGOCK_BUCKET_NAME, FLAMINGOCK_SCOPE_NAME, FLAMINGOCK_COLLECTION_NAME);
135+
136+
assertFalse(auditLog.isEmpty(), "Audit log should not be empty");
137+
138+
JsonObject entry = auditLog.stream()
139+
.filter(e -> "change-1".equals(e.getString("changeId")))
140+
.findFirst()
141+
.orElseThrow(() -> new AssertionError("Entry with changeId 'change-1' not found"));
142+
143+
assertEquals("change-1", entry.getString("changeId"));
144+
assertEquals("author1", entry.getString("author"));
145+
assertEquals("exec-1", entry.getString("executionId"));
146+
assertEquals("APPLIED", entry.getString("state"));
147+
assertTrue(entry.getBoolean("systemChange"));
148+
}
149+
150+
@Test
151+
@Disabled("restore when https://trello.com/c/4gEQ8Wb4/458-mongock-legacy-targetsystem done")
152+
void failIfEmptyOrigin() {
153+
Runner flamingock = FlamingockFactory.getCommunityBuilder()
154+
.setAuditStore(new CouchbaseAuditStore(cluster, FLAMINGOCK_BUCKET_NAME)
155+
.withScopeName(FLAMINGOCK_SCOPE_NAME)
156+
.withAuditRepositoryName(FLAMINGOCK_COLLECTION_NAME))
157+
.build();
158+
159+
org.junit.jupiter.api.Assertions.assertThrows(
160+
io.flamingock.internal.common.core.error.FlamingockException.class,
161+
flamingock::run
162+
);
163+
}
164+
}

core/importer/flamingock-importer-couchbase-tests/src/test/java/io/flamingock/importer/couchbase/legacy/_0002__ClientUpdater.java renamed to legacy/mongock-importer-couchbase/src/test/java/io/flamingock/importer/mongock/couchbase/changes/MongockChange1.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.flamingock.importer.couchbase.legacy;
16+
package io.flamingock.importer.mongock.couchbase.changes;
1717

18-
import io.flamingock.api.annotations.Change;
19-
import io.flamingock.api.annotations.Apply;
18+
import io.mongock.api.annotations.ChangeUnit;
19+
import io.mongock.api.annotations.Execution;
2020

21-
@Change(id = "client-updater", author = "mongock", transactional = false)
22-
public class _0002__ClientUpdater {
21+
@ChangeUnit(id = "client-initializer", order = "1", author = "flamingock-team", transactional = false)
22+
public class MongockChange1 {
2323

24-
@Apply
24+
@Execution
2525
public void apply() {
2626
System.out.println("Client Initializer");
2727
}

core/importer/flamingock-importer-couchbase-tests/src/test/java/io/flamingock/importer/couchbase/legacy/_0001__ClientInitializer.java renamed to legacy/mongock-importer-couchbase/src/test/java/io/flamingock/importer/mongock/couchbase/changes/MongockChange2.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.flamingock.importer.couchbase.legacy;
16+
package io.flamingock.importer.mongock.couchbase.changes;
1717

1818
import io.flamingock.api.annotations.Change;
1919
import io.flamingock.api.annotations.Apply;
20+
import io.mongock.api.annotations.ChangeUnit;
2021

21-
@Change(id = "client-initializer", author = "mongock", transactional = false)
22-
public class _0001__ClientInitializer {
22+
@ChangeUnit(id = "client-updater", order = "2", author = "flamingock-team", transactional = false)
23+
public class MongockChange2 {
2324

2425
@Apply
2526
public void apply() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2023 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.importer.mongock.couchbase.changes;
17+
18+
import com.couchbase.client.java.Bucket;
19+
import com.couchbase.client.java.Collection;
20+
import com.couchbase.client.java.json.JsonObject;
21+
import io.flamingock.api.annotations.Apply;
22+
import io.flamingock.api.annotations.Change;
23+
24+
import java.util.Collections;
25+
26+
@Change(id = "create-users-collection", author = "importer", transactional = false)
27+
public class _0001__CreateUsersCollectionChange {
28+
29+
@Apply
30+
public void apply(Bucket bucket) {
31+
Collection collection = bucket.defaultCollection();
32+
33+
JsonObject user = JsonObject.create()
34+
.put("email", "[email protected]")
35+
.put("name", "Admin")
36+
.put("roles", Collections.singletonList("superuser"));
37+
38+
collection.upsert("user::[email protected]", user);
39+
}
40+
}

legacy/mongock-importer-dynamodb/build.gradle.kts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,22 @@ dependencies {
2222
}
2323

2424

25-
description = "A MongoDB migration utility that imports Mongock’s execution history into Flamingock-Community’s audit store for smooth project upgrades."
25+
description = "A DynamoDB migration utility that imports Mongock’s execution history into Flamingock-Community’s audit store for smooth project upgrades."
2626

2727

2828
java {
2929
toolchain {
3030
languageVersion.set(JavaLanguageVersion.of(8))
3131
}
3232
}
33+
tasks.withType<JavaCompile>().configureEach {
34+
if (name.contains("Test", ignoreCase = true)) {
35+
options.compilerArgs.addAll(listOf(
36+
"-Asources=${projectDir}/src/test/java",
37+
"-Aresources=${projectDir}/src/test/resources"
38+
))
39+
}
40+
}
41+
configurations.testImplementation {
42+
extendsFrom(configurations.compileOnly.get())
43+
}

0 commit comments

Comments
 (0)