Skip to content

Commit

Permalink
Add test case for #313
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Zottnick committed Sep 19, 2016
1 parent 555c0d8 commit 7f49e74
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package au.com.dius.pact.consumer.exampleclients;

import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;

import java.io.IOException;

public class ArticlesRestClient {

public HttpResponse getArticles(String baseUrl)
throws IOException {

return Request.Get(baseUrl + "/articles.json")
.execute().returnResponse();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package au.com.dius.pact.consumer.examples;

import au.com.dius.pact.consumer.Pact;
import au.com.dius.pact.consumer.PactProviderRule;
import au.com.dius.pact.consumer.PactVerification;
import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.exampleclients.ArticlesRestClient;
import au.com.dius.pact.model.PactFragment;
import org.apache.commons.collections4.MapUtils;
import org.junit.Rule;
import org.junit.Test;

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

/**
* Example taken from https://groups.google.com/forum/#!topic/pact-support/-Kk_OxvcJQY
*/
public class ArticlesTest {
Map<String, String> headers = MapUtils.putAll(new HashMap<String, String>(),
new String[]{"Content-Type", "application/json"});

@Rule
public PactProviderRule provider = new PactProviderRule("ArticlesProvider", "localhost", 1234, this);

@Pact(provider = "ArticlesProvider", consumer = "ArticlesConsumer")
public PactFragment articlesFragment(PactDslWithProvider builder) {
return builder
.given("Pact for Issue 313")
.uponReceiving("retrieving article data")
.path("/articles.json")
.method("GET")
.willRespondWith()
.headers(headers)
.status(200)
.body(
new PactDslJsonBody()
.minArrayLike("articles", 1)
.object("variants")
.eachKeyLike("0032")
.stringType("description", "sample description")
.closeObject()
.closeObject()
.closeObject()
.closeArray()
)
.toFragment();
}

@PactVerification("ArticlesProvider")
@Test
public void testArticles() throws IOException {
ArticlesRestClient providerRestClient = new ArticlesRestClient();
providerRestClient.getArticles("http://localhost:1234");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package au.com.dius.pact.provider.junit;

import au.com.dius.pact.provider.junit.loader.PactFolder;
import au.com.dius.pact.provider.junit.target.HttpTarget;
import au.com.dius.pact.provider.junit.target.Target;
import au.com.dius.pact.provider.junit.target.TestTarget;
import org.junit.runner.RunWith;

@RunWith(PactRunner.class)
@Provider("ArticlesProvider")
@PactFolder("../pact-jvm-consumer-junit/build/2.11/pacts")
public class ArticlesContractTest {
@TestTarget
public final Target target = new HttpTarget(8000);
}
12 changes: 12 additions & 0 deletions pact-jvm-provider-junit/src/test/resources/articles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

{
"articles": [
{
"variants": {
"1234": {
"description": "sample description"
}
}
}
]
}

0 comments on commit 7f49e74

Please sign in to comment.