diff --git a/src/main/java/io/kuzzle/sdk/core/Collection.java b/src/main/java/io/kuzzle/sdk/core/Collection.java index 3eb0dcb..8adfdd3 100644 --- a/src/main/java/io/kuzzle/sdk/core/Collection.java +++ b/src/main/java/io/kuzzle/sdk/core/Collection.java @@ -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 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 { diff --git a/src/main/java/io/kuzzle/sdk/core/Options.java b/src/main/java/io/kuzzle/sdk/core/Options.java index 4e6f1f1..bcea07e 100644 --- a/src/main/java/io/kuzzle/sdk/core/Options.java +++ b/src/main/java/io/kuzzle/sdk/core/Options.java @@ -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; @@ -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; } diff --git a/src/test/java/io/kuzzle/test/core/KuzzleDataCollection/createDocumentTest.java b/src/test/java/io/kuzzle/test/core/KuzzleDataCollection/createDocumentTest.java index de8b2de..95714e6 100644 --- a/src/test/java/io/kuzzle/test/core/KuzzleDataCollection/createDocumentTest.java +++ b/src/test/java/io/kuzzle/test/core/KuzzleDataCollection/createDocumentTest.java @@ -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); @@ -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() { @@ -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));