简体   繁体   中英

RestAssured - using value of response from API A in the body of call to API B

so i have an API (API A) that i need to call that provides values. I need those values in the next test (API B)

public class testA {

String functionUrl = "https://dev.testA.v2";

@Test
public void RothHysa(ITestContext context) {

    Map<String, Object> jsonRequest = new HashMap<String, Object>();
    jsonRequest.put("product", "thisistheproduct");
        Map<String,String> jurisdictionMap = new HashMap<>();
            jurisdictionMap.put("category", "COUNTRY");
            jurisdictionMap.put("name", "US");
    jsonRequest.put("productCategories", productCategoriesMap);
    System.out.println(jsonRequest);

    Object RothHysa = RestAssured
        .given()
                .header("one-data-correlation-id", CommonUtility.getGuid())
                .body(jsonRequest)
            .when()
                .post(functionUrl)
            .then()
                .extract().response()
            .then().assertThat()
                .statusCode(200)
            .and()
                .assertThat().body("idofproduct", equalTo(Arrays.asList("xxxxxxx-xxxx-xxx-xxxxxx-xxxxxx")))
            .log().all()
                .extract()
                .jsonPath()
                .get("idofproduct");

    context.setAttribute("idofproduct", idToBeUsed);

    System.out.println(idToBeUsed);
}

the value i assert IS an array - the API returns it as ['value'] which is causing problems in the next test because it needs to be passed as a string. I've tried converting it to String various ways but they haven't worked. API B test:

public void Step1PCN(ITestContext context) {

String identifierIRArothHYSA1 = (String) context.getAttribute("identifier");

Map<String,Object> jsonRequest = new HashMap<>();

Map<String,String> value = new HashMap<>();
    value.put("key","value");
    value.put("key","value");
jsonRequest.put("key", value);

Map<String,String> value1 = new HashMap<>();
    value1.put("key","value");
    value1.put("key","value");
    value1.put("key","value");
jsonRequest.put("key", value1);

jsonRequest.put("key", "value");
jsonRequest.put("productID", idToBeUsed);

Object idForTestB = RestAssured
    .given()
        .header("one-data-correlation-id", CommonUtility.getGuid())
        .body(jsonRequest)
    .when()
        .post(functionUrl)
    .then()
        .contentType(ContentType.JSON)
        .extract().response()
    .then().assertThat()
        .statusCode(200)
    .and()
        .assertThat().body("status.StatusMessage", equalTo("Success"))
    .log().all()
        .extract()
        .jsonPath()
        .get("idForTestB");


context.setAttribute("idForTestB", idForTestB);

}

however the value of idToBeUsed is always [xxx-xxx-xxxxxx] but in this test it needs to be a string.

You can save var RothHysa as List<String> instead of Object .

List<String> RothHysa = RestAssured.given()...get("idofproduct");
context.setAttribute("idForTestB", RothHysa.get(0));
String modifiedId = idForTestB.toString();
    modifiedId = modifiedId.replaceAll("\\[", "");
    modifiedId = modifiedId.replaceAll("]", "");
    context.setAttribute("identifier[0]", modifiedId);
    System.out.println(modifiedId);

this is what i ended up doing. probably not the most eloquent solution but it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM