Skip to content

Commit

Permalink
Correct the PactDslJsonRootValue to generate correctly encoded JSON s…
Browse files Browse the repository at this point in the history
…trings #369
  • Loading branch information
Ronald Holshausen committed Jan 19, 2017
1 parent 1f9a90e commit 17554aa
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
@@ -0,0 +1,45 @@
package au.com.dius.pact.consumer;

import au.com.dius.pact.consumer.dsl.PactDslJsonRootValue;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.model.PactFragment;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class Defect369Test {

private static final String APPLICATION_JSON = "application/json";
@Rule
public PactProviderRule provider = new PactProviderRule("369_provider", "localhost", 8081, this);

@Pact(provider="369_provider", consumer="test_consumer")
public PactFragment createFragment(PactDslWithProvider builder) {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return builder
.uponReceiving("a request for a simple string")
.path("/provider/uri")
.method("GET")
.willRespondWith()
.status(200)
.headers(headers)
.body(PactDslJsonRootValue.stringType("Example"))
.toFragment();
}

@Test
@PactVerification("369_provider")
public void runTest() throws IOException {
assertEquals("\"Example\"", Request.Get("http://localhost:8081/provider/uri")
.addHeader("Accept", APPLICATION_JSON)
.execute().returnContent().asString());
}
}
Expand Up @@ -2,6 +2,7 @@

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

@Override
public Object getBody() {
if (value instanceof String) {
return JsonOutput.toJson(value);
}
return value;
}

Expand Down
Expand Up @@ -102,7 +102,7 @@ class PactDslJsonBodyMatcherSpec extends Specification {
then:
result.size() == 3
result.keySet() == keys
result.types == ['abc', 'abc']
result.types == ['"abc"', '"abc"']
subject.matchers == [
'$.body.types': [min: 0, match: 'type'],
'$.body.subscriptionId': [match: 'type'],
Expand All @@ -125,7 +125,7 @@ class PactDslJsonBodyMatcherSpec extends Specification {
then:
result.size() == 3
result.keySet() == keys
result.types == ['abc', 'abc']
result.types == ['"abc"', '"abc"']
subject.matchers == [
'$.body.types': [min: 2, match: 'type'],
'$.body.subscriptionId': [match: 'type'],
Expand All @@ -148,7 +148,7 @@ class PactDslJsonBodyMatcherSpec extends Specification {
then:
result.size() == 3
result.keySet() == keys
result.types == ['abc', 'abc']
result.types == ['"abc"', '"abc"']
subject.matchers == [
'$.body.types': [max: 10, match: 'type'],
'$.body.subscriptionId': [match: 'type'],
Expand Down
@@ -0,0 +1,35 @@
package au.com.dius.pact.consumer.dsl

import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll

class PactDslJsonRootValueSpec extends Specification {
@SuppressWarnings('PrivateFieldCouldBeFinal')
@Shared
private Date date = new Date(100, 1, 1, 20, 0, 0)

@Unroll
def 'correctly converts the value #value to JSON'() {
expect:
value.body as String == json

where:

value | json
PactDslJsonRootValue.stringType('TEST') | '"TEST"'
PactDslJsonRootValue.numberType(100) | '100'
PactDslJsonRootValue.integerType(100) | '100'
PactDslJsonRootValue.decimalType(100) | '100.0'
PactDslJsonRootValue.booleanType(true) | 'true'
PactDslJsonRootValue.stringMatcher('\\w+', 'test') | '"test"'
PactDslJsonRootValue.timestamp('yyyy-MM-dd HH:mm:ss', date) | '"2000-02-01 20:00:00"'
PactDslJsonRootValue.time('HH:mm:ss', date) | '"20:00:00"'
PactDslJsonRootValue.date('yyyy-MM-dd', date) | '"2000-02-01"'
PactDslJsonRootValue.ipAddress() | '"127.0.0.1"'
PactDslJsonRootValue.id(1000) | '1000'
PactDslJsonRootValue.hexValue('1000') | '"1000"'
PactDslJsonRootValue.uuid('e87f3c51-545c-4bc2-b1b5-284de67d627e') | '"e87f3c51-545c-4bc2-b1b5-284de67d627e"'
}

}

0 comments on commit 17554aa

Please sign in to comment.