Fixed multipart support in JUnit tests.
This commit is contained in:
@@ -6,8 +6,12 @@ import groovy.transform.TypeChecked
|
||||
import io.codearte.accurest.dsl.GroovyDsl
|
||||
import io.codearte.accurest.dsl.internal.ExecutionProperty
|
||||
import io.codearte.accurest.dsl.internal.Header
|
||||
import io.codearte.accurest.dsl.internal.NamedProperty
|
||||
import io.codearte.accurest.dsl.internal.Request
|
||||
|
||||
import static groovy.json.StringEscapeUtils.escapeJava
|
||||
import static io.codearte.accurest.util.ContentUtils.getJavaMultipartFileParameterContent
|
||||
|
||||
/**
|
||||
* @author Jakub Kubrynski
|
||||
* @author Olga Maciaszek-Sharma
|
||||
@@ -41,11 +45,6 @@ abstract class JUnitMethodBodyBuilder extends MethodBodyBuilder {
|
||||
return "assertThat(responseBody${property}).isEqualTo(\"${value}\");"
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getMultipartParameterLine(Map.Entry<String, Object> parameter) {
|
||||
return null //TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processBodyElement(BlockBuilder blockBuilder, String property, ExecutionProperty exec) {
|
||||
blockBuilder.addLine("${exec.insertValue("parsedJson.read(\"\\\$$property\")")};")
|
||||
@@ -59,7 +58,7 @@ abstract class JUnitMethodBodyBuilder extends MethodBodyBuilder {
|
||||
@Override
|
||||
protected String convertUnicodeEscapesIfRequired(String json) {
|
||||
String unescapedJson = StringEscapeUtils.unescapeJavaScript(json)
|
||||
return StringEscapeUtils.escapeJava(unescapedJson)
|
||||
return escapeJava(unescapedJson)
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -68,10 +67,10 @@ abstract class JUnitMethodBodyBuilder extends MethodBodyBuilder {
|
||||
}
|
||||
|
||||
private String getMapKeyReferenceString(Map.Entry entry) {
|
||||
if (entry.value instanceof ExecutionProperty){
|
||||
if (entry.value instanceof ExecutionProperty) {
|
||||
return "." + entry.key
|
||||
}
|
||||
return """.get(\\\"$entry.key\\\")"""
|
||||
return """.get(\\\"$entry.key\\\")"""
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -103,4 +102,14 @@ abstract class JUnitMethodBodyBuilder extends MethodBodyBuilder {
|
||||
protected String getBodyString(String bodyAsString) {
|
||||
return ".body(\"$bodyAsString\")"
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getMultipartFileParameterContent(String propertyName, NamedProperty propertyValue) {
|
||||
return getJavaMultipartFileParameterContent(propertyName, propertyValue)
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getParameterString(Map.Entry<String, Object> parameter) {
|
||||
return """.param("${escapeJava(parameter.key)}", "${escapeJava(parameter.value as String)}")"""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import io.codearte.accurest.dsl.internal.DslProperty
|
||||
import io.codearte.accurest.dsl.internal.ExecutionProperty
|
||||
import io.codearte.accurest.dsl.internal.Header
|
||||
import io.codearte.accurest.dsl.internal.MatchingStrategy
|
||||
import io.codearte.accurest.dsl.internal.NamedProperty
|
||||
import io.codearte.accurest.dsl.internal.QueryParameter
|
||||
import io.codearte.accurest.dsl.internal.Request
|
||||
import io.codearte.accurest.dsl.internal.Response
|
||||
@@ -19,6 +20,7 @@ import io.codearte.accurest.util.JsonToJsonPathsConverter
|
||||
import io.codearte.accurest.util.MapConverter
|
||||
|
||||
import static io.codearte.accurest.util.ContentUtils.extractValue
|
||||
import static io.codearte.accurest.util.ContentUtils.getGroovyMultipartFileParameterContent
|
||||
import static io.codearte.accurest.util.ContentUtils.recognizeContentTypeFromContent
|
||||
import static io.codearte.accurest.util.ContentUtils.recognizeContentTypeFromHeader
|
||||
|
||||
@@ -50,8 +52,6 @@ abstract class MethodBodyBuilder {
|
||||
|
||||
protected abstract String getResponseBodyPropertyComparisonString(String property, String value)
|
||||
|
||||
protected abstract String getMultipartParameterLine(Map.Entry<String, Object> parameter)
|
||||
|
||||
protected abstract void processBodyElement(BlockBuilder blockBuilder, String property, ExecutionProperty exec)
|
||||
|
||||
protected abstract void processBodyElement(BlockBuilder blockBuilder, String property, Map.Entry entry)
|
||||
@@ -72,6 +72,10 @@ abstract class MethodBodyBuilder {
|
||||
|
||||
protected abstract String getBodyString(String bodyAsString)
|
||||
|
||||
protected abstract String getMultipartFileParameterContent(String propertyName, NamedProperty propertyValue)
|
||||
|
||||
protected abstract String getParameterString(Map.Entry<String, Object> parameter)
|
||||
|
||||
void appendTo(BlockBuilder blockBuilder) {
|
||||
blockBuilder.startBlock()
|
||||
|
||||
@@ -126,7 +130,8 @@ abstract class MethodBodyBuilder {
|
||||
String url = buildUrl(request)
|
||||
String method = request.method.serverValue.toString().toLowerCase()
|
||||
|
||||
bb.addLine(/.${method}("$url");/)
|
||||
bb.addLine(/.${method}("$url")/)
|
||||
addColonIfRequired(bb)
|
||||
bb.unindent()
|
||||
}
|
||||
|
||||
@@ -275,7 +280,6 @@ abstract class MethodBodyBuilder {
|
||||
throw new IllegalStateException("URL is not set!")
|
||||
}
|
||||
|
||||
|
||||
@TypeChecked(TypeCheckingMode.SKIP)
|
||||
protected String buildUrlFromUrlPath(Url url) {
|
||||
if (hasQueryParams(url)) {
|
||||
@@ -290,6 +294,13 @@ abstract class MethodBodyBuilder {
|
||||
return MapConverter.getTestSideValues(url.serverValue)
|
||||
}
|
||||
|
||||
protected String getMultipartParameterLine(Map.Entry<String, Object> parameter) {
|
||||
if (parameter.value instanceof NamedProperty) {
|
||||
return ".multiPart(${getMultipartFileParameterContent(parameter.key, (NamedProperty) parameter.value)})"
|
||||
}
|
||||
return getParameterString(parameter)
|
||||
}
|
||||
|
||||
|
||||
private boolean hasQueryParams(Url url) {
|
||||
return url.queryParameters
|
||||
|
||||
@@ -9,7 +9,7 @@ import io.codearte.accurest.dsl.internal.Header
|
||||
import io.codearte.accurest.dsl.internal.NamedProperty
|
||||
import io.codearte.accurest.dsl.internal.Request
|
||||
|
||||
import static io.codearte.accurest.util.ContentUtils.getMultipartFileParameterContent
|
||||
import static io.codearte.accurest.util.ContentUtils.getGroovyMultipartFileParameterContent
|
||||
|
||||
/**
|
||||
* @author Jakub Kubrynski
|
||||
@@ -27,14 +27,6 @@ abstract class SpockMethodBodyBuilder extends MethodBodyBuilder {
|
||||
return "responseBody$property == \"${value}\""
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getMultipartParameterLine(Map.Entry<String, Object> parameter) {
|
||||
if (parameter.value instanceof NamedProperty) {
|
||||
return ".multiPart(${getMultipartFileParameterContent(parameter.key, (NamedProperty) parameter.value)})"
|
||||
}
|
||||
return ".param('$parameter.key', '$parameter.value')"
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processBodyElement(BlockBuilder blockBuilder, String property, ExecutionProperty exec) {
|
||||
blockBuilder.addLine("${exec.insertValue("parsedJson.read('\\\$$property')")}")
|
||||
@@ -95,4 +87,14 @@ abstract class SpockMethodBodyBuilder extends MethodBodyBuilder {
|
||||
return ".body('''$bodyAsString''')"
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getMultipartFileParameterContent(String propertyName, NamedProperty propertyValue) {
|
||||
return getGroovyMultipartFileParameterContent(propertyName, propertyValue)
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getParameterString(Map.Entry<String, Object> parameter) {
|
||||
return ".param('$parameter.key', '$parameter.value')"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.codehaus.groovy.runtime.GStringImpl
|
||||
import java.util.regex.Matcher
|
||||
import java.util.regex.Pattern
|
||||
|
||||
import static org.apache.commons.lang3.StringEscapeUtils.escapeJava
|
||||
import static org.apache.commons.lang3.StringEscapeUtils.escapeJson
|
||||
import static org.apache.commons.lang3.StringEscapeUtils.escapeXml11
|
||||
|
||||
@@ -321,8 +322,13 @@ class ContentUtils {
|
||||
return ContentType.UNKNOWN
|
||||
}
|
||||
|
||||
static String getMultipartFileParameterContent(String propertyName, NamedProperty propertyValue) {
|
||||
static String getGroovyMultipartFileParameterContent(String propertyName, NamedProperty propertyValue) {
|
||||
return "'$propertyName', '$propertyValue.name.serverValue', '$propertyValue.value.serverValue'.bytes"
|
||||
}
|
||||
|
||||
static String getJavaMultipartFileParameterContent(String propertyName, NamedProperty propertyValue) {
|
||||
return """"${escapeJava(propertyName)}", "${escapeJava(propertyValue.name.serverValue as String)}", "${escapeJava(propertyValue.value.serverValue as String)}".getBytes()"""
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import io.codearte.accurest.dsl.GroovyDsl
|
||||
import io.codearte.accurest.dsl.WireMockStubStrategy
|
||||
import io.codearte.accurest.dsl.WireMockStubVerifier
|
||||
import io.codearte.accurest.file.Contract
|
||||
import spock.lang.Ignore
|
||||
import spock.lang.Issue
|
||||
import spock.lang.Specification
|
||||
import spock.lang.Unroll
|
||||
@@ -904,8 +903,6 @@ World.'''""")
|
||||
}
|
||||
|
||||
@Issue('180')
|
||||
@Ignore
|
||||
//TODO: fix multiparts in JUnit
|
||||
def "should generate proper test code when having multipart parameters"() {
|
||||
given:
|
||||
GroovyDsl contractDsl = GroovyDsl.make {
|
||||
@@ -931,15 +928,13 @@ World.'''""")
|
||||
builder.given(blockBuilder)
|
||||
def jUnitTest = blockBuilder.toString()
|
||||
then:
|
||||
jUnitTest.contains("""'content-type', 'multipart/form-data;boundary=AaB03x'""")
|
||||
jUnitTest.contains(""".param('formParameter', '"formParameterValue"'""")
|
||||
jUnitTest.contains(""".param('someBooleanParameter', 'true')""")
|
||||
jUnitTest.contains(""".multiPart('file', 'filename.csv', 'file content'.bytes)""")
|
||||
jUnitTest.contains('"content-type", "multipart/form-data;boundary=AaB03x"')
|
||||
jUnitTest.contains('.param("formParameter", "\\"formParameterValue\\"")')
|
||||
jUnitTest.contains('.param("someBooleanParameter", "true")')
|
||||
jUnitTest.contains('.multiPart("file", "filename.csv", "file content".getBytes());')
|
||||
}
|
||||
|
||||
@Issue('180')
|
||||
//TODO: fix multiparts in JUnit
|
||||
@Ignore
|
||||
def "should generate proper test code when having multipart parameters with named as map"() {
|
||||
given:
|
||||
GroovyDsl contractDsl = GroovyDsl.make {
|
||||
|
||||
@@ -922,7 +922,7 @@ World.'''""")
|
||||
MockMvcSpockMethodBodyBuilder builder = new MockMvcSpockMethodBodyBuilder(contractDsl)
|
||||
BlockBuilder blockBuilder = new BlockBuilder(" ")
|
||||
when:
|
||||
builder.given(blockBuilder)
|
||||
builder.appendTo(blockBuilder)
|
||||
def spockTest = blockBuilder.toString()
|
||||
then:
|
||||
spockTest.contains("""'content-type', 'multipart/form-data;boundary=AaB03x'""")
|
||||
|
||||
Reference in New Issue
Block a user