Skip to content

Commit

Permalink
added validation for upserd request
Browse files Browse the repository at this point in the history
  • Loading branch information
kunal642 committed Apr 24, 2017
1 parent 6d6a230 commit 8d7076f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ public UpdateRequest(String index, String type, String id) {
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = super.validate();
if (version != Versions.MATCH_ANY && upsertRequest != null) {
validationException = addValidationError("can't provide both upsert request and a version", validationException);
}
if(upsertRequest != null && upsertRequest.version() != Versions.MATCH_ANY) {
validationException = addValidationError("can't provide version in upsert request", validationException);
}
if (type == null) {
validationException = addValidationError("type is missing", validationException);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import static org.elasticsearch.script.MockScriptEngine.mockInlineScript;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.notNullValue;
Expand Down Expand Up @@ -485,4 +486,19 @@ public void testToAndFromXContent() throws IOException {
BytesReference finalBytes = toXContent(parsedUpdateRequest, xContentType, humanReadable);
assertToXContentEquivalent(originalBytes, finalBytes, xContentType);
}

public void testToValidateUpsertRequestAndVersion() {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.version(1L);
updateRequest.upsert(new IndexRequest("index","type", "1"));
assertThat(updateRequest.validate().validationErrors(), contains("index is missing",
"can't provide both upsert request and a version","type is missing","id is missing","script or doc is missing"));
}

public void testToValidateUpsertRequestWithVersion() {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.upsert(new IndexRequest("index", "type", "1").version(1L));
assertEquals(updateRequest.validate().validationErrors(), contains("index is missing",
"can't provide version in upsert request","type is missing","id is missing","script or doc is missing"));
}
}

0 comments on commit 8d7076f

Please sign in to comment.