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
55 changes: 55 additions & 0 deletions doc/3/controllers/document/create-or-replace/index.md
Original file line number Diff line number Diff line change
@@ -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<ConcurrentHashMap<String, Object>> createOrReplace(
final String index,
final String collection,
final String id,
final ConcurrentHashMap<String, Object> document)
throws NotConnectedException, InternalException

public CompletableFuture<ConcurrentHashMap<String, Object>> createOrReplace(
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> | Document content |
| `waitForRefresh` | <pre>Boolean</pre> | If set to `true`, Kuzzle will wait for the persistence layer to finish indexing|
Comment thread
scottinet marked this conversation as resolved.

---

## 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/create-or-replace.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

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

ConcurrentHashMap<String, Object> 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
}
*/
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 0 additions & 7 deletions doc/3/controllers/realtime/subscribe/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConcurrentHashMap<String, Object>> createOrReplace(
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", "createOrReplace")
.put("body", document)
.put("_id", id)
.put("waitForRefresh", waitForRefresh);

return kuzzle
.query(query)
.thenApplyAsync(
(response) -> (ConcurrentHashMap<String, Object>) 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<ConcurrentHashMap<String, Object>> createOrReplace(
final String index,
final String collection,
final String id,
final ConcurrentHashMap<String, Object> document) throws NotConnectedException, InternalException {

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

/**
* Creates a document in a given collection and index.
*
* @param index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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().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<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 createOrReplaceDocumentTestB() 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().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<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 createOrReplaceDocumentShouldThrowWhenNotConnected() 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().createOrReplace(index, collection, "some-id", document);
}

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

Expand Down