Skip to content

Commit

Permalink
fix: tests (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
dieppa authored Dec 7, 2024
1 parent 233b42f commit 5ce54cf
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.flamingock.examples.mongodb.springboot.springdata.events.StageCompletedListener;
import io.flamingock.examples.mongodb.springboot.springdata.events.StageFailedListener;
import io.flamingock.examples.mongodb.springboot.springdata.events.StageStartedListener;
import io.flamingock.examples.mongodb.springboot.springdata.mongock.MongockExecutor;
import io.flamingock.examples.mongodb.springboot.springdata.mongock.MongockLegacyChangeUnit;
import io.flamingock.springboot.v2.context.EnableFlamingock;
import io.mongock.driver.mongodb.sync.v4.driver.MongoSync4Driver;
Expand All @@ -52,7 +53,7 @@ public class MongodbSpringbootSpringdata {
public final static String CLIENTS_COLLECTION_NAME = "clientCollection";

public static void main(String[] args) {
addMongockLegacyData();
MongockExecutor.addMongockLegacyData("mongodb://localhost:27017/", DATABASE_NAME);
SpringApplication.run(MongodbSpringbootSpringdata.class, args);
}

Expand Down Expand Up @@ -80,29 +81,5 @@ public PipelineFailedListener sailedFlamingockListener() {
@Bean
public StageFailedListener stageFailedListener() {return new StageFailedListener();}

private static void addMongockLegacyData() {
MongoClient mongoClient = getMongoClient("mongodb://localhost:27017/");
MongoSync4Driver mongockSync4Driver = io.mongock.driver.mongodb.sync.v4.driver.MongoSync4Driver
.withDefaultLock(mongoClient, DATABASE_NAME);

MongockStandalone.builder()
.setDriver(mongockSync4Driver)
.addMigrationClass(MongockLegacyChangeUnit.class)
.setTrackIgnored(true)
.setTransactional(true)
.buildRunner()
.execute();
}

private static MongoClient getMongoClient(String connectionString) {
CodecRegistry codecRegistry = fromRegistries(CodecRegistries.fromCodecs(new ZonedDateTimeCodec()),
MongoClientSettings.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder().automatic(true).build()));

MongoClientSettings.Builder builder = MongoClientSettings.builder();
builder.applyConnectionString(new ConnectionString(connectionString));
builder.codecRegistry(codecRegistry);
MongoClientSettings build = builder.build();
return MongoClients.create(build);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.flamingock.examples.mongodb.springboot.springdata.mongock;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import io.flamingock.examples.mongodb.springboot.springdata.ZonedDateTimeCodec;
import io.mongock.driver.mongodb.sync.v4.driver.MongoSync4Driver;
import io.mongock.runner.standalone.MongockStandalone;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.pojo.PojoCodecProvider;

import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;

public final class MongockExecutor {

private MongockExecutor(){}

public static void addMongockLegacyData(String mongoUrl, String database) {
MongoClient mongoClient = getMongoClient(mongoUrl);
MongoSync4Driver mongockSync4Driver = io.mongock.driver.mongodb.sync.v4.driver.MongoSync4Driver
.withDefaultLock(mongoClient, database);

MongockStandalone.builder()
.setDriver(mongockSync4Driver)
.addMigrationClass(MongockLegacyChangeUnit.class)
.setTrackIgnored(true)
.setTransactional(true)
.buildRunner()
.execute();
}

private static MongoClient getMongoClient(String connectionString) {
CodecRegistry codecRegistry = fromRegistries(CodecRegistries.fromCodecs(new ZonedDateTimeCodec()),
MongoClientSettings.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder().automatic(true).build()));

MongoClientSettings.Builder builder = MongoClientSettings.builder();
builder.applyConnectionString(new ConnectionString(connectionString));
builder.codecRegistry(codecRegistry);
MongoClientSettings build = builder.build();
return MongoClients.create(build);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,52 @@ void flamingockLogsTest() {
.find()
.into(new ArrayList<>());

Document aCreateCollection = flamingockDocuments.get(0);

//New changes added
Document aCreateCollection = flamingockDocuments.get(5);
assertEquals("create-collection", aCreateCollection.get("changeId"));
assertEquals("EXECUTED", aCreateCollection.get("state"));
assertEquals("io.flamingock.examples.mongodb.springboot.springdata.changes.ACreateCollection", aCreateCollection.get("changeLogClass"));

Document bInsertDocument = flamingockDocuments.get(1);
Document bInsertDocument = flamingockDocuments.get(6);
assertEquals("insert-document", bInsertDocument.get("changeId"));
assertEquals("EXECUTED", bInsertDocument.get("state"));
assertEquals("io.flamingock.examples.mongodb.springboot.springdata.changes.BInsertDocument", bInsertDocument.get("changeLogClass"));

Document cInsertAnotherDocument = flamingockDocuments.get(2);
Document cInsertAnotherDocument = flamingockDocuments.get(7);
assertEquals("insert-another-document", cInsertAnotherDocument.get("changeId"));
assertEquals("EXECUTED", cInsertAnotherDocument.get("state"));
assertEquals("io.flamingock.examples.mongodb.springboot.springdata.changes.CInsertAnotherDocument", cInsertAnotherDocument.get("changeLogClass"));

assertEquals(3, flamingockDocuments.size());
//Form importing mongock legacy data
Document mongoSystemChangeBefore = flamingockDocuments.get(0);
assertEquals("system-change-00001_before", mongoSystemChangeBefore.get("changeId"));
assertEquals("EXECUTED", mongoSystemChangeBefore.get("state"));
assertEquals("io.mongock.runner.core.executor.system.changes.SystemChangeUnit00001", mongoSystemChangeBefore.get("changeLogClass"));

Document mongoSystemChange = flamingockDocuments.get(1);
assertEquals("system-change-00001", mongoSystemChange.get("changeId"));
assertEquals("EXECUTED", mongoSystemChange.get("state"));
assertEquals("io.mongock.runner.core.executor.system.changes.SystemChangeUnit00001", mongoSystemChange.get("changeLogClass"));

Document changeUnitExecutedInMongockBefore = flamingockDocuments.get(2);
assertEquals("legacy-mongock-change-unit_before", changeUnitExecutedInMongockBefore.get("changeId"));
assertEquals("EXECUTED", changeUnitExecutedInMongockBefore.get("state"));
assertEquals("io.flamingock.examples.mongodb.springboot.springdata.mongock.MongockLegacyChangeUnit", changeUnitExecutedInMongockBefore.get("changeLogClass"));

Document changeUnitExecutedInMongock = flamingockDocuments.get(3);
assertEquals("legacy-mongock-change-unit", changeUnitExecutedInMongock.get("changeId"));
assertEquals("EXECUTED", changeUnitExecutedInMongock.get("state"));
assertEquals("io.flamingock.examples.mongodb.springboot.springdata.mongock.MongockLegacyChangeUnit", changeUnitExecutedInMongock.get("changeLogClass"));

Document legacyImporterChangeUnit = flamingockDocuments.get(4);
assertEquals("mongock-local-legacy-importer-mongodb-3", legacyImporterChangeUnit.get("changeId"));
assertEquals("EXECUTED", legacyImporterChangeUnit.get("state"));
assertEquals("io.flamingock.oss.driver.mongodb.sync.v4.internal.mongock.MongockLocalLegacyImporterChangeUnit", legacyImporterChangeUnit.get("changeLogClass"));


//8 changes: 3 new changes we are adding plus that come from legacy importer
assertEquals(8, flamingockDocuments.size());
}


Expand All @@ -119,8 +149,9 @@ void events() {
assertTrue(pipelineCompletedListener.executed);
assertFalse(pipelineFailedListener.executed);

assertEquals(1, stageStartedListener.executed);
assertEquals(1, stageCompletedListener.executed);
//2 because the importer runs on its own stage
assertEquals(2, stageStartedListener.executed);
assertEquals(2, stageCompletedListener.executed);
assertEquals(0, stageFailedListener.executed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.flamingock.examples.mongodb.springboot.springdata.config;

import io.flamingock.examples.mongodb.springboot.springdata.mongock.MongockExecutor;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
Expand All @@ -36,10 +37,15 @@ public class MongoInitializer implements ApplicationContextInitializer<Configur
@Override
public void initialize(ConfigurableApplicationContext context) {
mongoDBContainer.start();
String replicaSetUrl = mongoDBContainer.getReplicaSetUrl();
List<String> addedProperties = Collections.singletonList(
"spring.data.mongodb.uri=" + mongoDBContainer.getReplicaSetUrl()
"spring.data.mongodb.uri=" + replicaSetUrl
);
TestPropertyValues.of(addedProperties).applyTo(context.getEnvironment());
String[] urlChunks = replicaSetUrl.split("/");
String database = urlChunks[urlChunks.length - 1 ];
String mongoHost = urlChunks[urlChunks.length - 2 ];
MongockExecutor.addMongockLegacyData("mongodb://" + mongoHost + "/", database);
}
}

0 comments on commit 5ce54cf

Please sign in to comment.