Monday, November 9, 2015

Test RestFull service post method using Rest Assured Framework+Test Case Example



1)This is the my hatchstation_registration.json file

{
  "actionName": "SUBMIT",
  "cookie": {
    "data": [],
    "refererServiceId": "563081ed59f1a83e1aa3a432"
  },
  "formData": [
    {
      "cardId": "56307db259f1a83e1aa3a42e",
      "fieldData": [
        {
          "fieldName": "mobileNumber",
          "fieldValue": "9010163733"
        }
      ]
    },
    {
      "cardId": "56307e0559f1a83e1aa3a42f",
      "fieldData": [
        {
          "fieldName": "firstName",
          "fieldValue": "satya"
        }
      ]
    },
    {
      "cardId": "56307e5d59f1a83e1aa3a430",
      "fieldData": [
        {
          "fieldName": "emailAddress",
          "fieldValue": "satya@gmail.com"
        }
      ]
    },
    {
      "cardId": "56307ea559f1a83e1aa3a431",
      "fieldData": [
        {
          "fieldName": "summaryCard",
          "fieldValue": ""
        }
      ]
    }
  ],
  "deckId": "",
  "serviceId": "56307c85fd63ddcbfb47a1fa"

}


2).Using Rest Assured Framework we can easily test the rest full service


@Produces(MediaType.APPLICATION_JSON)
@Test
public void testSubmitActionRequest() {

String jsonString = "";
try{
String APIUrl = "http://localhost:9090/api/service/cardaction/submit";
InputStream inputStream = new FileInputStream("G:/Business JSON Strctures/hatchstation/hatchstation_registration.json");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(inputStreamReader);
String line;
while ((line = br.readLine()) != null) {
jsonString += line + "\n";
}

RequestSpecBuilder builder = new RequestSpecBuilder();
builder.setBody(jsonString);
builder.setContentType("application/json; charset=UTF-8");
builder.addHeader("x-username", "99999999999");
RequestSpecification requestSpec = builder.build();
Response response = given().authentication().preemptive().basic("","").spec(requestSpec).when().post(APIUrl);
JSONObject JSONResponseBody = new JSONObject(response.body().asString());
System.out.println("JSONResponseBody   "+JSONResponseBody.toString());

}
catch(Exception e){
e.printStackTrace();
}
}


3) Sample response from url is like the following json

{
  "cookie" : {
    "customerId" : "5640dc3e974f5393a83131bd",
    "refererServiceId" : "563a0b52e0705e93005c7a6b"
  },
  "serverDirectives" : {
    "clearCache" : true,
    "redirectCardId" : "563a0b63e0705e93005c7a6c"
  },
  "statusCode" : 0,
  "statusMessage" : "SUCCESS"
}

No comments:

Post a Comment