0

Something of a newbie question:

I have to assert against an address on a page. I am querying the API firstly to get the address and I am then asserting against it on the webpage.

Here is my method to get data from API:

   public Set<String> getPlaceAddressLinesFromApi(String addressId) {

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://app.ard.domain.local:8084/addresses/" + addressId);

    try {
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity);
        String json = content.toString();
        Set<String> addresslines = new HashSet<String>();
        addresslines.add(JsonPath.read(json, "$.location.postalAddress.lines[0]").toString());
        addresslines.add(JsonPath.read(json, "$.location.postalAddress.town").toString());
        addresslines.add(JsonPath.read(json, "$.location.postalAddress.county").toString());
        addresslines.add(JsonPath.read(json, "$.location.postalAddress.postcode").toString());
        return addresslines;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

}

When I use the method in an assertion I get an error:

assertEquals(addressLinesFromApi, addressLinesFromWebsite, "The address displayed and address returned from api do not match");

Expected :TownHouse, Exeter, Devon, EX15 2LE
Actual   :[Exeter, Devon, EX15 2LE, TownHouse]

How can I order the String's returned from the JSON to use in the assertion?

How can I remove the '[]' brackets?

Steerpike
  • 1,712
  • 6
  • 38
  • 71

1 Answers1

1

You can split your addressLinesFromWebsite by ,\\s* to HashSet and compare:

assertEquals(addressLinesFromApi, new HashSet<String>(Arrays.asList(addressLinesFromWebsite.split(",\\s*"))), "The address displayed and address returned from api do not match");
chengpohi
  • 14,064
  • 1
  • 24
  • 42