[#5] Extract common DSL elements
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T, V> {
|
||||
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<String, String> {
|
||||
@Override
|
||||
String toClientSide() {
|
||||
return super.toClientSide() as String
|
||||
}
|
||||
|
||||
@Override
|
||||
String toServerSide() {
|
||||
return super.toServerSide() as String
|
||||
}
|
||||
}
|
||||
|
||||
private class NoOpCustomizableProperty<T> extends CustomizableProperty<T, T> {
|
||||
NoOpCustomizableProperty(T value) {
|
||||
client(value)
|
||||
server(value)
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toClientSide()
|
||||
}
|
||||
}
|
||||
|
||||
class Headers {
|
||||
private Map<String, WithValuePattern> headers = [:]
|
||||
|
||||
WithValuePattern header(String headerName) {
|
||||
WithValuePattern withValuePattern = new WithValuePattern()
|
||||
headers[headerName] = withValuePattern
|
||||
return withValuePattern
|
||||
}
|
||||
|
||||
Set<Map.Entry<String, WithValuePattern>> entries() {
|
||||
return headers.entrySet()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* From Wiremock
|
||||
private Map<String, ValuePattern> queryParamPatterns;
|
||||
private List<ValuePattern> 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<String> equalToJson
|
||||
NoOpCustomizableProperty<String> equalToXml
|
||||
StringCustomizableProperty matchesXPath
|
||||
NoOpCustomizableProperty<JSONCompareMode> jsonCompareMode
|
||||
NoOpCustomizableProperty<String> equalTo
|
||||
StringCustomizableProperty contains
|
||||
StringCustomizableProperty matches
|
||||
StringCustomizableProperty doesNotMatch
|
||||
CustomizableProperty<Boolean, Boolean> 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 <T> void delegateToClosure(@DelegatesTo(T) Closure closure, T delegate) {
|
||||
closure.delegate = delegate
|
||||
closure()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String, Request.WithValuePattern> entry -> [(entry.key): buildClientHeaderFromValuePattern(entry.value)]
|
||||
Map.Entry<String, WithValuePattern> entry -> [(entry.key): buildClientHeaderFromValuePattern(entry.value)]
|
||||
}
|
||||
}
|
||||
|
||||
private Map buildServerHeadersSection(Request.Headers headers) {
|
||||
private Map buildServerHeadersSection(Headers headers) {
|
||||
return createHeadersSection(headers) {
|
||||
Map.Entry<String, Request.WithValuePattern> entry -> [(entry.key): buildServerHeaderFromValuePattern(entry.value)]
|
||||
Map.Entry<String, WithValuePattern> 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<String, Request.CustomizableProperty> getValuePatternSection(Request.WithValuePattern valuePattern) {
|
||||
private Map<String, CustomizableProperty> 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]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<T, V> {
|
||||
|
||||
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<String, String> {
|
||||
}
|
||||
|
||||
class NoOpCustomizableProperty<T> extends CustomizableProperty<T, T> {
|
||||
NoOpCustomizableProperty(T value) {
|
||||
client(value)
|
||||
server(value)
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toClientSide()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package io.coderate.accurest.dsl.internal
|
||||
|
||||
//TODO: There is problem with a trait usage
|
||||
class DelegateHelper {
|
||||
|
||||
public static <T> void delegateToClosure(@DelegatesTo(T) Closure closure, T delegate) {
|
||||
closure.delegate = delegate
|
||||
closure()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.coderate.accurest.dsl.internal
|
||||
|
||||
class Headers {
|
||||
|
||||
private Map<String, WithValuePattern> headers = [:]
|
||||
|
||||
WithValuePattern header(String headerName) {
|
||||
WithValuePattern withValuePattern = new WithValuePattern()
|
||||
headers[headerName] = withValuePattern
|
||||
return withValuePattern
|
||||
}
|
||||
|
||||
Set<Map.Entry<String, WithValuePattern>> entries() {
|
||||
//TODO: Make it immutable
|
||||
return headers.entrySet()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package io.coderate.accurest.dsl.internal
|
||||
|
||||
enum JSONCompareMode {
|
||||
STRICT, LENIENT, NON_EXTENSIBLE, STRICT_ORDER;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package io.coderate.accurest.dsl.internal
|
||||
|
||||
import static io.coderate.accurest.dsl.internal.DelegateHelper.delegateToClosure
|
||||
|
||||
class WithValuePattern {
|
||||
|
||||
NoOpCustomizableProperty<String> equalToJson
|
||||
NoOpCustomizableProperty<String> equalToXml
|
||||
StringCustomizableProperty matchesXPath
|
||||
NoOpCustomizableProperty<JSONCompareMode> jsonCompareMode
|
||||
NoOpCustomizableProperty<String> equalTo
|
||||
StringCustomizableProperty contains
|
||||
StringCustomizableProperty matches
|
||||
StringCustomizableProperty doesNotMatch
|
||||
CustomizableProperty<Boolean, Boolean> 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
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user