pact-foundation / pact-jvm

JVM version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://docs.pact.io
Apache License 2.0
1.07k stars 475 forks source link

Pact not generated with headers and body : both with and without headers not working #1136

Open ghost opened 4 years ago

ghost commented 4 years ago

I am not able to generate with headers and body given below

package nl.ing.closeaccount.integration.service;

import au.com.dius.pact.consumer.dsl.PactDslJsonBody; import au.com.dius.pact.consumer.dsl.PactDslWithProvider; import au.com.dius.pact.consumer.junit.PactProviderRule; import au.com.dius.pact.consumer.junit.PactVerification; import au.com.dius.pact.core.model.RequestResponsePact; import au.com.dius.pact.core.model.annotations.Pact; import com.google.common.collect.ImmutableList; import com.ing.api.trust.exception.SignException; import com.ing.api.trust.jwt.p2p.DefaultPeerToPeerTrustTokenBuilder; import com.ing.api.trust.jwt.p2p.PeerToPeerTrustToken; import com.ing.api.trust.jwt.p2p.PeerToPeerTrustTokenBuilder; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import junit.framework.Assert; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.springframework.core.io.ClassPathResource; import org.springframework.http.; import org.springframework.web.client.RestTemplate; import org.hamcrest.;

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

import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat;

import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when;

//import com.ing.bakercatalogue.productmanagement.packagemanagement.PackageAgreementManagementAPIConnector; //import com.ing.bakercatalogue.util.HttpUtils; public class ClosePackagePact { PactDslJsonBody requestBody;

@Rule
public PactProviderRule mockProvider = new PactProviderRule("PackageManagementAPI", "localhost", 8080, this);

@Pact(provider = "PackageManagementAPI", consumer = "pCloseAccountAPI")
public RequestResponsePact createPact(PactDslWithProvider builder) {
    Map<String, String> headers = new HashMap<>();
    headers.put("Content-Type", "application/json");
    headers.put("X-ING-REQ-CTX", "P2P-token");
    //headers.put("Accept", "application/json");
    headers.put("X-XSRF-TOKEN", "abcd");
    headers.put("Cookie", "XSRF-TOKEN=abcd");

     requestBody = new PactDslJsonBody()
            .stringValue("accountNumber", "NL")
            .stringValue("Iban","Iban")
            .stringValue("ChannelCode","Channelcode")
            .stringValue("requestorRBG","RBG");

    /*PactDslJsonBody responseBody = new PactDslJsonBody()
            .stringValue("status", "Success")
            .uuid("internalRequestId");*/

    // return builder

    return builder.
            uponReceiving("Creating a new request for the PackageManagementAPI")
            .path("/api/packages/for-closing")
            .method("POST")
            .body("{\"name\": \"Michael\"}")
            .headers(headers)
            .willRespondWith()
            .status(201)
            .toPact();
}

@Test
@PactVerification(fragment = "createPact")
public void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody() {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
   //String jsonBody = "{\"name\": \"Michael\"}";

    String jsonBody = requestBody.toString();
    System.out.println(jsonBody);

    // when
    ResponseEntity<String> postResponse = new RestTemplate()
            .exchange(
                    mockProvider.getUrl() + "/api/packages/for-closing",
                    HttpMethod.POST,
                    new HttpEntity<>(jsonBody, httpHeaders),
                    String.class
       );

    assertThat(" Status code validation",postResponse.getStatusCode().value(),equalTo(201));

}}

anto-ac commented 4 years ago

Could you put together a reproduceable example?