Merge branch '2.1.x'

This commit is contained in:
Marcin Grzejszczak
2019-11-06 16:22:31 +01:00
5 changed files with 81 additions and 0 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.contract.verifier.builder;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -41,6 +42,10 @@ class BodyReader {
String readStringFromFileString(SingleContractMetadata metadata,
FromFileProperty property, CommunicationType side) {
if (!Charset.defaultCharset().toString().equals(property.getCharset())) {
return "new String(" + readBytesFromFileString(metadata, property, side)
+ ", \"" + property.getCharset() + "\")";
}
return "new String(" + readBytesFromFileString(metadata, property, side) + ")";
}

View File

@@ -624,6 +624,38 @@ class SingleTestGeneratorSpec extends Specification {
testFramework << [JUNIT, JUNIT5, TESTNG, SPOCK]
}
@Issue('#1199')
def 'should generate tests with body from file with custom charset [#testFramework]'() {
given:
file = tmpFolder.newFile()
writeContract(file)
tmp = tmpFolder.newFolder()
File classpath = new File(SingleTestGeneratorSpec.class.getResource('/charset/').toURI())
FileSystemUtils.copyRecursively(classpath, tmp)
and:
File output = new File(tmp, 'readFromFileWithCharset.groovy')
File contractLocation = output
File temp = tmpFolder.newFolder()
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties(
testFramework: testFramework, contractsDslDir: contractLocation.parentFile,
basePackageForTests: 'a.b', generatedTestSourcesDir: temp,
generatedTestResourcesDir: tmpFolder.newFolder()
)
TestGenerator testGenerator = new TestGenerator(properties)
when:
int count = testGenerator.generate()
then:
count == 1
and:
String test = new File(temp, "a/b/ContractVerifier${getTestName(testFramework)}").text
test.contains('readFromFileWithCharset_request_request.json')
test.contains('RESPONSE')
test.contains('body(new String(fileToBytes(this, "readFromFileWithCharset_request_request.json"), "US-ASCII"))')
where:
testFramework << [JUNIT, JUNIT5, SPOCK]
}
@Issue('#260')
def "should generate tests in a folder taken from baseClassForTests's package when it is set for [#testFramework]"() {
given:

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.nio.charset.Charset
import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
method('PUT')
headers {
contentType(applicationJson())
}
body(file("request.json", Charset.forName("US-ASCII")))
url("/1")
}
response {
status OK()
body(file("response.json", Charset.forName("US-ASCII")))
headers {
contentType(applicationJson())
}
}
}

View File

@@ -0,0 +1,3 @@
{
"status": "REQUEST"
}

View File

@@ -0,0 +1,3 @@
{
"status": "RESPONSE"
}