-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-3418 Added volume, so mongodb data is in a subfolder; the servic…
…e closes when the server goes down; added tests
- Loading branch information
1 parent
b53847f
commit 9b3ef36
Showing
9 changed files
with
137 additions
and
21 deletions.
There are no files selected for viewing
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
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
6 changes: 3 additions & 3 deletions
6
mongodb/src/main/kotlin/com/example/entities/ArticleExtension.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
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
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 |
---|---|---|
@@ -1,21 +1,103 @@ | ||
package com.example | ||
|
||
import com.example.entities.Article | ||
import com.google.gson.Gson | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.server.testing.* | ||
import kotlin.test.* | ||
import io.ktor.http.* | ||
import com.example.plugins.* | ||
import com.example.service.ArticleService | ||
import io.ktor.client.statement.* | ||
import io.ktor.http.* | ||
|
||
class ApplicationTest { | ||
@Test | ||
fun testRoot() = testApplication { | ||
fun testPostArticle() = testApplication { | ||
application { | ||
val articleService = ArticleService() | ||
configureRouting(articleService = articleService) | ||
} | ||
client.post("/article") { | ||
header(HttpHeaders.Accept, ContentType.Application.Json) | ||
contentType(ContentType.Application.Json) | ||
val gson = Gson() | ||
setBody(gson.toJson(Article(title="Who are you?", body="Whatever you are, be a good one"))) | ||
}.apply { | ||
assertEquals(HttpStatusCode.Created, status) | ||
} | ||
} | ||
|
||
@Test | ||
fun testGetArticleId() = testApplication { | ||
application { | ||
val articleService = ArticleService() | ||
configureRouting(articleService = articleService) | ||
} | ||
val testId = getTestArticleId() | ||
client.get("/article/{id}") { | ||
parameter("id", testId) | ||
}.apply { | ||
assertEquals(HttpStatusCode.OK, status) | ||
} | ||
} | ||
|
||
@Test | ||
fun testDeleteArticleIdDelete() = testApplication { | ||
application { | ||
configureRouting() | ||
val articleService = ArticleService() | ||
configureRouting(articleService = articleService) | ||
} | ||
client.get("/").apply { | ||
val testId = getTestArticleId() | ||
client.delete("/article/{id}/delete") { | ||
parameter(key = "id", value = testId) | ||
}.apply { | ||
assertEquals(HttpStatusCode.OK, status) | ||
assertEquals("Hello World!", bodyAsText()) | ||
assertEquals("Article was deleted", bodyAsText()) | ||
} | ||
} | ||
|
||
@Test | ||
fun testPutArticleIdEdit() = testApplication { | ||
application { | ||
val articleService = ArticleService() | ||
configureRouting(articleService = articleService) | ||
} | ||
client.put("/article/{id}/edit") { | ||
parameter(key = "id", value = getTestArticleId()) | ||
header(HttpHeaders.Accept, ContentType.Application.Json) | ||
contentType(ContentType.Application.Json) | ||
val gson = Gson() | ||
setBody( | ||
gson.toJson( | ||
Article( | ||
title="Find new opportunities", | ||
body="Opportunities don't happen, you create them" | ||
) | ||
) | ||
) | ||
}.apply { | ||
assertEquals(HttpStatusCode.OK, status) | ||
assertEquals("Article was edited", bodyAsText()) | ||
} | ||
} | ||
|
||
@Test | ||
fun testGetArticleList() = testApplication { | ||
application { | ||
val articleService = ArticleService() | ||
configureRouting(articleService = articleService) | ||
} | ||
client.get("/article/list").apply { | ||
assertEquals(HttpStatusCode.OK, status) | ||
} | ||
} | ||
|
||
fun getTestArticleId(): String? { | ||
val article = Article(title = "title", body = "body") | ||
val articleService = ArticleService() | ||
articleService.create(article)?.let { userId -> | ||
return userId.toString() | ||
} | ||
return null | ||
} | ||
} |
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