From e5ee0e437d0c15c9459c8f011d53b129b05b52d9 Mon Sep 17 00:00:00 2001 From: Marcin Zajaczkowski Date: Wed, 4 Feb 2015 12:00:03 +0100 Subject: [PATCH 1/9] [#5] Extract common DSL elements --- .../io/coderate/accurest/dsl/GroovyDsl.groovy | 9 ++ .../io/coderate/accurest/dsl/Request.groovy | 143 +----------------- .../accurest/dsl/WiremockStubStrategy.groovy | 21 +-- .../dsl/internal/CustomizableProperty.groovy | 39 +++++ .../dsl/internal/DelegateHelper.groovy | 10 ++ .../accurest/dsl/internal/Headers.groovy | 17 +++ .../dsl/internal/JSONCompareMode.groovy | 5 + .../dsl/internal/WithValuePattern.groovy | 51 +++++++ 8 files changed, 148 insertions(+), 147 deletions(-) create mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/CustomizableProperty.groovy create mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DelegateHelper.groovy create mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy create mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/JSONCompareMode.groovy create mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy index 35a2acbc7d..3eb01b9e8c 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy @@ -1,11 +1,13 @@ package io.coderate.accurest.dsl import groovy.transform.TypeChecked +import io.coderate.accurest.dsl.internal.Response @TypeChecked class GroovyDsl { Request request + Response response static GroovyDsl make(Closure closure) { GroovyDsl dsl = new GroovyDsl() @@ -20,4 +22,11 @@ class GroovyDsl { closure.delegate = request closure() } + + void response(@DelegatesTo(Response) Closure closure) { + Response response = new Response() + this.response = response + closure.delegate = response + closure() + } } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/Request.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/Request.groovy index d4f40ef6ba..60fd51b785 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/Request.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/Request.groovy @@ -1,9 +1,14 @@ package io.coderate.accurest.dsl import groovy.transform.TypeChecked +import io.coderate.accurest.dsl.internal.Headers +import io.coderate.accurest.dsl.internal.StringCustomizableProperty + +import static io.coderate.accurest.dsl.internal.DelegateHelper.delegateToClosure @TypeChecked class Request { + String method String url StringCustomizableProperty urlPattern @@ -33,140 +38,4 @@ class Request { void urlPath(String urlPath) { this.urlPath = urlPath } - - //TODO: Can we have different types for Client/Server (client: pattern(String), server: value(Boolean/Int)) ? - class CustomizableProperty { - private T client - private V server - - void client(T client) { - this.client = client - } - - void server(V server) { - this.server = server - } - - T toClientSide() { - return client - } - - V toServerSide() { - return server - } - } - - private class StringCustomizableProperty extends CustomizableProperty { - @Override - String toClientSide() { - return super.toClientSide() as String - } - - @Override - String toServerSide() { - return super.toServerSide() as String - } - } - - private class NoOpCustomizableProperty extends CustomizableProperty { - NoOpCustomizableProperty(T value) { - client(value) - server(value) - } - - @Override - public String toString() { - return toClientSide() - } - } - - class Headers { - private Map headers = [:] - - WithValuePattern header(String headerName) { - WithValuePattern withValuePattern = new WithValuePattern() - headers[headerName] = withValuePattern - return withValuePattern - } - - Set> entries() { - return headers.entrySet() - } - } - -/** - * - * From Wiremock - private Map queryParamPatterns; - private List bodyPatterns; - - private String equalToJson; - private String equalToXml; - private String matchesXPath; - private JSONCompareMode jsonCompareMode; - private String equalTo; - private String contains; - private String matches; - private String doesNotMatch; - private Boolean absent; - private String matchesJsonPath; - - */ - class WithValuePattern { - NoOpCustomizableProperty equalToJson - NoOpCustomizableProperty equalToXml - StringCustomizableProperty matchesXPath - NoOpCustomizableProperty jsonCompareMode - NoOpCustomizableProperty equalTo - StringCustomizableProperty contains - StringCustomizableProperty matches - StringCustomizableProperty doesNotMatch - CustomizableProperty absent - StringCustomizableProperty matchesJsonPath - -// void equalToJson(String equalToJson) { -// this.equalToJson = equalToJson -// } -// - void equalTo(String equalTo) { - this.equalTo = new NoOpCustomizableProperty(equalTo) - } - -// void equalToXml(String equalToXml) { -// this.equalToXml = equalToXml -// } - - void matches(@DelegatesTo(StringCustomizableProperty) Closure closure) { - StringCustomizableProperty placeholderHaving = new StringCustomizableProperty() - this.matches = placeholderHaving - delegateToClosure(closure, placeholderHaving) - } - - void doesNotMatch(@DelegatesTo(StringCustomizableProperty) Closure closure) { - StringCustomizableProperty placeholderHaving = new StringCustomizableProperty() - this.doesNotMatch = placeholderHaving - delegateToClosure(closure, placeholderHaving) - } - - void contains(@DelegatesTo(StringCustomizableProperty) Closure closure) { - StringCustomizableProperty placeholderHaving = new StringCustomizableProperty() - this.contains = placeholderHaving - delegateToClosure(closure, placeholderHaving) - } - -// void jsonCompareMode(JSONCompareMode jsonCompareMode) { -// this.jsonCompareMode = jsonCompareMode -// } - - } - - enum JSONCompareMode { - STRICT, LENIENT, NON_EXTENSIBLE, STRICT_ORDER; - } - - private static void delegateToClosure(@DelegatesTo(T) Closure closure, T delegate) { - closure.delegate = delegate - closure() - } - -} \ No newline at end of file +} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockStubStrategy.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockStubStrategy.groovy index ea975a0ca8..ee846b1a28 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockStubStrategy.groovy @@ -2,6 +2,9 @@ package io.coderate.accurest.dsl import groovy.json.JsonOutput import groovy.transform.CompileStatic +import io.coderate.accurest.dsl.internal.CustomizableProperty +import io.coderate.accurest.dsl.internal.Headers +import io.coderate.accurest.dsl.internal.WithValuePattern @CompileStatic class WiremockStubStrategy { @@ -14,7 +17,6 @@ class WiremockStubStrategy { String toWiremockClientStub() { return JsonOutput.toJson(buildClientRequest(request)) - } String toWiremockServerStub() { @@ -41,36 +43,36 @@ class WiremockStubStrategy { headers : buildHeaders()].findAll { it.value }] } - private Map buildClientHeadersSection(Request.Headers headers) { + private Map buildClientHeadersSection(Headers headers) { return createHeadersSection(headers) { - Map.Entry entry -> [(entry.key): buildClientHeaderFromValuePattern(entry.value)] + Map.Entry entry -> [(entry.key): buildClientHeaderFromValuePattern(entry.value)] } } - private Map buildServerHeadersSection(Request.Headers headers) { + private Map buildServerHeadersSection(Headers headers) { return createHeadersSection(headers) { - Map.Entry entry -> [(entry.key): buildServerHeaderFromValuePattern(entry.value)] + Map.Entry entry -> [(entry.key): buildServerHeaderFromValuePattern(entry.value)] } } - private Map createHeadersSection(Request.Headers headers, Closure closure) { + private Map createHeadersSection(Headers headers, Closure closure) { return headers?.entries()?.collectEntries(closure) } - private Map buildClientHeaderFromValuePattern(Request.WithValuePattern valuePattern) { + private Map buildClientHeaderFromValuePattern(WithValuePattern valuePattern) { return getValuePatternSection(valuePattern) .findAll { it.value } .collectEntries { [(it.key): it.value.toClientSide()] } } - private Map buildServerHeaderFromValuePattern(Request.WithValuePattern valuePattern) { + private Map buildServerHeaderFromValuePattern(WithValuePattern valuePattern) { return getValuePatternSection(valuePattern) .findAll { it.value } .collectEntries { [(it.key): it.value.toServerSide()] } } - private Map getValuePatternSection(Request.WithValuePattern valuePattern) { + private Map getValuePatternSection(WithValuePattern valuePattern) { return [equalToJson : valuePattern.equalToJson, equalToXml : valuePattern.equalToXml, matchesXPath : valuePattern.matchesXPath, @@ -82,5 +84,4 @@ class WiremockStubStrategy { absent : valuePattern.absent, matchesJsonPath: valuePattern.matchesJsonPath] } - } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/CustomizableProperty.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/CustomizableProperty.groovy new file mode 100644 index 0000000000..592fff82a0 --- /dev/null +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/CustomizableProperty.groovy @@ -0,0 +1,39 @@ +package io.coderate.accurest.dsl.internal + +//TODO: Can we have different types for Client/Server (client: pattern(String), server: value(Boolean/Int)) ? +class CustomizableProperty { + + private T client + private V server + + void client(T client) { + this.client = client + } + + void server(V server) { + this.server = server + } + + T toClientSide() { + return client + } + + V toServerSide() { + return server + } +} + +class StringCustomizableProperty extends CustomizableProperty { +} + +class NoOpCustomizableProperty extends CustomizableProperty { + NoOpCustomizableProperty(T value) { + client(value) + server(value) + } + + @Override + public String toString() { + return toClientSide() + } +} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DelegateHelper.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DelegateHelper.groovy new file mode 100644 index 0000000000..baa8240dcf --- /dev/null +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DelegateHelper.groovy @@ -0,0 +1,10 @@ +package io.coderate.accurest.dsl.internal + +//TODO: There is problem with a trait usage +class DelegateHelper { + + public static void delegateToClosure(@DelegatesTo(T) Closure closure, T delegate) { + closure.delegate = delegate + closure() + } +} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy new file mode 100644 index 0000000000..3ab38d42ec --- /dev/null +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy @@ -0,0 +1,17 @@ +package io.coderate.accurest.dsl.internal + +class Headers { + + private Map headers = [:] + + WithValuePattern header(String headerName) { + WithValuePattern withValuePattern = new WithValuePattern() + headers[headerName] = withValuePattern + return withValuePattern + } + + Set> entries() { + //TODO: Make it immutable + return headers.entrySet() + } +} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/JSONCompareMode.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/JSONCompareMode.groovy new file mode 100644 index 0000000000..591432866e --- /dev/null +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/JSONCompareMode.groovy @@ -0,0 +1,5 @@ +package io.coderate.accurest.dsl.internal + +enum JSONCompareMode { + STRICT, LENIENT, NON_EXTENSIBLE, STRICT_ORDER; +} \ No newline at end of file diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy new file mode 100644 index 0000000000..5f92b4f27f --- /dev/null +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy @@ -0,0 +1,51 @@ +package io.coderate.accurest.dsl.internal + +import static io.coderate.accurest.dsl.internal.DelegateHelper.delegateToClosure + +class WithValuePattern { + + NoOpCustomizableProperty equalToJson + NoOpCustomizableProperty equalToXml + StringCustomizableProperty matchesXPath + NoOpCustomizableProperty jsonCompareMode + NoOpCustomizableProperty equalTo + StringCustomizableProperty contains + StringCustomizableProperty matches + StringCustomizableProperty doesNotMatch + CustomizableProperty absent + StringCustomizableProperty matchesJsonPath + +// void equalToJson(String equalToJson) { +// this.equalToJson = equalToJson +// } +// + void equalTo(String equalTo) { + this.equalTo = new NoOpCustomizableProperty(equalTo) + } + +// void equalToXml(String equalToXml) { +// this.equalToXml = equalToXml +// } + + void matches(@DelegatesTo(StringCustomizableProperty) Closure closure) { + StringCustomizableProperty placeholderHaving = new StringCustomizableProperty() + this.matches = placeholderHaving + delegateToClosure(closure, placeholderHaving) + } + + void doesNotMatch(@DelegatesTo(StringCustomizableProperty) Closure closure) { + StringCustomizableProperty placeholderHaving = new StringCustomizableProperty() + this.doesNotMatch = placeholderHaving + delegateToClosure(closure, placeholderHaving) + } + + void contains(@DelegatesTo(StringCustomizableProperty) Closure closure) { + StringCustomizableProperty placeholderHaving = new StringCustomizableProperty() + this.contains = placeholderHaving + delegateToClosure(closure, placeholderHaving) + } + +// void jsonCompareMode(JSONCompareMode jsonCompareMode) { +// this.jsonCompareMode = jsonCompareMode +// } +} From daffd14cc3b87016b64574fd93aadb014923a73a Mon Sep 17 00:00:00 2001 From: Marcin Zajaczkowski Date: Wed, 4 Feb 2015 13:36:41 +0100 Subject: [PATCH 2/9] [#5] Add basic response implementation, extract base strategy elements --- .gitignore | 2 + ...groovy => BaseWiremockStubStrategy.groovy} | 43 +-------------- .../io/coderate/accurest/dsl/GroovyDsl.groovy | 1 + .../dsl/WiremockRequestStubStrategy.groovy | 43 +++++++++++++++ .../dsl/WiremockResponseStubStrategy.groovy | 29 ++++++++++ .../dsl/{ => internal}/Request.groovy | 2 +- .../accurest/dsl/internal/Response.groovy | 29 ++++++++++ .../dsl/WiremockGroovyDslResponseSpec.groovy | 55 +++++++++++++++++++ .../accurest/dsl/WiremockGroovyDslSpec.groovy | 18 +++--- 9 files changed, 172 insertions(+), 50 deletions(-) rename accurest-core/src/main/groovy/io/coderate/accurest/dsl/{WiremockStubStrategy.groovy => BaseWiremockStubStrategy.groovy} (58%) create mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy create mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy rename accurest-core/src/main/groovy/io/coderate/accurest/dsl/{ => internal}/Request.groovy (96%) create mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy create mode 100644 accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy diff --git a/.gitignore b/.gitignore index c1785e61a0..5fd4a2d374 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ *.iml .idea/ +.gradle/ target/ +build/ hs_err_pid* diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockStubStrategy.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/BaseWiremockStubStrategy.groovy similarity index 58% rename from accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockStubStrategy.groovy rename to accurest-core/src/main/groovy/io/coderate/accurest/dsl/BaseWiremockStubStrategy.groovy index ee846b1a28..8c02509ad8 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/BaseWiremockStubStrategy.groovy @@ -1,55 +1,19 @@ package io.coderate.accurest.dsl -import groovy.json.JsonOutput import groovy.transform.CompileStatic import io.coderate.accurest.dsl.internal.CustomizableProperty import io.coderate.accurest.dsl.internal.Headers import io.coderate.accurest.dsl.internal.WithValuePattern @CompileStatic -class WiremockStubStrategy { - - private final Request request - - WiremockStubStrategy(GroovyDsl groovyDsl) { - this.request = groovyDsl.request - } - - String toWiremockClientStub() { - return JsonOutput.toJson(buildClientRequest(request)) - } - - String toWiremockServerStub() { - return JsonOutput.toJson(buildServerRequest(request)) - } - - private Map buildClientRequest(Request request) { - return getRequestSection(request, - { request.urlPattern?.toClientSide() }, - { buildClientHeadersSection(request.headers) }) - } - - private Map buildServerRequest(Request request) { - return getRequestSection(request, - { request.urlPattern?.toServerSide() }, - { buildServerHeadersSection(request.headers) }) - } - - private Map> getRequestSection(Request request, Closure buildUrlPattern, Closure buildHeaders) { - return [request: [method : request.method, - url : request.url, - urlPattern: buildUrlPattern(), - urlPath : request.urlPath, - headers : buildHeaders()].findAll { it.value }] - } - - private Map buildClientHeadersSection(Headers headers) { +abstract class BaseWiremockStubStrategy { + protected Map buildClientHeadersSection(Headers headers) { return createHeadersSection(headers) { Map.Entry entry -> [(entry.key): buildClientHeaderFromValuePattern(entry.value)] } } - private Map buildServerHeadersSection(Headers headers) { + protected Map buildServerHeadersSection(Headers headers) { return createHeadersSection(headers) { Map.Entry entry -> [(entry.key): buildServerHeaderFromValuePattern(entry.value)] } @@ -59,7 +23,6 @@ class WiremockStubStrategy { return headers?.entries()?.collectEntries(closure) } - private Map buildClientHeaderFromValuePattern(WithValuePattern valuePattern) { return getValuePatternSection(valuePattern) .findAll { it.value } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy index 3eb01b9e8c..1ae6fe4644 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy @@ -1,6 +1,7 @@ package io.coderate.accurest.dsl import groovy.transform.TypeChecked +import io.coderate.accurest.dsl.internal.Request import io.coderate.accurest.dsl.internal.Response @TypeChecked diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy new file mode 100644 index 0000000000..3693d7864e --- /dev/null +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy @@ -0,0 +1,43 @@ +package io.coderate.accurest.dsl + +import groovy.json.JsonOutput +import groovy.transform.CompileStatic +import io.coderate.accurest.dsl.internal.Request + +@CompileStatic +class WiremockRequestStubStrategy extends BaseWiremockStubStrategy { + + private final Request request + + WiremockRequestStubStrategy(GroovyDsl groovyDsl) { + this.request = groovyDsl.request + } + + String toWiremockClientStub() { + return JsonOutput.toJson(buildClientRequest(request)) + } + + String toWiremockServerStub() { + return JsonOutput.toJson(buildServerRequest(request)) + } + + private Map buildClientRequest(Request request) { + return getRequestSection(request, + { request.urlPattern?.toClientSide() }, + { buildClientHeadersSection(request.headers) }) + } + + private Map buildServerRequest(Request request) { + return getRequestSection(request, + { request.urlPattern?.toServerSide() }, + { buildServerHeadersSection(request.headers) }) + } + + private Map> getRequestSection(Request request, Closure buildUrlPattern, Closure buildHeaders) { + return [request: [method : request.method, + url : request.url, + urlPattern: buildUrlPattern(), + urlPath : request.urlPath, + headers : buildHeaders()].findAll { it.value }] + } +} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy new file mode 100644 index 0000000000..297b001345 --- /dev/null +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy @@ -0,0 +1,29 @@ +package io.coderate.accurest.dsl + +import groovy.json.JsonOutput +import groovy.transform.CompileStatic +import io.coderate.accurest.dsl.internal.Response + +@CompileStatic +class WiremockResponseStubStrategy extends BaseWiremockStubStrategy { + + Response response + + WiremockResponseStubStrategy(GroovyDsl groovyDsl) { //TODO: Or Response? + this.response = groovyDsl.response + } + + String toWiremockClientStub() { + return JsonOutput.toJson(buildClientRequest(response)) + } + + private Map buildClientRequest(Response response) { + return getResponseSection(response, { "TODO" }, { buildClientHeadersSection(response.headers) }) + } + + private Map> getResponseSection(Response response, Closure buildUrlPattern, Closure buildHeaders) { + return [response: [status : response.status, + headers: buildHeaders()] + .findAll { it.value }] + } +} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/Request.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy similarity index 96% rename from accurest-core/src/main/groovy/io/coderate/accurest/dsl/Request.groovy rename to accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy index 60fd51b785..d89bd1bd84 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/Request.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy @@ -1,4 +1,4 @@ -package io.coderate.accurest.dsl +package io.coderate.accurest.dsl.internal import groovy.transform.TypeChecked import io.coderate.accurest.dsl.internal.Headers diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy new file mode 100644 index 0000000000..6b0c0d1916 --- /dev/null +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy @@ -0,0 +1,29 @@ +package io.coderate.accurest.dsl.internal + +import groovy.transform.TypeChecked + +import static io.coderate.accurest.dsl.internal.DelegateHelper.delegateToClosure + +@TypeChecked +class Response { + + private int status + private Headers headers + + void status(int status) { + this.status = status + } + + void headers(@DelegatesTo(Headers) Closure closure) { + this.headers = new Headers() + delegateToClosure(closure, headers) + } + + int getStatus() { + return status + } + + Headers getHeaders() { + return headers + } +} diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy new file mode 100644 index 0000000000..752cdec417 --- /dev/null +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy @@ -0,0 +1,55 @@ +package io.codearte.accurest.dsl + +import groovy.json.JsonSlurper +import io.coderate.accurest.dsl.GroovyDsl +import io.coderate.accurest.dsl.WiremockResponseStubStrategy +import spock.lang.Specification + +class WiremockGroovyDslResponseSpec extends Specification { + + def 'should generate response without body for client side'() { + given: + GroovyDsl dsl = GroovyDsl.make { + response { + status 200 + } + } + when: + String wiremockStub = new WiremockResponseStubStrategy(dsl).toWiremockClientStub() + then: + new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' + { + "response": { + "status": 200 + } + } + ''') + } + + def 'should generate headers for response for client side'() { + given: + GroovyDsl dsl = GroovyDsl.make { + response { + headers { + header('Content-Type').equalTo('text/xml') + } + status 200 + } + } + when: + String wiremockStub = new WiremockResponseStubStrategy(dsl).toWiremockClientStub() + then: + new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' + { + "response": { + "headers": { + "Content-Type": { + "equalTo": "text/xml" + }, + }, + "status": 200 + } + } + ''') + } +} diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy index f6feb8710e..493151c210 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy @@ -2,7 +2,7 @@ package io.codearte.accurest.dsl import groovy.json.JsonSlurper import io.coderate.accurest.dsl.GroovyDsl -import io.coderate.accurest.dsl.WiremockStubStrategy +import io.coderate.accurest.dsl.WiremockRequestStubStrategy import spock.lang.Ignore import spock.lang.Specification @@ -61,7 +61,7 @@ class WiremockGroovyDslSpec extends Specification { } } when: - String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockClientStub() + String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockClientStub() then: new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' { @@ -81,7 +81,7 @@ class WiremockGroovyDslSpec extends Specification { } } when: - String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockClientStub() + String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockClientStub() then: new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' { @@ -103,7 +103,7 @@ class WiremockGroovyDslSpec extends Specification { } } when: - String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockClientStub() + String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockClientStub() then: new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' { @@ -125,7 +125,7 @@ class WiremockGroovyDslSpec extends Specification { } } when: - String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockServerStub() + String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockServerStub() then: new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' { @@ -144,7 +144,7 @@ class WiremockGroovyDslSpec extends Specification { } } when: - String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockClientStub() + String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockClientStub() then: new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' { @@ -163,7 +163,7 @@ class WiremockGroovyDslSpec extends Specification { } } when: - String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockServerStub() + String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockServerStub() then: new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' { @@ -196,7 +196,7 @@ class WiremockGroovyDslSpec extends Specification { } } when: - String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockClientStub() + String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockClientStub() then: new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' { @@ -242,7 +242,7 @@ class WiremockGroovyDslSpec extends Specification { } } when: - String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockServerStub() + String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockServerStub() then: new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' { From c9eb699bc34eecfe6cfd37953bc66f8a3227817d Mon Sep 17 00:00:00 2001 From: Marcin Zajaczkowski Date: Thu, 5 Feb 2015 12:45:38 +0100 Subject: [PATCH 3/9] [#5] Simplify tests for Response --- .../dsl/WiremockResponseStubStrategy.groovy | 12 ++++- .../dsl/WiremockGroovyDslResponseSpec.groovy | 46 +++++++++++++++++-- .../accurest/dsl/WiremockGroovyDslSpec.groovy | 4 ++ 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy index 297b001345..1df83b5f58 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy @@ -14,13 +14,21 @@ class WiremockResponseStubStrategy extends BaseWiremockStubStrategy { } String toWiremockClientStub() { - return JsonOutput.toJson(buildClientRequest(response)) + return JsonOutput.toJson(buildClientResponse(response)) } - private Map buildClientRequest(Response response) { + private Map buildClientResponse(Response response) { return getResponseSection(response, { "TODO" }, { buildClientHeadersSection(response.headers) }) } + String toWiremockServerStub() { + return JsonOutput.toJson(buildServerResponse(response)) + } + + private Map buildServerResponse(Response response) { + return getResponseSection(response, { "TODO" }, { buildServerHeadersSection(response.headers) }) + } + private Map> getResponseSection(Response response, Closure buildUrlPattern, Closure buildHeaders) { return [response: [status : response.status, headers: buildHeaders()] diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy index 752cdec417..65241c420d 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy @@ -3,11 +3,12 @@ package io.codearte.accurest.dsl import groovy.json.JsonSlurper import io.coderate.accurest.dsl.GroovyDsl import io.coderate.accurest.dsl.WiremockResponseStubStrategy +import spock.lang.Ignore import spock.lang.Specification class WiremockGroovyDslResponseSpec extends Specification { - def 'should generate response without body for client side'() { + def 'should generate response without body for #side side'() { given: GroovyDsl dsl = GroovyDsl.make { response { @@ -15,15 +16,26 @@ class WiremockGroovyDslResponseSpec extends Specification { } } when: - String wiremockStub = new WiremockResponseStubStrategy(dsl).toWiremockClientStub() + String wiremockStub = new WiremockResponseStubStrategy(dsl)."toWiremock${side}Stub"() then: - new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' + new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(expectedStub) + where: + side << ['Client', 'Server'] + expectedStub << [''' { "response": { "status": 200 } } - ''') + ''', + + ''' + { + "response": { + "status": 200 + } + } + '''] } def 'should generate headers for response for client side'() { @@ -52,4 +64,30 @@ class WiremockGroovyDslResponseSpec extends Specification { } ''') } + + @Ignore("Not implemented yet") + def 'should generate headers for response for server side'() { + given: + GroovyDsl dsl = GroovyDsl.make { + response { + status 200 + headers { + header('Content-Type').equalTo('text/xml') + } + } + } + when: + String wiremockStub = new WiremockResponseStubStrategy(dsl).toWiremockClientStub() + then: + new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' + { + "response": { + "status": 200, + "headers": + "Content-Type": "text/xml" + } + } + } + ''') + } } diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy index 493151c210..a26983984d 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy @@ -266,5 +266,9 @@ class WiremockGroovyDslSpec extends Specification { ''') } + @Ignore("Not implemented yet") + def "should generate stub with request body matching for server side"() {} + @Ignore("Not implemented yet") + def "should generate stub with request query parameter matching for server side"() {} } From 759e9a163d707eba4e3ae10410814270cd2b209c Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 11 Feb 2015 15:12:10 +0100 Subject: [PATCH 4/9] Minor refactoring - changed class name and added immutability on the headers set --- .../accurest/dsl/internal/CustomizableProperty.groovy | 4 ++-- .../io/coderate/accurest/dsl/internal/Headers.groovy | 3 +-- .../accurest/dsl/internal/WithValuePattern.groovy | 10 +++++----- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/CustomizableProperty.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/CustomizableProperty.groovy index 592fff82a0..fc6dfcb503 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/CustomizableProperty.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/CustomizableProperty.groovy @@ -26,8 +26,8 @@ class CustomizableProperty { class StringCustomizableProperty extends CustomizableProperty { } -class NoOpCustomizableProperty extends CustomizableProperty { - NoOpCustomizableProperty(T value) { +class SingleTypeCustomizableProperty extends CustomizableProperty { + SingleTypeCustomizableProperty(T value) { client(value) server(value) } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy index 3ab38d42ec..ca7e87e7cb 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy @@ -11,7 +11,6 @@ class Headers { } Set> entries() { - //TODO: Make it immutable - return headers.entrySet() + return Collections.unmodifiableSet(headers.entrySet()) } } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy index 5f92b4f27f..9e44935cfa 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy @@ -4,11 +4,11 @@ import static io.coderate.accurest.dsl.internal.DelegateHelper.delegateToClosure class WithValuePattern { - NoOpCustomizableProperty equalToJson - NoOpCustomizableProperty equalToXml + SingleTypeCustomizableProperty equalToJson + SingleTypeCustomizableProperty equalToXml StringCustomizableProperty matchesXPath - NoOpCustomizableProperty jsonCompareMode - NoOpCustomizableProperty equalTo + SingleTypeCustomizableProperty jsonCompareMode + SingleTypeCustomizableProperty equalTo StringCustomizableProperty contains StringCustomizableProperty matches StringCustomizableProperty doesNotMatch @@ -20,7 +20,7 @@ class WithValuePattern { // } // void equalTo(String equalTo) { - this.equalTo = new NoOpCustomizableProperty(equalTo) + this.equalTo = new SingleTypeCustomizableProperty(equalTo) } // void equalToXml(String equalToXml) { From 4898470613ec39a05ea0f8ea8b82fccd38b894f4 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Thu, 12 Feb 2015 00:33:22 +0100 Subject: [PATCH 5/9] [#5] Implemented Request and Response with headers and body (not yet fully though ;) ) --- .../dsl/BaseWiremockStubStrategy.groovy | 18 +- .../dsl/WiremockRequestStubStrategy.groovy | 31 +-- .../dsl/WiremockResponseStubStrategy.groovy | 34 ++- .../accurest/dsl/WiremockStubStrategy.groovy | 26 ++ .../accurest/dsl/internal/Body.groovy | 24 ++ .../accurest/dsl/internal/DslProperty.groovy | 19 ++ .../accurest/dsl/internal/Headers.groovy | 18 +- .../accurest/dsl/internal/Response.groovy | 27 ++ .../dsl/WiremockGroovyDslResponseSpec.groovy | 71 +++--- .../accurest/dsl/WiremockGroovyDslSpec.groovy | 230 ++++++++++-------- 10 files changed, 315 insertions(+), 183 deletions(-) create mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockStubStrategy.groovy create mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy create mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/BaseWiremockStubStrategy.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/BaseWiremockStubStrategy.groovy index 8c02509ad8..d30ace044b 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/BaseWiremockStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/BaseWiremockStubStrategy.groovy @@ -8,19 +8,25 @@ import io.coderate.accurest.dsl.internal.WithValuePattern @CompileStatic abstract class BaseWiremockStubStrategy { protected Map buildClientHeadersSection(Headers headers) { - return createHeadersSection(headers) { - Map.Entry entry -> [(entry.key): buildClientHeaderFromValuePattern(entry.value)] + if (!headers) { + return null } + return withAssertionHeaders(headers) { + Map.Entry entry -> [(entry.key): buildClientHeaderFromValuePattern(entry.value)] + } << headers?.valueHeaders() } protected Map buildServerHeadersSection(Headers headers) { - return createHeadersSection(headers) { - Map.Entry entry -> [(entry.key): buildServerHeaderFromValuePattern(entry.value)] + if (!headers) { + return null } + return withAssertionHeaders(headers) { + Map.Entry entry -> [(entry.key): buildServerHeaderFromValuePattern(entry.value)] + } << headers.valueHeaders() } - private Map createHeadersSection(Headers headers, Closure closure) { - return headers?.entries()?.collectEntries(closure) + private Map withAssertionHeaders(Headers headers, Closure closure) { + return headers?.assertionEntries()?.collectEntries(closure) } private Map buildClientHeaderFromValuePattern(WithValuePattern valuePattern) { diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy index 3693d7864e..c4dfa605d5 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy @@ -1,10 +1,11 @@ package io.coderate.accurest.dsl -import groovy.json.JsonOutput import groovy.transform.CompileStatic +import groovy.transform.PackageScope import io.coderate.accurest.dsl.internal.Request @CompileStatic +@PackageScope class WiremockRequestStubStrategy extends BaseWiremockStubStrategy { private final Request request @@ -13,31 +14,23 @@ class WiremockRequestStubStrategy extends BaseWiremockStubStrategy { this.request = groovyDsl.request } - String toWiremockClientStub() { - return JsonOutput.toJson(buildClientRequest(request)) - } - - String toWiremockServerStub() { - return JsonOutput.toJson(buildServerRequest(request)) - } - - private Map buildClientRequest(Request request) { - return getRequestSection(request, + @PackageScope Map buildClientRequestContent() { + return buildRequestContent(request, { request.urlPattern?.toClientSide() }, { buildClientHeadersSection(request.headers) }) } - private Map buildServerRequest(Request request) { - return getRequestSection(request, + @PackageScope Map buildServerRequestContent() { + return buildRequestContent(request, { request.urlPattern?.toServerSide() }, { buildServerHeadersSection(request.headers) }) } - private Map> getRequestSection(Request request, Closure buildUrlPattern, Closure buildHeaders) { - return [request: [method : request.method, - url : request.url, - urlPattern: buildUrlPattern(), - urlPath : request.urlPath, - headers : buildHeaders()].findAll { it.value }] + private Map buildRequestContent(Request request, Closure buildUrlPattern, Closure buildHeaders) { + return [method : request.method, + url : request.url, + urlPattern: buildUrlPattern(), + urlPath : request.urlPath, + headers : buildHeaders()].findAll { it.value } } } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy index 1df83b5f58..da013d07f3 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy @@ -1,37 +1,35 @@ package io.coderate.accurest.dsl -import groovy.json.JsonOutput import groovy.transform.CompileStatic +import groovy.transform.PackageScope import io.coderate.accurest.dsl.internal.Response @CompileStatic +@PackageScope class WiremockResponseStubStrategy extends BaseWiremockStubStrategy { - Response response + private final Response response - WiremockResponseStubStrategy(GroovyDsl groovyDsl) { //TODO: Or Response? + WiremockResponseStubStrategy(GroovyDsl groovyDsl) { this.response = groovyDsl.response } - String toWiremockClientStub() { - return JsonOutput.toJson(buildClientResponse(response)) + @PackageScope Map buildClientResponseContent() { + return buildResponseContent(response, + { response.getBody().forClientSide() }, + { buildClientHeadersSection(response.headers) }) } - private Map buildClientResponse(Response response) { - return getResponseSection(response, { "TODO" }, { buildClientHeadersSection(response.headers) }) + @PackageScope Map buildServerResponseContent() { + return buildResponseContent(response, + { response.getBody().forServerSide() }, + { buildServerHeadersSection(response.headers) }) } - String toWiremockServerStub() { - return JsonOutput.toJson(buildServerResponse(response)) + private Map buildResponseContent(Response response, Closure> buildBody, Closure buildHeaders) { + return [status : response.status, + body : buildBody(), + headers: buildHeaders()].findAll { it.value } } - private Map buildServerResponse(Response response) { - return getResponseSection(response, { "TODO" }, { buildServerHeadersSection(response.headers) }) - } - - private Map> getResponseSection(Response response, Closure buildUrlPattern, Closure buildHeaders) { - return [response: [status : response.status, - headers: buildHeaders()] - .findAll { it.value }] - } } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockStubStrategy.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockStubStrategy.groovy new file mode 100644 index 0000000000..c4ced294d6 --- /dev/null +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockStubStrategy.groovy @@ -0,0 +1,26 @@ +package io.coderate.accurest.dsl + +import groovy.json.JsonOutput +import groovy.transform.CompileStatic + +@CompileStatic +class WiremockStubStrategy { + + private final WiremockRequestStubStrategy wiremockRequestStubStrategy + private final WiremockResponseStubStrategy wiremockResponseStubStrategy + + WiremockStubStrategy(GroovyDsl groovyDsl) { + this.wiremockRequestStubStrategy = new WiremockRequestStubStrategy(groovyDsl) + this.wiremockResponseStubStrategy = new WiremockResponseStubStrategy(groovyDsl) + } + + String toWiremockClientStub() { + return JsonOutput.toJson([request: wiremockRequestStubStrategy.buildClientRequestContent(), + response: wiremockResponseStubStrategy.buildClientResponseContent()]) + } + + String toWiremockServerStub() { + return JsonOutput.toJson([request: wiremockRequestStubStrategy.buildServerRequestContent(), + response: wiremockResponseStubStrategy.buildServerResponseContent()]) + } +} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy new file mode 100644 index 0000000000..8d395ee2be --- /dev/null +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy @@ -0,0 +1,24 @@ +package io.coderate.accurest.dsl.internal +import groovy.transform.CompileStatic + +@CompileStatic +class Body { + + private final Map body + + Body() { + this.body = [:] + } + + Body(Map body) { + this.body = body + } + + Map forClientSide() { + return body.collectEntries { Map.Entry entry -> [(entry.key) : entry.value.clientValue] } as Map + } + + Map forServerSide() { + return body.collectEntries { Map.Entry entry -> [(entry.key) : entry.value.serverValue] } as Map + } +} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy new file mode 100644 index 0000000000..f52eabef59 --- /dev/null +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy @@ -0,0 +1,19 @@ +package io.coderate.accurest.dsl.internal +import groovy.transform.CompileStatic + +@CompileStatic +class DslProperty { + + final Object clientValue + final Object serverValue + + DslProperty(Object clientValue, Object serverValue) { + this.clientValue = clientValue + this.serverValue = serverValue + } + + DslProperty(Object singleValue) { + this.clientValue = singleValue + this.serverValue = singleValue + } +} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy index ca7e87e7cb..9ee8dfb242 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy @@ -2,15 +2,25 @@ package io.coderate.accurest.dsl.internal class Headers { - private Map headers = [:] + private Map assertionHeaders = [:] + private Map valueHeaders = [:] WithValuePattern header(String headerName) { WithValuePattern withValuePattern = new WithValuePattern() - headers[headerName] = withValuePattern + assertionHeaders[headerName] = withValuePattern return withValuePattern } - Set> entries() { - return Collections.unmodifiableSet(headers.entrySet()) + void header(Map singleHeader) { + Map.Entry first = singleHeader.entrySet().first() + valueHeaders[first?.key] = first?.value + } + + Map valueHeaders() { + return Collections.unmodifiableMap(valueHeaders) + } + + Set> assertionEntries() { + return Collections.unmodifiableSet(assertionHeaders.entrySet()) } } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy index 6b0c0d1916..e2993fdf5e 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy @@ -7,8 +7,12 @@ import static io.coderate.accurest.dsl.internal.DelegateHelper.delegateToClosure @TypeChecked class Response { + private static final String CLIENT_PROP_KEY = 'client' + private static final String SERVER_PROP_KEY = 'server' + private int status private Headers headers + private Body body = new Body() void status(int status) { this.status = status @@ -19,6 +23,29 @@ class Response { delegateToClosure(closure, headers) } + void body(Map body) { + this.body = new Body(convertObjectsToDslProperties(body)) + } + + private Map convertObjectsToDslProperties(Map body) { + return body.collectEntries { + Map.Entry entry -> + [(entry.key): entry.value instanceof DslProperty ? entry.value : new DslProperty(entry.value)] + } as Map + } + + DslProperty property(Map properties) { + return new DslProperty(properties[CLIENT_PROP_KEY], properties[SERVER_PROP_KEY]) + } + + DslProperty $(Map properties) { + return property(properties) + } + + Body getBody() { + return body + } + int getStatus() { return status } diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy index 65241c420d..201d495af6 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy @@ -3,7 +3,6 @@ package io.codearte.accurest.dsl import groovy.json.JsonSlurper import io.coderate.accurest.dsl.GroovyDsl import io.coderate.accurest.dsl.WiremockResponseStubStrategy -import spock.lang.Ignore import spock.lang.Specification class WiremockGroovyDslResponseSpec extends Specification { @@ -15,25 +14,19 @@ class WiremockGroovyDslResponseSpec extends Specification { status 200 } } - when: - String wiremockStub = new WiremockResponseStubStrategy(dsl)."toWiremock${side}Stub"() - then: - new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(expectedStub) + expect: + new WiremockResponseStubStrategy(dsl)."build${side}ResponseContent"() == new JsonSlurper().parseText(expectedStub) where: side << ['Client', 'Server'] expectedStub << [''' { - "response": { - "status": 200 - } + "status": 200 } ''', ''' { - "response": { - "status": 200 - } + "status": 200 } '''] } @@ -43,49 +36,69 @@ class WiremockGroovyDslResponseSpec extends Specification { GroovyDsl dsl = GroovyDsl.make { response { headers { - header('Content-Type').equalTo('text/xml') + header('Content-Type').matches { + client('text/xml') + server('text/*') + } } status 200 } } - when: - String wiremockStub = new WiremockResponseStubStrategy(dsl).toWiremockClientStub() - then: - new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' + expect: + new WiremockResponseStubStrategy(dsl).buildClientResponseContent() == new JsonSlurper().parseText(''' { - "response": { "headers": { "Content-Type": { - "equalTo": "text/xml" + "matches": "text/xml" }, }, "status": 200 - } } ''') } - @Ignore("Not implemented yet") def 'should generate headers for response for server side'() { given: GroovyDsl dsl = GroovyDsl.make { response { status 200 headers { - header('Content-Type').equalTo('text/xml') + header('Content-Type').matches { + client('text/xml') + server('text/*') + } } } } - when: - String wiremockStub = new WiremockResponseStubStrategy(dsl).toWiremockClientStub() - then: - new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' + expect: + new WiremockResponseStubStrategy(dsl).buildServerResponseContent() == new JsonSlurper().parseText(''' { - "response": { - "status": 200, - "headers": - "Content-Type": "text/xml" + "status": 200, + "headers": { + "Content-Type": { + "matches": "text/*" + } + } + } + ''') + } + + def 'should generate an exact header for response for both sides '() { + given: + GroovyDsl dsl = GroovyDsl.make { + response { + status 200 + headers { + header 'Content-Type': 'text/xml' + } + } } + expect: + new WiremockResponseStubStrategy(dsl).buildServerResponseContent() == new JsonSlurper().parseText(''' + { + "status": 200, + "headers": { + "Content-Type": "text/xml" } } ''') diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy index a26983984d..f5011af153 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy @@ -3,40 +3,38 @@ package io.codearte.accurest.dsl import groovy.json.JsonSlurper import io.coderate.accurest.dsl.GroovyDsl import io.coderate.accurest.dsl.WiremockRequestStubStrategy +import io.coderate.accurest.dsl.WiremockStubStrategy import spock.lang.Ignore import spock.lang.Specification class WiremockGroovyDslSpec extends Specification { - // TODO: add alias instead of placeholder - @Ignore - def 'should convert groovy dsl stub to wiremock stub'() { + def 'should convert groovy dsl stub to wiremock stub for the client side'() { given: - GroovyDsl dsl = GroovyDsl.make { - request { - method('GET') - urlPattern { - client('/[0-9]{2}') - server('/12') + GroovyDsl groovyDsl = GroovyDsl.make { + request { + method('GET') + urlPattern { + client('/[0-9]{2}') + server('/12') + } + } + response { + status(200) + body ( + id : property(client: '123', server: { regex('[0-9]+') } ), + name: 'Jan', + created : $(client: '2014-02-02 12:23:43', server: { currentDate(it) }) + ) + headers { + header('Content-Type': 'text/plain') + } } } - response { - status(200) - body { - withPlaceholder(client: '2015-01-14', server: '$anyInt($it)') - withTemplate(''' - { - "date" : "$placeholder0" - } - ''') - } - headers { - Content-Type('text/plain') - } - } - } - expect: - dsl.toWiremockClientStub() == ''' + when: + String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockClientStub() + then: + new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' { "request": { "method": "GET", @@ -44,13 +42,63 @@ class WiremockGroovyDslSpec extends Specification { }, "response": { "status": 200, - "body": "2015-01-14", + "body": { + "id": "123", + "name": "Jan", + "created" : "2014-02-02 12:23:43" + }, "headers": { "Content-Type": "text/plain" } } } -''' +''') + } + + def 'should convert groovy dsl stub to wiremock stub for the server side'() { + given: + GroovyDsl groovyDsl = GroovyDsl.make { + request { + method('GET') + urlPattern { + client('/[0-9]{2}') + server('/12') + } + } + response { + status(200) + body ( + id : property(client: '123', server: '321' ), + name: 'Jan', + created : $(client: '2014-02-02 12:23:43', server: '1999-01-01 01:23:45') + ) + headers { + header('Content-Type': 'text/plain') + } + } + } + when: + String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockServerStub() + then: + new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' +{ + "request": { + "method": "GET", + "urlPattern": "/12" + }, + "response": { + "status": 200, + "body": { + "id": "321", + "name": "Jan", + "created" : "1999-01-01 01:23:45" + }, + "headers": { + "Content-Type": "text/plain" + } + } +} +''') } def "should generate stub with GET"() { @@ -60,14 +108,10 @@ class WiremockGroovyDslSpec extends Specification { method("GET") } } - when: - String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockClientStub() - then: - new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' + expect: + new WiremockRequestStubStrategy(groovyDsl).buildClientRequestContent() == new JsonSlurper().parseText(''' { - "request":{ - "method":"GET" - } + "method":"GET" } ''') } @@ -80,15 +124,11 @@ class WiremockGroovyDslSpec extends Specification { url("/sth") } } - when: - String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockClientStub() - then: - new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' + expect: + new WiremockRequestStubStrategy(groovyDsl).buildClientRequestContent() == new JsonSlurper().parseText(''' { - "request":{ - "method":"GET", - "url":"/sth" - } + "method":"GET", + "url":"/sth" } ''') } @@ -102,14 +142,10 @@ class WiremockGroovyDslSpec extends Specification { } } } - when: - String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockClientStub() - then: - new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' + expect: + new WiremockRequestStubStrategy(groovyDsl).buildClientRequestContent() == new JsonSlurper().parseText(''' { - "request":{ - "urlPattern":"/^[0-9]{2}$" - } + "urlPattern":"/^[0-9]{2}$" } ''') } @@ -124,14 +160,10 @@ class WiremockGroovyDslSpec extends Specification { } } } - when: - String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockServerStub() - then: - new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' + expect: + new WiremockRequestStubStrategy(groovyDsl).buildServerRequestContent() == new JsonSlurper().parseText(''' { - "request":{ - "urlPattern":"/12" - } + "urlPattern":"/12" } ''') } @@ -143,14 +175,10 @@ class WiremockGroovyDslSpec extends Specification { urlPath ('/12') } } - when: - String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockClientStub() - then: - new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' + expect: + new WiremockRequestStubStrategy(groovyDsl).buildClientRequestContent() == new JsonSlurper().parseText(''' { - "request":{ - "urlPath":"/12" - } + "urlPath":"/12" } ''') } @@ -162,14 +190,10 @@ class WiremockGroovyDslSpec extends Specification { urlPath ('/12') } } - when: - String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockServerStub() - then: - new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' + expect: + new WiremockRequestStubStrategy(groovyDsl).buildClientRequestContent() == new JsonSlurper().parseText(''' { - "request":{ - "urlPath":"/12" - } + "urlPath":"/12" } ''') } @@ -195,25 +219,21 @@ class WiremockGroovyDslSpec extends Specification { } } } - when: - String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockClientStub() - then: - new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' + expect: + new WiremockRequestStubStrategy(groovyDsl).buildClientRequestContent() == new JsonSlurper().parseText(''' { - "request":{ - "headers": { - "Content-Type": { - "equalTo": "text/xml" - }, - "Accept": { - "matches": "text/.*" - }, - "etag": { - "doesNotMatch": "abcd.*" - }, - "X-Custom-Header": { - "contains": "2134" - } + "headers": { + "Content-Type": { + "equalTo": "text/xml" + }, + "Accept": { + "matches": "text/.*" + }, + "etag": { + "doesNotMatch": "abcd.*" + }, + "X-Custom-Header": { + "contains": "2134" } } } @@ -241,25 +261,21 @@ class WiremockGroovyDslSpec extends Specification { } } } - when: - String wiremockStub = new WiremockRequestStubStrategy(groovyDsl).toWiremockServerStub() - then: - new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText(''' + expect: + new WiremockRequestStubStrategy(groovyDsl).buildServerRequestContent() == new JsonSlurper().parseText(''' { - "request":{ - "headers": { - "Content-Type": { - "equalTo": "text/xml" - }, - "Accept": { - "matches": "text/plain" - }, - "etag": { - "doesNotMatch": "abcdef" - }, - "X-Custom-Header": { - "contains": "121345" - } + "headers": { + "Content-Type": { + "equalTo": "text/xml" + }, + "Accept": { + "matches": "text/plain" + }, + "etag": { + "doesNotMatch": "abcdef" + }, + "X-Custom-Header": { + "contains": "121345" } } } From 88537c139ea7135064c42ea6d713ee2b2c210279 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Thu, 12 Feb 2015 15:48:55 +0100 Subject: [PATCH 6/9] [#5] Changed the approach to use $(), value() instead of closures with client and server --- .../dsl/BaseWiremockStubStrategy.groovy | 9 +- .../dsl/WiremockRequestStubStrategy.groovy | 30 +++-- .../dsl/WiremockResponseStubStrategy.groovy | 24 ++-- .../dsl/internal/ClientDslProperty.groovy | 13 ++ .../accurest/dsl/internal/Common.groovy | 53 ++++++++ .../dsl/internal/CustomizableProperty.groovy | 39 ------ .../accurest/dsl/internal/DslProperty.groovy | 10 +- .../dsl/internal/JSONCompareMode.groovy | 2 +- .../accurest/dsl/internal/Request.groovy | 67 +++++++--- .../accurest/dsl/internal/Response.groovy | 54 ++++---- .../dsl/internal/ServerDslProperty.groovy | 13 ++ .../dsl/internal/WithValuePattern.groovy | 120 ++++++++++++------ .../dsl/WiremockGroovyDslResponseSpec.groovy | 10 +- .../accurest/dsl/WiremockGroovyDslSpec.groovy | 91 +++++++------ 14 files changed, 342 insertions(+), 193 deletions(-) create mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ClientDslProperty.groovy create mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Common.groovy delete mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/CustomizableProperty.groovy create mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ServerDslProperty.groovy diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/BaseWiremockStubStrategy.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/BaseWiremockStubStrategy.groovy index d30ace044b..3925ada9f3 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/BaseWiremockStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/BaseWiremockStubStrategy.groovy @@ -1,7 +1,6 @@ package io.coderate.accurest.dsl - import groovy.transform.CompileStatic -import io.coderate.accurest.dsl.internal.CustomizableProperty +import io.coderate.accurest.dsl.internal.DslProperty import io.coderate.accurest.dsl.internal.Headers import io.coderate.accurest.dsl.internal.WithValuePattern @@ -32,16 +31,16 @@ abstract class BaseWiremockStubStrategy { private Map buildClientHeaderFromValuePattern(WithValuePattern valuePattern) { return getValuePatternSection(valuePattern) .findAll { it.value } - .collectEntries { [(it.key): it.value.toClientSide()] } + .collectEntries { [(it.key): it.value.clientValue] } } private Map buildServerHeaderFromValuePattern(WithValuePattern valuePattern) { return getValuePatternSection(valuePattern) .findAll { it.value } - .collectEntries { [(it.key): it.value.toServerSide()] } + .collectEntries { [(it.key): it.value.serverValue] } } - private Map getValuePatternSection(WithValuePattern valuePattern) { + private Map getValuePatternSection(WithValuePattern valuePattern) { return [equalToJson : valuePattern.equalToJson, equalToXml : valuePattern.equalToXml, matchesXPath : valuePattern.matchesXPath, diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy index c4dfa605d5..369b082cf8 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockRequestStubStrategy.groovy @@ -2,7 +2,9 @@ package io.coderate.accurest.dsl import groovy.transform.CompileStatic import groovy.transform.PackageScope +import io.coderate.accurest.dsl.internal.ClientRequest import io.coderate.accurest.dsl.internal.Request +import io.coderate.accurest.dsl.internal.ServerRequest @CompileStatic @PackageScope @@ -15,22 +17,26 @@ class WiremockRequestStubStrategy extends BaseWiremockStubStrategy { } @PackageScope Map buildClientRequestContent() { - return buildRequestContent(request, - { request.urlPattern?.toClientSide() }, - { buildClientHeadersSection(request.headers) }) + return buildRequestContent(new ClientRequest(request)) } @PackageScope Map buildServerRequestContent() { - return buildRequestContent(request, - { request.urlPattern?.toServerSide() }, - { buildServerHeadersSection(request.headers) }) + return buildRequestContent(new ServerRequest(request)) } - private Map buildRequestContent(Request request, Closure buildUrlPattern, Closure buildHeaders) { - return [method : request.method, - url : request.url, - urlPattern: buildUrlPattern(), - urlPath : request.urlPath, - headers : buildHeaders()].findAll { it.value } + private Map buildRequestContent(ClientRequest request) { + return [method : request?.method?.clientValue, + url : request?.url?.clientValue, + urlPattern: request?.urlPattern?.clientValue, + urlPath : request?.urlPath?.clientValue, + headers : buildClientHeadersSection(request.headers)].findAll { it.value } + } + + private Map buildRequestContent(ServerRequest request) { + return [method : request?.method?.serverValue, + url : request?.url?.serverValue, + urlPattern: request?.urlPattern?.serverValue, + urlPath : request?.urlPath?.serverValue, + headers : buildServerHeadersSection(request.headers)].findAll { it.value } } } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy index da013d07f3..76f7f38a3b 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy @@ -2,7 +2,9 @@ package io.coderate.accurest.dsl import groovy.transform.CompileStatic import groovy.transform.PackageScope +import io.coderate.accurest.dsl.internal.ClientResponse import io.coderate.accurest.dsl.internal.Response +import io.coderate.accurest.dsl.internal.ServerResponse @CompileStatic @PackageScope @@ -15,21 +17,23 @@ class WiremockResponseStubStrategy extends BaseWiremockStubStrategy { } @PackageScope Map buildClientResponseContent() { - return buildResponseContent(response, - { response.getBody().forClientSide() }, - { buildClientHeadersSection(response.headers) }) + return buildResponseContent(new ClientResponse(response)) } @PackageScope Map buildServerResponseContent() { - return buildResponseContent(response, - { response.getBody().forServerSide() }, - { buildServerHeadersSection(response.headers) }) + return buildResponseContent(new ServerResponse(response)) } - private Map buildResponseContent(Response response, Closure> buildBody, Closure buildHeaders) { - return [status : response.status, - body : buildBody(), - headers: buildHeaders()].findAll { it.value } + private Map buildResponseContent(ClientResponse response) { + return [status : response?.status?.clientValue, + body : response?.getBody()?.forClientSide(), + headers: buildClientHeadersSection(response.headers)].findAll { it.value } + } + + private Map buildResponseContent(ServerResponse response) { + return [status : response?.status?.serverValue, + body : response?.getBody()?.forServerSide(), + headers: buildServerHeadersSection(response.headers)].findAll { it.value } } } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ClientDslProperty.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ClientDslProperty.groovy new file mode 100644 index 0000000000..005d52bc7b --- /dev/null +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ClientDslProperty.groovy @@ -0,0 +1,13 @@ +package io.coderate.accurest.dsl.internal +import groovy.transform.CompileStatic + +@CompileStatic +class ClientDslProperty extends DslProperty { + + ClientDslProperty(Object clientValue, Object serverValue) { + super(clientValue, serverValue) } + + ClientDslProperty(Object singleValue) { + super(singleValue) + } +} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Common.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Common.groovy new file mode 100644 index 0000000000..7fbd42caea --- /dev/null +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Common.groovy @@ -0,0 +1,53 @@ +package io.coderate.accurest.dsl.internal + +import groovy.transform.PackageScope +import groovy.transform.TypeChecked + +/** + * @TypeChecked instead of @CompileStatic due to usage of double dispatch. + * Double dispatch doesn't work if you're using @CompileStatic + */ +@TypeChecked +@PackageScope +class Common { + + Map convertObjectsToDslProperties(Map body) { + return body.collectEntries { + Map.Entry entry -> + [(entry.key): toDslProperty(entry.value)] + } as Map + } + + DslProperty toDslProperty(Object property) { + return new DslProperty(property) + } + + DslProperty toDslProperty(DslProperty property) { + return property + } + + DslProperty value(ClientDslProperty client, ServerDslProperty server) { + return new DslProperty(client.clientValue, server.serverValue) + } + + DslProperty value(ServerDslProperty server, ClientDslProperty client) { + return new DslProperty(client.clientValue, server.serverValue) + } + + DslProperty $(ClientDslProperty client, ServerDslProperty server) { + return value(client, server) + } + + DslProperty $(ServerDslProperty server, ClientDslProperty client) { + return value(client, server) + } + + ClientDslProperty client(Object clientValue) { + return new ClientDslProperty(clientValue) + } + + ServerDslProperty server(Object serverValue) { + return new ServerDslProperty(serverValue) + } + +} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/CustomizableProperty.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/CustomizableProperty.groovy deleted file mode 100644 index fc6dfcb503..0000000000 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/CustomizableProperty.groovy +++ /dev/null @@ -1,39 +0,0 @@ -package io.coderate.accurest.dsl.internal - -//TODO: Can we have different types for Client/Server (client: pattern(String), server: value(Boolean/Int)) ? -class CustomizableProperty { - - private T client - private V server - - void client(T client) { - this.client = client - } - - void server(V server) { - this.server = server - } - - T toClientSide() { - return client - } - - V toServerSide() { - return server - } -} - -class StringCustomizableProperty extends CustomizableProperty { -} - -class SingleTypeCustomizableProperty extends CustomizableProperty { - SingleTypeCustomizableProperty(T value) { - client(value) - server(value) - } - - @Override - public String toString() { - return toClientSide() - } -} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy index f52eabef59..11ede5a82e 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy @@ -2,17 +2,17 @@ package io.coderate.accurest.dsl.internal import groovy.transform.CompileStatic @CompileStatic -class DslProperty { +class DslProperty { - final Object clientValue - final Object serverValue + final T clientValue + final T serverValue - DslProperty(Object clientValue, Object serverValue) { + DslProperty(T clientValue, T serverValue) { this.clientValue = clientValue this.serverValue = serverValue } - DslProperty(Object singleValue) { + DslProperty(T singleValue) { this.clientValue = singleValue this.serverValue = singleValue } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/JSONCompareMode.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/JSONCompareMode.groovy index 591432866e..7f07abb723 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/JSONCompareMode.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/JSONCompareMode.groovy @@ -1,5 +1,5 @@ package io.coderate.accurest.dsl.internal enum JSONCompareMode { - STRICT, LENIENT, NON_EXTENSIBLE, STRICT_ORDER; + STRICT, LENIENT, NON_EXTENSIBLE, STRICT_ORDER } \ No newline at end of file diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy index d89bd1bd84..d28bc159c0 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy @@ -1,32 +1,51 @@ package io.coderate.accurest.dsl.internal - +import groovy.transform.CompileStatic import groovy.transform.TypeChecked -import io.coderate.accurest.dsl.internal.Headers -import io.coderate.accurest.dsl.internal.StringCustomizableProperty import static io.coderate.accurest.dsl.internal.DelegateHelper.delegateToClosure @TypeChecked -class Request { +class Request extends Common { - String method - String url - StringCustomizableProperty urlPattern - String urlPath + DslProperty method + DslProperty url + DslProperty urlPattern + DslProperty urlPath Headers headers + Request() { + } + + Request(Request request) { + this.method = request.method + this.url = request.url + this.urlPattern = request.urlPattern + this.urlPath = request.urlPath + this.headers = request.headers + } + void method(String method) { - this.method = method + this.method = toDslProperty(method) + } + + void method(DslProperty method) { + this.method = toDslProperty(method) } void url(String url) { - this.url = url + this.url = toDslProperty(url) } - void urlPattern(@DelegatesTo(StringCustomizableProperty) Closure closure) { - StringCustomizableProperty urlPattern = new StringCustomizableProperty() - this.urlPattern = urlPattern - delegateToClosure(closure, urlPattern) + void url(DslProperty url) { + this.url = toDslProperty(url) + } + + void urlPattern(String urlPattern) { + this.urlPattern = toDslProperty(urlPattern) + } + + void urlPattern(DslProperty urlPattern) { + this.urlPattern = toDslProperty(urlPattern) } void headers(@DelegatesTo(Headers) Closure closure) { @@ -36,6 +55,24 @@ class Request { } void urlPath(String urlPath) { - this.urlPath = urlPath + this.urlPath = toDslProperty(urlPath) + } + + void urlPath(DslProperty urlPath) { + this.urlPath = toDslProperty(urlPath) + } +} + +@CompileStatic +class ServerRequest extends Request { + ServerRequest(Request request) { + super(request) + } +} + +@CompileStatic +class ClientRequest extends Request { + ClientRequest(Request request) { + super(request) } } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy index e2993fdf5e..fe15280bde 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy @@ -1,21 +1,32 @@ package io.coderate.accurest.dsl.internal +import groovy.transform.CompileStatic import groovy.transform.TypeChecked import static io.coderate.accurest.dsl.internal.DelegateHelper.delegateToClosure @TypeChecked -class Response { +class Response extends Common { - private static final String CLIENT_PROP_KEY = 'client' - private static final String SERVER_PROP_KEY = 'server' - - private int status + private DslProperty status private Headers headers private Body body = new Body() + Response() { + } + + Response(Response response) { + this.status = response.status + this.headers = response.headers + this.body = response.body + } + void status(int status) { - this.status = status + this.status = toDslProperty(status) + } + + void status(DslProperty status) { + this.status = toDslProperty(status) } void headers(@DelegatesTo(Headers) Closure closure) { @@ -27,26 +38,11 @@ class Response { this.body = new Body(convertObjectsToDslProperties(body)) } - private Map convertObjectsToDslProperties(Map body) { - return body.collectEntries { - Map.Entry entry -> - [(entry.key): entry.value instanceof DslProperty ? entry.value : new DslProperty(entry.value)] - } as Map - } - - DslProperty property(Map properties) { - return new DslProperty(properties[CLIENT_PROP_KEY], properties[SERVER_PROP_KEY]) - } - - DslProperty $(Map properties) { - return property(properties) - } - Body getBody() { return body } - int getStatus() { + DslProperty getStatus() { return status } @@ -54,3 +50,17 @@ class Response { return headers } } + +@CompileStatic +class ServerResponse extends Response { + ServerResponse(Response request) { + super(request) + } +} + +@CompileStatic +class ClientResponse extends Response { + ClientResponse(Response request) { + super(request) + } +} \ No newline at end of file diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ServerDslProperty.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ServerDslProperty.groovy new file mode 100644 index 0000000000..a23cc80c23 --- /dev/null +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ServerDslProperty.groovy @@ -0,0 +1,13 @@ +package io.coderate.accurest.dsl.internal +import groovy.transform.CompileStatic + +@CompileStatic +class ServerDslProperty extends DslProperty { + + ServerDslProperty(Object clientValue, Object serverValue) { + super(clientValue, serverValue) } + + ServerDslProperty(Object singleValue) { + super(singleValue) + } +} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy index 9e44935cfa..32dfddf741 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy @@ -1,51 +1,99 @@ package io.coderate.accurest.dsl.internal +import groovy.transform.TypeChecked -import static io.coderate.accurest.dsl.internal.DelegateHelper.delegateToClosure - +@TypeChecked class WithValuePattern { - SingleTypeCustomizableProperty equalToJson - SingleTypeCustomizableProperty equalToXml - StringCustomizableProperty matchesXPath - SingleTypeCustomizableProperty jsonCompareMode - SingleTypeCustomizableProperty equalTo - StringCustomizableProperty contains - StringCustomizableProperty matches - StringCustomizableProperty doesNotMatch - CustomizableProperty absent - StringCustomizableProperty matchesJsonPath + DslProperty equalTo + DslProperty equalToJson + DslProperty equalToXml + DslProperty matchesXPath + DslProperty jsonCompareMode + DslProperty contains + DslProperty matches + DslProperty doesNotMatch + DslProperty absent + DslProperty matchesJsonPath -// void equalToJson(String equalToJson) { -// this.equalToJson = equalToJson -// } -// void equalTo(String equalTo) { - this.equalTo = new SingleTypeCustomizableProperty(equalTo) + this.equalTo = new DslProperty(equalTo) } -// void equalToXml(String equalToXml) { -// this.equalToXml = equalToXml -// } - - void matches(@DelegatesTo(StringCustomizableProperty) Closure closure) { - StringCustomizableProperty placeholderHaving = new StringCustomizableProperty() - this.matches = placeholderHaving - delegateToClosure(closure, placeholderHaving) + void equalTo(DslProperty equalTo) { + this.equalTo = equalTo } - void doesNotMatch(@DelegatesTo(StringCustomizableProperty) Closure closure) { - StringCustomizableProperty placeholderHaving = new StringCustomizableProperty() - this.doesNotMatch = placeholderHaving - delegateToClosure(closure, placeholderHaving) + void equalToJson(String equalToJson) { + this.equalToJson = new DslProperty(equalToJson) } - void contains(@DelegatesTo(StringCustomizableProperty) Closure closure) { - StringCustomizableProperty placeholderHaving = new StringCustomizableProperty() - this.contains = placeholderHaving - delegateToClosure(closure, placeholderHaving) + void equalToJson(DslProperty equalToJson) { + this.equalToJson = equalToJson } -// void jsonCompareMode(JSONCompareMode jsonCompareMode) { -// this.jsonCompareMode = jsonCompareMode -// } + void equalToXml(String equalToXml) { + this.equalToXml = new DslProperty(equalToXml) + } + + void equalToXml(DslProperty equalToXml) { + this.equalToXml = equalToXml + } + + void matchesXPath(String matchesXPath) { + this.matchesXPath = new DslProperty(matchesXPath) + } + + void matchesXPath(DslProperty matchesXPath) { + this.matchesXPath = matchesXPath + } + + void jsonCompareMode(JSONCompareMode jsonCompareMode) { + this.jsonCompareMode = new DslProperty(jsonCompareMode) + } + + void jsonCompareMode(DslProperty jsonCompareMode) { + this.jsonCompareMode = jsonCompareMode + } + + void contains(String contains) { + this.contains = new DslProperty(contains) + } + + void contains(DslProperty contains) { + this.contains = contains + } + + void matches(String matches) { + this.matches = new DslProperty(matches) + } + + void matches(DslProperty matches) { + this.matches = matches + } + + void doesNotMatch(String doesNotMatch) { + this.doesNotMatch = new DslProperty(doesNotMatch) + } + + void doesNotMatch(DslProperty doesNotMatch) { + this.doesNotMatch = doesNotMatch + } + + void absent(String absent) { + this.absent = new DslProperty(absent) + } + + void absent(DslProperty absent) { + this.absent = absent + } + + void matchesJsonPath(String matchesJsonPath) { + this.matchesJsonPath = new DslProperty(matchesJsonPath) + } + + void matchesJsonPath(DslProperty matchesJsonPath) { + this.matchesJsonPath = matchesJsonPath + } + + } diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy index 201d495af6..f8aa6a6890 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslResponseSpec.groovy @@ -36,10 +36,7 @@ class WiremockGroovyDslResponseSpec extends Specification { GroovyDsl dsl = GroovyDsl.make { response { headers { - header('Content-Type').matches { - client('text/xml') - server('text/*') - } + header('Content-Type').matches $(client('text/xml'), server('text/*')) } status 200 } @@ -63,10 +60,7 @@ class WiremockGroovyDslResponseSpec extends Specification { response { status 200 headers { - header('Content-Type').matches { - client('text/xml') - server('text/*') - } + header('Content-Type').matches $(client('text/xml'), server('text/*')) } } } diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy index f5011af153..3ee9cd7f11 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WiremockGroovyDslSpec.groovy @@ -14,20 +14,24 @@ class WiremockGroovyDslSpec extends Specification { GroovyDsl groovyDsl = GroovyDsl.make { request { method('GET') - urlPattern { - client('/[0-9]{2}') - server('/12') - } + urlPattern $(client('/[0-9]{2}'), server('/12')) } response { - status(200) + status 200 body ( - id : property(client: '123', server: { regex('[0-9]+') } ), + id : value( + client('123'), + server({ regex('[0-9]+') }) + ), + surname : $( + client('Kowalsky'), + server('Lewandowski') + ), name: 'Jan', - created : $(client: '2014-02-02 12:23:43', server: { currentDate(it) }) + created : $(client('2014-02-02 12:23:43'), server({ currentDate(it) })) ) headers { - header('Content-Type': 'text/plain') + header 'Content-Type': 'text/plain' } } } @@ -44,6 +48,7 @@ class WiremockGroovyDslSpec extends Specification { "status": 200, "body": { "id": "123", + "surname": "Kowalsky", "name": "Jan", "created" : "2014-02-02 12:23:43" }, @@ -60,17 +65,21 @@ class WiremockGroovyDslSpec extends Specification { GroovyDsl groovyDsl = GroovyDsl.make { request { method('GET') - urlPattern { - client('/[0-9]{2}') - server('/12') - } + urlPattern $(client('/[0-9]{2}'), server('/12')) } response { status(200) body ( - id : property(client: '123', server: '321' ), - name: 'Jan', - created : $(client: '2014-02-02 12:23:43', server: '1999-01-01 01:23:45') + id : value( + client('123'), + server('321') + ), + surname : $( + client('Kowalsky'), + server('Lewandowski') + ), + name: 'Jan', + created : $(client('2014-02-02 12:23:43'), server('1999-01-01 01:23:45')) ) headers { header('Content-Type': 'text/plain') @@ -90,6 +99,7 @@ class WiremockGroovyDslSpec extends Specification { "status": 200, "body": { "id": "321", + "surname": "Lewandowski", "name": "Jan", "created" : "1999-01-01 01:23:45" }, @@ -137,9 +147,10 @@ class WiremockGroovyDslSpec extends Specification { given: GroovyDsl groovyDsl = GroovyDsl.make { request { - urlPattern { - client('/^[0-9]{2}$') - } + urlPattern $( + client('/^[0-9]{2}$'), + server('/12') + ) } } expect: @@ -154,10 +165,10 @@ class WiremockGroovyDslSpec extends Specification { given: GroovyDsl groovyDsl = GroovyDsl.make { request { - urlPattern { - client('/^[0-9]{2}$') - server('/12') - } + urlPattern $( + client('/[0-9]{2}'), + server('/12') + ) } } expect: @@ -204,18 +215,18 @@ class WiremockGroovyDslSpec extends Specification { request { headers { header('Content-Type').equalTo('text/xml') - header('Accept').matches { - client('text/.*') + header('Accept').matches $( + client('text/.*'), server('text/plain') - } - header('etag').doesNotMatch { - client('abcd.*') + ) + header('etag').doesNotMatch $( + client('abcd.*'), server('abcdef') - } - header('X-Custom-Header').contains { - client('2134') + ) + header('X-Custom-Header').contains $( + client('2134'), server('121345') - } + ) } } } @@ -246,18 +257,18 @@ class WiremockGroovyDslSpec extends Specification { request { headers { header('Content-Type').equalTo('text/xml') - header('Accept').matches { - client('text/.*') + header('Accept').matches $( + client('text/.*'), server('text/plain') - } - header('etag').doesNotMatch { - client('abcd.*') + ) + header('etag').doesNotMatch $( + client('abcd.*'), server('abcdef') - } - header('X-Custom-Header').contains { - client('2134') + ) + header('X-Custom-Header').contains $( + client('2134'), server('121345') - } + ) } } } From e5d218a2d2167964eccf25c6ef2c510aab4dd5c5 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Thu, 12 Feb 2015 16:01:22 +0100 Subject: [PATCH 7/9] [#5] Minor refactoring --- .../accurest/dsl/WiremockResponseStubStrategy.groovy | 4 ++-- .../groovy/io/coderate/accurest/dsl/internal/Body.groovy | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy index 76f7f38a3b..ab62663b51 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/WiremockResponseStubStrategy.groovy @@ -26,13 +26,13 @@ class WiremockResponseStubStrategy extends BaseWiremockStubStrategy { private Map buildResponseContent(ClientResponse response) { return [status : response?.status?.clientValue, - body : response?.getBody()?.forClientSide(), + body : response?.body?.forClientSide(), headers: buildClientHeadersSection(response.headers)].findAll { it.value } } private Map buildResponseContent(ServerResponse response) { return [status : response?.status?.serverValue, - body : response?.getBody()?.forServerSide(), + body : response?.body?.forServerSide(), headers: buildServerHeadersSection(response.headers)].findAll { it.value } } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy index 8d395ee2be..90ae62d726 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy @@ -15,10 +15,14 @@ class Body { } Map forClientSide() { - return body.collectEntries { Map.Entry entry -> [(entry.key) : entry.value.clientValue] } as Map + return body.collectEntries { + Map.Entry entry -> [(entry.key) : entry.value.clientValue] + } as Map } Map forServerSide() { - return body.collectEntries { Map.Entry entry -> [(entry.key) : entry.value.serverValue] } as Map + return body.collectEntries { + Map.Entry entry -> [(entry.key) : entry.value.serverValue] + } as Map } } From 4e0296252521fffa51bc1e7ef303bbc242f1230c Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Thu, 12 Feb 2015 17:21:45 +0100 Subject: [PATCH 8/9] [#10] Added initial version of Wiremock -> GroovyDsl --- .../wiremock/WiremockToDslConverter.groovy | 37 +++++++++++++ .../WiremockToDslConverterSpec.groovy | 54 +++++++++++++++++++ .../io/coderate/accurest/dsl/GroovyDsl.groovy | 4 ++ .../accurest/dsl/internal/Body.groovy | 4 ++ .../accurest/dsl/internal/DslProperty.groovy | 4 ++ .../accurest/dsl/internal/Headers.groovy | 5 ++ .../accurest/dsl/internal/Request.groovy | 8 +++ .../accurest/dsl/internal/Response.groovy | 8 +++ .../dsl/internal/ServerDslProperty.groovy | 4 ++ .../dsl/internal/WithValuePattern.groovy | 5 ++ build.gradle | 6 +++ settings.gradle | 5 +- 12 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 accurest-converters/src/main/groovy/io/codearte/accurest/wiremock/WiremockToDslConverter.groovy create mode 100644 accurest-converters/src/test/groovy/io/codearte/accurest/wiremock/WiremockToDslConverterSpec.groovy diff --git a/accurest-converters/src/main/groovy/io/codearte/accurest/wiremock/WiremockToDslConverter.groovy b/accurest-converters/src/main/groovy/io/codearte/accurest/wiremock/WiremockToDslConverter.groovy new file mode 100644 index 0000000000..cdd09eee72 --- /dev/null +++ b/accurest-converters/src/main/groovy/io/codearte/accurest/wiremock/WiremockToDslConverter.groovy @@ -0,0 +1,37 @@ +package io.codearte.accurest.wiremock +import groovy.json.JsonSlurper +import io.coderate.accurest.dsl.GroovyDsl + +class WiremockToDslConverter { + static GroovyDsl fromWiremockStub(String wiremockStringStub) { + Object wiremockStub = new JsonSlurper().parseText(wiremockStringStub) + def wiremockRequest = wiremockStub.request + def wiremockResponse = wiremockStub.response + return GroovyDsl.make { + request { + wiremockRequest.method ? method(wiremockRequest.method as String) : null + wiremockRequest.url ? url(wiremockRequest.url as String) : null + wiremockRequest.urlPattern ? urlPattern(wiremockRequest.urlPattern as String) : null + wiremockRequest.urlPath ? urlPath(wiremockRequest.urlPath as String) : null + wiremockRequest.headers ? headers { + wiremockRequest.headers.each { + def assertion = it.value + String headerName = it.key as String + header(headerName)."$assertion.key"(assertion.value) + } + } : null + } + response { + status wiremockResponse.status ? wiremockResponse.status as Integer : null + wiremockResponse.body ? body ( + wiremockResponse.body as Map + ) : null + wiremockResponse.headers ? headers { + wiremockResponse.headers.each { + header([(it.key) : it.value]) + } + } : null + } + } + } +} diff --git a/accurest-converters/src/test/groovy/io/codearte/accurest/wiremock/WiremockToDslConverterSpec.groovy b/accurest-converters/src/test/groovy/io/codearte/accurest/wiremock/WiremockToDslConverterSpec.groovy new file mode 100644 index 0000000000..6a4534a20b --- /dev/null +++ b/accurest-converters/src/test/groovy/io/codearte/accurest/wiremock/WiremockToDslConverterSpec.groovy @@ -0,0 +1,54 @@ +package io.codearte.accurest.wiremock + +import io.coderate.accurest.dsl.GroovyDsl +import spock.lang.Specification + +class WiremockToDslConverterSpec extends Specification { + + def 'should produce a Groovy DSL from Wiremock stub'() { + given: + String wiremockStub = ''' +{ + "request": { + "method": "GET", + "urlPattern": "/[0-9]{2}" + }, + "response": { + "status": 200, + "body": { + "id": "123", + "surname": "Kowalsky", + "name": "Jan", + "created" : "2014-02-02 12:23:43" + }, + "headers": { + "Content-Type": "text/plain" + } + } +} +''' + and: + GroovyDsl expectedGroovyDsl = GroovyDsl.make { + request { + method 'GET' + urlPattern '/[0-9]{2}' + } + response { + status 200 + body ( + id : '123', + surname : 'Kowalsky', + name: 'Jan', + created : '2014-02-02 12:23:43' + ) + headers { + header 'Content-Type': 'text/plain' + } + } + } + when: + GroovyDsl groovyDsl = WiremockToDslConverter.fromWiremockStub(wiremockStub) + then: + groovyDsl == expectedGroovyDsl + } +} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy index 1ae6fe4644..47a0e8e72f 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy @@ -1,10 +1,14 @@ package io.coderate.accurest.dsl +import groovy.transform.EqualsAndHashCode +import groovy.transform.ToString import groovy.transform.TypeChecked import io.coderate.accurest.dsl.internal.Request import io.coderate.accurest.dsl.internal.Response @TypeChecked +@EqualsAndHashCode(includeFields = true) +@ToString(includePackage = false) class GroovyDsl { Request request diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy index 90ae62d726..e3b75c9eb4 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Body.groovy @@ -1,7 +1,11 @@ package io.coderate.accurest.dsl.internal import groovy.transform.CompileStatic +import groovy.transform.EqualsAndHashCode +import groovy.transform.ToString @CompileStatic +@ToString(includePackage = false, includeFields = true) +@EqualsAndHashCode(includeFields = true) class Body { private final Map body diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy index 11ede5a82e..f1de600e8a 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy @@ -1,7 +1,11 @@ package io.coderate.accurest.dsl.internal import groovy.transform.CompileStatic +import groovy.transform.EqualsAndHashCode +import groovy.transform.ToString @CompileStatic +@EqualsAndHashCode(includeFields = true) +@ToString(includePackage = false) class DslProperty { final T clientValue diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy index 9ee8dfb242..5487b10b53 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Headers.groovy @@ -1,5 +1,10 @@ package io.coderate.accurest.dsl.internal +import groovy.transform.EqualsAndHashCode +import groovy.transform.ToString + +@EqualsAndHashCode(includeFields = true) +@ToString(includePackage = false) class Headers { private Map assertionHeaders = [:] diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy index d28bc159c0..3d0f7fec66 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy @@ -1,10 +1,14 @@ package io.coderate.accurest.dsl.internal import groovy.transform.CompileStatic +import groovy.transform.EqualsAndHashCode +import groovy.transform.ToString import groovy.transform.TypeChecked import static io.coderate.accurest.dsl.internal.DelegateHelper.delegateToClosure @TypeChecked +@EqualsAndHashCode(includeFields = true) +@ToString(includePackage = false) class Request extends Common { DslProperty method @@ -64,6 +68,8 @@ class Request extends Common { } @CompileStatic +@EqualsAndHashCode(includeFields = true) +@ToString(includePackage = false) class ServerRequest extends Request { ServerRequest(Request request) { super(request) @@ -71,6 +77,8 @@ class ServerRequest extends Request { } @CompileStatic +@EqualsAndHashCode(includeFields = true) +@ToString(includePackage = false) class ClientRequest extends Request { ClientRequest(Request request) { super(request) diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy index fe15280bde..190ad7b3d9 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy @@ -1,11 +1,15 @@ package io.coderate.accurest.dsl.internal import groovy.transform.CompileStatic +import groovy.transform.EqualsAndHashCode +import groovy.transform.ToString import groovy.transform.TypeChecked import static io.coderate.accurest.dsl.internal.DelegateHelper.delegateToClosure @TypeChecked +@EqualsAndHashCode(includeFields = true) +@ToString(includePackage = false, includeFields = true) class Response extends Common { private DslProperty status @@ -52,6 +56,8 @@ class Response extends Common { } @CompileStatic +@EqualsAndHashCode(includeFields = true) +@ToString(includePackage = false) class ServerResponse extends Response { ServerResponse(Response request) { super(request) @@ -59,6 +65,8 @@ class ServerResponse extends Response { } @CompileStatic +@EqualsAndHashCode(includeFields = true) +@ToString(includePackage = false) class ClientResponse extends Response { ClientResponse(Response request) { super(request) diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ServerDslProperty.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ServerDslProperty.groovy index a23cc80c23..71827545dd 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ServerDslProperty.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ServerDslProperty.groovy @@ -1,7 +1,11 @@ package io.coderate.accurest.dsl.internal import groovy.transform.CompileStatic +import groovy.transform.EqualsAndHashCode +import groovy.transform.ToString @CompileStatic +@EqualsAndHashCode(includeFields = true) +@ToString(includePackage = false) class ServerDslProperty extends DslProperty { ServerDslProperty(Object clientValue, Object serverValue) { diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy index 32dfddf741..8a3b1d1f20 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy @@ -1,7 +1,12 @@ package io.coderate.accurest.dsl.internal + +import groovy.transform.EqualsAndHashCode +import groovy.transform.ToString import groovy.transform.TypeChecked @TypeChecked +@EqualsAndHashCode(includeFields = true) +@ToString(includePackage = false) class WithValuePattern { DslProperty equalTo diff --git a/build.gradle b/build.gradle index 33ecf9a2db..a74ec2a59f 100644 --- a/build.gradle +++ b/build.gradle @@ -44,6 +44,12 @@ subprojects { project(':accurest-core') { } +project(':accurest-converters') { + dependencies { + compile project(':accurest-core') + } +} + project(':accurest-gradle-plugin') { dependencies { compile project(':accurest-core') diff --git a/settings.gradle b/settings.gradle index dff32d91dd..148580177b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,3 @@ -include "accurest-core", "accurest-gradle-plugin" -rootProject.name = "accurest" \ No newline at end of file +include "accurest-core", "accurest-gradle-plugin", 'accurest-converters' +rootProject.name = "accurest" + From 71757e1ec8aa31cd285b86f620dd111e05af0e39 Mon Sep 17 00:00:00 2001 From: Marcin Zajaczkowski Date: Fri, 13 Feb 2015 00:11:00 +0100 Subject: [PATCH 9/9] [#5] Minor enhancements --- .../groovy/io/coderate/accurest/dsl/GroovyDsl.groovy | 6 ++---- .../accurest/dsl/internal/ClientDslProperty.groovy | 3 --- .../io/coderate/accurest/dsl/internal/Common.groovy | 3 +-- .../accurest/dsl/internal/DelegateHelper.groovy | 10 ---------- .../coderate/accurest/dsl/internal/DslProperty.groovy | 1 + .../io/coderate/accurest/dsl/internal/Request.groovy | 8 +++----- .../io/coderate/accurest/dsl/internal/Response.groovy | 5 ++--- .../accurest/dsl/internal/ServerDslProperty.groovy | 4 +--- .../accurest/dsl/internal/WithValuePattern.groovy | 2 -- 9 files changed, 10 insertions(+), 32 deletions(-) delete mode 100644 accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DelegateHelper.groovy diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy index 1ae6fe4644..4c7ef95839 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/GroovyDsl.groovy @@ -18,15 +18,13 @@ class GroovyDsl { } void request(@DelegatesTo(Request) Closure closure) { - Request request = new Request() - this.request = request + this.request = new Request() closure.delegate = request closure() } void response(@DelegatesTo(Response) Closure closure) { - Response response = new Response() - this.response = response + this.response = new Response() closure.delegate = response closure() } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ClientDslProperty.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ClientDslProperty.groovy index 005d52bc7b..5551688e02 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ClientDslProperty.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ClientDslProperty.groovy @@ -4,9 +4,6 @@ import groovy.transform.CompileStatic @CompileStatic class ClientDslProperty extends DslProperty { - ClientDslProperty(Object clientValue, Object serverValue) { - super(clientValue, serverValue) } - ClientDslProperty(Object singleValue) { super(singleValue) } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Common.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Common.groovy index 7fbd42caea..fc5135af33 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Common.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Common.groovy @@ -39,7 +39,7 @@ class Common { } DslProperty $(ServerDslProperty server, ClientDslProperty client) { - return value(client, server) + return value(server, client) } ClientDslProperty client(Object clientValue) { @@ -49,5 +49,4 @@ class Common { ServerDslProperty server(Object serverValue) { return new ServerDslProperty(serverValue) } - } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DelegateHelper.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DelegateHelper.groovy deleted file mode 100644 index baa8240dcf..0000000000 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DelegateHelper.groovy +++ /dev/null @@ -1,10 +0,0 @@ -package io.coderate.accurest.dsl.internal - -//TODO: There is problem with a trait usage -class DelegateHelper { - - public static void delegateToClosure(@DelegatesTo(T) Closure closure, T delegate) { - closure.delegate = delegate - closure() - } -} diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy index 11ede5a82e..485ff61a79 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/DslProperty.groovy @@ -1,4 +1,5 @@ package io.coderate.accurest.dsl.internal + import groovy.transform.CompileStatic @CompileStatic diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy index d28bc159c0..7f41fe41fd 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Request.groovy @@ -2,8 +2,6 @@ package io.coderate.accurest.dsl.internal import groovy.transform.CompileStatic import groovy.transform.TypeChecked -import static io.coderate.accurest.dsl.internal.DelegateHelper.delegateToClosure - @TypeChecked class Request extends Common { @@ -49,9 +47,9 @@ class Request extends Common { } void headers(@DelegatesTo(Headers) Closure closure) { - Headers headers = new Headers() - this.headers = headers - delegateToClosure(closure, headers) + this.headers = new Headers() + closure.delegate = headers + closure() } void urlPath(String urlPath) { diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy index fe15280bde..928d441dc5 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/Response.groovy @@ -3,8 +3,6 @@ package io.coderate.accurest.dsl.internal import groovy.transform.CompileStatic import groovy.transform.TypeChecked -import static io.coderate.accurest.dsl.internal.DelegateHelper.delegateToClosure - @TypeChecked class Response extends Common { @@ -31,7 +29,8 @@ class Response extends Common { void headers(@DelegatesTo(Headers) Closure closure) { this.headers = new Headers() - delegateToClosure(closure, headers) + closure.delegate = headers + closure() } void body(Map body) { diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ServerDslProperty.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ServerDslProperty.groovy index a23cc80c23..1e468f10e5 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ServerDslProperty.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/ServerDslProperty.groovy @@ -1,12 +1,10 @@ package io.coderate.accurest.dsl.internal + import groovy.transform.CompileStatic @CompileStatic class ServerDslProperty extends DslProperty { - ServerDslProperty(Object clientValue, Object serverValue) { - super(clientValue, serverValue) } - ServerDslProperty(Object singleValue) { super(singleValue) } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy index 32dfddf741..d9fb4cc588 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/dsl/internal/WithValuePattern.groovy @@ -94,6 +94,4 @@ class WithValuePattern { void matchesJsonPath(DslProperty matchesJsonPath) { this.matchesJsonPath = matchesJsonPath } - - }