Skip to content

Instantly share code, notes, and snippets.

View mathias21's full-sized avatar

Jorge mathias21

View GitHub Profile
@mathias21
mathias21 / Application.kt
Created July 28, 2020 20:40
KtorEasy main function
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(
@mathias21
mathias21 / Module.kt
Created July 28, 2020 20:50
KtorEasy server setup
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
@mathias21
mathias21 / RegistrationModule.kt
Created July 28, 2020 21:13
KtorEasy route definition
fun Routing.registrationModule() {
val unauthenticatedController by inject<RegistrationController>()
post("user") {
val postUser = call.receive<PostUserBody>()
val user = unauthenticatedController.createUser(postUser)
call.respond(user)
}
@mathias21
mathias21 / RegistrationController.kt
Created July 28, 2020 21:31
KtorEasy controller layer
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")
@mathias21
mathias21 / BaseRoutingTest.kt
Created July 31, 2020 10:58
KtorEasy routing testing tooling 1
fun <R> withBaseTestApplication(test: TestApplicationEngine.() -> R) {
withTestApplication({
install(ContentNegotiation) { gson { } }
koinModules?.let {
install(Koin) {
modules(it)
}
}
moduleList()
}) {
@mathias21
mathias21 / RegistrationRoutingTest.kt
Last active July 31, 2020 11:08
KtorEasy routing testing example 1
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class RegistrationRoutingTest : BaseRoutingTest() {
private val registrationController: RegistrationController = mockk()
@BeforeAll
fun setup() {
koinModules = module {
single { registrationController }
}
@mathias21
mathias21 / RegistrationControllerImp.kt
Created August 3, 2020 21:45
KtorEasy controller testing: controller's overview
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")
@mathias21
mathias21 / BaseControllerDbQuery.kt
Last active August 3, 2020 21:59
KtorEasy controller testing: base controller
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 {
@mathias21
mathias21 / BaseControllerTest.kt
Created August 3, 2020 22:17
KtorEasy controller testing: instrumentation 1
abstract class BaseControllerTest {
private val databaseProvider: DatabaseProviderContract = mockk()
init {
stopKoin()
}
open fun before() {
clearMocks(databaseProvider)
@mathias21
mathias21 / CreateUserTest.kt
Last active August 3, 2020 22:43
KtorEasy controller testing: controller's testing
@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 {