Skip to content

Commit

Permalink
Fix validation workflow (#33384)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbismuth committed Oct 31, 2018
1 parent 04fce0b commit c3c9be0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static class RestGetSourceResponseListener extends RestResponseListener<GetRespo

@Override
public RestResponse buildResponse(final GetResponse response) throws Exception {
checkIfSourceEmpty(response);
checkResource(response);

final XContentBuilder builder = channel.newBuilder(request.getXContentType(), false);
final BytesReference source = response.getSourceInternal();
Expand All @@ -102,19 +102,20 @@ public RestResponse buildResponse(final GetResponse response) throws Exception {
}

/**
* Checks if the requested source or document itself is missing.
* Checks if the requested document or source is missing.
*
* @param response a response
* @throws ResourceNotFoundException if source or doc itself is missing
* @throws ResourceNotFoundException if the document or source is missing
*/
private void checkIfSourceEmpty(final GetResponse response) {
if (response.isSourceEmpty()) {
final String resourceType = response.isExists() == false ? "Document" : "Source";
final String index = response.getIndex();
final String type = response.getType();
final String id = response.getId();

throw new ResourceNotFoundException(resourceType + " not found [" + index + "]/[" + type + "]/[" + id + "]");
private void checkResource(final GetResponse response) {
final String index = response.getIndex();
final String type = response.getType();
final String id = response.getId();

if (response.isExists() == false) {
throw new ResourceNotFoundException("Document not found [" + index + "]/[" + type + "]/[" + id + "]");
} else if (response.isSourceEmpty()) {
throw new ResourceNotFoundException("Source not found [" + index + "]/[" + type + "]/[" + id + "]");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,27 @@ public static void cleanupReferences() {

public void testRestGetSourceAction() throws Exception {
final BytesReference source = new BytesArray("{\"foo\": \"bar\"}");
final GetResponse getResponse = new GetResponse(new GetResult("index1", "_doc", "1", -1, true, source, emptyMap()));
final GetResponse response = new GetResponse(new GetResult("index1", "_doc", "1", -1, true, source, emptyMap()));

final RestResponse restResponse = listener.buildResponse(getResponse);
final RestResponse restResponse = listener.buildResponse(response);

assertThat(restResponse.status(), equalTo(OK));
assertThat(restResponse.contentType(), equalTo("application/json; charset=UTF-8"));
assertThat(restResponse.content(), equalTo(new BytesArray("{\"foo\": \"bar\"}")));
}

public void testRestGetSourceActionWithMissingDocument() {
final GetResponse getResponse = new GetResponse(new GetResult("index1", "_doc", "1", -1, false, null, emptyMap()));
final GetResponse response = new GetResponse(new GetResult("index1", "_doc", "1", -1, false, null, emptyMap()));

final ResourceNotFoundException exception = expectThrows(ResourceNotFoundException.class,
() -> listener.buildResponse(getResponse));
final ResourceNotFoundException exception = expectThrows(ResourceNotFoundException.class, () -> listener.buildResponse(response));

assertThat(exception.getMessage(), equalTo("Document not found [index1]/[_doc]/[1]"));
}

public void testRestGetSourceActionWithMissingDocumentSource() {
final GetResponse getResponse = new GetResponse(new GetResult("index1", "_doc", "1", -1, true, null, emptyMap()));
final GetResponse response = new GetResponse(new GetResult("index1", "_doc", "1", -1, true, null, emptyMap()));

final ResourceNotFoundException exception = expectThrows(ResourceNotFoundException.class,
() -> listener.buildResponse(getResponse));
final ResourceNotFoundException exception = expectThrows(ResourceNotFoundException.class, () -> listener.buildResponse(response));

assertThat(exception.getMessage(), equalTo("Source not found [index1]/[_doc]/[1]"));
}
Expand Down

0 comments on commit c3c9be0

Please sign in to comment.