From 66685246716ae7ea17ea9aee8c5e79402ea61b52 Mon Sep 17 00:00:00 2001 From: Do Trong Long Date: Thu, 13 Feb 2020 22:47:49 +0800 Subject: [PATCH] v2.1.0 (#21) Rebuild plugins --- mocks/delay.json | 9 ++++++--- mocks/users_random.json | 8 ++++---- mocks/users_timestamp.json | 11 +++++++---- .../com/dotronglong/faker/contract/Plugin.kt | 3 ++- .../kotlin/com/dotronglong/faker/pojo/Spec.kt | 7 ++++++- .../faker/service/handler/JsonSpecHandler.kt | 17 +++++++++-------- .../faker/service/plugin/BasePlugin.kt | 8 +++++++- .../faker/service/plugin/DelayResponsePlugin.kt | 7 ++++--- .../faker/service/plugin/JsonResponsePlugin.kt | 7 ++++--- .../faker/service/plugin/RandomPlugin.kt | 6 ++++-- .../faker/service/plugin/TimestampPlugin.kt | 15 ++++----------- src/main/resources/application.properties | 2 +- .../dotronglong/faker/FakerApplicationTests.kt | 4 ++-- 13 files changed, 60 insertions(+), 44 deletions(-) diff --git a/mocks/delay.json b/mocks/delay.json index d6a8dba..3c23226 100644 --- a/mocks/delay.json +++ b/mocks/delay.json @@ -1,7 +1,10 @@ { - "plugins": { - "delay": 500 - }, + "plugins": [ + { + "name": "delay", + "args": { "duration": 500 } + } + ], "request": { "method": "get", "path": "/delay" diff --git a/mocks/users_random.json b/mocks/users_random.json index 7109172..8ad0ee2 100644 --- a/mocks/users_random.json +++ b/mocks/users_random.json @@ -1,10 +1,10 @@ { - "plugins": { - "random": true - }, + "plugins": [ + {"name": "random"} + ], "request": { "method": "GET", - "path": "/v1/users?random" + "path": "/random" }, "response": { "body": [ diff --git a/mocks/users_timestamp.json b/mocks/users_timestamp.json index bf0b042..e7ad696 100644 --- a/mocks/users_timestamp.json +++ b/mocks/users_timestamp.json @@ -1,10 +1,13 @@ { - "plugins": { - "timestamp": true - }, + "plugins": [ + { + "name": "timestamp", + "args": { "in": "milliseconds" } + } + ], "request": { "method": "GET", - "path": "/v1/users?timestamp" + "path": "/timestamp" }, "response": { "body": [ diff --git a/src/main/kotlin/com/dotronglong/faker/contract/Plugin.kt b/src/main/kotlin/com/dotronglong/faker/contract/Plugin.kt index ade37ea..3497997 100644 --- a/src/main/kotlin/com/dotronglong/faker/contract/Plugin.kt +++ b/src/main/kotlin/com/dotronglong/faker/contract/Plugin.kt @@ -5,5 +5,6 @@ import reactor.core.publisher.Mono interface Plugin { val name: String - fun run(response: MutableResponse, parameters: Any): Mono + fun run(response: MutableResponse, arguments: Map?): Mono + fun run(response: MutableResponse): Mono = run(response, null) } \ No newline at end of file diff --git a/src/main/kotlin/com/dotronglong/faker/pojo/Spec.kt b/src/main/kotlin/com/dotronglong/faker/pojo/Spec.kt index 5d6467f..bdc13fb 100644 --- a/src/main/kotlin/com/dotronglong/faker/pojo/Spec.kt +++ b/src/main/kotlin/com/dotronglong/faker/pojo/Spec.kt @@ -1,7 +1,7 @@ package com.dotronglong.faker.pojo data class Spec( - val plugins: Map?, + val plugins: List?, val request: Request, val response: Response ) { @@ -19,4 +19,9 @@ data class Spec( var headers: Map?, var body: Any? ) + + data class Plugin( + val name: String, + val args: Map? + ) } \ No newline at end of file diff --git a/src/main/kotlin/com/dotronglong/faker/service/handler/JsonSpecHandler.kt b/src/main/kotlin/com/dotronglong/faker/service/handler/JsonSpecHandler.kt index cd4a22d..9df7e2a 100644 --- a/src/main/kotlin/com/dotronglong/faker/service/handler/JsonSpecHandler.kt +++ b/src/main/kotlin/com/dotronglong/faker/service/handler/JsonSpecHandler.kt @@ -4,7 +4,10 @@ import com.dotronglong.faker.contract.Handler import com.dotronglong.faker.contract.Plugin import com.dotronglong.faker.pojo.MutableResponse import com.dotronglong.faker.pojo.Spec -import com.dotronglong.faker.service.plugin.* +import com.dotronglong.faker.service.plugin.DelayResponsePlugin +import com.dotronglong.faker.service.plugin.JsonResponsePlugin +import com.dotronglong.faker.service.plugin.RandomPlugin +import com.dotronglong.faker.service.plugin.TimestampPlugin import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.registerKotlinModule import org.slf4j.Logger @@ -17,7 +20,7 @@ import reactor.core.publisher.Mono class JsonSpecHandler constructor(private val spec: Spec) : Handler { private val logger: Logger = LoggerFactory.getLogger(javaClass) - private val plugins: HashMap = HashMap() + private val plugins: MutableMap = HashMap() private val mapper = ObjectMapper() init { @@ -35,15 +38,13 @@ class JsonSpecHandler constructor(private val spec: Spec) : Handler { val mutableResponse = MutableResponse(statusCode, mapper.writeValueAsString(spec.response.body), HashMap()) val tasks: MutableList> = ArrayList() if (spec.plugins != null) { - for ((name, parameters) in spec.plugins) { - if (plugins.containsKey(name)) { - if (plugins[name] != null) { - tasks.add(plugins[name]!!.run(mutableResponse, parameters)) - } + for (plugin in spec.plugins) { + if (plugins.containsKey(plugin.name)) { + tasks.add(plugins[plugin.name]!!.run(mutableResponse, plugin.args)) } } } - val reply = JsonResponsePlugin(response).run(mutableResponse, true) + val reply = JsonResponsePlugin(response).run(mutableResponse) var completes = tasks.size if (completes > 0) { val done = { diff --git a/src/main/kotlin/com/dotronglong/faker/service/plugin/BasePlugin.kt b/src/main/kotlin/com/dotronglong/faker/service/plugin/BasePlugin.kt index 1f2811e..a4494a0 100644 --- a/src/main/kotlin/com/dotronglong/faker/service/plugin/BasePlugin.kt +++ b/src/main/kotlin/com/dotronglong/faker/service/plugin/BasePlugin.kt @@ -1,7 +1,13 @@ package com.dotronglong.faker.service.plugin abstract class BasePlugin { - protected fun parseArguments(text: String): Map { + /** + * Parse inline arguments. + * Inline Arguments starts and ends with '#' + * + * Returns a map of arguments + */ + protected fun parseInlineArguments(text: String): Map { val arguments = HashMap() if (text.isNotEmpty()) { val pairs = text.split("&") diff --git a/src/main/kotlin/com/dotronglong/faker/service/plugin/DelayResponsePlugin.kt b/src/main/kotlin/com/dotronglong/faker/service/plugin/DelayResponsePlugin.kt index 4620ef8..b1cb241 100644 --- a/src/main/kotlin/com/dotronglong/faker/service/plugin/DelayResponsePlugin.kt +++ b/src/main/kotlin/com/dotronglong/faker/service/plugin/DelayResponsePlugin.kt @@ -11,11 +11,12 @@ class DelayResponsePlugin : Plugin { override val name: String get() = "delay" - override fun run(response: MutableResponse, parameters: Any): Mono { + override fun run(response: MutableResponse, arguments: Map?): Mono { return Mono.create { s -> - if (parameters is Int && parameters > 0) { + val duration = (arguments?.get("duration") as Int?) ?: 0 + if (duration > 0) { GlobalScope.launch { - delay((parameters).toLong()) + delay((duration).toLong()) s.success() } } else { diff --git a/src/main/kotlin/com/dotronglong/faker/service/plugin/JsonResponsePlugin.kt b/src/main/kotlin/com/dotronglong/faker/service/plugin/JsonResponsePlugin.kt index de0a340..3cfefda 100644 --- a/src/main/kotlin/com/dotronglong/faker/service/plugin/JsonResponsePlugin.kt +++ b/src/main/kotlin/com/dotronglong/faker/service/plugin/JsonResponsePlugin.kt @@ -9,16 +9,17 @@ import reactor.core.publisher.Mono class JsonResponsePlugin constructor(private val serverHttpResponse: ServerHttpResponse) : Plugin { private companion object { const val CONTENT_JSON_UTF8 = "application/json; charset=utf-8" + const val HEADER_CONTENT_TYPE = "Content-Type" } override val name: String get() = "json" - override fun run(response: MutableResponse, parameters: Any): Mono { + override fun run(response: MutableResponse, arguments: Map?): Mono { return Mono.create { s -> serverHttpResponse.statusCode = HttpStatus.valueOf(response.statusCode) - if (!response.headers.containsKey("Content-Type")) { - serverHttpResponse.headers.set("Content-Type", CONTENT_JSON_UTF8) + if (!response.headers.containsKey(HEADER_CONTENT_TYPE)) { + serverHttpResponse.headers.set(HEADER_CONTENT_TYPE, CONTENT_JSON_UTF8) } response.headers.forEach { (key, value) -> serverHttpResponse.headers.set(key, value) } val buffer = serverHttpResponse.bufferFactory().wrap(response.body.toByteArray()) diff --git a/src/main/kotlin/com/dotronglong/faker/service/plugin/RandomPlugin.kt b/src/main/kotlin/com/dotronglong/faker/service/plugin/RandomPlugin.kt index a8ea86e..d07cf21 100644 --- a/src/main/kotlin/com/dotronglong/faker/service/plugin/RandomPlugin.kt +++ b/src/main/kotlin/com/dotronglong/faker/service/plugin/RandomPlugin.kt @@ -29,7 +29,9 @@ class RandomPlugin : BasePlugin(), Plugin { override val name: String get() = "random" - override fun run(response: MutableResponse, parameters: Any): Mono { + override fun run(response: MutableResponse, arguments: Map?): Mono = run(response) + + override fun run(response: MutableResponse): Mono { return Mono.create { s -> try { val body = response.body @@ -37,7 +39,7 @@ class RandomPlugin : BasePlugin(), Plugin { val matcher = pattern.matcher(body) while (matcher.find()) { val type = matcher.group(1) - val arguments = parseArguments(matcher.group(2)) + val arguments = parseInlineArguments(matcher.group(2)) val find = matcher.group() when (type) { "string" -> { diff --git a/src/main/kotlin/com/dotronglong/faker/service/plugin/TimestampPlugin.kt b/src/main/kotlin/com/dotronglong/faker/service/plugin/TimestampPlugin.kt index 3b35017..5bb53f5 100644 --- a/src/main/kotlin/com/dotronglong/faker/service/plugin/TimestampPlugin.kt +++ b/src/main/kotlin/com/dotronglong/faker/service/plugin/TimestampPlugin.kt @@ -3,24 +3,17 @@ package com.dotronglong.faker.service.plugin import com.dotronglong.faker.contract.Plugin import com.dotronglong.faker.pojo.MutableResponse import reactor.core.publisher.Mono -import java.util.regex.Pattern class TimestampPlugin : BasePlugin(), Plugin { override val name: String get() = "timestamp" - override fun run(response: MutableResponse, parameters: Any): Mono { + override fun run(response: MutableResponse, arguments: Map?): Mono { return Mono.create { s -> try { - val body = response.body - val pattern = Pattern.compile("#timestamp:?([^:#]*)?#") - val matcher = pattern.matcher(body) - while (matcher.find()) { - val arguments = parseArguments(matcher.group(1)) - val find = matcher.group() - val replace = timestamp(arguments) - response.body = response.body.replaceFirst("\"$find\"", "$replace") - } + val find = "#timestamp#" + val replace = timestamp(arguments ?: mapOf()) + response.body = response.body.replace("\"$find\"", "$replace") s.success() } catch (e: Exception) { s.error(e) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 67e687e..8cbbf11 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,3 @@ -faker.version=2.0.7 +faker.version=2.1.0 faker.source=./mocks faker.watch=false diff --git a/src/test/kotlin/com/dotronglong/faker/FakerApplicationTests.kt b/src/test/kotlin/com/dotronglong/faker/FakerApplicationTests.kt index 11e0914..c45a457 100644 --- a/src/test/kotlin/com/dotronglong/faker/FakerApplicationTests.kt +++ b/src/test/kotlin/com/dotronglong/faker/FakerApplicationTests.kt @@ -57,7 +57,7 @@ class FakerApplicationTests(@Autowired val restTemplate: TestRestTemplate) { @Test fun testResponseWithRandomPluginEnabled() { - val entity = restTemplate.getForEntity("/v1/users?random") + val entity = restTemplate.getForEntity("/random") assertThat(entity.statusCode).isEqualTo(HttpStatus.OK) assertThat(entity.body is List<*>).isTrue() @@ -82,7 +82,7 @@ class FakerApplicationTests(@Autowired val restTemplate: TestRestTemplate) { @Test fun testResponseWithTimestampPluginEnabled() { - val entity = restTemplate.getForEntity("/v1/users?timestamp") + val entity = restTemplate.getForEntity("/timestamp") assertThat(entity.statusCode).isEqualTo(HttpStatus.OK) assertThat(entity.body is List<*>).isTrue()