diff --git a/doc/3/controllers/document/create-or-replace/index.md b/doc/3/controllers/document/create-or-replace/index.md new file mode 100644 index 00000000..1a3ef99f --- /dev/null +++ b/doc/3/controllers/document/create-or-replace/index.md @@ -0,0 +1,55 @@ +--- +code: true +type: page +title: createOrReplace +description: Creates or replaces a document +--- + +# createOrReplace + +Creates a new document in the persistent data storage, or replaces its content if it already exists. + +--- + +## Arguments + +```java +public CompletableFuture> createOrReplace( + final String index, + final String collection, + final String id, + final ConcurrentHashMap document) +throws NotConnectedException, InternalException + +public CompletableFuture> createOrReplace( + final String index, + final String collection, + final String id, + final ConcurrentHashMap document, + final Boolean waitForRefresh) +throws NotConnectedException, InternalException +``` + +| Arguments | Type | Description | +| ------------------ | -------------------------------------------- | --------------------------------- | +| `index` |
String
| Index | +| `collection` |
String
| Collection | +| `id` |
String
| Document ID | +| `document` |
ConcurrentHashMap
| Document content | +| `waitForRefresh` |
Boolean
| If set to `true`, Kuzzle will wait for the persistence layer to finish indexing| + +--- + +## Return + +A `ConcurrentHashMap` which has the following properties: + +| Property | Type | Description | +|------------- |----------------------------- |--------------------------------- | +| `_source` |
ConcurrentHashMap
| Document content | +| `_id` |
String
| ID of the document | +| `_version` |
Integer
| Version of the document in the persistent data storage | + +## Usage + +<<< ./snippets/create-or-replace.java diff --git a/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java new file mode 100644 index 00000000..f88a3796 --- /dev/null +++ b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.java @@ -0,0 +1,25 @@ + + ConcurrentHashMap document = new ConcurrentHashMap<>(); + document.put("firstname", "John"); + + ConcurrentHashMap result = kuzzle + .getDocumentController() + .createOrReplace("nyc-open-data", "yellow-taxi", "some-id", document) + .get(); + +/* + { + _source= + { + firstname=John, + _kuzzle_info={ + createdAt=1582892323254, + author=-1, + updatedAt=1582892323254, + updater=-1 + } + }, + _id=some-id, + _version=1 + } + */ diff --git a/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml new file mode 100644 index 00000000..fe3e45b8 --- /dev/null +++ b/doc/3/controllers/document/create-or-replace/snippets/create-or-replace.test.yml @@ -0,0 +1,10 @@ +name: document#createOrReplace +description: Creates or Replace 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 + after: +template: print-result +expected: "firstname=John" \ No newline at end of file diff --git a/doc/3/controllers/realtime/subscribe/index.md b/doc/3/controllers/realtime/subscribe/index.md index bcba6c66..24feb86b 100644 --- a/doc/3/controllers/realtime/subscribe/index.md +++ b/doc/3/controllers/realtime/subscribe/index.md @@ -52,10 +52,3 @@ _Subscription to document notifications with scope option_ <<< ./snippets/document-notifications-leave-scope.java -_Subscription to message notifications_ - -<<< ./snippets/message-notifications.java - -_Subscription to user notifications_ - -<<< ./snippets/user-notifications.java 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 c3c97cd6..4e87cd9f 100644 --- a/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java +++ b/src/main/java/io/kuzzle/sdk/API/Controllers/DocumentController.java @@ -16,6 +16,62 @@ public DocumentController(final Kuzzle kuzzle) { } /** + * Creates or Replace a document in a given collection and index. + * + * @param index + * @param collection + * @param id + * @param document + * @param waitForRefresh + * @return a CompletableFuture + * @throws NotConnectedException + * @throws InternalException + */ + public CompletableFuture> createOrReplace( + final String index, + final String collection, + final String id, + final ConcurrentHashMap document, + final Boolean waitForRefresh) throws NotConnectedException, InternalException { + + final KuzzleMap query = new KuzzleMap(); + + query + .put("index", index) + .put("collection", collection) + .put("controller", "document") + .put("action", "createOrReplace") + .put("body", document) + .put("_id", id) + .put("waitForRefresh", waitForRefresh); + + return kuzzle + .query(query) + .thenApplyAsync( + (response) -> (ConcurrentHashMap) response.result); + } + + /** + * Creates or Replace a document in a given collection and index. + * + * @param index + * @param collection + * @param id + * @param document + * @return a CompletableFuture + * @throws NotConnectedException + * @throws InternalException + */ + public CompletableFuture> createOrReplace( + final String index, + final String collection, + final String id, + final ConcurrentHashMap document) throws NotConnectedException, InternalException { + + return this.createOrReplace(index, collection, id, document, null); + } + + /** * Creates a document in a given collection and index. * * @param 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 9bf7177c..775ca99a 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 @@ -24,6 +24,72 @@ public class DocumentTest { private AbstractProtocol networkProtocol = Mockito.mock(WebSocket.class); + @Test + public void createOrReplaceDocumentTestA() throws NotConnectedException, InternalException { + + 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"); + + 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()); + + 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 { + + 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"); + + ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); + + 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"); + } + + @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"; + + 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); + } + @Test public void createDocumentTestA() throws NotConnectedException, InternalException {