Skip to content

Commit 67a9587

Browse files
author
Ronald Holshausen
committed
Correct the PactDslJsonRootValue to generate correctly encoded JSON strings #369
1 parent 226fe53 commit 67a9587

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package au.com.dius.pact.consumer;
2+
3+
import au.com.dius.pact.consumer.dsl.PactDslJsonRootValue;
4+
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
5+
import au.com.dius.pact.model.PactFragment;
6+
import org.apache.http.client.fluent.Request;
7+
import org.apache.http.entity.ContentType;
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
11+
import java.io.IOException;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
import static org.junit.Assert.assertEquals;
16+
17+
public class Defect369Test {
18+
19+
private static final String APPLICATION_JSON = "application/json";
20+
@Rule
21+
public PactProviderRule provider = new PactProviderRule("369_provider", "localhost", 8081, this);
22+
23+
@Pact(provider="369_provider", consumer="test_consumer")
24+
public PactFragment createFragment(PactDslWithProvider builder) {
25+
Map<String, String> headers = new HashMap<String, String>();
26+
headers.put("Content-Type", "application/json");
27+
return builder
28+
.uponReceiving("a request for a simple string")
29+
.path("/provider/uri")
30+
.method("GET")
31+
.willRespondWith()
32+
.status(200)
33+
.headers(headers)
34+
.body(PactDslJsonRootValue.stringType("Example"))
35+
.toFragment();
36+
}
37+
38+
@Test
39+
@PactVerification("369_provider")
40+
public void runTest() throws IOException {
41+
assertEquals("\"Example\"", Request.Get("http://localhost:8081/provider/uri")
42+
.addHeader("Accept", APPLICATION_JSON)
43+
.execute().returnContent().asString());
44+
}
45+
}

pact-jvm-consumer/src/main/java/au/com/dius/pact/consumer/dsl/PactDslJsonRootValue.java

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import au.com.dius.pact.consumer.InvalidMatcherException;
44
import com.mifmif.common.regex.Generex;
5+
import groovy.json.JsonOutput;
56
import org.apache.commons.lang3.RandomStringUtils;
67
import org.apache.commons.lang3.time.DateFormatUtils;
78
import org.apache.commons.lang3.time.FastDateFormat;
@@ -35,6 +36,9 @@ protected void putArray(DslPart object) {
3536

3637
@Override
3738
public Object getBody() {
39+
if (value instanceof String) {
40+
return JsonOutput.toJson(value);
41+
}
3842
return value;
3943
}
4044

pact-jvm-consumer/src/test/groovy/au/com/dius/pact/consumer/PactDslJsonBodyMatcherSpec.groovy

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class PactDslJsonBodyMatcherSpec extends Specification {
102102
then:
103103
result.size() == 3
104104
result.keySet() == keys
105-
result.types == ['abc', 'abc']
105+
result.types == ['"abc"', '"abc"']
106106
subject.matchers == [
107107
'$.body.types': [min: 0, match: 'type'],
108108
'$.body.subscriptionId': [match: 'type'],
@@ -125,7 +125,7 @@ class PactDslJsonBodyMatcherSpec extends Specification {
125125
then:
126126
result.size() == 3
127127
result.keySet() == keys
128-
result.types == ['abc', 'abc']
128+
result.types == ['"abc"', '"abc"']
129129
subject.matchers == [
130130
'$.body.types': [min: 2, match: 'type'],
131131
'$.body.subscriptionId': [match: 'type'],
@@ -148,7 +148,7 @@ class PactDslJsonBodyMatcherSpec extends Specification {
148148
then:
149149
result.size() == 3
150150
result.keySet() == keys
151-
result.types == ['abc', 'abc']
151+
result.types == ['"abc"', '"abc"']
152152
subject.matchers == [
153153
'$.body.types': [max: 10, match: 'type'],
154154
'$.body.subscriptionId': [match: 'type'],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package au.com.dius.pact.consumer.dsl
2+
3+
import spock.lang.Shared
4+
import spock.lang.Specification
5+
import spock.lang.Unroll
6+
7+
class PactDslJsonRootValueSpec extends Specification {
8+
@SuppressWarnings('PrivateFieldCouldBeFinal')
9+
@Shared
10+
private Date date = new Date(100, 1, 1, 20, 0, 0)
11+
12+
@Unroll
13+
def 'correctly converts the value #value to JSON'() {
14+
expect:
15+
value.body as String == json
16+
17+
where:
18+
19+
value | json
20+
PactDslJsonRootValue.stringType('TEST') | '"TEST"'
21+
PactDslJsonRootValue.numberType(100) | '100'
22+
PactDslJsonRootValue.integerType(100) | '100'
23+
PactDslJsonRootValue.decimalType(100) | '100.0'
24+
PactDslJsonRootValue.booleanType(true) | 'true'
25+
PactDslJsonRootValue.stringMatcher('\\w+', 'test') | '"test"'
26+
PactDslJsonRootValue.timestamp('yyyy-MM-dd HH:mm:ss', date) | '"2000-02-01 20:00:00"'
27+
PactDslJsonRootValue.time('HH:mm:ss', date) | '"20:00:00"'
28+
PactDslJsonRootValue.date('yyyy-MM-dd', date) | '"2000-02-01"'
29+
PactDslJsonRootValue.ipAddress() | '"127.0.0.1"'
30+
PactDslJsonRootValue.id(1000) | '1000'
31+
PactDslJsonRootValue.hexValue('1000') | '"1000"'
32+
PactDslJsonRootValue.uuid('e87f3c51-545c-4bc2-b1b5-284de67d627e') | '"e87f3c51-545c-4bc2-b1b5-284de67d627e"'
33+
}
34+
35+
}

0 commit comments

Comments
 (0)