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
50 changes: 50 additions & 0 deletions doc/3/controllers/document/delete/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
code: true
type: page
title: delete
description: Deletes a document
---

# delete

Deletes a document.

---

## Arguments

```java
public CompletableFuture<ConcurrentHashMap<String, Object>> delete(
final String index,
final String collection,
final String id)
throws NotConnectedException, InternalException

public CompletableFuture<ConcurrentHashMap<String, Object>> delete(
final String index,
final String collection,
final String id,
final Boolean waitForRefresh)
throws NotConnectedException, InternalException
```

| Arguments | Type | Description |
| ------------------ | -------------------------------------------- | --------------------------------- |
| `index` | <pre>String</pre> | Index |
| `collection` | <pre>String</pre> | Collection |
| `id ` | <pre>String</pre> | Document ID |
| `waitForRefresh` | <pre>Boolean</pre> (optional) | If set to `true`, Kuzzle will wait for the persistence layer to finish indexing|

---

## Return

A `ConcurrentHashMap` which has the following property:

| Property | Type | Description |
|------------- |----------------------------- |--------------------------------- |
| `_id` | <pre>String</pre> | ID of the deleted document |

## Usage

<<< ./snippets/delete.java
3 changes: 3 additions & 0 deletions doc/3/controllers/document/delete/snippets/delete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

kuzzle.getDocumentController().delete("nyc-open-data", "yellow-taxi", "some-id")
.get();
11 changes: 11 additions & 0 deletions doc/3/controllers/document/delete/snippets/delete.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: document#delete
description: deletes 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
curl -XPOST -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" kuzzle:7512/nyc-open-data/yellow-taxi/some-id/_create
after:
template: default
expected: Success
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 a document in a given collection and index.
*
* @param index
* @param collection
* @param id
* @param waitForRefresh
* @return a CompletableFuture
* @throws NotConnectedException
* @throws InternalException
*/
public CompletableFuture<ConcurrentHashMap<String, Object>> delete(
final String index,
final String collection,
final String id,
final Boolean waitForRefresh) throws NotConnectedException, InternalException {

final KuzzleMap query = new KuzzleMap();

query
.put("index", index)
.put("collection", collection)
.put("controller", "document")
.put("action", "delete")
.put("_id", id)
.put("waitForRefresh", waitForRefresh);

return kuzzle
.query(query)
.thenApplyAsync(
(response) -> (ConcurrentHashMap<String, Object>) response.result);
}

/**
* Deletes a document in a given collection and index.
*
* @param index
* @param collection
* @param id
* @return a CompletableFuture
* @throws NotConnectedException
* @throws InternalException
*/
public CompletableFuture<ConcurrentHashMap<String, Object>> delete(
final String index,
final String collection,
final String id) throws NotConnectedException, InternalException {

return this.delete(index, collection, id, null);
}

/**
* Gets multiple documents in a given collection and index.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,56 @@ public class DocumentTest {

private AbstractProtocol networkProtocol = Mockito.mock(WebSocket.class);

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

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

ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class);

kuzzleMock.getDocumentController().delete(index, collection, "some-id", true);
Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture());

assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document");
assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "delete");
assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data");
assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id");
assertEquals(((KuzzleMap) arg.getValue()).getBoolean("waitForRefresh"), true);
}

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

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

ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class);

kuzzleMock.getDocumentController().delete(index, collection, "some-id");
Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture());

assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document");
assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "delete");
assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data");
assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id");
assertEquals(((KuzzleMap) arg.getValue()).getBoolean("waitForRefresh"), null);
}

@Test(expected = NotConnectedException.class)
public void deleteDocumentShouldThrowWhenNotConnected() 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";

kuzzleMock.getDocumentController().delete(index, collection, "some-id");
}

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

Expand Down