0

I am looking for functionality to send multiple requests each with different request body payloads, and I am unable to find a way to do that whether sequentially or concurrently. It only works below if I do not loop through the list of payloads and try to send the request for each payload, but works when I only do one request only with a single payload (ignore the redundant methods for the request as it hasn't been refactored fully yet). In my validateTheStatusCodeForTheMasterAPIPostRequest() method, the postRequestWithPayload() method never does anything per the logs, and I am not able to see any print out of the response. Is there a reason I am not able to see different requests be sent and response printed out, based on each payload in the payload list?:

 public static final HttpClient client() {
        httpClient = HttpClient.newBuilder()
                .sslContext(handleSslFromLowerEnv())
                .executor(executorService)
                .version(HttpClient.Version.HTTP_2)
                .connectTimeout(Duration.ofSeconds(120))
                .build();
        return httpClient;
    };

public static boolean validateStatusCode(Integer actualStatusCode, Integer expectedStatusCode) {
        String pass_fail = actualStatusCode.equals(expectedStatusCode) ? "PASS" : "FAIL";
        String desc = "Status Code validation :: Expected = " + expectedStatusCode + " Actual = " + actualStatusCode;
        return actualStatusCode.equals(expectedStatusCode);
    }

public void validateTheStatusCodeForTheMasterAPIPostRequest() {
    try {
        httpResponse = postRequestWithPayload();
        if (httpResponse != null) {
            boolean isSuccessStatusCode = validateStatusCode(httpResponse.statusCode(), expectedStatusCode);
            Assert.assertTrue(isSuccessStatusCode);
            Reporter.log("SUCCESS:" + "(Success - Expected:" + expectedStatusCode + " and Actual:" + httpResponse.statusCode(), true);
        }
    } catch (AssertionError e) {
        Reporter.log("ERROR:" + "(Failed - Expected:" + expectedStatusCode + " and Actual:" + httpResponse.statusCode(), true);
    }
}

private HttpResponse<String> postRequestWithPayload() {
    HttpResponse<String> resp = null;
    try {
        String productDivPayload = PayloadUtil.getPayload(masterPostJsonFile, "Product_Div");
        String productSubDivPayload = PayloadUtil.getPayload(masterPostJsonFile, "Product_SubDiv");
        String productPgPayload = PayloadUtil.getPayload(masterPostJsonFile, "Product_PG");
        String productAsrtPayload = PayloadUtil.getPayload(masterPostJsonFile, "Product_Asrt");
        String productItemPayload = PayloadUtil.getPayload(masterPostJsonFile, "Product_Item");
        var reqString = List.of(productDivPayload, productSubDivPayload, productPgPayload, productAsrtPayload, productItemPayload);
        //System.out.println("Product_DIv is " + productDivPayload + " Product Subdiv is " + productSubDivPayload);
        //Below works for a single request without looping through payload list
        //resp = postRequest(productDivPayload);
        System.out.println("Response code is " + resp.body().toString());
        for (String reqType : reqString) {
            resp = postRequest(reqType);
        }
    } catch (Exception e) {
        Reporter.log("ERROR: Issue executing post request: " + e.getMessage());
    }
    return resp;
}

private HttpResponse<String> postRequest(String payload) {
    HttpResponse<String> response = null;
    try {
        HttpRequest request2 = HttpRequest.newBuilder()
                .uri(new URI(RestAssured.baseURI + "/vcp/vendor-reference-api/value"))
                .setHeader("Content-Type", "application/json")
                .setHeader("Authorization", "Bearer " + bearerToken)
                .setHeader("Cookie", "amlbcookie=" + devCookie1 + "; " + "dvgwauthcookie=" + devCookie2)
                .setHeader("Cookie", "amlbcookie=" + devCookie1 + "; " + "dvgwauthcookie=" + devCookie2)
                .POST(HttpRequest.BodyPublishers.ofString(payload))
                .build();
        response = client().send(request2, HttpResponse.BodyHandlers.ofString());
        System.out.println("Status code is " + response.statusCode());
        System.out.println("Response body is " + response.body());
        // print response headers
        //   HttpHeaders headers = response.headers();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}
wheelerlc64
  • 435
  • 2
  • 5
  • 17
  • You seem to be using one client per request - and to release the reference to that client before you have received the response. Is that intentional? Your `postRequestWithPayload` in its current state will not work because you're calling `body()` on a `resp` which is `null`. So `postRequest` is never called. Also it is unclear why this method only returns the last response, and seem to lose interest in any of the previous one? – daniel Sep 14 '22 at 15:10

0 Answers0