Skip to content

Commit

Permalink
#2219 Enceladus to run with new, supported version of MongoDB
Browse files Browse the repository at this point in the history
new version of embeded mongo db gadget
  • Loading branch information
ABMC831 committed Nov 12, 2024
1 parent 8f6fa06 commit 3a51a43
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
4 changes: 2 additions & 2 deletions menas/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<scala.xml.version>1.0.4</scala.xml.version>
<webui.basedir>${project.basedir}/ui</webui.basedir>
<scalastyle.configLocation>${project.parent.basedir}/scalastyle-config.xml</scalastyle.configLocation>
<embedded.mongo.version>2.2.0</embedded.mongo.version>
<embedded.mongo.version>4.18.0</embedded.mongo.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -340,7 +340,7 @@
<args>
<arg>-Xfatal-warnings</arg>
<arg>-unchecked</arg>
<arg>-deprecation</arg>
<arg>-deprecation:false</arg>
<arg>-feature</arg>
</args>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

package za.co.absa.enceladus.menas.integration.mongo

import de.flapdoodle.embed.mongo.config.{MongodConfigBuilder, Net}
import de.flapdoodle.embed.mongo.distribution.Version
import de.flapdoodle.embed.mongo.{MongodExecutable, MongodStarter}
import de.flapdoodle.embed.process.runtime.Network
import de.flapdoodle.embed.mongo.transitions.{Mongod, RunningMongodProcess}
import de.flapdoodle.reverse.TransitionWalker

import javax.annotation.{PostConstruct, PreDestroy}
import org.mongodb.scala.{MongoClient, MongoDatabase}
import org.slf4j.LoggerFactory
Expand All @@ -33,43 +33,28 @@ import za.co.absa.enceladus.menas.utils.implicits.codecRegistry
@Profile(Array("withEmbeddedMongo"))
class EmbeddedMongo {
private val logger = LoggerFactory.getLogger(this.getClass)
private var mongodExecutable: MongodExecutable = _
private var mongoPort: Int = _

def getMongoUri: String = s"mongodb://localhost:$mongoPort/?ssl=false"
private var runningMongod: TransitionWalker.ReachedState[RunningMongodProcess] = _

def getMongoPort: Int = mongoPort
def getMongoUri: String = f"mongodb://${runningMongod.current().getServerAddress}"

@Value("${menas.mongo.connection.database}")
val database: String = ""

@PostConstruct
def runDummyMongo(): Unit = {
val starter = MongodStarter.getDefaultInstance

synchronized {
mongoPort = Network.getFreeServerPort()
val mongodConfig = new MongodConfigBuilder()
.version(Version.Main.V4_0)
.net(new Net("localhost", mongoPort, Network.localhostIsIPv6()))
.build()

mongodExecutable = starter.prepare(mongodConfig)
}

mongodExecutable.start()
logger.debug(s"*** mongod started at port $mongoPort")
runningMongod = Mongod.instance().start(Version.Main.V4_0)
logger.debug(s"*** mongod started at $getMongoUri")
}

@PreDestroy
def shutdownDummyMongo(): Unit = {
mongodExecutable.stop()
runningMongod.close()
}

@Primary // will override non-primary MongoDatabase-typed bean when in scope - here: the 'defaultMongoDb' bean
@Bean
def embeddedMongoDb: MongoDatabase = {
print(f"\n===TEST MONGO DB URI===: $getMongoUri ===\n")
MongoClient(getMongoUri).getDatabase(database).withCodecRegistry(codecRegistry)
}

}
2 changes: 1 addition & 1 deletion utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<args>
<arg>-Xfatal-warnings</arg>
<arg>-unchecked</arg>
<arg>-deprecation</arg>
<arg>-deprecation:false</arg>
<arg>-feature</arg>
</args>
</configuration>
Expand Down

0 comments on commit 3a51a43

Please sign in to comment.