Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #113 from kuzzleio/rename-updateifexist-option
Browse files Browse the repository at this point in the history
Collection.createDocument: rename the updateIfExist option
  • Loading branch information
benoitvidis committed Feb 28, 2017
2 parents 92d5dc1 + faf19c4 commit 9a0dcd0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/main/java/io/kuzzle/sdk/core/Collection.java
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,13 @@ public Collection createDocument(final Document document, final ResponseListener
* @return the kuzzle data collection
*/
public Collection createDocument(final Document document, final Options options, final ResponseListener<Document> listener) {
String action = (options != null && options.isUpdateIfExists()) ? "createOrReplace" : "create";
String action = "create";
JSONObject data = document.serialize();

if (options != null && options.getIfExist().equals("replace")) {
action = "createOrReplace";
}

this.kuzzle.addHeaders(data, this.getHeaders());

try {
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/io/kuzzle/sdk/core/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Options {
private int queueMaxSize = 500;
private int queueTTL = 120000;
private long reconnectionDelay = 1000;
private boolean updateIfExists = false;
private String ifExist = "error";
private Mode connect = Mode.AUTO;
private Mode offlineMode = Mode.MANUAL;
private int replayInterval = 10;
Expand Down Expand Up @@ -77,20 +77,25 @@ public Options setHeaders(JSONObject headers) {
/**
* Is update if exists boolean.
*
* @return the boolean
* @return value of the ifExists parameter
*/
public boolean isUpdateIfExists() {
return updateIfExists;
public String getIfExist() {
return ifExist;
}

/**
* Sets update if exists.
*
* @param updateIfExists the update if exists
* @param value the update if exists
* @return the update if exists
* @throws
*/
public Options setUpdateIfExists(boolean updateIfExists) {
this.updateIfExists = updateIfExists;
public Options setIfExist(String value) {
if (value != "error" && value != "replace") {
throw new IllegalArgumentException("Invalid value for option 'ifExists': " + value);
}

this.ifExist = value;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void checkSignaturesVariants() throws JSONException {
Document doc = mock(Document.class);
JSONObject content = new JSONObject();
String id = "foo";
Options opts = mock(Options.class);
Options opts = new Options();

collection = spy(collection);

Expand Down Expand Up @@ -84,6 +84,13 @@ public void testCreateDocumentQueryException() throws JSONException {
collection.createDocument(mock(Document.class), listener);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateDocumentIllegalIfExistValue() {
Options opts = new Options();

opts.setIfExist("foobar");
}

@Test(expected = RuntimeException.class)
public void testCreateDocumentException() throws JSONException {
doAnswer(new Answer() {
Expand Down Expand Up @@ -128,7 +135,7 @@ public void testCreateDocumentWithOptions() throws JSONException {
Document doc = new Document(collection);
doc.setContent("foo", "bar");
Options options = new Options();
options.setUpdateIfExists(true);
options.setIfExist("replace");
collection.createDocument(doc, options);
ArgumentCaptor argument = ArgumentCaptor.forClass(io.kuzzle.sdk.core.Kuzzle.QueryArgs.class);
verify(kuzzle, times(1)).query((io.kuzzle.sdk.core.Kuzzle.QueryArgs) argument.capture(), any(JSONObject.class), any(Options.class), any(OnQueryDoneListener.class));
Expand Down

0 comments on commit 9a0dcd0

Please sign in to comment.