package au.com.dius.pact.consumer.pactproviderrule;

import au.com.dius.pact.consumer.DefaultRequestValues;
import au.com.dius.pact.consumer.DefaultResponseValues;
import au.com.dius.pact.consumer.Pact;
import au.com.dius.pact.consumer.PactProviderRuleMk2;
import au.com.dius.pact.consumer.PactVerification;
import au.com.dius.pact.consumer.dsl.PactDslRequestWithoutPath;
import au.com.dius.pact.consumer.dsl.PactDslResponse;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.exampleclients.ConsumerClient;
import au.com.dius.pact.model.RequestResponsePact;
import org.junit.Assert;
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 PactProviderWithMultipleFragmentsTest {

    @Rule
    public PactProviderRuleMk2 mockTestProvider = new PactProviderRuleMk2("test_provider", this);

    @DefaultRequestValues
    public void defaultRequestValues(PactDslRequestWithoutPath request) {
      Map<String, String> headers = new HashMap<String, String>();
      headers.put("testreqheader", "testreqheadervalue");
      request.headers(headers);
    }

    @DefaultResponseValues
    public void defaultResponseValues(PactDslResponse response) {
      Map<String, String> headers = new HashMap<String, String>();
      headers.put("testresheader", "testresheadervalue");
      response.headers(headers);
    }

    @Pact(consumer="test_consumer")
    public RequestResponsePact createFragment(PactDslWithProvider builder) {
        return builder
            .given("good state")
            .uponReceiving("PactProviderTest test interaction")
                .path("/")
                .method("GET")
            .willRespondWith()
                .status(200)
                .body("{\"responsetest\": true, \"name\": \"harry\"}")
            .uponReceiving("PactProviderTest second test interaction")
                .method("OPTIONS")
                .path("/second")
                .body("")
            .willRespondWith()
                .status(200)
                .body("")
            .toPact();
    }

    @Pact(consumer="test_consumer")
    public RequestResponsePact createFragment2(PactDslWithProvider builder) {
        return builder
                .given("good state")
                .uponReceiving("PactProviderTest test interaction 2")
                    .path("/")
                    .method("GET")
                .willRespondWith()
                    .status(200)
                    .body("{\"responsetest\": true, \"name\": \"fred\"}")
                .toPact();
    }

    @Test
    @PactVerification(value = "test_provider", fragment = "createFragment2")
    public void runTestWithFragment2() throws IOException {
        Map expectedResponse = new HashMap();
        expectedResponse.put("responsetest", true);
        expectedResponse.put("name", "fred");
        assertEquals(new ConsumerClient(mockTestProvider.getUrl()).getAsMap("/", ""), expectedResponse);
    }

    @Test
    @PactVerification(value = "test_provider", fragment = "createFragment")
    public void runTestWithFragment1() throws IOException {
        Assert.assertEquals(new ConsumerClient(mockTestProvider.getUrl()).options("/second"), 200);
        Map expectedResponse = new HashMap();
        expectedResponse.put("responsetest", true);
        expectedResponse.put("name", "harry");
        assertEquals(new ConsumerClient(mockTestProvider.getUrl()).getAsMap("/", ""), expectedResponse);
    }
}

@@ -297,24 +297,76 @@
 rification;%0A
+import au.com.dius.pact.consumer.PactVerifications;%0A
 import au.co
@@ -417,16 +417,16 @@
 utPath;%0A
-
 import a
@@ -1002,16 +1002,127 @@
 this);%0A%0A
+    @Rule%0A    public PactProviderRuleMk2 mockTestProvider2 = new PactProviderRuleMk2(%22test_provider2%22, this);%0A%0A
     @Def
@@ -1661,32 +1661,60 @@
 =%22test_consumer%22
+, provider = %22test_provider%22
 )%0A    public Req
@@ -2400,16 +2400,45 @@
 onsumer%22
+, provider = %22test_provider2%22
 )%0A    pu
@@ -2927,32 +2927,33 @@
 = %22test_provider
+2
 %22, fragment = %22c
@@ -3221,32 +3221,33 @@
 mockTestProvider
+2
 .getUrl()).getAs
@@ -3673,24 +3673,24 @@
 , %22harry%22);%0A
-
         asse
@@ -3787,11 +3787,880 @@
 );%0A    %7D
+%0A%0A    @Test%0A    @PactVerifications(%7B%0A      @PactVerification(value = %22test_provider%22, fragment = %22createFragment%22),%0A      @PactVerification(value = %22test_provider2%22, fragment = %22createFragment2%22)%0A    %7D)%0A    public void runTestWithBothFragments() throws IOException %7B%0A        Assert.assertEquals(new ConsumerClient(mockTestProvider.getUrl()).options(%22/second%22), 200);%0A        Map expectedResponse = new HashMap();%0A        expectedResponse.put(%22responsetest%22, true);%0A        expectedResponse.put(%22name%22, %22harry%22);%0A        assertEquals(new ConsumerClient(mockTestProvider.getUrl()).getAsMap(%22/%22, %22%22), expectedResponse);%0A%0A        expectedResponse = new HashMap();%0A        expectedResponse.put(%22responsetest%22, true);%0A        expectedResponse.put(%22name%22, %22fred%22);%0A        assertEquals(new ConsumerClient(mockTestProvider2.getUrl()).getAsMap(%22/%22, %22%22), expectedResponse);%0A    %7D
 %0A%7D%0A

add a test with mutiple providers #820