-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add custom repository injection for atlas search
- Loading branch information
Showing
9 changed files
with
88 additions
and
44 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
...godb/src/test/kotlin/com/github/inflab/example/spring/data/mongodb/extension/AtlasTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
48 changes: 48 additions & 0 deletions
48
.../com/github/inflab/example/spring/data/mongodb/extension/AtlasTestConstructorExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
11 changes: 5 additions & 6 deletions
11
...hub/inflab/example/spring/data/mongodb/repository/EmbeddedDocumentSearchRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 5 additions & 6 deletions
11
...lin/com/github/inflab/example/spring/data/mongodb/repository/FacetSearchRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 5 additions & 6 deletions
11
...in/com/github/inflab/example/spring/data/mongodb/repository/PhraseSearchRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 5 additions & 6 deletions
11
...lin/com/github/inflab/example/spring/data/mongodb/repository/ScoreSearchRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 5 additions & 6 deletions
11
...tlin/com/github/inflab/example/spring/data/mongodb/repository/SearchMetaRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 5 additions & 6 deletions
11
...tlin/com/github/inflab/example/spring/data/mongodb/repository/TextSearchRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters