From b0fb0e23a76edc4c88fe8fa2c2798ba4bae37680 Mon Sep 17 00:00:00 2001 From: Yoann Abbes Date: Fri, 21 Feb 2020 14:13:01 +0100 Subject: [PATCH 01/10] document:createOrReplace --- .../document/create-or-replace/index.md | 62 +++++++++++++++ .../snippets/create-or-replace.java | 7 ++ .../snippets/create-or-replace.test.yml | 10 +++ .../API/Controllers/DocumentController.java | 75 +++++++++++++++++++ src/main/java/io/kuzzle/sdk/Kuzzle.java | 5 ++ .../DocumentTest/DocumentTest.java | 63 ++++++++++++++++ 6 files changed, 222 insertions(+) create mode 100644 doc/3/controllers/document/create-or-replace/index.md create mode 100644 doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java create mode 100644 doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml create mode 100644 src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java create mode 100644 src/test/java/io/kuzzle/test/API/Controllers/DocumentTest/DocumentTest.java diff --git a/doc/3/controllers/document/create-or-replace/index.md b/doc/3/controllers/document/create-or-replace/index.md new file mode 100644 index 00000000..75e7cb62 --- /dev/null +++ b/doc/3/controllers/document/create-or-replace/index.md @@ -0,0 +1,62 @@ +--- +code: true +type: page +title: createOrReplace +description: Creates or Replace a new document +--- + +# createOrReplace + +Creates or replace a document in the provided index and collection. + +--- + +## Arguments + +```java +public CompletableFuture> createOrReplace( + final String index, + final String collection, + final String id, + final ConcurrentHashMap document) +throws NotConnectedException, InternalException + +public CompletableFuture> createOrReplace( + final String index, + final String collection, + final String id, + final ConcurrentHashMap document, + final ConcurrentHashMap options) +throws NotConnectedException, InternalException +``` + +| Arguments | Type | Description | +| ------------------ | -------------------------------------------- | --------------------------------- | +| `index` |
String
| Index | +| `collection` |
String
| Collection | +| `id` |
String
| Document ID | +| `document` |
ConcurrentHashMap
| Document content | +| `options` |
ConcurrentHashMap
| Optional parameters | + +--- + +### options + +| Arguments | Type | Description | +| ------------------ | -------------------------------------------- | --------------------------------- | +| `queuable` |
Boolean
| Document identifier. Auto-generated if not specified | +| `waitForRefresh` |
Boolean
| If set to `true`, Kuzzle will wait for the persistence layer to finish indexing| + +## Return + +A `ConcurrentHashMap` which has the following properties: + +| Property | Type | Description | +|------------- |----------------------------- |--------------------------------- | +| `_source` |
ConcurrentHashMap
| Document content | +| `_id` |
String
| ID of the document | +| `_version` |
Integer
| Version of the document in the persistent data storage | + +## Usage + +<<< ./snippets/createOrReplace.java diff --git a/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java new file mode 100644 index 00000000..dd8a2dc7 --- /dev/null +++ b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java @@ -0,0 +1,7 @@ + + ConcurrentHashMap document = new ConcurrentHashMap<>(); + document.put("firstname", "John"); + document.put("lastname", "Smith"); + + kuzzle.getDocumentController().createOrReplace("nyc-open-data", "yellow-taxi", "some-id", document) + .get(); diff --git a/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml new file mode 100644 index 00000000..d457ab2b --- /dev/null +++ b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml @@ -0,0 +1,10 @@ +name: document#createOrReplace +description: Creates or Replace a document +hooks: + before: | + curl -XDELETE kuzzle:7512/nyc-open-data + curl -XPOST kuzzle:7512/nyc-open-data/_create + curl -XPUT kuzzle:7512/nyc-open-data/yellow-taxi + after: +template: default +expected: Success \ No newline at end of file diff --git a/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java b/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java new file mode 100644 index 00000000..5ccbaa28 --- /dev/null +++ b/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java @@ -0,0 +1,75 @@ +package io.kuzzle.sdk.API.Controllers; + +import io.kuzzle.sdk.CoreClasses.Maps.KuzzleMap; +import io.kuzzle.sdk.Exceptions.InternalException; +import io.kuzzle.sdk.Exceptions.NotConnectedException; +import io.kuzzle.sdk.Kuzzle; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; + +public class DocumentController extends BaseController { + public DocumentController(final Kuzzle kuzzle) { + super(kuzzle); + } + + /** + * Creates or Replace a document in a given collection and index. + * + * @param index + * @param collection + * @param id + * @param document + * @param options + * @return a CompletableFuture + * @throws NotConnectedException + * @throws InternalException + */ + public CompletableFuture> createOrReplace( + final String index, + final String collection, + final String id, + final ConcurrentHashMap document, + final ConcurrentHashMap options) throws NotConnectedException, InternalException { + + final KuzzleMap query = new KuzzleMap(); + final KuzzleMap _options = KuzzleMap + .from(options); + query + .put("index", index) + .put("collection", collection) + .put("controller", "document") + .put("action", "createOrReplace") + .put("body", document) + .put("_id", id) + .put("queuable", _options.getBoolean("queuable")) + .put("waitForRefresh", _options.getBoolean("waitForRefresh")); + + return kuzzle + .query(query) + .thenApplyAsync( + (response) -> (ConcurrentHashMap) response.result); + } + + /** + * Creates or Replace a document in a given collection and index. + * + * @param index + * @param collection + * @param id + * @param document + * @return a CompletableFuture + * @throws NotConnectedException + * @throws InternalException + */ + public CompletableFuture> createOrReplace( + final String index, + final String collection, + final String id, + final ConcurrentHashMap document) throws NotConnectedException, InternalException { + + final ConcurrentHashMap options = new ConcurrentHashMap<>(); + return this.createOrReplace(index, collection, id, document, options); + } + +} diff --git a/src/main/java/io/kuzzle/sdk/Kuzzle.java b/src/main/java/io/kuzzle/sdk/Kuzzle.java index 3acb9446..3f20cbca 100644 --- a/src/main/java/io/kuzzle/sdk/Kuzzle.java +++ b/src/main/java/io/kuzzle/sdk/Kuzzle.java @@ -1,5 +1,6 @@ package io.kuzzle.sdk; +import io.kuzzle.sdk.API.Controllers.DocumentController; import io.kuzzle.sdk.CoreClasses.Json.JsonSerializer; import io.kuzzle.sdk.CoreClasses.Maps.KuzzleMap; import io.kuzzle.sdk.API.Controllers.AuthController; @@ -70,6 +71,10 @@ public AuthController getAuthController() { return new AuthController(this); } + public DocumentController getDocumentController() { + return new DocumentController(this); + } + /** * Initialize a new instance of Kuzzle * diff --git a/src/test/java/io/kuzzle/test/API/Controllers/DocumentTest/DocumentTest.java b/src/test/java/io/kuzzle/test/API/Controllers/DocumentTest/DocumentTest.java new file mode 100644 index 00000000..12c7d1e3 --- /dev/null +++ b/src/test/java/io/kuzzle/test/API/Controllers/DocumentTest/DocumentTest.java @@ -0,0 +1,63 @@ +package io.kuzzle.test.API.Controllers.DocumentTest; + +import io.kuzzle.sdk.CoreClasses.Maps.KuzzleMap; +import io.kuzzle.sdk.Exceptions.InternalException; +import io.kuzzle.sdk.Exceptions.NotConnectedException; +import io.kuzzle.sdk.Kuzzle; +import io.kuzzle.sdk.Protocol.AbstractProtocol; +import io.kuzzle.sdk.Protocol.ProtocolState; +import io.kuzzle.sdk.Protocol.WebSocket; + +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import org.mockito.stubbing.Answer; + +import java.util.concurrent.ConcurrentHashMap; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; + +public class DocumentTest { + + private AbstractProtocol networkProtocol = Mockito.mock(WebSocket.class); + @Test + public void createOrReplaceDocumentTest() throws NotConnectedException, InternalException { + + Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; + + ConcurrentHashMap document = new ConcurrentHashMap<>(); + document.put("name", "Yoann"); + document.put("nickname", "El angel de la muerte que hace el JAVA"); + + ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); + + kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document); + Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); + + assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); + assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "createOrReplace"); + assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); + assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); + assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); + assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("nickname").toString(), "El angel de la muerte que hace el JAVA"); + } + + @Test(expected = NotConnectedException.class) + public void createOrReplaceDocumentShouldThrowWhenNotConnected() throws NotConnectedException, InternalException { + AbstractProtocol fakeNetworkProtocol = Mockito.mock(WebSocket.class); + Mockito.when(fakeNetworkProtocol.getState()).thenAnswer((Answer) invocation -> ProtocolState.CLOSE); + + Kuzzle kuzzleMock = spy(new Kuzzle(fakeNetworkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; + + ConcurrentHashMap document = new ConcurrentHashMap<>(); + document.put("name", "Yoann"); + document.put("nickname", "El angel de la muerte que hace el JAVA"); + + kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document); + } +} From ca70b08f00cc94c2f4741e4ac3748d3bd944545e Mon Sep 17 00:00:00 2001 From: Yoann Abbes Date: Fri, 21 Feb 2020 15:36:01 +0100 Subject: [PATCH 02/10] remove queuable --- .../controllers/document/create-or-replace/index.md | 13 +++---------- .../sdk/API/Controllers/DocumentController.java | 13 +++++-------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/doc/3/controllers/document/create-or-replace/index.md b/doc/3/controllers/document/create-or-replace/index.md index 75e7cb62..d4138499 100644 --- a/doc/3/controllers/document/create-or-replace/index.md +++ b/doc/3/controllers/document/create-or-replace/index.md @@ -26,7 +26,7 @@ public CompletableFuture> createOrReplace( final String collection, final String id, final ConcurrentHashMap document, - final ConcurrentHashMap options) + final Boolean waitForRefresh) throws NotConnectedException, InternalException ``` @@ -36,17 +36,10 @@ throws NotConnectedException, InternalException | `collection` |
String
| Collection | | `id` |
String
| Document ID | | `document` |
ConcurrentHashMap
| Document content | -| `options` |
ConcurrentHashMap
| Optional parameters | +| `waitForRefresh` |
Boolean
| If set to `true`, Kuzzle will wait for the persistence layer to finish indexing| --- -### options - -| Arguments | Type | Description | -| ------------------ | -------------------------------------------- | --------------------------------- | -| `queuable` |
Boolean
| Document identifier. Auto-generated if not specified | -| `waitForRefresh` |
Boolean
| If set to `true`, Kuzzle will wait for the persistence layer to finish indexing| - ## Return A `ConcurrentHashMap` which has the following properties: @@ -59,4 +52,4 @@ A `ConcurrentHashMap` which has the following properties: ## Usage -<<< ./snippets/createOrReplace.java +<<< ./snippets/create-or-replace.java diff --git a/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java b/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java index 5ccbaa28..6c0ccbc1 100644 --- a/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java +++ b/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java @@ -20,7 +20,7 @@ public DocumentController(final Kuzzle kuzzle) { * @param collection * @param id * @param document - * @param options + * @param waitForRefresh * @return a CompletableFuture * @throws NotConnectedException * @throws InternalException @@ -30,11 +30,10 @@ public CompletableFuture> createOrReplace( final String collection, final String id, final ConcurrentHashMap document, - final ConcurrentHashMap options) throws NotConnectedException, InternalException { + final Boolean waitForRefresh) throws NotConnectedException, InternalException { final KuzzleMap query = new KuzzleMap(); - final KuzzleMap _options = KuzzleMap - .from(options); + query .put("index", index) .put("collection", collection) @@ -42,8 +41,7 @@ public CompletableFuture> createOrReplace( .put("action", "createOrReplace") .put("body", document) .put("_id", id) - .put("queuable", _options.getBoolean("queuable")) - .put("waitForRefresh", _options.getBoolean("waitForRefresh")); + .put("waitForRefresh", waitForRefresh); return kuzzle .query(query) @@ -68,8 +66,7 @@ public CompletableFuture> createOrReplace( final String id, final ConcurrentHashMap document) throws NotConnectedException, InternalException { - final ConcurrentHashMap options = new ConcurrentHashMap<>(); - return this.createOrReplace(index, collection, id, document, options); + return this.createOrReplace(index, collection, id, document, false); } } From 72d2b5778d1812d346c0fdf716b0c1eace92f6d5 Mon Sep 17 00:00:00 2001 From: Yoann-Abbes <44844010+Yoann-Abbes@users.noreply.github.com> Date: Wed, 26 Feb 2020 15:00:35 +0100 Subject: [PATCH 03/10] Update src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Sébastien Cottinet --- .../java/io/kuzzle/sdk/API/Controllers/DocumentController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java b/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java index 6c0ccbc1..789e11c1 100644 --- a/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java +++ b/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java @@ -66,7 +66,7 @@ public CompletableFuture> createOrReplace( final String id, final ConcurrentHashMap document) throws NotConnectedException, InternalException { - return this.createOrReplace(index, collection, id, document, false); + return this.createOrReplace(index, collection, id, document, null); } } From 0a9e0c8053c203c4504c5dc4b40341d8bb354746 Mon Sep 17 00:00:00 2001 From: Yoann Abbes Date: Wed, 26 Feb 2020 15:21:08 +0100 Subject: [PATCH 04/10] requested changes --- .../snippets/create-or-replace.java | 3 +- .../snippets/create-or-replace.test.yml | 2 +- .../DocumentTest/DocumentTest.java | 29 ++++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java index dd8a2dc7..52577781 100644 --- a/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java +++ b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java @@ -3,5 +3,6 @@ document.put("firstname", "John"); document.put("lastname", "Smith"); - kuzzle.getDocumentController().createOrReplace("nyc-open-data", "yellow-taxi", "some-id", document) + ConcurrentHashMap response = kuzzle.getDocumentController().createOrReplace("nyc-open-data", "yellow-taxi", "some-id", document) .get(); + System.out.println(response); \ No newline at end of file diff --git a/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml index d457ab2b..296ef3e4 100644 --- a/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml +++ b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml @@ -7,4 +7,4 @@ hooks: curl -XPUT kuzzle:7512/nyc-open-data/yellow-taxi after: template: default -expected: Success \ No newline at end of file +expected: "_id=some-id" \ No newline at end of file diff --git a/src/test/java/io/kuzzle/test/API/Controllers/DocumentTest/DocumentTest.java b/src/test/java/io/kuzzle/test/API/Controllers/DocumentTest/DocumentTest.java index 12c7d1e3..ca11e13b 100644 --- a/src/test/java/io/kuzzle/test/API/Controllers/DocumentTest/DocumentTest.java +++ b/src/test/java/io/kuzzle/test/API/Controllers/DocumentTest/DocumentTest.java @@ -21,8 +21,34 @@ public class DocumentTest { private AbstractProtocol networkProtocol = Mockito.mock(WebSocket.class); + + @Test + public void createOrReplaceDocumentTestA() throws NotConnectedException, InternalException { + + Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; + + ConcurrentHashMap document = new ConcurrentHashMap<>(); + document.put("name", "Yoann"); + document.put("nickname", "El angel de la muerte que hace el JAVA"); + + ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); + + kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document, true); + Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); + + assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); + assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "createOrReplace"); + assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); + assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); + assertEquals(((KuzzleMap) arg.getValue()).getBoolean("waitForRefresh"), true); + assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); + assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("nickname").toString(), "El angel de la muerte que hace el JAVA"); + } + @Test - public void createOrReplaceDocumentTest() throws NotConnectedException, InternalException { + public void createOrReplaceDocumentTestB() throws NotConnectedException, InternalException { Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); String index = "nyc-open-data"; @@ -41,6 +67,7 @@ public void createOrReplaceDocumentTest() throws NotConnectedException, Internal assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "createOrReplace"); assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); + assertEquals(((KuzzleMap) arg.getValue()).getString("waitForRefresh"), null); assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("nickname").toString(), "El angel de la muerte que hace el JAVA"); } From f68a1188be62399f25e89c48ce140fad05dfcdd7 Mon Sep 17 00:00:00 2001 From: Yoann-Abbes <44844010+Yoann-Abbes@users.noreply.github.com> Date: Thu, 27 Feb 2020 10:14:34 +0100 Subject: [PATCH 05/10] Update doc/3/controllers/document/create-or-replace/index.md Co-Authored-By: Adrien Maret --- doc/3/controllers/document/create-or-replace/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/3/controllers/document/create-or-replace/index.md b/doc/3/controllers/document/create-or-replace/index.md index d4138499..e9e52fda 100644 --- a/doc/3/controllers/document/create-or-replace/index.md +++ b/doc/3/controllers/document/create-or-replace/index.md @@ -2,7 +2,7 @@ code: true type: page title: createOrReplace -description: Creates or Replace a new document +description: Creates or Replace a document --- # createOrReplace From 12b11759e79a766458ba3b38c37fed30ac6bf43e Mon Sep 17 00:00:00 2001 From: Yoann Abbes Date: Fri, 28 Feb 2020 13:21:47 +0100 Subject: [PATCH 06/10] @aschen requested change --- .../snippets/create-or-replace.java | 16 +++++++++++++--- .../snippets/create-or-replace.test.yml | 4 ++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java index 52577781..8227b8b7 100644 --- a/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java +++ b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java @@ -1,8 +1,18 @@ ConcurrentHashMap document = new ConcurrentHashMap<>(); document.put("firstname", "John"); - document.put("lastname", "Smith"); - ConcurrentHashMap response = kuzzle.getDocumentController().createOrReplace("nyc-open-data", "yellow-taxi", "some-id", document) + ConcurrentHashMap result = kuzzle.getDocumentController().createOrReplace("nyc-open-data", "yellow-taxi", "some-id", document) .get(); - System.out.println(response); \ No newline at end of file + +/* + { + _source= + { + firstname=John, + _kuzzle_info={ createdAt=1582892323254, author=-1, updatedAt=1582892323254, updater=-1} + }, + _id=some-id, + _version=1 + } + */ \ No newline at end of file diff --git a/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml index 296ef3e4..fe3e45b8 100644 --- a/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml +++ b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml @@ -6,5 +6,5 @@ hooks: curl -XPOST kuzzle:7512/nyc-open-data/_create curl -XPUT kuzzle:7512/nyc-open-data/yellow-taxi after: -template: default -expected: "_id=some-id" \ No newline at end of file +template: print-result +expected: "firstname=John" \ No newline at end of file From 4f9d3955e20f64d9fcc2719f31e0ba6fb2361600 Mon Sep 17 00:00:00 2001 From: Yoann Date: Mon, 2 Mar 2020 09:58:58 +0100 Subject: [PATCH 07/10] doc --- doc/3/controllers/document/create-or-replace/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/3/controllers/document/create-or-replace/index.md b/doc/3/controllers/document/create-or-replace/index.md index e9e52fda..1a3ef99f 100644 --- a/doc/3/controllers/document/create-or-replace/index.md +++ b/doc/3/controllers/document/create-or-replace/index.md @@ -2,12 +2,12 @@ code: true type: page title: createOrReplace -description: Creates or Replace a document +description: Creates or replaces a document --- # createOrReplace -Creates or replace a document in the provided index and collection. +Creates a new document in the persistent data storage, or replaces its content if it already exists. --- From 33606cbf0ff7517eefb5009618309808153a5373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Cottinet?= Date: Fri, 6 Mar 2020 10:16:42 +0100 Subject: [PATCH 08/10] Update doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java --- .../snippets/create-or-replace.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java index 8227b8b7..f88a3796 100644 --- a/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java +++ b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java @@ -2,17 +2,24 @@ ConcurrentHashMap document = new ConcurrentHashMap<>(); document.put("firstname", "John"); - ConcurrentHashMap result = kuzzle.getDocumentController().createOrReplace("nyc-open-data", "yellow-taxi", "some-id", document) - .get(); + ConcurrentHashMap result = kuzzle + .getDocumentController() + .createOrReplace("nyc-open-data", "yellow-taxi", "some-id", document) + .get(); /* { _source= { firstname=John, - _kuzzle_info={ createdAt=1582892323254, author=-1, updatedAt=1582892323254, updater=-1} + _kuzzle_info={ + createdAt=1582892323254, + author=-1, + updatedAt=1582892323254, + updater=-1 + } }, _id=some-id, _version=1 } - */ \ No newline at end of file + */ From a41aa3281cb19756f7434e5d2d0597f2b8188f03 Mon Sep 17 00:00:00 2001 From: Yoann Abbes Date: Fri, 6 Mar 2020 11:25:39 +0100 Subject: [PATCH 09/10] conflict [ci skip] --- doc/3/controllers/realtime/subscribe/index.md | 7 - .../API/Controllers/DocumentController.java | 111 ++++++++------- .../DocumentTest/DocumentTest.java | 130 +++++++++--------- 3 files changed, 120 insertions(+), 128 deletions(-) diff --git a/doc/3/controllers/realtime/subscribe/index.md b/doc/3/controllers/realtime/subscribe/index.md index bcba6c66..24feb86b 100644 --- a/doc/3/controllers/realtime/subscribe/index.md +++ b/doc/3/controllers/realtime/subscribe/index.md @@ -52,10 +52,3 @@ _Subscription to document notifications with scope option_ <<< ./snippets/document-notifications-leave-scope.java -_Subscription to message notifications_ - -<<< ./snippets/message-notifications.java - -_Subscription to user notifications_ - -<<< ./snippets/user-notifications.java diff --git a/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java b/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java index 789e11c1..6ed2781d 100644 --- a/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java +++ b/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java @@ -13,60 +13,59 @@ public DocumentController(final Kuzzle kuzzle) { super(kuzzle); } - /** - * Creates or Replace a document in a given collection and index. - * - * @param index - * @param collection - * @param id - * @param document - * @param waitForRefresh - * @return a CompletableFuture - * @throws NotConnectedException - * @throws InternalException - */ - public CompletableFuture> createOrReplace( - final String index, - final String collection, - final String id, - final ConcurrentHashMap document, - final Boolean waitForRefresh) throws NotConnectedException, InternalException { - - final KuzzleMap query = new KuzzleMap(); - - query - .put("index", index) - .put("collection", collection) - .put("controller", "document") - .put("action", "createOrReplace") - .put("body", document) - .put("_id", id) - .put("waitForRefresh", waitForRefresh); - - return kuzzle - .query(query) - .thenApplyAsync( - (response) -> (ConcurrentHashMap) response.result); - } - - /** - * Creates or Replace a document in a given collection and index. - * - * @param index - * @param collection - * @param id - * @param document - * @return a CompletableFuture - * @throws NotConnectedException - * @throws InternalException - */ - public CompletableFuture> createOrReplace( - final String index, - final String collection, - final String id, - final ConcurrentHashMap document) throws NotConnectedException, InternalException { - - return this.createOrReplace(index, collection, id, document, null); - } - +// /** +// * Creates or Replace a document in a given collection and index. +// * +// * @param index +// * @param collection +// * @param id +// * @param document +// * @param waitForRefresh +// * @return a CompletableFuture +// * @throws NotConnectedException +// * @throws InternalException +// */ +// public CompletableFuture> createOrReplace( +// final String index, +// final String collection, +// final String id, +// final ConcurrentHashMap document, +// final Boolean waitForRefresh) throws NotConnectedException, InternalException { +// +// final KuzzleMap query = new KuzzleMap(); +// +// query +// .put("index", index) +// .put("collection", collection) +// .put("controller", "document") +// .put("action", "createOrReplace") +// .put("body", document) +// .put("_id", id) +// .put("waitForRefresh", waitForRefresh); +// +// return kuzzle +// .query(query) +// .thenApplyAsync( +// (response) -> (ConcurrentHashMap) response.result); +// } +// +// /** +// * Creates or Replace a document in a given collection and index. +// * +// * @param index +// * @param collection +// * @param id +// * @param document +// * @return a CompletableFuture +// * @throws NotConnectedException +// * @throws InternalException +// */ +// public CompletableFuture> createOrReplace( +// final String index, +// final String collection, +// final String id, +// final ConcurrentHashMap document) throws NotConnectedException, InternalException { +// +// return this.createOrReplace(index, collection, id, document, null); +// } } diff --git a/src/test/java/io/kuzzle/test/API/Controllers/DocumentTest/DocumentTest.java b/src/test/java/io/kuzzle/test/API/Controllers/DocumentTest/DocumentTest.java index ca11e13b..d6367440 100644 --- a/src/test/java/io/kuzzle/test/API/Controllers/DocumentTest/DocumentTest.java +++ b/src/test/java/io/kuzzle/test/API/Controllers/DocumentTest/DocumentTest.java @@ -22,69 +22,69 @@ public class DocumentTest { private AbstractProtocol networkProtocol = Mockito.mock(WebSocket.class); - @Test - public void createOrReplaceDocumentTestA() throws NotConnectedException, InternalException { - - Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); - String index = "nyc-open-data"; - String collection = "yellow-taxi"; - - ConcurrentHashMap document = new ConcurrentHashMap<>(); - document.put("name", "Yoann"); - document.put("nickname", "El angel de la muerte que hace el JAVA"); - - ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); - - kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document, true); - Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); - - assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); - assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "createOrReplace"); - assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); - assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); - assertEquals(((KuzzleMap) arg.getValue()).getBoolean("waitForRefresh"), true); - assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); - assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("nickname").toString(), "El angel de la muerte que hace el JAVA"); - } - - @Test - public void createOrReplaceDocumentTestB() throws NotConnectedException, InternalException { - - Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); - String index = "nyc-open-data"; - String collection = "yellow-taxi"; - - ConcurrentHashMap document = new ConcurrentHashMap<>(); - document.put("name", "Yoann"); - document.put("nickname", "El angel de la muerte que hace el JAVA"); - - ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); - - kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document); - Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); - - assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); - assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "createOrReplace"); - assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); - assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); - assertEquals(((KuzzleMap) arg.getValue()).getString("waitForRefresh"), null); - assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); - assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("nickname").toString(), "El angel de la muerte que hace el JAVA"); - } - - @Test(expected = NotConnectedException.class) - public void createOrReplaceDocumentShouldThrowWhenNotConnected() throws NotConnectedException, InternalException { - AbstractProtocol fakeNetworkProtocol = Mockito.mock(WebSocket.class); - Mockito.when(fakeNetworkProtocol.getState()).thenAnswer((Answer) invocation -> ProtocolState.CLOSE); - - Kuzzle kuzzleMock = spy(new Kuzzle(fakeNetworkProtocol)); - String index = "nyc-open-data"; - String collection = "yellow-taxi"; - - ConcurrentHashMap document = new ConcurrentHashMap<>(); - document.put("name", "Yoann"); - document.put("nickname", "El angel de la muerte que hace el JAVA"); - - kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document); - } +// @Test +// public void createOrReplaceDocumentTestA() throws NotConnectedException, InternalException { +// +// Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); +// String index = "nyc-open-data"; +// String collection = "yellow-taxi"; +// +// ConcurrentHashMap document = new ConcurrentHashMap<>(); +// document.put("name", "Yoann"); +// document.put("nickname", "El angel de la muerte que hace el JAVA"); +// +// ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); +// +// kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document, true); +// Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); +// +// assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); +// assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "createOrReplace"); +// assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); +// assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); +// assertEquals(((KuzzleMap) arg.getValue()).getBoolean("waitForRefresh"), true); +// assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); +// assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("nickname").toString(), "El angel de la muerte que hace el JAVA"); +// } +// +// @Test +// public void createOrReplaceDocumentTestB() throws NotConnectedException, InternalException { +// +// Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); +// String index = "nyc-open-data"; +// String collection = "yellow-taxi"; +// +// ConcurrentHashMap document = new ConcurrentHashMap<>(); +// document.put("name", "Yoann"); +// document.put("nickname", "El angel de la muerte que hace el JAVA"); +// +// ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); +// +// kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document); +// Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); +// +// assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); +// assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "createOrReplace"); +// assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); +// assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); +// assertEquals(((KuzzleMap) arg.getValue()).getString("waitForRefresh"), null); +// assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); +// assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("nickname").toString(), "El angel de la muerte que hace el JAVA"); +// } +// +// @Test(expected = NotConnectedException.class) +// public void createOrReplaceDocumentShouldThrowWhenNotConnected() throws NotConnectedException, InternalException { +// AbstractProtocol fakeNetworkProtocol = Mockito.mock(WebSocket.class); +// Mockito.when(fakeNetworkProtocol.getState()).thenAnswer((Answer) invocation -> ProtocolState.CLOSE); +// +// Kuzzle kuzzleMock = spy(new Kuzzle(fakeNetworkProtocol)); +// String index = "nyc-open-data"; +// String collection = "yellow-taxi"; +// +// ConcurrentHashMap document = new ConcurrentHashMap<>(); +// document.put("name", "Yoann"); +// document.put("nickname", "El angel de la muerte que hace el JAVA"); +// +// kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document); +// } } From 32883d603ac0b301807b0b3bb662ad50f521b352 Mon Sep 17 00:00:00 2001 From: Yoann Abbes Date: Fri, 6 Mar 2020 11:29:21 +0100 Subject: [PATCH 10/10] update Kuzzle.java --- src/main/java/io/kuzzle/sdk/Kuzzle.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/io/kuzzle/sdk/Kuzzle.java b/src/main/java/io/kuzzle/sdk/Kuzzle.java index 9db240d2..696f0720 100644 --- a/src/main/java/io/kuzzle/sdk/Kuzzle.java +++ b/src/main/java/io/kuzzle/sdk/Kuzzle.java @@ -78,10 +78,6 @@ public AuthController getAuthController() { return new AuthController(this); } - public DocumentController getDocumentController() { - return new DocumentController(this); - } - /** * @return The DocumentController */