Skip to content

Commit

Permalink
test: add custom repository injection for atlas search
Browse files Browse the repository at this point in the history
  • Loading branch information
jbl428 committed Sep 24, 2023
1 parent eaede28 commit a338e43
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.github.inflab.example.spring.data.mongodb.extension

internal annotation class AtlasTest(val database: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.inflab.example.spring.data.mongodb.extension

import io.kotest.core.annotation.AutoScan
import io.kotest.core.extensions.ConstructorExtension
import io.kotest.core.spec.Spec
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory
import kotlin.reflect.KClass
import kotlin.reflect.full.primaryConstructor

@AutoScan
internal object AtlasTestConstructorExtension : ConstructorExtension {
private const val ATLAS_DOMAIN = "<username>:<password>@<host>"
override fun <T : Spec> instantiate(clazz: KClass<T>): Spec? {
val atlasTest = clazz.annotations.find { it is AtlasTest } as AtlasTest? ?: return null

val testConstructor = clazz.primaryConstructor
if (testConstructor == null || testConstructor.parameters.isEmpty()) {
return null
}

val connectionString = "mongodb+srv://$ATLAS_DOMAIN/${atlasTest.database}?retryWrites=true&w=majority"
val mongoTemplate = MongoTemplate(SimpleMongoClientDatabaseFactory(connectionString))

val parameters = testConstructor.parameters.associateWith { parameter ->
val parameterClass = parameter.type.classifier as KClass<*>
check(parameterClass.annotations.any { it is org.springframework.stereotype.Repository }) {
"The parameter type of constructor must be annotated with @Repository but ${parameterClass.qualifiedName}"
}
val repositoryConstructor = checkNotNull(parameterClass.primaryConstructor) {
"The parameter type of constructor must have primary constructor but ${parameterClass.qualifiedName}"
}

val repositoryParameters = repositoryConstructor.parameters.associateWith { repositoryParameter ->
val repositoryParameterClass = repositoryParameter.type.classifier as KClass<*>
check(repositoryParameterClass == MongoTemplate::class) {
"The parameter type of repository constructor must be MongoTemplate but ${repositoryParameterClass.qualifiedName} from ${parameterClass.qualifiedName}"
}

mongoTemplate
}

repositoryConstructor.callBy(repositoryParameters)
}

return testConstructor.callBy(parameters)
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.github.inflab.example.spring.data.mongodb.repository

import com.github.inflab.example.spring.data.mongodb.extension.AtlasTest
import io.kotest.core.annotation.Ignored
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.collections.shouldBeMonotonicallyDecreasing
import io.kotest.matchers.longs.shouldBeGreaterThan
import io.kotest.matchers.shouldBe
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory

@Ignored
internal class EmbeddedDocumentSearchRepositoryTest : FreeSpec({
val connectionString = "mongodb+srv://<username>:<password>@<host>/sample_supplies?retryWrites=true&w=majority"
val mongoTemplate = MongoTemplate(SimpleMongoClientDatabaseFactory(connectionString))
val embeddedDocumentSearchRepository = EmbeddedDocumentSearchRepository(mongoTemplate)
@AtlasTest(database = "sample_supplies")
internal class EmbeddedDocumentSearchRepositoryTest(
private val embeddedDocumentSearchRepository: EmbeddedDocumentSearchRepository,
) : FreeSpec({

"findItemsByMean" {
// when
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.github.inflab.example.spring.data.mongodb.repository

import com.github.inflab.example.spring.data.mongodb.extension.AtlasTest
import io.kotest.core.annotation.Ignored
import io.kotest.core.spec.style.FreeSpec
import io.kotest.inspectors.forAll
import io.kotest.matchers.collections.shouldBeMonotonicallyDecreasing
import io.kotest.matchers.longs.shouldBeGreaterThan
import io.kotest.matchers.shouldBe
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory
import java.util.Date

@Ignored
internal class FacetSearchRepositoryTest : FreeSpec({
val connectionString = "mongodb+srv://<username>:<password>@<host>/sample_mflix?retryWrites=true&w=majority"
val mongoTemplate = MongoTemplate(SimpleMongoClientDatabaseFactory(connectionString))
val facetSearchRepository = FacetSearchRepository(mongoTemplate)
@AtlasTest(database = "sample_mflix")
internal class FacetSearchRepositoryTest(
private val facetSearchRepository: FacetSearchRepository,
) : FreeSpec({

"findYearWithStringFacet" {
// when
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.github.inflab.example.spring.data.mongodb.repository

import com.github.inflab.example.spring.data.mongodb.extension.AtlasTest
import io.kotest.core.annotation.Ignored
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.collections.shouldBeMonotonicallyDecreasing
import io.kotest.matchers.equals.shouldBeEqual
import io.kotest.matchers.shouldBe
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory

@Ignored
class PhraseSearchRepositoryTest : FreeSpec({
val connectionString = "mongodb+srv://<username>:<password>@<host>/sample_mflix?retryWrites=true&w=majority"
val mongoTemplate = MongoTemplate(SimpleMongoClientDatabaseFactory(connectionString))
val phraseSearchRepository = PhraseSearchRepository(mongoTemplate)
@AtlasTest(database = "sample_mflix")
internal class PhraseSearchRepositoryTest(
private val phraseSearchRepository: PhraseSearchRepository,
) : FreeSpec({

"findTitleWithNewYork" {
// when
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package com.github.inflab.example.spring.data.mongodb.repository

import com.github.inflab.example.spring.data.mongodb.extension.AtlasTest
import io.kotest.core.annotation.Ignored
import io.kotest.core.spec.style.FreeSpec
import io.kotest.inspectors.forAll
import io.kotest.matchers.date.shouldBeBetween
import io.kotest.matchers.shouldBe
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory
import java.time.LocalDateTime

@Ignored
internal class RangeSearchRepositoryTest : FreeSpec({
val connectionString = "mongodb+srv://<username>:<password>@<host>/sample_mflix?retryWrites=true&w=majority"
val mongoTemplate = MongoTemplate(SimpleMongoClientDatabaseFactory(connectionString))
val textSearchRepository = RangeSearchRepository(mongoTemplate)
@AtlasTest(database = "sample_mflix")
internal class RangeSearchRepositoryTest(
private val rangeSearchRepository: RangeSearchRepository,
) : FreeSpec({

"findRuntimeBetween" {
// when
val result = textSearchRepository.findRuntimeBetween()
val result = rangeSearchRepository.findRuntimeBetween()

// then
result.mappedResults.take(5).map { it.title } shouldBe listOf(
Expand All @@ -35,7 +34,7 @@ internal class RangeSearchRepositoryTest : FreeSpec({
val start = LocalDateTime.of(2010, 1, 1, 0, 0, 0)
val end = LocalDateTime.of(2015, 1, 1, 0, 0, 0)

val result = textSearchRepository.findReleasedBetween()
val result = rangeSearchRepository.findReleasedBetween()

// then
result.mappedResults.take(5).map { it.released }.forAll {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.github.inflab.example.spring.data.mongodb.repository

import com.github.inflab.example.spring.data.mongodb.extension.AtlasTest
import io.kotest.core.annotation.Ignored
import io.kotest.core.spec.style.FreeSpec
import io.kotest.inspectors.forAll
import io.kotest.matchers.doubles.shouldBeBetween
import io.kotest.matchers.doubles.shouldBeGreaterThan
import io.kotest.matchers.shouldBe
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory

@Ignored
internal class ScoreSearchRepositoryTest : FreeSpec({
val connectionString = "mongodb+srv://<username>:<password>@<host>/sample_mflix?retryWrites=true&w=majority"
val mongoTemplate = MongoTemplate(SimpleMongoClientDatabaseFactory(connectionString))
val scoreSearchRepository = ScoreSearchRepository(mongoTemplate)
@AtlasTest(database = "sample_mflix")
internal class ScoreSearchRepositoryTest(
private val scoreSearchRepository: ScoreSearchRepository,
) : FreeSpec({

"findTitleAndPlotWithBoost" {
// when
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.github.inflab.example.spring.data.mongodb.repository

import com.github.inflab.example.spring.data.mongodb.extension.AtlasTest
import io.kotest.core.annotation.Ignored
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.longs.shouldBeGreaterThan
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory

@Ignored
internal class SearchMetaRepositoryTest : FreeSpec({
val connectionString = "mongodb+srv://<username>:<password>@<host>/sample_mflix?retryWrites=true&w=majority"
val mongoTemplate = MongoTemplate(SimpleMongoClientDatabaseFactory(connectionString))
val searchMetaRepository = SearchMetaRepository(mongoTemplate)
@AtlasTest(database = "sample_mflix")
internal class SearchMetaRepositoryTest(
private val searchMetaRepository: SearchMetaRepository,
) : FreeSpec({

"findCountBetweenYear" {
// when
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.github.inflab.example.spring.data.mongodb.repository

import com.github.inflab.example.spring.data.mongodb.extension.AtlasTest
import io.kotest.core.annotation.Ignored
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.collections.shouldBeMonotonicallyDecreasing
import io.kotest.matchers.shouldBe
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory

@Ignored
internal class TextSearchRepositoryTest : FreeSpec({
val connectionString = "mongodb+srv://<username>:<password>@<host>/sample_mflix?retryWrites=true&w=majority"
val mongoTemplate = MongoTemplate(SimpleMongoClientDatabaseFactory(connectionString))
val textSearchRepository = TextSearchRepository(mongoTemplate)
@AtlasTest(database = "sample_mflix")
internal class TextSearchRepositoryTest(
private val textSearchRepository: TextSearchRepository,
) : FreeSpec({

"findTitleWithSufer" {
// when
Expand Down

0 comments on commit a338e43

Please sign in to comment.