From 6cbe8bad3414a457cb0733f178f40ed7a9a7b2f3 Mon Sep 17 00:00:00 2001 From: Yoann Abbes Date: Wed, 18 Mar 2020 15:41:39 +0100 Subject: [PATCH 1/3] add document:deleteByQuery --- .../templates/print-result-arraylist.tpl.java | 30 ++ .../document/delete-by-query/index.md | 46 +++ .../snippets/delete-by-query.java | 5 + .../snippets/delete-by-query.test.yml | 22 ++ .../API/Controllers/DocumentController.java | 51 +++ .../DocumentTest/DocumentTest.java | 347 +++++++++++------- 6 files changed, 360 insertions(+), 141 deletions(-) create mode 100644 .ci/doc/templates/print-result-arraylist.tpl.java create mode 100644 doc/3/controllers/document/delete-by-query/index.md create mode 100644 doc/3/controllers/document/delete-by-query/snippets/delete-by-query.java create mode 100644 doc/3/controllers/document/delete-by-query/snippets/delete-by-query.test.yml diff --git a/.ci/doc/templates/print-result-arraylist.tpl.java b/.ci/doc/templates/print-result-arraylist.tpl.java new file mode 100644 index 00000000..ca5a96dc --- /dev/null +++ b/.ci/doc/templates/print-result-arraylist.tpl.java @@ -0,0 +1,30 @@ +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.Options.SubscribeOptions; +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) { + 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/delete-by-query/index.md b/doc/3/controllers/document/delete-by-query/index.md new file mode 100644 index 00000000..c59a5abc --- /dev/null +++ b/doc/3/controllers/document/delete-by-query/index.md @@ -0,0 +1,46 @@ +--- +code: true +type: page +title: deleteByQuery +description: Delete documents matching query +--- + +# deleteByQuery + +Deletes documents matching the provided search query. + +Kuzzle uses the [ElasticSearch Query DSL](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl.html) syntax. + +An empty or null query will match all documents in the collection. + +
+ +```java + public CompletableFuture> deleteByQuery( + final String index, + final String collection, + final ConcurrentHashMap searchQuery) throws NotConnectedException, InternalException + + public CompletableFuture> deleteByQuery( + final String index, + final String collection, + final ConcurrentHashMap searchQuery, + final Boolean waitForRefresh) throws NotConnectedException, InternalException +``` + +| Argument | Type | Description | +| ------------------ | -------------------------------------------- | --------------- | +| `index` |
String
| Index name | +| `collection` |
String
| Collection name | +| `searchQuery` |
ConcurrentHashMap
| Query to match | +| `waitForRefresh` |
Boolean
(optional) | If set to `true`, Kuzzle will wait for the persistence layer to finish indexing| + +--- + +## Returns + +Returns an `ArrayList` containing the deleted document ids. + +## Usage + +<<< ./snippets/delete-by-query.js \ No newline at end of file diff --git a/doc/3/controllers/document/delete-by-query/snippets/delete-by-query.java b/doc/3/controllers/document/delete-by-query/snippets/delete-by-query.java new file mode 100644 index 00000000..295c5079 --- /dev/null +++ b/doc/3/controllers/document/delete-by-query/snippets/delete-by-query.java @@ -0,0 +1,5 @@ + ConcurrentHashMap searchQuery = new ConcurrentHashMap<>(); + ConcurrentHashMap match = new ConcurrentHashMap<>(); + match.put("capacity", 4); + searchQuery.put("match", match); + ArrayList result = kuzzle.getDocumentController().deleteByQuery("nyc-open-data", "yellow-taxi", searchQuery).get(); \ No newline at end of file diff --git a/doc/3/controllers/document/delete-by-query/snippets/delete-by-query.test.yml b/doc/3/controllers/document/delete-by-query/snippets/delete-by-query.test.yml new file mode 100644 index 00000000..5d3fd03d --- /dev/null +++ b/doc/3/controllers/document/delete-by-query/snippets/delete-by-query.test.yml @@ -0,0 +1,22 @@ +name: document#deleteByQuery +description: Delete documents matching query +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 + for i in 1 2 3 4 5; do + curl -H "Content-type: application/json" -d '{"capacity": 4}' kuzzle:7512/nyc-open-data/yellow-taxi/document_$i/_create + done + for i in 1 2 3 4 5; do + curl -H "Content-type: application/json" -d '{"capacity": 7}' kuzzle:7512/nyc-open-data/yellow-taxi/_create + done + curl -XPOST kuzzle:7512/nyc-open-data/_refresh + after: +template: print-result-arraylist +expected: + - "document_1" + - "document_2" + - "document_3" + - "document_4" + - "document_5" \ 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 5ecd9e1f..897bce95 100644 --- a/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java +++ b/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java @@ -593,4 +593,55 @@ public CompletableFuture>> mCreateOr return this.mCreateOrReplace(index, collection, documents, null); } + + /** + * Deletes documents matching the provided search query. + * + * @param index + * @param collection + * @param searchQuery + * @param waitForRefresh + * @return a CompletableFuture + * @throws NotConnectedException + * @throws InternalException + */ + public CompletableFuture> deleteByQuery( + final String index, + final String collection, + final ConcurrentHashMap searchQuery, + final Boolean waitForRefresh) throws NotConnectedException, InternalException { + + final KuzzleMap query = new KuzzleMap(); + + query + .put("index", index) + .put("collection", collection) + .put("controller", "document") + .put("action", "deleteByQuery") + .put("body", new KuzzleMap().put("query", searchQuery)) + .put("waitForRefresh", waitForRefresh); + + return kuzzle + .query(query) + .thenApplyAsync( + (response) -> (ArrayList)((ConcurrentHashMap) response.result).get("ids")); + } + + /** + * Deletes documents matching the provided search query. + * + * @param index + * @param collection + * @param searchQuery + * @return a CompletableFuture + * @throws NotConnectedException + * @throws InternalException + */ + public CompletableFuture> deleteByQuery( + final String index, + final String collection, + final ConcurrentHashMap searchQuery) throws NotConnectedException, InternalException { + + return this.deleteByQuery(index, collection, searchQuery, null); + } } 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 ee3a3950..5b195598 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 @@ -26,104 +26,104 @@ public class DocumentTest { private AbstractProtocol networkProtocol = Mockito.mock(WebSocket.class); - @Test - public void getDocumentTest() throws NotConnectedException, InternalException { + @Test + public void getDocumentTest() throws NotConnectedException, InternalException { - Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); - String index = "nyc-open-data"; - String collection = "yellow-taxi"; + Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; - ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); + ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); - kuzzleMock.getDocumentController().get(index, collection, "some-id"); - Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); + kuzzleMock.getDocumentController().get(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"), "get"); - assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); - assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); - } + assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); + assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "get"); + assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); + assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); + } - @Test(expected = NotConnectedException.class) - public void getDocumentShouldThrowWhenNotConnected() throws NotConnectedException, InternalException { - AbstractProtocol fakeNetworkProtocol = Mockito.mock(WebSocket.class); - Mockito.when(fakeNetworkProtocol.getState()).thenAnswer((Answer) invocation -> ProtocolState.CLOSE); + @Test(expected = NotConnectedException.class) + public void getDocumentShouldThrowWhenNotConnected() 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"; + Kuzzle kuzzleMock = spy(new Kuzzle(fakeNetworkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; - kuzzleMock.getDocumentController().get(index, collection, "some-id"); - } + kuzzleMock.getDocumentController().get(index, collection, "some-id"); + } - @Test - public void createOrReplaceDocumentTestA() throws NotConnectedException, InternalException { + @Test + public void createOrReplaceDocumentTestA() throws NotConnectedException, InternalException { - Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); - String index = "nyc-open-data"; - String collection = "yellow-taxi"; + Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; - ConcurrentHashMap document = new ConcurrentHashMap<>(); - document.put("name", "Yoann"); - document.put("nickname", "El angel de la muerte que hace el JAVA"); + ConcurrentHashMap document = new ConcurrentHashMap<>(); + document.put("name", "Yoann"); + document.put("nickname", "El angel de la muerte que hace el JAVA"); - ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); + ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); - kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document, true); - Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); + kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document, true); + Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); - assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); - assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "createOrReplace"); - assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); - assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); - assertEquals(((KuzzleMap) arg.getValue()).getBoolean("waitForRefresh"), true); - assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); - assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("nickname").toString(), "El angel de la muerte que hace el JAVA"); - } + assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); + assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "createOrReplace"); + assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); + assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); + assertEquals(((KuzzleMap) arg.getValue()).getBoolean("waitForRefresh"), true); + assertEquals(((ConcurrentHashMap) (((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); + assertEquals(((ConcurrentHashMap) (((KuzzleMap) arg.getValue()).get("body"))).get("nickname").toString(), "El angel de la muerte que hace el JAVA"); + } - @Test - public void createOrReplaceDocumentTestB() throws NotConnectedException, InternalException { + @Test + public void createOrReplaceDocumentTestB() throws NotConnectedException, InternalException { - Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); - String index = "nyc-open-data"; - String collection = "yellow-taxi"; + Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; - ConcurrentHashMap document = new ConcurrentHashMap<>(); - document.put("name", "Yoann"); - document.put("nickname", "El angel de la muerte que hace el JAVA"); + ConcurrentHashMap document = new ConcurrentHashMap<>(); + document.put("name", "Yoann"); + document.put("nickname", "El angel de la muerte que hace el JAVA"); - ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); + ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); - kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document); - Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); + kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document); + Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); - assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); - assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "createOrReplace"); - assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); - assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); - assertEquals(((KuzzleMap) arg.getValue()).getString("waitForRefresh"), null); - assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); - assertEquals(((ConcurrentHashMap)(((KuzzleMap) arg.getValue()).get("body"))).get("nickname").toString(), "El angel de la muerte que hace el JAVA"); - } + assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); + assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "createOrReplace"); + assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); + assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); + assertEquals(((KuzzleMap) arg.getValue()).getString("waitForRefresh"), null); + assertEquals(((ConcurrentHashMap) (((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); + assertEquals(((ConcurrentHashMap) (((KuzzleMap) arg.getValue()).get("body"))).get("nickname").toString(), "El angel de la muerte que hace el JAVA"); + } - @Test(expected = NotConnectedException.class) - public void createOrReplaceDocumentShouldThrowWhenNotConnected() throws NotConnectedException, InternalException { - AbstractProtocol fakeNetworkProtocol = Mockito.mock(WebSocket.class); - Mockito.when(fakeNetworkProtocol.getState()).thenAnswer((Answer) invocation -> ProtocolState.CLOSE); + @Test(expected = NotConnectedException.class) + public void createOrReplaceDocumentShouldThrowWhenNotConnected() 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"; + Kuzzle kuzzleMock = spy(new Kuzzle(fakeNetworkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; - ConcurrentHashMap document = new ConcurrentHashMap<>(); - document.put("name", "Yoann"); - document.put("nickname", "El angel de la muerte que hace el JAVA"); + ConcurrentHashMap document = new ConcurrentHashMap<>(); + document.put("name", "Yoann"); + document.put("nickname", "El angel de la muerte que hace el JAVA"); - kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document); - } + kuzzleMock.getDocumentController().createOrReplace(index, collection, "some-id", document); + } - @Test - public void createDocumentTestA() throws NotConnectedException, InternalException { + @Test + public void createDocumentTestA() throws NotConnectedException, InternalException { Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); String index = "nyc-open-data"; @@ -193,77 +193,77 @@ public void createDocumentThrowWhenNotConnected() throws NotConnectedException, kuzzleMock.getDocumentController().create(index, collection, document); } - @Test - public void udpateDocumentTestA() throws NotConnectedException, InternalException { + @Test + public void udpateDocumentTestA() throws NotConnectedException, InternalException { - Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); - String index = "nyc-open-data"; - String collection = "yellow-taxi"; + Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; - ConcurrentHashMap document = new ConcurrentHashMap<>(); - document.put("name", "Yoann"); + ConcurrentHashMap document = new ConcurrentHashMap<>(); + document.put("name", "Yoann"); - UpdateOptions options = new UpdateOptions(); - options.setWaitForRefresh(false); - options.setSource(true); - options.setRetryOnConflict(1); + UpdateOptions options = new UpdateOptions(); + options.setWaitForRefresh(false); + options.setSource(true); + options.setRetryOnConflict(1); - ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); + ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); - kuzzleMock.getDocumentController().update(index, collection, "some-id", document, options); - Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); + kuzzleMock.getDocumentController().update(index, collection, "some-id", document, options); + Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); - assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); - assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "update"); - assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); - assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); - assertEquals(((KuzzleMap) arg.getValue()).getNumber("retryOnConflict"), 1); - assertEquals(((KuzzleMap) arg.getValue()).getBoolean("waitForRefresh"), false); - assertEquals(((KuzzleMap) arg.getValue()).getBoolean("source"), true); - assertEquals(((ConcurrentHashMap) (((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); - } + assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); + assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "update"); + assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); + assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); + assertEquals(((KuzzleMap) arg.getValue()).getNumber("retryOnConflict"), 1); + assertEquals(((KuzzleMap) arg.getValue()).getBoolean("waitForRefresh"), false); + assertEquals(((KuzzleMap) arg.getValue()).getBoolean("source"), true); + assertEquals(((ConcurrentHashMap) (((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); + } - @Test - public void udpateDocumentTestB() throws NotConnectedException, InternalException { + @Test + public void udpateDocumentTestB() throws NotConnectedException, InternalException { - Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); - String index = "nyc-open-data"; - String collection = "yellow-taxi"; + Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; - ConcurrentHashMap document = new ConcurrentHashMap<>(); + ConcurrentHashMap document = new ConcurrentHashMap<>(); - document.put("name", "Yoann"); - ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); + document.put("name", "Yoann"); + ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); - kuzzleMock.getDocumentController().update(index, collection, "some-id", document); - Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); + kuzzleMock.getDocumentController().update(index, collection, "some-id", document); + Mockito.verify(kuzzleMock, Mockito.times(1)).query((KuzzleMap) arg.capture()); - assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); - assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "update"); - assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); - assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); - assertEquals(((KuzzleMap) arg.getValue()).getNumber("retryOnConflict"), null); - assertEquals(((KuzzleMap) arg.getValue()).getBoolean("waitForRefresh"), null); - assertEquals(((KuzzleMap) arg.getValue()).getBoolean("source"), null); - assertEquals(((ConcurrentHashMap) (((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); - } + assertEquals(((KuzzleMap) arg.getValue()).getString("controller"), "document"); + assertEquals(((KuzzleMap) arg.getValue()).getString("action"), "update"); + assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data"); + assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id"); + assertEquals(((KuzzleMap) arg.getValue()).getNumber("retryOnConflict"), null); + assertEquals(((KuzzleMap) arg.getValue()).getBoolean("waitForRefresh"), null); + assertEquals(((KuzzleMap) arg.getValue()).getBoolean("source"), null); + assertEquals(((ConcurrentHashMap) (((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann"); + } - @Test(expected = NotConnectedException.class) - public void updateShouldThrowWhenNotConnected() throws NotConnectedException, InternalException { + @Test(expected = NotConnectedException.class) + public void updateShouldThrowWhenNotConnected() throws NotConnectedException, InternalException { - AbstractProtocol fakeNetworkProtocol = Mockito.mock(WebSocket.class); - Mockito.when(fakeNetworkProtocol.getState()).thenAnswer((Answer) invocation -> ProtocolState.CLOSE); + 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"; - String id = "some-id"; + Kuzzle kuzzleMock = spy(new Kuzzle(fakeNetworkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; + String id = "some-id"; - ConcurrentHashMap document = new ConcurrentHashMap<>(); - document.put("name", "Yoann"); + ConcurrentHashMap document = new ConcurrentHashMap<>(); + document.put("name", "Yoann"); - kuzzleMock.getDocumentController().update(index, collection, id, document); - } + kuzzleMock.getDocumentController().update(index, collection, id, document); + } @Test public void mCreateDocumentTestA() throws NotConnectedException, InternalException { @@ -298,8 +298,8 @@ public void mCreateDocumentTestA() throws NotConnectedException, InternalExcepti assertEquals((arg.getValue()).getString("action"), "mCreate"); assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); assertEquals((arg.getValue()).getBoolean("waitForRefresh"), null); - assertEquals(((ArrayList>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(0).get("_id").toString(), "some-id1"); - assertEquals(((ArrayList>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(1).get("_id").toString(), "some-id2"); + assertEquals(((ArrayList>) (((KuzzleMap) (arg.getValue()).get("body"))).get("documents")).get(0).get("_id").toString(), "some-id1"); + assertEquals(((ArrayList>) (((KuzzleMap) (arg.getValue()).get("body"))).get("documents")).get(1).get("_id").toString(), "some-id2"); } @Test @@ -334,8 +334,8 @@ public void mCreateDocumentTestB() throws NotConnectedException, InternalExcepti assertEquals((arg.getValue()).getString("action"), "mCreate"); assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); assertEquals((arg.getValue()).getBoolean("waitForRefresh"), false); - assertEquals(((ArrayList>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(0).get("_id").toString(), "some-id1"); - assertEquals(((ArrayList>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(1).get("_id").toString(), "some-id2"); + assertEquals(((ArrayList>) (((KuzzleMap) (arg.getValue()).get("body"))).get("documents")).get(0).get("_id").toString(), "some-id1"); + assertEquals(((ArrayList>) (((KuzzleMap) (arg.getValue()).get("body"))).get("documents")).get(1).get("_id").toString(), "some-id2"); } @Test(expected = NotConnectedException.class) @@ -615,8 +615,8 @@ public void mReplaceDocumentTestA() throws NotConnectedException, InternalExcept assertEquals((arg.getValue()).getString("action"), "mReplace"); assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); assertEquals((arg.getValue()).getBoolean("waitForRefresh"), null); - assertEquals(((ArrayList>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(0).get("_id").toString(), "some-id1"); - assertEquals(((ArrayList>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(1).get("_id").toString(), "some-id2"); + assertEquals(((ArrayList>) (((KuzzleMap) (arg.getValue()).get("body"))).get("documents")).get(0).get("_id").toString(), "some-id1"); + assertEquals(((ArrayList>) (((KuzzleMap) (arg.getValue()).get("body"))).get("documents")).get(1).get("_id").toString(), "some-id2"); } @Test @@ -651,8 +651,8 @@ public void mReplaceDocumentTestB() throws NotConnectedException, InternalExcept assertEquals((arg.getValue()).getString("action"), "mReplace"); assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); assertEquals((arg.getValue()).getBoolean("waitForRefresh"), false); - assertEquals(((ArrayList>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(0).get("_id").toString(), "some-id1"); - assertEquals(((ArrayList>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(1).get("_id").toString(), "some-id2"); + assertEquals(((ArrayList>) (((KuzzleMap) (arg.getValue()).get("body"))).get("documents")).get(0).get("_id").toString(), "some-id1"); + assertEquals(((ArrayList>) (((KuzzleMap) (arg.getValue()).get("body"))).get("documents")).get(1).get("_id").toString(), "some-id2"); } @Test(expected = NotConnectedException.class) @@ -713,7 +713,7 @@ public void existsDocumentShouldThrowWhenNotConnected() throws NotConnectedExcep kuzzleMock.getDocumentController().exists(index, collection, "some-id"); } - + @Test public void mCreateOrReplaceDocumentTestA() throws NotConnectedException, InternalException { @@ -747,8 +747,8 @@ public void mCreateOrReplaceDocumentTestA() throws NotConnectedException, Intern assertEquals((arg.getValue()).getString("action"), "mCreateOrReplace"); assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); assertEquals((arg.getValue()).getBoolean("waitForRefresh"), null); - assertEquals(((ArrayList>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(0).get("_id").toString(), "some-id1"); - assertEquals(((ArrayList>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(1).get("_id").toString(), "some-id2"); + assertEquals(((ArrayList>) (((KuzzleMap) (arg.getValue()).get("body"))).get("documents")).get(0).get("_id").toString(), "some-id1"); + assertEquals(((ArrayList>) (((KuzzleMap) (arg.getValue()).get("body"))).get("documents")).get(1).get("_id").toString(), "some-id2"); } @Test @@ -783,8 +783,8 @@ public void mCreateOrReplaceDocumentTestB() throws NotConnectedException, Intern assertEquals((arg.getValue()).getString("action"), "mCreateOrReplace"); assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); assertEquals((arg.getValue()).getBoolean("waitForRefresh"), false); - assertEquals(((ArrayList>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(0).get("_id").toString(), "some-id1"); - assertEquals(((ArrayList>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(1).get("_id").toString(), "some-id2"); + assertEquals(((ArrayList>) (((KuzzleMap) (arg.getValue()).get("body"))).get("documents")).get(0).get("_id").toString(), "some-id1"); + assertEquals(((ArrayList>) (((KuzzleMap) (arg.getValue()).get("body"))).get("documents")).get(1).get("_id").toString(), "some-id2"); } @Test(expected = NotConnectedException.class) @@ -814,4 +814,69 @@ public void mCreateOrReplaceDocumentShouldThrowWhenNotConnected() throws NotConn kuzzleMock.getDocumentController().mCreateOrReplace(index, collection, documents); } + + @Test + public void deleteByQueryDocumentTestA() throws NotConnectedException, InternalException { + + Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; + + ConcurrentHashMap searchQuery = new ConcurrentHashMap<>(); + ConcurrentHashMap match = new ConcurrentHashMap<>(); + match.put("Hello", "Clarisse"); + searchQuery.put("match", match); + + ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); + + kuzzleMock.getDocumentController().deleteByQuery(index, collection, searchQuery); + Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture()); + + assertEquals((arg.getValue()).getString("controller"), "document"); + assertEquals((arg.getValue()).getString("action"), "deleteByQuery"); + assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); + assertEquals((arg.getValue()).getBoolean("waitForRefresh"), null); + assertEquals(((ConcurrentHashMap) ((ConcurrentHashMap) (((KuzzleMap) (arg.getValue()).get("body"))).get("query")).get("match")).get("Hello"), "Clarisse"); + } + + @Test + public void deleteByQueryDocumentTestB() throws NotConnectedException, InternalException { + + Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; + + ConcurrentHashMap searchQuery = new ConcurrentHashMap<>(); + ConcurrentHashMap match = new ConcurrentHashMap<>(); + match.put("Hello", "Clarisse"); + searchQuery.put("match", match); + + ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); + + kuzzleMock.getDocumentController().deleteByQuery(index, collection, searchQuery, false); + Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture()); + + assertEquals((arg.getValue()).getString("controller"), "document"); + assertEquals((arg.getValue()).getString("action"), "deleteByQuery"); + assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); + assertEquals((arg.getValue()).getBoolean("waitForRefresh"), false); + assertEquals(((ConcurrentHashMap) ((ConcurrentHashMap) (((KuzzleMap) (arg.getValue()).get("body"))).get("query")).get("match")).get("Hello"), "Clarisse"); + } + + @Test(expected = NotConnectedException.class) + public void deleteByQueryDocumentShouldThrowWhenNotConnected() 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"; + + ConcurrentHashMap searchQuery = new ConcurrentHashMap<>(); + ConcurrentHashMap match = new ConcurrentHashMap<>(); + match.put("Hello", "Clarisse"); + searchQuery.put("match", match); + + kuzzleMock.getDocumentController().deleteByQuery(index, collection, searchQuery); + } } From b5f5adada88751a3b09f782e16fbdd09c2854c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Cottinet?= Date: Fri, 27 Mar 2020 13:21:26 +0100 Subject: [PATCH 2/3] line wrap --- .../document/delete-by-query/snippets/delete-by-query.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/3/controllers/document/delete-by-query/snippets/delete-by-query.java b/doc/3/controllers/document/delete-by-query/snippets/delete-by-query.java index 295c5079..5f9460cf 100644 --- a/doc/3/controllers/document/delete-by-query/snippets/delete-by-query.java +++ b/doc/3/controllers/document/delete-by-query/snippets/delete-by-query.java @@ -2,4 +2,7 @@ ConcurrentHashMap match = new ConcurrentHashMap<>(); match.put("capacity", 4); searchQuery.put("match", match); - ArrayList result = kuzzle.getDocumentController().deleteByQuery("nyc-open-data", "yellow-taxi", searchQuery).get(); \ No newline at end of file + ArrayList result = kuzzle + .getDocumentController() + .deleteByQuery("nyc-open-data", "yellow-taxi", searchQuery) + .get(); From bba99f4ac2c805610a531a64ab2e48eb354f9cd6 Mon Sep 17 00:00:00 2001 From: Yoann Abbes Date: Thu, 2 Apr 2020 14:28:19 +0200 Subject: [PATCH 3/3] requested changes [ci skip] --- .../snippets/delete-by-query.java | 4 +- .../API/Controllers/DocumentController.java | 100 ++++++------- .../DocumentTest/DocumentTest.java | 132 +++++++++--------- 3 files changed, 121 insertions(+), 115 deletions(-) diff --git a/doc/3/controllers/document/delete-by-query/snippets/delete-by-query.java b/doc/3/controllers/document/delete-by-query/snippets/delete-by-query.java index 5f9460cf..46b10a9a 100644 --- a/doc/3/controllers/document/delete-by-query/snippets/delete-by-query.java +++ b/doc/3/controllers/document/delete-by-query/snippets/delete-by-query.java @@ -1,7 +1,9 @@ ConcurrentHashMap searchQuery = new ConcurrentHashMap<>(); + ConcurrentHashMap query = new ConcurrentHashMap<>(); ConcurrentHashMap match = new ConcurrentHashMap<>(); match.put("capacity", 4); - searchQuery.put("match", match); + query.put("match", match); + searchQuery.put("query", query); ArrayList result = kuzzle .getDocumentController() .deleteByQuery("nyc-open-data", "yellow-taxi", searchQuery) 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 897bce95..41319fec 100644 --- a/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java +++ b/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java @@ -594,54 +594,54 @@ public CompletableFuture>> mCreateOr return this.mCreateOrReplace(index, collection, documents, null); } - /** - * Deletes documents matching the provided search query. - * - * @param index - * @param collection - * @param searchQuery - * @param waitForRefresh - * @return a CompletableFuture - * @throws NotConnectedException - * @throws InternalException - */ - public CompletableFuture> deleteByQuery( - final String index, - final String collection, - final ConcurrentHashMap searchQuery, - final Boolean waitForRefresh) throws NotConnectedException, InternalException { - - final KuzzleMap query = new KuzzleMap(); - - query - .put("index", index) - .put("collection", collection) - .put("controller", "document") - .put("action", "deleteByQuery") - .put("body", new KuzzleMap().put("query", searchQuery)) - .put("waitForRefresh", waitForRefresh); - - return kuzzle - .query(query) - .thenApplyAsync( - (response) -> (ArrayList)((ConcurrentHashMap) response.result).get("ids")); - } - - /** - * Deletes documents matching the provided search query. - * - * @param index - * @param collection - * @param searchQuery - * @return a CompletableFuture - * @throws NotConnectedException - * @throws InternalException - */ - public CompletableFuture> deleteByQuery( - final String index, - final String collection, - final ConcurrentHashMap searchQuery) throws NotConnectedException, InternalException { - - return this.deleteByQuery(index, collection, searchQuery, null); - } +// /** +// * Deletes documents matching the provided search query. +// * +// * @param index +// * @param collection +// * @param searchQuery +// * @param waitForRefresh +// * @return a CompletableFuture +// * @throws NotConnectedException +// * @throws InternalException +// */ +// public CompletableFuture> deleteByQuery( +// final String index, +// final String collection, +// final ConcurrentHashMap searchQuery, +// final Boolean waitForRefresh) throws NotConnectedException, InternalException { +// +// final KuzzleMap query = new KuzzleMap(); +// +// query +// .put("index", index) +// .put("collection", collection) +// .put("controller", "document") +// .put("action", "deleteByQuery") +// .put("body", new KuzzleMap(searchQuery)) +// .put("waitForRefresh", waitForRefresh); +// +// return kuzzle +// .query(query) +// .thenApplyAsync( +// (response) -> (ArrayList)((ConcurrentHashMap) response.result).get("ids")); +// } +// +// /** +// * Deletes documents matching the provided search query. +// * +// * @param index +// * @param collection +// * @param searchQuery +// * @return a CompletableFuture +// * @throws NotConnectedException +// * @throws InternalException +// */ +// public CompletableFuture> deleteByQuery( +// final String index, +// final String collection, +// final ConcurrentHashMap searchQuery) throws NotConnectedException, InternalException { +// +// return this.deleteByQuery(index, collection, searchQuery, null); +// } } 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 5b195598..c5ea17bf 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 @@ -815,68 +815,72 @@ public void mCreateOrReplaceDocumentShouldThrowWhenNotConnected() throws NotConn kuzzleMock.getDocumentController().mCreateOrReplace(index, collection, documents); } - @Test - public void deleteByQueryDocumentTestA() throws NotConnectedException, InternalException { - - Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); - String index = "nyc-open-data"; - String collection = "yellow-taxi"; - - ConcurrentHashMap searchQuery = new ConcurrentHashMap<>(); - ConcurrentHashMap match = new ConcurrentHashMap<>(); - match.put("Hello", "Clarisse"); - searchQuery.put("match", match); - - ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); - - kuzzleMock.getDocumentController().deleteByQuery(index, collection, searchQuery); - Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture()); - - assertEquals((arg.getValue()).getString("controller"), "document"); - assertEquals((arg.getValue()).getString("action"), "deleteByQuery"); - assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); - assertEquals((arg.getValue()).getBoolean("waitForRefresh"), null); - assertEquals(((ConcurrentHashMap) ((ConcurrentHashMap) (((KuzzleMap) (arg.getValue()).get("body"))).get("query")).get("match")).get("Hello"), "Clarisse"); - } - - @Test - public void deleteByQueryDocumentTestB() throws NotConnectedException, InternalException { - - Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); - String index = "nyc-open-data"; - String collection = "yellow-taxi"; - - ConcurrentHashMap searchQuery = new ConcurrentHashMap<>(); - ConcurrentHashMap match = new ConcurrentHashMap<>(); - match.put("Hello", "Clarisse"); - searchQuery.put("match", match); - - ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); - - kuzzleMock.getDocumentController().deleteByQuery(index, collection, searchQuery, false); - Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture()); - - assertEquals((arg.getValue()).getString("controller"), "document"); - assertEquals((arg.getValue()).getString("action"), "deleteByQuery"); - assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); - assertEquals((arg.getValue()).getBoolean("waitForRefresh"), false); - assertEquals(((ConcurrentHashMap) ((ConcurrentHashMap) (((KuzzleMap) (arg.getValue()).get("body"))).get("query")).get("match")).get("Hello"), "Clarisse"); - } - - @Test(expected = NotConnectedException.class) - public void deleteByQueryDocumentShouldThrowWhenNotConnected() 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"; - - ConcurrentHashMap searchQuery = new ConcurrentHashMap<>(); - ConcurrentHashMap match = new ConcurrentHashMap<>(); - match.put("Hello", "Clarisse"); - searchQuery.put("match", match); - - kuzzleMock.getDocumentController().deleteByQuery(index, collection, searchQuery); - } +// @Test +// public void deleteByQueryDocumentTestA() throws NotConnectedException, InternalException { +// +// Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); +// String index = "nyc-open-data"; +// String collection = "yellow-taxi"; +// +// ConcurrentHashMap searchQuery = new ConcurrentHashMap<>(); +// ConcurrentHashMap query = new ConcurrentHashMap<>(); +// ConcurrentHashMap match = new ConcurrentHashMap<>(); +// match.put("Hello", "Clarisse"); +// query.put("match", match); +// searchQuery.put("query", query); +// +// ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); +// +// kuzzleMock.getDocumentController().deleteByQuery(index, collection, searchQuery); +// Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture()); +// +// assertEquals((arg.getValue()).getString("controller"), "document"); +// assertEquals((arg.getValue()).getString("action"), "deleteByQuery"); +// assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); +// assertEquals((arg.getValue()).getBoolean("waitForRefresh"), null); +// assertEquals(((ConcurrentHashMap) ((ConcurrentHashMap) (((KuzzleMap) (arg.getValue()).get("body"))).get("query")).get("match")).get("Hello"), "Clarisse"); +// } +// +// @Test +// public void deleteByQueryDocumentTestB() throws NotConnectedException, InternalException { +// +// Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); +// String index = "nyc-open-data"; +// String collection = "yellow-taxi"; +// +// ConcurrentHashMap searchQuery = new ConcurrentHashMap<>(); +// ConcurrentHashMap query = new ConcurrentHashMap<>(); +// ConcurrentHashMap match = new ConcurrentHashMap<>(); +// match.put("Hello", "Clarisse"); +// query.put("match", match); +// searchQuery.put("query", query); +// +// ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); +// +// kuzzleMock.getDocumentController().deleteByQuery(index, collection, searchQuery, false); +// Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture()); +// +// assertEquals((arg.getValue()).getString("controller"), "document"); +// assertEquals((arg.getValue()).getString("action"), "deleteByQuery"); +// assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); +// assertEquals((arg.getValue()).getBoolean("waitForRefresh"), false); +// assertEquals(((ConcurrentHashMap) ((ConcurrentHashMap) (((KuzzleMap) (arg.getValue()).get("body"))).get("query")).get("match")).get("Hello"), "Clarisse"); +// } +// +// @Test(expected = NotConnectedException.class) +// public void deleteByQueryDocumentShouldThrowWhenNotConnected() 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"; +// +// ConcurrentHashMap searchQuery = new ConcurrentHashMap<>(); +// ConcurrentHashMap match = new ConcurrentHashMap<>(); +// match.put("Hello", "Clarisse"); +// searchQuery.put("match", match); +// +// kuzzleMock.getDocumentController().deleteByQuery(index, collection, searchQuery); +// } }