• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

raharrison/kotlin-ktor-exposed-starter: Starter RESTful service with websocket n ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

raharrison/kotlin-ktor-exposed-starter

开源软件地址(OpenSource Url):

https://github.com/raharrison/kotlin-ktor-exposed-starter

开源编程语言(OpenSource Language):

Kotlin 90.4%

开源软件介绍(OpenSource Introduction):

Kotlin Ktor Build codecov

Starter project to create a simple RESTful web service in Kotlin

Updated for Kotlin 1.6.20 and Ktor 2.0.0

Companion article: https://ryanharrison.co.uk/2018/04/14/kotlin-ktor-exposed-starter.html

Getting Started

  1. Clone the repo.
  2. In the root directory execute ./gradlew run
  3. By default, the server will start on port 8080. See below Routes section for more information.

Libraries used:

The starter project creates a new in-memory H2 database with one table for Widget instances.

As ktor is async and based on coroutines, standard blocking JDBC may cause performance issues when used directly on the main thread pool (as threads must be reused for other requests). Therefore, another dedicated thread pool is created for all database queries, alongside connection pooling with HikariCP.

Routes:

GET /widgets --> get all widgets in the database

GET /widgets/{id} --> get one widget instance by id (integer)

POST /widgets --> add a new widget to the database by providing a JSON object (converted to a NewWidget instance). e.g -

{
    "name": "new widget",
    "quantity": 64
}

returns

{
    "id": 3,
    "name": "new widget",
    "quantity": 64,
    "dateUpdated": 1519926898
}

PUT /widgets --> update an existing widgets name or quantity. Pass in the id in the JSON request to determine which record to update

DELETE /widgets/{id} --> delete the widget with the specified id

Notifications (WebSocket)

All updates (creates, updates and deletes) to Widget instances are served as notifications through a WebSocket endpoint:

WS /updates --> returns Notification instances containing the change type, id and entity (if applicable) e.g:

{ 
    "type": "CREATE", 
    "id": 12, 
    "entity": { 
      "id": 12, 
      "name": "widget1", 
      "quantity": 5, 
      "dateUpdated": 1533583858169 
    }
}

The websocket listener will also log out any text messages send by the client. Refer to this blog post for some useful tools to test the websocket behaviour.

Testing

The sample Widget service and corresponding endpoints are also tested with 100% coverage. Upon startup of the main JUnit suite (via the test source folder), the server is started ready for testing and is torn down after all tests are run.

  • Unit testing of services with AssertJ - DAO and business logic is tested by initialising an in-memory H2 database with Exposed, using the same schema as the main app. With this approach database queries are fully tested without any mocking.
  • Integration testing of endpoints using a fully running server with Rest Assured - routing tests/status codes/response structure. This utilises the fact that Ktor is a small microframework that can be easily spun up and down as part of the test suite. You could also use the special test engine that Ktor provides, however my preference is to always start a full version of the server so that HTTP behaviour can be tested without relying on special internal mechanisms.



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap