p

pact-jvm-provider_2.10

Pact provider ============= sub project of https://github.com/DiUS/pact-jvm The pact provider is responsible for verifying that an API provider adheres to a number of pacts authored by its clients This library provides the basic tools required to automate the process, and should be usable on its own in many instances. Framework and build tool specific bindings will be provided in separate libraries that build on top of this core functionality. ### Running Pacts Main takes 2 arguments: The first is the root folder of your pact files (all .json files in root and subfolders are assumed to be pacts) The second is the location of your pact config json file. ### Pact config The pact config is a simple mapping of provider names to endpoint url's paths will be appended to endpoint url's when interactions are attempted for an example see: https://github.com/DiUS/pact-jvm/blob/master/pact-jvm-provider/src/test/resources/pact-config.json ### Provider State Before each interaction is executed, the provider under test will have the opportunity to enter a state. Generally the state maps to a set of fixture data for mocking out services that the provider is a consumer of (they will have their own pacts) The pact framework will instruct the test server to enter that state by sending: POST "${config.stateChangeUrl.url}/setup" { "state" : "${interaction.stateName}" } ### An example of running provider verification with junit This example uses java, junit and hamcrest matchers to run the provider verification. As the provider service is a DropWizard application, it uses the DropwizardAppRule to startup the service before running any test. Warning: It only grabs the first interaction from the pact file with the consumer, where there could be many. (This could possibly be solved with a parameterized test) ```java public class PactJVMProviderJUnitTest { @ClassRule public static TestRule startServiceRule = new DropwizardAppRule<DropwizardAppConfig>(DropwizardApp.class, "config.yml"); private static ProviderInfo serviceProvider; private static Pact testConsumerPact; @BeforeClass public static void setupProvider() { serviceProvider = new ProviderInfo("Dropwizard App"); serviceProvider.setProtocol("http"); serviceProvider.setHost("localhost"); serviceProvider.setPort(8080); serviceProvider.setPath("/"); ConsumerInfo consumer = new ConsumerInfo(); consumer.setName("test_consumer"); consumer.setPactFile(new File("target/pacts/ping_client-ping_service.json")); // serviceProvider.getConsumers().add(consumer); testConsumerPact = (Pact) new PactReader().loadPact(consumer.getPactFile()); } @Test @SuppressWarnings("unchecked") public void runConsumerPacts() { //grab the first interaction from the pact with consumer List<Interaction> interactions = scala.collection.JavaConversions.seqAsJavaList(testConsumerPact.interactions()); Interaction interaction1 = interactions.get(0); //setup any provider state //setup the client and interaction to fire against the provider ProviderClient client = new ProviderClient(); client.setProvider(serviceProvider); client.setRequest(interaction1.request()); Map<String, Object> clientResponse = (Map<String, Object>) client.makeRequest(); Map<String, Object> result = (Map<String, Object>) ResponseComparison.compareResponse(interaction1.response(), clientResponse, (int) clientResponse.get("statusCode"), (Map) clientResponse.get("headers"), (String) clientResponse.get("data")); //assert all good assertThat(result.get("method"), is(true)); // method type matches Map headers = (Map) result.get("headers"); //headers match headers.forEach( (k, v) -> assertThat(format("Header: [%s] does not match", k), v, org.hamcrest.Matchers.equalTo(true)) ); assertThat((Collection<Object>)((Map)result.get("body")).values(), org.hamcrest.Matchers.hasSize(0)); // empty list of body mismatches } } ``` ### An example of running provider verification with spock This example uses groovy and spock to run the provider verification. Again the provider service is a DropWizard application, and is using the DropwizardAppRule to startup the service. This example runs all interactions using spocks Unroll feature ```groovy class PactJVMProviderSpockSpec extends Specification { @ClassRule @Shared TestRule startServiceRule = new DropwizardAppRule<DropwizardAppConfig>(DropwizardApp.class, "config.yml"); @Shared ProviderInfo serviceProvider @Shared Pact testConsumerPact def setupSpec() { serviceProvider = new ProviderInfo("Dropwizard App") serviceProvider.protocol = "http" serviceProvider.host = "localhost" serviceProvider.port = 8080; serviceProvider.path = "/" def consumer = serviceProvider.hasPactWith("ping_consumer", { pactFile = new File('target/pacts/ping_client-ping_service.json') }) testConsumerPact = (Pact) new PactReader().loadPact(consumer.getPactFile()); } def cleanup() { //cleanup provider state //ie. db.truncateAllTables() } def cleanupSpec() { //cleanup provider } @Unroll def "Provider Pact - With Consumer"() { given: //setup provider state // ie. db.setupRecords() // serviceProvider.requestFilter = { req -> // req.addHeader('Authorization', token) // } when: ProviderClient client = new ProviderClient(provider: serviceProvider, request: interaction.request()) Map clientResponse = (Map) client.makeRequest() Map result = (Map) ResponseComparison.compareResponse(interaction.response(), clientResponse, clientResponse.statusCode, clientResponse.headers, clientResponse.data) then: // method matches result.method == true // headers all match, spock needs the size checked before // asserting each result if (result.headers.size() > 0) { result.headers.each() { k, v -> assert v == true } } // empty list of body mismatches result.body.size() == 0 where: interaction << scala.collection.JavaConversions.seqAsJavaList(testConsumerPact.interactions()) } } ```
https://github.com/DiUS/pact-jvm
Apache 2
Travis Dixon Ronald Holshausen
cleanup requestfilter using assertthat matches dropwizard feature setpath quot host static setport possibly under number header interaction1 grabs dropwizardappconfig project each automate assumed parameterized before simple serviceprovider data collection instances running attempted executed setupspec token could https needs javaconversions fire arguments string library assert runs public checked hassize beforeclass libraries body resources client dropwizardapprule root tool good consumer localhost setname takes values basic shared haspactwith size that hamcrest names against pactjvmproviderjunittest example getpactfile does ping_service result grab testrule bindings matchers testconsumerpact makerequest responsecomparison mocking response startservicerule json setupprovider generally interactions functionality separate protocol provides 8080 provider setuprecords junit main statechangeurl there service clientresponse provided server core java github pacts statuscode unroll they test seqasjavalist given file ping_client empty config only services groovy pactjvmproviderspockspec again interaction pactfile asserting spock startup cleanupspec endpoint many verification http then will providerinfo port ping_consumer mapping truncatealltables build private runconsumerpacts dropwizardapp files post responsible loadpact void true sethost should framework master process folder sending getconsumers consumerinfo statename setprovider compareresponse method warning headers appended second location authorization where type setrequest tools providerclient authored instruct apos when request required test_consumer unchecked extends solved pactreader classrule class enter format setpactfile pact usable match this addheader setprotocol foreach adheres fixture verifying specific have state object from scala paths setup suppresswarnings path application subfolders with your list spocks blob opportunity maps target specification equalto uses their mismatches first dius clients
Aggregated version Version Update time
2.4 2.4.20 Apr 14, 2018
2.4.19 Aug 17, 2017
2.4.18 Dec 30, 2016
2.4.17 Oct 22, 2016
2.4.16 Oct 02, 2016
2.4.15 Aug 23, 2016
2.4.14 Aug 07, 2016
2.4.13 Jul 31, 2016
2.4.12 Jul 15, 2016
2.4.11 Jul 06, 2016
2.4.10 Jun 18, 2016
2.4.9 May 04, 2016
2.4.8 Mar 29, 2016
2.4.7 Mar 25, 2016
2.4.6 Feb 20, 2016
2.4.5 Feb 11, 2016
2.4.4 Jan 21, 2016
2.4.3 Jan 17, 2016
2.4.2 Dec 13, 2015
2.4.1 Nov 28, 2015
2.4.0 Nov 22, 2015
2.3 2.3.3 Nov 14, 2015
2.3.2 Oct 24, 2015
2.3.1 Oct 20, 2015
2.3.0 Oct 06, 2015
2.2 2.2.15 Sep 17, 2015
2.2.14 Sep 07, 2015
2.2.13 Aug 29, 2015
2.2.12 Aug 09, 2015
2.2.11 Aug 02, 2015
2.2.10 Jul 25, 2015
2.2.9 Jul 13, 2015
2.2.8 Jul 08, 2015
2.2.7 Jul 07, 2015
2.2.6 Jul 02, 2015
2.2.5 Jun 23, 2015
2.2.4 Jun 17, 2015
2.2.3 Jun 14, 2015
2.2.2 Jun 10, 2015
2.2.1 May 21, 2015
81 Records