diff --git a/doc/3/controllers/document/m-delete/index.md b/doc/3/controllers/document/m-delete/index.md new file mode 100644 index 00000000..d7de90bd --- /dev/null +++ b/doc/3/controllers/document/m-delete/index.md @@ -0,0 +1,46 @@ +--- +code: true +type: page +title: mDelete +description: Deletes multiple documents +--- + +# mDelete + +Deletes multiple documents. + +--- + +## Arguments + +```java +public CompletableFuture>> mDelete( + 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`: +The `successes` array contains the successfully deleted document IDs. + +Each deletion error is an object of the errors array with the following properties: + +| Property | Type | Description | +|------------- |--------------------------------------------- |--------------------------------- | +| `_id` |
String
| Document ID | +| `reason` |
String
| Human readable reason | + +## Usage + +<<< ./snippets/m-delete.java \ No newline at end of file diff --git a/doc/3/controllers/document/m-delete/snippets/m-delete.java b/doc/3/controllers/document/m-delete/snippets/m-delete.java new file mode 100644 index 00000000..513b6071 --- /dev/null +++ b/doc/3/controllers/document/m-delete/snippets/m-delete.java @@ -0,0 +1,14 @@ + final ArrayList ids = new ArrayList<>(); + ids.add("some-id"); + ids.add("some-id2"); + + ConcurrentHashMap> result = kuzzle.getDocumentController().mDelete("nyc-open-data", "yellow-taxi", ids) + .get(); +/* + result = + { + successes=[some-id, some-id2], + errors=[] + } + +*/ \ No newline at end of file diff --git a/doc/3/controllers/document/m-delete/snippets/m-delete.test.yml b/doc/3/controllers/document/m-delete/snippets/m-delete.test.yml new file mode 100644 index 00000000..a7dd0966 --- /dev/null +++ b/doc/3/controllers/document/m-delete/snippets/m-delete.test.yml @@ -0,0 +1,14 @@ +name: document#mDelete +description: Delete 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: + - "some-id" + - "some-id2" \ No newline at end of file diff --git a/doc/3/controllers/document/replace/index.md b/doc/3/controllers/document/replace/index.md index 43dde4e7..fc3bb906 100644 --- a/doc/3/controllers/document/replace/index.md +++ b/doc/3/controllers/document/replace/index.md @@ -35,7 +35,7 @@ throws NotConnectedException, InternalException | `index` |
String
| Index | | `collection` |
String
| Collection | | `id` |
String
| Document ID | -| `document` |
ConcurrentHashMap
| Updated ocument content | +| `document` |
ConcurrentHashMap
| New content of the document to update | | `waitForRefresh` |
Boolean
| If set to `true`, Kuzzle will wait for the persistence layer to finish indexing| --- 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 88d9e4b6..10f153d4 100644 --- a/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java +++ b/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java @@ -5,15 +5,66 @@ 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; -import java.util.ArrayList; public class DocumentController extends BaseController { public DocumentController(final Kuzzle kuzzle) { super(kuzzle); } + /** + * Deletes multiple documents. + * + * @param index + * @param collection + * @param ids + * @param waitForRefresh + * @return a CompletableFuture + * @throws NotConnectedException + * @throws InternalException + */ + public CompletableFuture>> mDelete( + final String index, + final String collection, + final ArrayList ids, + final Boolean waitForRefresh) throws NotConnectedException, InternalException { + + + final KuzzleMap query = new KuzzleMap(); + query + .put("index", index) + .put("collection", collection) + .put("controller", "document") + .put("action", "mDelete") + .put("waitForRefresh", waitForRefresh) + .put("body", new KuzzleMap().put("ids", ids)); + + return kuzzle + .query(query) + .thenApplyAsync( + (response) -> (ConcurrentHashMap>) response.result); + } + + /** + * Deletes multiple documents. + * + * @param index + * @param collection + * @param ids + * @return a CompletableFuture + * @throws NotConnectedException + * @throws InternalException + */ + public CompletableFuture>> mDelete( + final String index, + final String collection, + final ArrayList ids) throws NotConnectedException, InternalException { + + return mDelete(index, collection, ids, null); + } + /** * Replace a document in a given collection and index. * 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 fd909eea..1b0d1906 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 @@ -23,6 +23,68 @@ public class DocumentTest { private AbstractProtocol networkProtocol = Mockito.mock(WebSocket.class); + @Test + public void mDeleteDocumentTestA() 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().mDelete(index, collection, ids); + Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture()); + + assertEquals((arg.getValue()).getString("controller"), "document"); + assertEquals((arg.getValue()).getString("action"), "mDelete"); + assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); + assertEquals((arg.getValue()).getBoolean("waitForRefresh"), null); + 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 + public void mDeleteDocumentTestB() 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().mDelete(index, collection, ids, false); + Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture()); + + assertEquals((arg.getValue()).getString("controller"), "document"); + assertEquals((arg.getValue()).getString("action"), "mDelete"); + assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); + assertEquals((arg.getValue()).getBoolean("waitForRefresh"), false); + 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 mDeleteDocumentShouldThrowWhenNotConnected() 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().mDelete(index, collection, ids); + } + @Test public void replaceDocumentTestA() throws NotConnectedException, InternalException {