Skip to content

Commit

Permalink
Added Test Cases
Browse files Browse the repository at this point in the history
  • Loading branch information
iammohdzaki committed Jul 15, 2024
1 parent 047cad0 commit 32d230d
Show file tree
Hide file tree
Showing 12 changed files with 412 additions and 124 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/code_scan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: "Code Scanning"

on: [push, pull_request]

env:
GRADLE_OPTS: -Dorg.gradle.jvmargs="-Xmx6g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8"

jobs:
build:
runs-on: 'ubuntu-latest'

env:
JWT_SECRET: ${{ secrets.JWT_SECRET }}

steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Java
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 19

- name: Build
uses: gradle/gradle-build-action@v3
with:
arguments: build --full-stacktrace

- name: Upload reports
if: always()
uses: actions/upload-artifact@v4
with:
name: 'reports-${{ matrix.os }}'
path: '**/build/reports/**'

- name: Upload Code Scanning code analysis report
if: always()
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: 'build/reports/detekt/main.sarif'
93 changes: 0 additions & 93 deletions .github/workflows/codeql.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/kotlin_lint.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: kotlin_lint
name: "Kotlin Lint Checks"

on:
push:
Expand Down
26 changes: 10 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
# Starter Project Ktor

[![Kotlin](https://img.shields.io/badge/Kotlin-2.0-blue.svg)](https://kotlinlang.org)
[![Ktor](https://img.shields.io/badge/Ktor-2.0.0-blueviolet.svg)](https://ktor.io)
[![MongoDB](https://img.shields.io/badge/MongoDB-5.0-green.svg)](https://www.mongodb.com)
[![Koin](https://img.shields.io/badge/Koin-3.1.2-lightgrey.svg)](https://insert-koin.io)
[![Swagger](https://img.shields.io/badge/Swagger-3.0.0-brightgreen.svg)](https://swagger.io)
[![codecov](https://codecov.io/github/iammohdzaki/Starter-Project-Ktor/graph/badge.svg?token=J779Y2CYPA)](https://codecov.io/github/iammohdzaki/Starter-Project-Ktor)
[![kotlin_lint](https://github.com/iammohdzaki/Starter-Project-Ktor/actions/workflows/kotlin_lint.yml/badge.svg)](https://github.com/iammohdzaki/Starter-Project-Ktor/actions/workflows/kotlin_lint.yml)
[![License](https://img.shields.io/github/license/iammohdzaki/Starter-Project-Ktor)](https://github.com/iammohdzaki/Starter-Project-Ktor/blob/main/LICENSE)

## Overview
Expand Down Expand Up @@ -69,14 +67,6 @@ mongodb {

Swagger is used for API documentation. Once the application is running, you can access the Swagger UI at `http://localhost:8080/docs`.

## Dependency Injection with Koin

This project uses Koin for dependency injection. Modules are defined in the `di` package and loaded in the `Application.kt` file.

## Serialization

Kotlin Serialization is used for converting Kotlin objects to JSON and vice versa. Ensure your data classes are annotated with `@Serializable`.

## Project Structure

```
Expand All @@ -90,12 +80,16 @@ Starter-Project-Ktor
└── src
├── main
│ ├── kotlin
│ │ ├── com.example
│ │ │ ├── di
│ │ │ ├── models
│ │ │ ├── routes
│ │ ├── di
│ │ ├── models
│ │ ├── routes
│ │ │ └── services
│ ├── resources
│ │ ├── languages
│ │ │ ├── lang_ar.json
│ │ │ └── lang_en.json
│ │ ├── openapi
│ │ │ └── documentation.yml
│ │ ├── application.conf
│ │ └── logback.xml
└── test
Expand Down
13 changes: 10 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ group = "com"
version = "0.0.1"
application {
mainClass.set("io.ktor.server.netty.EngineMain")

val isDevelopment: Boolean = project.ext.has("development")
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
}
Expand All @@ -24,12 +23,16 @@ repositories {
maven { url = uri("https://jitpack.io") }
}

tasks.test {
useJUnitPlatform()
}

dependencies {
//Swagger
implementation("io.ktor:ktor-server-swagger:$ktorVersion")

//Generator
implementation("com.github.iammohdzaki:Password-Generator:0.5")
implementation("com.github.iammohdzaki:Password-Generator:0.6")

// Mongo Database
implementation("org.litote.kmongo:kmongo:$kmongoVersion")
Expand Down Expand Up @@ -63,6 +66,10 @@ dependencies {
implementation("ch.qos.logback:logback-classic:$logbackVersion")

//Testing
testImplementation("io.ktor:ktor-server-tests-jvm:$ktorVersion")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.8.2")
testImplementation("io.ktor:ktor-server-tests:$ktorVersion")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion")
testImplementation("io.mockk:mockk:1.13.4")
testImplementation("io.insert-koin:koin-test:$koinVersion")
}
1 change: 0 additions & 1 deletion src/main/kotlin/com/api/routes/ConfigRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ fun Route.ConfigRoutes() {
call.respond(response)
}

// Not Working Will Check Later
post(Endpoints.UPDATE_CONFIG) {
val requestBody = getBodyContent<Configuration>()
val response = configController.updateConfiguration(requestBody, call.language())
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/com/api/routes/ServerRoutes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.api.routes

import io.ktor.server.application.call
import io.ktor.server.response.respondText
import io.ktor.server.routing.Route
import io.ktor.server.routing.get

fun Route.ServerRoutes() {
get("/") {
call.respondText("Server is Running!")
}
}
8 changes: 2 additions & 6 deletions src/main/kotlin/com/plugins/Routing.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.plugins

import com.api.routes.ConfigRoutes
import com.api.routes.ServerRoutes
import com.api.routes.UserRoutes
import io.ktor.server.application.Application
import io.ktor.server.application.call
import io.ktor.server.plugins.swagger.swaggerUI
import io.ktor.server.response.respondText
import io.ktor.server.routing.get
import io.ktor.server.routing.routing

fun Application.configureRouting() {
routing {
get("/") {
call.respondText("Server is Running!")
}
ServerRoutes()
swaggerUI(path = "docs", swaggerFile = "openapi/documentation.yaml")
UserRoutes()
ConfigRoutes()
Expand Down
12 changes: 8 additions & 4 deletions src/test/kotlin/com/ApplicationTest.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package com

import com.plugins.configureRouting
import com.api.routes.ServerRoutes
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpStatusCode
import io.ktor.server.config.MapApplicationConfig
import io.ktor.server.testing.testApplication
import kotlin.test.Test
import kotlin.test.assertEquals

class ApplicationTest {
@Test
fun testRoot() = testApplication {
application {
configureRouting()
routing {
ServerRoutes()
}
environment {
config = MapApplicationConfig("JWT_SECRET" to "TEST_SECRET_KEY")
}
client.get("/").apply {
assertEquals(HttpStatusCode.OK, status)
assertEquals("Hello World!", bodyAsText())
assertEquals("Server is Running!", bodyAsText())
}
}
}
19 changes: 19 additions & 0 deletions src/test/kotlin/com/api/Extensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.api

import com.plugins.configureSerialization
import io.ktor.server.config.MapApplicationConfig
import io.ktor.server.testing.ApplicationTestBuilder

fun ApplicationTestBuilder.configureServer() {
application {
configureSerialization()
}
environment {
config = MapApplicationConfig(
"jwt.issuer" to "http://0.0.0.0:8080",
"jwt.audience" to "users",
"jwt.realm" to "ktor starter app",
"JWT_SECRET" to "TEST_SECRET_KEY"
)
}
}
Loading

0 comments on commit 32d230d

Please sign in to comment.