diff --git a/doc/3/controllers/document/delete/index.md b/doc/3/controllers/document/delete/index.md new file mode 100644 index 00000000..67b309aa --- /dev/null +++ b/doc/3/controllers/document/delete/index.md @@ -0,0 +1,50 @@ +--- +code: true +type: page +title: delete +description: Deletes a document +--- + +# delete + +Deletes a document. + +--- + +## Arguments + +```java +public CompletableFuture> delete( + final String index, + final String collection, + final String id) +throws NotConnectedException, InternalException + +public CompletableFuture> delete( + final String index, + final String collection, + final String id, + final Boolean waitForRefresh) +throws NotConnectedException, InternalException +``` + +| Arguments | Type | Description | +| ------------------ | -------------------------------------------- | --------------------------------- | +| `index` |
String
| Index | +| `collection` |
String
| Collection | +| `id ` |
String
| Document ID | +| `waitForRefresh` |
Boolean
(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` |
String
| ID of the deleted document | + +## Usage + +<<< ./snippets/delete.java diff --git a/doc/3/controllers/document/delete/snippets/delete.java b/doc/3/controllers/document/delete/snippets/delete.java new file mode 100644 index 00000000..433a71de --- /dev/null +++ b/doc/3/controllers/document/delete/snippets/delete.java @@ -0,0 +1,3 @@ + + kuzzle.getDocumentController().delete("nyc-open-data", "yellow-taxi", "some-id") + .get(); diff --git a/doc/3/controllers/document/delete/snippets/delete.test.yml b/doc/3/controllers/document/delete/snippets/delete.test.yml new file mode 100644 index 00000000..46aeccbb --- /dev/null +++ b/doc/3/controllers/document/delete/snippets/delete.test.yml @@ -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 \ 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 index 5b2271b3..21a24844 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 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> 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) 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> 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. * 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 5721cb3d..7b703774 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,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) 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 {