Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.
Merged
55 changes: 55 additions & 0 deletions doc/3/controllers/document/replace/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
code: true
type: page
title: replace
description: Replaces a document
---

# replace

Replaces the content of an existing document.

---

## Arguments

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

public CompletableFuture<ConcurrentHashMap<String, Object>> replace(
final String index,
final String collection,
final String id,
final ConcurrentHashMap<String, Object> document,
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 |
| `document` | <pre>ConcurrentHashMap<String, Object></pre> | Updated ocument content |
| `waitForRefresh` | <pre>Boolean</pre> | 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` | <pre>ConcurrentHashMap</pre> | Document content |
| `_id` | <pre>String</pre> | ID of the document |
| `_version` | <pre>Integer</pre> | Version of the document in the persistent data storage |

## Usage

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

ConcurrentHashMap<String, Object> document = new ConcurrentHashMap<>();
document.put("firstname", "John");

ConcurrentHashMap<String, Object> result = kuzzle.getDocumentController().replace("nyc-open-data", "yellow-taxi", "some-id", document)
.get();

/*
result =
{
_source=
{
firstname=John,
_kuzzle_info={ createdAt=1582892606555, author=-1, updatedAt=1582892606555, updater=-1 }
},
_id=some-id,
_version=2
}
*/
11 changes: 11 additions & 0 deletions doc/3/controllers/document/replace/snippets/replace.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: document#replace
description: replaces 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: print-result
expected: "firstname=John"
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,62 @@ public DocumentController(final Kuzzle kuzzle) {
super(kuzzle);
}

/**
* 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<ConcurrentHashMap<String, Object>> replace(
final String index,
final String collection,
final String id,
final ConcurrentHashMap<String, Object> document,
final Boolean waitForRefresh) throws NotConnectedException, InternalException {

final KuzzleMap query = new KuzzleMap();

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

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

/**
* 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<ConcurrentHashMap<String, Object>> replace(
final String index,
final String collection,
final String id,
final ConcurrentHashMap<String, Object> document) throws NotConnectedException, InternalException {

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

/**
* Deletes 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,72 @@ public class DocumentTest {

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

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

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

ConcurrentHashMap<String, Object> 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().replace(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"), "replace");
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<String, Object>)(((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann");
assertEquals(((ConcurrentHashMap<String, Object>)(((KuzzleMap) arg.getValue()).get("body"))).get("nickname").toString(), "El angel de la muerte que hace el JAVA");
}

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

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

ConcurrentHashMap<String, Object> 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().replace(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"), "replace");
assertEquals(((KuzzleMap) arg.getValue()).getString("index"), "nyc-open-data");
assertEquals(((KuzzleMap) arg.getValue()).getString("_id"), "some-id");
assertEquals(((KuzzleMap) arg.getValue()).getBoolean("waitForRefresh"), null);
assertEquals(((ConcurrentHashMap<String, Object>)(((KuzzleMap) arg.getValue()).get("body"))).get("name").toString(), "Yoann");
assertEquals(((ConcurrentHashMap<String, Object>)(((KuzzleMap) arg.getValue()).get("body"))).get("nickname").toString(), "El angel de la muerte que hace el JAVA");
}

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

ConcurrentHashMap<String, Object> document = new ConcurrentHashMap<>();
document.put("name", "Yoann");
document.put("nickname", "El angel de la muerte que hace el JAVA");

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

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

Expand Down