Skip to content

Commit

Permalink
Feature/#2219 enceladus to run with new supported version of mongo db (
Browse files Browse the repository at this point in the history
…#2225)

* actually use version 8.0 of mongo in integration test

---------

Co-authored-by: David Benedeki <14905969+benedeki@users.noreply.github.com>
  • Loading branch information
ABMC831 and benedeki authored Dec 23, 2024
1 parent 3227723 commit 18453fa
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 31 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 @@ -308,7 +308,8 @@ class SchemaApiFeaturesIntegrationSuite extends BaseRestApiTest with BeforeAndAf

val actual = response.getBody
val expected = UsedIn(Some(Seq(MenasReference(None, "dataset", 1))), Some(Seq()))
assert(actual == expected)
val expectedMongo4_4 = UsedIn(Some(Seq(MenasReference(Some("dataset"), "dataset", 1))), Some(Seq()))
assert(actual == expectedMongo4_4)
}
}
"some version of the Schema is used by a enabled MappingTable" should {
Expand All @@ -325,7 +326,8 @@ class SchemaApiFeaturesIntegrationSuite extends BaseRestApiTest with BeforeAndAf

val actual = response.getBody
val expected = UsedIn(Some(Seq()), Some(Seq(MenasReference(None, "mapping", 1))))
assert(actual == expected)
val expectedMongo4_4 = UsedIn(Some(Seq()), Some(Seq(MenasReference(Some("mapping_table"), "mapping", 1))))
assert(actual == expectedMongo4_4)
}
}
}
Expand Down Expand Up @@ -460,7 +462,8 @@ class SchemaApiFeaturesIntegrationSuite extends BaseRestApiTest with BeforeAndAf

val actual = response.getBody
val expected = UsedIn(Some(Seq(MenasReference(None, "dataset1", 1))), Some(Seq()))
assert(actual == expected)
val expectedMongo4_4 = UsedIn(Some(Seq(MenasReference(Some("dataset"), "dataset1", 1))), Some(Seq()))
assert(actual == expectedMongo4_4)
}
}
"some version of the Schema is used by a enabled MappingTable" should {
Expand All @@ -478,7 +481,8 @@ class SchemaApiFeaturesIntegrationSuite extends BaseRestApiTest with BeforeAndAf

val actual = response.getBody
val expected = UsedIn(Some(Seq()), Some(Seq(MenasReference(None, "mapping1", 1))))
assert(actual == expected)
val expectedMongo4_4 = UsedIn(Some(Seq()), Some(Seq(MenasReference(Some("mapping_table"), "mapping1", 1))))
assert(actual == expectedMongo4_4)
}
}
}
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,27 @@ 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.V8_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 = {
MongoClient(getMongoUri).getDatabase(database).withCodecRegistry(codecRegistry)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2018 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import org.junit.runner.RunWith
import org.mongodb.scala.model.Filters.equal
import org.mongodb.scala.model.Filters
import org.mongodb.scala.model.Projections.{computed, fields, include}
import org.mongodb.scala.{MongoCollection, MongoDatabase}
import org.scalatest.wordspec.AnyWordSpec
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.junit4.SpringRunner
import za.co.absa.enceladus.menas.integration.TestContextManagement
import za.co.absa.enceladus.model.menas.MenasReference

import scala.concurrent.Await
import scala.concurrent.duration.Duration

@RunWith(classOf[SpringRunner])
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles(Array("withEmbeddedMongo"))
class EmbededMongoIntegrationSuite extends AnyWordSpec with TestContextManagement {
@Autowired
val mongoDb: MongoDatabase = null

override def afterAll(): Unit = {
super.afterAll()
Await.result(mongoDb.drop().toFuture(), Duration.Inf)
}

s"mongo" can {
s"perform find" when {
s"version is above 4.4.1 or below 4.2.23" should {
s"populate or ignore computed field accordingly" in {

// Prior to v 4.4
// inlcuding field in find expression purely included it in result
// meaninig, original value / null value is preserved regardless of users wish to override it
// Since 4.4
// provided value is actually respected
// https://www.mongodb.com/docs/v4.4/release-notes/4.4-compatibility/#projection-compatibility-changes

val collection: MongoCollection[MenasReference] = mongoDb.getCollection[MenasReference]("TestMeCollection")
val sampleReference = MenasReference(None, "dedo jozef", 123)
val filter = Filters.and(equal("name", "dedo jozef"), equal("version", 123))
val mongoInsertQuery = collection.insertMany(Seq(sampleReference))
val mongoFindQuery = collection
.find[MenasReference](filter)
.projection(fields(include("name", "version"), computed("collection", "tato zlato")))

Await.result(mongoInsertQuery.toFuture(), Duration.Inf)
val actual = Await.result(mongoFindQuery.toFuture(), Duration.Inf)

val expectedV4_2_23orBefore = Seq(MenasReference(None, "dedo jozef", 123))
val expectedV4_4_1orAfter = Seq(MenasReference(Some("tato zlato"), "dedo jozef", 123))
assert(actual == expectedV4_4_1orAfter)
}
}
}
}
}
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 18453fa

Please sign in to comment.