I am trying below code for generating pact file but only one detest() is getting passed that is for negative one and for another one which is positive that is failing. but when i try to run the positive one by commenting negative it is getting passed what could be the reason ?
public class XYZ_PostTest extends CommonModules {
static CommonModules common = new CommonModules();
public static final String providerServiceName = "XYZ_provider";
public static final String consumerServiceName = "XYZ_Consumer";
public static int providerServicePort = 8110;
public static String providerUrl = "
http://localhost:" + providerServicePort + "/xyzbasepath";
public String postbody1positive = new String(Files.readAllBytes(Paths.get("src/Resources/XYZ.txt")), StandardCharsets.UTF_8);
public String postbody2negative = new String(Files.readAllBytes(Paths.get("src/Resources/LMN.txt")), StandardCharsets.UTF_8);
public XYZ_PostTest() throws IOException { }
@Rule
public PactProviderRuleMk2 provider = new PactProviderRuleMk2(providerServiceName, "localhost", providerServicePort, this);
@Pact(consumer = consumerServiceName)
public RequestResponsePact createPact(PactDslWithProvider builder) {
Map<String, String> reqheaders = new HashMap();
reqheaders.put("Content-Type", "application/json");
// adding required headers under this
Map<String, String> resheaders = new HashMap();
resheaders.put("Content-Type", "application/json");
//adding required response headers under this
DslPart reqBodywithoutmandatoryfiels = new PactDslJsonBody()
.stringType("Uuid", common.randomUUID())
// adding reqBody under this without mandatory fields for negative validation with error code 400
DslPart resBody = new PactDslJsonBody()
// adding responsebody for the 400 error code
.asBody();
return builder
.given("publishe sevent details without mandatory fields")
.uponReceiving("sends acknowledgement for bad request")
.method("POST")
.headers(reqheaders)
.body(reqBodywithoutmandatoryfiels)
.path("/xyzbasepath")
.willRespondWith()
.status(400)
.headers(resheaders)
.body(resBody)
.toPact();
}
@Pact(consumer = consumerServiceName)
public RequestResponsePact createPactpositivescenario(PactDslWithProvider builder) {
System.out.println("JWT Token: "+JWTtoken);
Map<String, String> reqheaders1 = new HashMap();
reqheaders1.put("Content-Type", "application/json");
// adding required headers under this
Map<String, String> resheaders1 = new HashMap();
resheaders1.put("Content-Type", "application/json");
//adding required response headers under this
DslPart reqBodyWithmandatoryFields1 = new PactDslJsonBody()
.stringType("locationTypeCode","COUNTRY")
//adding reqBody under this with mandatory fields for positive validation with success code 200
.asBody();
DslPart resBody1 = new PactDslJsonBody()
// adding responsebody for the 202 success code
return builder
.given("publishesevent details with mandatory fields")
.uponReceiving("sends acknowledgement for success")
.method("POST")
.headers(reqheaders1)
.body(reqBodyWithmandatoryFields1)
.path("/xyzbasepath")
.willRespondWith()
.status(202)
.headers(resheaders1)
.body(resBody1)
.toPact();
}
@Test
@Category(Create_Location_C_ORS_Consumer.class)
@PactVerification()
public void doTest1() throws IOException {
HttpPost httpPost = null;
HttpClient httpClient = new DefaultHttpClient();
String url=String.format(providerUrl);
HttpEntity httpEntity = new StringEntity((postbody2negative), "utf-8");
httpPost = new HttpPost(url);
httpPost.setHeader("Content-type", "application/json");
// added required headers
httpPost.setEntity(httpEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
}
@Test
@Category(Create_Location_C_ORS_Consumer.class)
@PactVerification()
public void doTest2() throws IOException {
HttpPost httpPost = null;
HttpClient httpClient = new DefaultHttpClient();
String url=String.format(providerUrl);
HttpEntity httpEntity = new StringEntity((postbody1positive), "utf-8");
httpPost = new HttpPost(url);
httpPost.setHeader("Content-type", "application/json");
//added required headers
httpPost.setEntity(httpEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
}
}