Skip to content

Commit

Permalink
v2.1.0 (#21)
Browse files Browse the repository at this point in the history
Rebuild plugins
  • Loading branch information
Do Trong Long authored Feb 13, 2020
1 parent c49c33b commit 6668524
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 44 deletions.
9 changes: 6 additions & 3 deletions mocks/delay.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"plugins": {
"delay": 500
},
"plugins": [
{
"name": "delay",
"args": { "duration": 500 }
}
],
"request": {
"method": "get",
"path": "/delay"
Expand Down
8 changes: 4 additions & 4 deletions mocks/users_random.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"plugins": {
"random": true
},
"plugins": [
{"name": "random"}
],
"request": {
"method": "GET",
"path": "/v1/users?random"
"path": "/random"
},
"response": {
"body": [
Expand Down
11 changes: 7 additions & 4 deletions mocks/users_timestamp.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"plugins": {
"timestamp": true
},
"plugins": [
{
"name": "timestamp",
"args": { "in": "milliseconds" }
}
],
"request": {
"method": "GET",
"path": "/v1/users?timestamp"
"path": "/timestamp"
},
"response": {
"body": [
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/dotronglong/faker/contract/Plugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import reactor.core.publisher.Mono

interface Plugin {
val name: String
fun run(response: MutableResponse, parameters: Any): Mono<Void>
fun run(response: MutableResponse, arguments: Map<String, Any>?): Mono<Void>
fun run(response: MutableResponse): Mono<Void> = run(response, null)
}
7 changes: 6 additions & 1 deletion src/main/kotlin/com/dotronglong/faker/pojo/Spec.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.dotronglong.faker.pojo

data class Spec(
val plugins: Map<String, Any>?,
val plugins: List<Plugin>?,
val request: Request,
val response: Response
) {
Expand All @@ -19,4 +19,9 @@ data class Spec(
var headers: Map<String, String>?,
var body: Any?
)

data class Plugin(
val name: String,
val args: Map<String, Any>?
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, Plugin> = HashMap()
private val plugins: MutableMap<String, Plugin> = HashMap()
private val mapper = ObjectMapper()

init {
Expand All @@ -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<Mono<Void>> = 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 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.dotronglong.faker.service.plugin

abstract class BasePlugin {
protected fun parseArguments(text: String): Map<String, Any> {
/**
* Parse inline arguments.
* Inline Arguments starts and ends with '#'
*
* Returns a map of arguments
*/
protected fun parseInlineArguments(text: String): Map<String, Any> {
val arguments = HashMap<String, Any>()
if (text.isNotEmpty()) {
val pairs = text.split("&")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ class DelayResponsePlugin : Plugin {
override val name: String
get() = "delay"

override fun run(response: MutableResponse, parameters: Any): Mono<Void> {
override fun run(response: MutableResponse, arguments: Map<String, Any>?): Mono<Void> {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> {
override fun run(response: MutableResponse, arguments: Map<String, Any>?): Mono<Void> {
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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ class RandomPlugin : BasePlugin(), Plugin {
override val name: String
get() = "random"

override fun run(response: MutableResponse, parameters: Any): Mono<Void> {
override fun run(response: MutableResponse, arguments: Map<String, Any>?): Mono<Void> = run(response)

override fun run(response: MutableResponse): Mono<Void> {
return Mono.create { s ->
try {
val body = response.body
val pattern = Pattern.compile("#random:(\\w+):?([^:#]*)?#")
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" -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> {
override fun run(response: MutableResponse, arguments: Map<String, Any>?): Mono<Void> {
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)
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
faker.version=2.0.7
faker.version=2.1.0
faker.source=./mocks
faker.watch=false
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class FakerApplicationTests(@Autowired val restTemplate: TestRestTemplate) {

@Test
fun testResponseWithRandomPluginEnabled() {
val entity = restTemplate.getForEntity<Any>("/v1/users?random")
val entity = restTemplate.getForEntity<Any>("/random")
assertThat(entity.statusCode).isEqualTo(HttpStatus.OK)
assertThat(entity.body is List<*>).isTrue()

Expand All @@ -82,7 +82,7 @@ class FakerApplicationTests(@Autowired val restTemplate: TestRestTemplate) {

@Test
fun testResponseWithTimestampPluginEnabled() {
val entity = restTemplate.getForEntity<Any>("/v1/users?timestamp")
val entity = restTemplate.getForEntity<Any>("/timestamp")
assertThat(entity.statusCode).isEqualTo(HttpStatus.OK)
assertThat(entity.body is List<*>).isTrue()

Expand Down

0 comments on commit 6668524

Please sign in to comment.