From 32599820ab27bca100a521dc702326f4b8c6c3fc Mon Sep 17 00:00:00 2001 From: Yoann Abbes Date: Fri, 28 Feb 2020 16:17:52 +0100 Subject: [PATCH 1/2] document:mget --- .ci/doc/templates/default.tpl.java | 1 + .ci/doc/templates/print-result-array.tpl.java | 29 +++++++++ doc/3/controllers/document/m-get/index.md | 47 ++++++++++++++ .../document/m-get/snippets/m-get.java | 37 +++++++++++ .../document/m-get/snippets/m-get.test.yml | 14 +++++ .../API/Controllers/DocumentController.java | 46 ++++++++++++++ src/main/java/io/kuzzle/sdk/Kuzzle.java | 10 ++- .../DocumentTest/DocumentTest.java | 63 +++++++++++++++++++ 8 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 .ci/doc/templates/print-result-array.tpl.java create mode 100644 doc/3/controllers/document/m-get/index.md create mode 100644 doc/3/controllers/document/m-get/snippets/m-get.java create mode 100644 doc/3/controllers/document/m-get/snippets/m-get.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/.ci/doc/templates/default.tpl.java b/.ci/doc/templates/default.tpl.java index 49b426c9..03d84d23 100644 --- a/.ci/doc/templates/default.tpl.java +++ b/.ci/doc/templates/default.tpl.java @@ -1,4 +1,5 @@ import io.kuzzle.sdk.Kuzzle; +import java.util.ArrayList; import io.kuzzle.sdk.Protocol.WebSocket; import io.kuzzle.sdk.Options.Protocol.WebSocketOptions; import io.kuzzle.sdk.Options.KuzzleOptions; diff --git a/.ci/doc/templates/print-result-array.tpl.java b/.ci/doc/templates/print-result-array.tpl.java new file mode 100644 index 00000000..9c751d0b --- /dev/null +++ b/.ci/doc/templates/print-result-array.tpl.java @@ -0,0 +1,29 @@ +import io.kuzzle.sdk.Kuzzle; +import io.kuzzle.sdk.Protocol.WebSocket; +import io.kuzzle.sdk.Options.Protocol.WebSocketOptions; +import io.kuzzle.sdk.Options.KuzzleOptions; +import io.kuzzle.sdk.CoreClasses.Responses.Response; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.ArrayList; + +public class SnippetTest { + private static Kuzzle kuzzle; + + public static void main(String[] argv) { + try { + kuzzle = new Kuzzle(new WebSocket("kuzzle")); + kuzzle.connect(); + [snippet-code] + for (Object o : result.get("successes")) { + System.out.println(o); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (kuzzle != null) { + kuzzle.disconnect(); + } + } + } +} \ No newline at end of file diff --git a/doc/3/controllers/document/m-get/index.md b/doc/3/controllers/document/m-get/index.md new file mode 100644 index 00000000..0064842a --- /dev/null +++ b/doc/3/controllers/document/m-get/index.md @@ -0,0 +1,47 @@ +--- +code: true +type: page +title: mGet +description: Gets multiple documents +--- + +# mGet + +Gets multiple documents. + +--- + +## Arguments + +```java +public CompletableFuture>> mCreate( + final String index, + final String collection, + final ArrayList ids) +throws NotConnectedException, InternalException + +``` + +| Arguments | Type | Description | +| ------------------ | ------------------------------------------------------- | --------------------------------- | +| `index` |
String
| Index name | +| `collection` |
String
| Collection name | +| `ids` |
ArrayList
| Document IDs | +--- + +## Return + +A `ConcurrentHashMap>` which has a `successes` and `errors` `ArrayList`: +Each created document is an object of the `successes` array with the following properties: + +| Property | Type | Description | +|------------- |--------------------------------------------- |--------------------------------- | +| `_source` |
ConcurrentHashMap
| Document content | +| `_id` |
String
| Document ID | +| `_version` |
Integer
| Version of the document in the persistent data storage | + +The `errors` array contain the IDs of not found documents. + +## Usage + +<<< ./snippets/m-get.java diff --git a/doc/3/controllers/document/m-get/snippets/m-get.java b/doc/3/controllers/document/m-get/snippets/m-get.java new file mode 100644 index 00000000..2e3ad1ab --- /dev/null +++ b/doc/3/controllers/document/m-get/snippets/m-get.java @@ -0,0 +1,37 @@ + final ArrayList ids = new ArrayList<>(); + ids.add("some-id"); + ids.add("some-id2"); + + ConcurrentHashMap> result = kuzzle.getDocumentController().mGet("nyc-open-data", "yellow-taxi", ids) + .get(); + +/* + result = + { + successes= + [ + { + result=created, + _source= + { + key=value, + _kuzzle_info={createdAt=1582892842099, author=-1} + }, + _id=some-id, + _version=1 + }, + { + result=created, + _source= + { + key=value, + _kuzzle_info={createdAt=1582892842099, author=-1} + }, + _id=some-id2, + _version=1 + } + ], + errors=[] + } + +*/ \ No newline at end of file diff --git a/doc/3/controllers/document/m-get/snippets/m-get.test.yml b/doc/3/controllers/document/m-get/snippets/m-get.test.yml new file mode 100644 index 00000000..0ea219d0 --- /dev/null +++ b/doc/3/controllers/document/m-get/snippets/m-get.test.yml @@ -0,0 +1,14 @@ +name: document#mGet +description: Get multiple documents +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 + curl -XPOST -d '{"name":"John"}' -H "Content-Type: application/json" kuzzle:7512/nyc-open-data/yellow-taxi/some-id/_create + curl -XPOST -d '{"color":"purple"}' -H "Content-Type: application/json" kuzzle:7512/nyc-open-data/yellow-taxi/some-id2/_create + after: +template: print-result-array +expected: + - "id=some-id, _version=1" + - "id=some-id2, _version=1" \ 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..5b2271b3 --- /dev/null +++ b/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java @@ -0,0 +1,46 @@ +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.ArrayList; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; + +public class DocumentController extends BaseController { + public DocumentController(final Kuzzle kuzzle) { + super(kuzzle); + } + + /** + * Gets multiple documents in a given collection and index. + * + * @param index + * @param collection + * @param ids + * @return a CompletableFuture + * @throws NotConnectedException + * @throws InternalException + */ + public CompletableFuture>> mGet( + final String index, + final String collection, + final ArrayList ids) throws NotConnectedException, InternalException { + + + final KuzzleMap query = new KuzzleMap(); + query + .put("index", index) + .put("collection", collection) + .put("controller", "document") + .put("action", "mGet") + .put("body", new KuzzleMap().put("ids", ids)); + + return kuzzle + .query(query) + .thenApplyAsync( + (response) -> (ConcurrentHashMap>) response.result); + } +} diff --git a/src/main/java/io/kuzzle/sdk/Kuzzle.java b/src/main/java/io/kuzzle/sdk/Kuzzle.java index 062a2b4c..f1b3f8b2 100644 --- a/src/main/java/io/kuzzle/sdk/Kuzzle.java +++ b/src/main/java/io/kuzzle/sdk/Kuzzle.java @@ -1,10 +1,11 @@ package io.kuzzle.sdk; +import io.kuzzle.sdk.API.Controllers.AuthController; +import io.kuzzle.sdk.API.Controllers.DocumentController; import io.kuzzle.sdk.API.Controllers.IndexController; import io.kuzzle.sdk.API.Controllers.RealtimeController; import io.kuzzle.sdk.CoreClasses.Json.JsonSerializer; import io.kuzzle.sdk.CoreClasses.Maps.KuzzleMap; -import io.kuzzle.sdk.API.Controllers.AuthController; import io.kuzzle.sdk.CoreClasses.Task; import io.kuzzle.sdk.Exceptions.*; import io.kuzzle.sdk.Options.KuzzleOptions; @@ -77,6 +78,13 @@ public AuthController getAuthController() { return new AuthController(this); } + /** + * @return The AuthController + */ + public DocumentController getDocumentController() { + return new DocumentController(this); + } + /** * @return The IndexController */ 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..80e8439c --- /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.ArrayList; +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 mGetDocumentTest() throws NotConnectedException, InternalException { + + Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; + + final ArrayList ids = new ArrayList<>(); + ids.add("some-id1"); + ids.add("some-id2"); + ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); + + kuzzleMock.getDocumentController().mGet(index, collection, ids); + Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture()); + + assertEquals((arg.getValue()).getString("controller"), "document"); + assertEquals((arg.getValue()).getString("action"), "mGet"); + assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); + assertEquals(((ArrayList)(((KuzzleMap)(arg.getValue()).get("body"))).get("ids")).get(0), "some-id1"); + assertEquals(((ArrayList)(((KuzzleMap)(arg.getValue()).get("body"))).get("ids")).get(1), "some-id2"); + } + + @Test(expected = NotConnectedException.class) + public void mCreateDocumentShouldThrowWhenNotConnected() 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"; + + final ArrayList ids = new ArrayList<>(); + ids.add("some-id1"); + ids.add("some-id2"); + + kuzzleMock.getDocumentController().mGet(index, collection, ids); + } +} From 5bbf360476db3d1da5371ee6c08b4387e1c1edf0 Mon Sep 17 00:00:00 2001 From: Yoann Abbes Date: Fri, 28 Feb 2020 16:23:30 +0100 Subject: [PATCH 2/2] nit --- doc/3/controllers/document/m-get/index.md | 4 ++-- src/main/java/io/kuzzle/sdk/Kuzzle.java | 2 +- .../test/API/Controllers/DocumentTest/DocumentTest.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/3/controllers/document/m-get/index.md b/doc/3/controllers/document/m-get/index.md index 0064842a..4c424036 100644 --- a/doc/3/controllers/document/m-get/index.md +++ b/doc/3/controllers/document/m-get/index.md @@ -14,7 +14,7 @@ Gets multiple documents. ## Arguments ```java -public CompletableFuture>> mCreate( +public CompletableFuture>> mGet( final String index, final String collection, final ArrayList ids) @@ -26,7 +26,7 @@ throws NotConnectedException, InternalException | ------------------ | ------------------------------------------------------- | --------------------------------- | | `index` |
String
| Index name | | `collection` |
String
| Collection name | -| `ids` |
ArrayList
| Document IDs | +| `ids` |
ArrayList
| Document IDs | --- ## Return diff --git a/src/main/java/io/kuzzle/sdk/Kuzzle.java b/src/main/java/io/kuzzle/sdk/Kuzzle.java index f1b3f8b2..2584a7c4 100644 --- a/src/main/java/io/kuzzle/sdk/Kuzzle.java +++ b/src/main/java/io/kuzzle/sdk/Kuzzle.java @@ -79,7 +79,7 @@ public AuthController getAuthController() { } /** - * @return The AuthController + * @return The DocumentController */ public DocumentController getDocumentController() { return new DocumentController(this); 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 80e8439c..5721cb3d 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 @@ -46,7 +46,7 @@ public void mGetDocumentTest() throws NotConnectedException, InternalException { } @Test(expected = NotConnectedException.class) - public void mCreateDocumentShouldThrowWhenNotConnected() throws NotConnectedException, InternalException { + public void mGetDocumentShouldThrowWhenNotConnected() throws NotConnectedException, InternalException { AbstractProtocol fakeNetworkProtocol = Mockito.mock(WebSocket.class); Mockito.when(fakeNetworkProtocol.getState()).thenAnswer((Answer) invocation -> ProtocolState.CLOSE);