is there any way to download pact contract (.json) file from pact broker using java?

432 Views Asked by At

by using Java, i am trying to download pact contract on provider side. Please suggest any way or tool to Download contract from pact broker on provider side.

1

There are 1 best solutions below

0
KB1 On BEST ANSWER

by using below dependency in java project (i have used gradle)

implementation group: 'au.com.dius', name: 'pact-jvm-provider-junit_2.11', version: '2.4.3'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'

and use below code for contract

PactLoader loader = new au.com.dius.pact.provider.junit.loader.PactBrokerLoader("localhost", 8101,"http");
    List<Pact> icd10 = loader.load("YourProviderName");
    String json = new Gson().toJson(icd10);
    JSONObject jsonObject;
    JSONParser parser = new JSONParser();
    JSONArray jsonArray =  (JSONArray) parser.parse(json);
    jsonObject = (JSONObject) jsonArray.get(0);

    try {
        FileWriter file = new FileWriter("\\ContractDownloadDemo\\Contract.json");
        file.write(jsonObject.toJSONString());
        file.close();
    } catch (IOException e) {
        e.printStackTrace();
    }