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
fun main(args: Array<String>) { | |
val environment = System.getenv()["ENVIRONMENT"] ?: handleDefaultEnvironment() | |
val config = extractConfig(environment, HoconApplicationConfig(ConfigFactory.load())) | |
embeddedServer(Netty, port = config.port) { | |
println("Starting instance in ${config.host}:${config.port}") | |
module { | |
install(Koin) { | |
modules( |
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
fun Application.module() { | |
val userApi by inject<UserApi>() | |
val databaseProvider by inject<DatabaseProviderContract>() | |
val jwtVerifier by inject<JWTVerifier>() | |
//Init database here | |
databaseProvider.init() | |
install(CallLogging) { | |
level = Level.DEBUG |
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
fun Routing.registrationModule() { | |
val unauthenticatedController by inject<RegistrationController>() | |
post("user") { | |
val postUser = call.receive<PostUserBody>() | |
val user = unauthenticatedController.createUser(postUser) | |
call.respond(user) | |
} |
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
class RegistrationControllerImp : BaseController(), RegistrationController, KoinComponent { | |
private val userApi by inject<UserApi>() | |
private val passwordManager by inject<PasswordManagerContract>() | |
private val tokenProvider by inject<TokenProvider>() | |
override suspend fun createUser(postUser: PostUserBody): ResponseUser { | |
val user = dbQuery { | |
userApi.getUserByUsername(postUser.username)?.let { | |
throw InvalidUserException("User is already taken") |
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
fun <R> withBaseTestApplication(test: TestApplicationEngine.() -> R) { | |
withTestApplication({ | |
install(ContentNegotiation) { gson { } } | |
koinModules?.let { | |
install(Koin) { | |
modules(it) | |
} | |
} | |
moduleList() | |
}) { |
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
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | |
class RegistrationRoutingTest : BaseRoutingTest() { | |
private val registrationController: RegistrationController = mockk() | |
@BeforeAll | |
fun setup() { | |
koinModules = module { | |
single { registrationController } | |
} |
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
class RegistrationControllerImp : BaseController(), RegistrationController, KoinComponent { | |
private val userApi by inject<UserApi>() | |
override suspend fun createUser(postUser: PostUserBody): ResponseUser { | |
val user = dbQuery { | |
userApi.getUserByUsername(postUser.username)?.let { | |
throw InvalidUserException("User is already taken") | |
} | |
userApi.createUser(postUser) ?: throw UnknownError("Internal server error") |
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
abstract class BaseController : KoinComponent { | |
private val dbProvider by inject<DatabaseProviderContract>() | |
suspend fun <T> dbQuery(block: () -> T): T { | |
return dbProvider.dbQuery(block) | |
} | |
} | |
class DatabaseProvider : DatabaseProviderContract, KoinComponent { |
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
abstract class BaseControllerTest { | |
private val databaseProvider: DatabaseProviderContract = mockk() | |
init { | |
stopKoin() | |
} | |
open fun before() { | |
clearMocks(databaseProvider) |
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
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | |
class CreateUserTest : BaseControllerTest() { | |
private val userApi: UserApi = mockk() | |
private val controller: RegistrationController by lazy { RegistrationControllerImp() } | |
private val userId = 11 | |
init { | |
startInjection( | |
module { |
OlderNewer