Skip to content

Commit

Permalink
Use getTextFromJsonBody
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Aug 9, 2023
1 parent 7c7ad9c commit c233b07
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,14 @@ public void testDlsWithTermsQuery() throws Exception {
HttpStatus.SC_OK,
(res = rh.executeGetRequest("/terms/_search?pretty", encodeBasicHeader("dept_manager", "password"))).getStatusCode()
);
Assert.assertTrue(res.getBody().contains("\"value\" : 1,\n \"relation"));
Assert.assertTrue(res.getBody().contains("\"failed\" : 0"));
Assert.assertEquals(res.getTextFromJsonBody("/hits/total/value"), "1");
Assert.assertEquals(res.getTextFromJsonBody("/_shards/failed"), "0");

Assert.assertEquals(
HttpStatus.SC_OK,
(res = rh.executeGetRequest("/terms/_doc/0", encodeBasicHeader("dept_manager", "password"))).getStatusCode()
);
Assert.assertTrue(res.getBody().contains("\"foo\": \"bar\""));
Assert.assertEquals(res.getTextFromJsonBody("/_source/foo"), "bar");

Assert.assertEquals(
HttpStatus.SC_NOT_FOUND,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
Expand Down Expand Up @@ -433,6 +434,22 @@ public boolean isJsonContentType() {
return ct.contains("application/json");
}

public String getTextFromJsonBody(String jsonPointer) {
return getJsonNodeAt(jsonPointer).asText();
}

private JsonNode getJsonNodeAt(String jsonPointer) {
try {
return toJsonNode().at(jsonPointer);
} catch (IOException e) {
throw new IllegalArgumentException("Cound not convert response body to JSON node ", e);
}
}

private JsonNode toJsonNode() throws JsonProcessingException, IOException {
return DefaultObjectMapper.objectMapper.readTree(getBody());
}

public SimpleHttpResponse getInner() {
return inner;
}
Expand Down

0 comments on commit c233b07

Please sign in to comment.