Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions doc/3/controllers/document/m-delete/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
code: true
type: page
title: mDelete
description: Deletes multiple documents
---

# mDelete

Deletes multiple documents.

---

## Arguments

```java
public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> mDelete(
final String index,
final String collection,
final ArrayList<String> ids)
throws NotConnectedException, InternalException

```

| Arguments | Type | Description |
| ------------------ | ------------------------------------------------------- | --------------------------------- |
| `index` | <pre>String</pre> | Index name |
| `collection` | <pre>String</pre> | Collection name |
| `ids` | <pre>ArrayList<String></pre> | Document IDs |
---

## Return

A `ConcurrentHashMap<String, ArrayList<Object>>` which has a `successes` and `errors` `ArrayList<Object>`:
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` | <pre>String</pre> | Document ID |
| `reason` | <pre>String</pre> | Human readable reason |

## Usage

<<< ./snippets/m-delete.java
14 changes: 14 additions & 0 deletions doc/3/controllers/document/m-delete/snippets/m-delete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
final ArrayList<String> ids = new ArrayList<>();
ids.add("some-id");
ids.add("some-id2");

ConcurrentHashMap<String, ArrayList<Object>> result = kuzzle.getDocumentController().mDelete("nyc-open-data", "yellow-taxi", ids)
.get();
/*
result =
{
successes=[some-id, some-id2],
errors=[]
}

*/
14 changes: 14 additions & 0 deletions doc/3/controllers/document/m-delete/snippets/m-delete.test.yml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion doc/3/controllers/document/replace/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ throws NotConnectedException, InternalException
| `index` | <pre>String</pre> | Index |
| `collection` | <pre>String</pre> | Collection |
| `id` | <pre>String</pre> | Document ID |
| `document` | <pre>ConcurrentHashMap<String, Object></pre> | Updated ocument content |
| `document` | <pre>ConcurrentHashMap<String, Object></pre> | New content of the document to update |
| `waitForRefresh` | <pre>Boolean</pre> | If set to `true`, Kuzzle will wait for the persistence layer to finish indexing|

---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConcurrentHashMap<String, ArrayList<Object>>> mDelete(
final String index,
final String collection,
final ArrayList<String> 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<String, ArrayList<Object>>) response.result);
}

/**
* Deletes multiple documents.
*
* @param index
* @param collection
* @param ids
* @return a CompletableFuture
* @throws NotConnectedException
* @throws InternalException
*/
public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> mDelete(
final String index,
final String collection,
final ArrayList<String> ids) throws NotConnectedException, InternalException {

return mDelete(index, collection, ids, null);
}

/**
* Replace a document in a given collection and index.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> ids = new ArrayList<>();
ids.add("some-id1");
ids.add("some-id2");
ArgumentCaptor<KuzzleMap> 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<String>)(((KuzzleMap)(arg.getValue()).get("body"))).get("ids")).get(0), "some-id1");
assertEquals(((ArrayList<String>)(((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<String> ids = new ArrayList<>();
ids.add("some-id1");
ids.add("some-id2");
ArgumentCaptor<KuzzleMap> 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<String>)(((KuzzleMap)(arg.getValue()).get("body"))).get("ids")).get(0), "some-id1");
assertEquals(((ArrayList<String>)(((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<ProtocolState>) invocation -> ProtocolState.CLOSE);

Kuzzle kuzzleMock = spy(new Kuzzle(fakeNetworkProtocol));
String index = "nyc-open-data";
String collection = "yellow-taxi";

final ArrayList<String> ids = new ArrayList<>();
ids.add("some-id1");
ids.add("some-id2");

kuzzleMock.getDocumentController().mDelete(index, collection, ids);
}

@Test
public void replaceDocumentTestA() throws NotConnectedException, InternalException {

Expand Down