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
70 changes: 70 additions & 0 deletions doc/3/controllers/document/m-create/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
code: true
type: page
title: mCreate
description: Creates multiple documents
---

# mCreate

Creates multiple documents.

---

## Arguments

```java
public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> mCreate(
final String index,
final String collection,
final ArrayList<ConcurrentHashMap<String, Object>> documents)
throws NotConnectedException, InternalException

public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> mCreate(
final String index,
final String collection,
final ArrayList<ConcurrentHashMap<String, Object>> documents,
final Boolean waitForRefresh)
throws NotConnectedException, InternalException
```

| Arguments | Type | Description |
| ------------------ | ------------------------------------------------------- | --------------------------------- |
| `index` | <pre>String</pre> | Index |
| `collection` | <pre>String</pre> | Collection |
| `documents` | <pre>ArrayList<ConcurrentHashMap<String, Object>></pre> | ArrayList containing the documents to create |
| `waitForRefresh` | <pre>Boolean</pre> | If set to `true`, Kuzzle will wait for the persistence layer to finish indexing |

---

### documents

Each document has the following properties:

| Arguments | Type | Description |
| ------------------ | -------------------------------------------- | --------------------------------- |
| `_id` | <pre>String</pre> | Optional document ID. Will be auto-generated if not defined. |
| `body` | <pre>ConcurrentHashMap<String, Object></pre> | Document body |

## Return

A `ConcurrentHashMap<String, ArrayList<Object>>` which has a `successes` and `errors` `ArrayList<Object>`:
Each created document is an object of the `successes` array with the following properties:
Comment thread
scottinet marked this conversation as resolved.

| Property | Type | Description |
|------------- |--------------------------------------------- |--------------------------------- |
| `_source` | <pre>ConcurrentHashMap<String, Object></pre> | Created document |
| `_id` | <pre>String</pre> | ID of the newly created document |
| `_version` | <pre>Integer</pre> | Version of the document in the persistent data storage |

Each errored document is an object of the `errors` array with the following properties:

| Property | Type | Description |
|------------- |--------------------------------------------- |--------------------------------- |
| `document` | <pre>ConcurrentHashMap<String, Object></pre> | Document that causes the error |
| `status` | <pre>Integer</pre> | HTTP error status |
| `reason` | <pre>String</pre> | Human readable reason |

## Usage

<<< ./snippets/m-create.java
54 changes: 54 additions & 0 deletions doc/3/controllers/document/m-create/snippets/m-create.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
ConcurrentHashMap<String, Object> document1 = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> document2 = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> body = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> body2 = new ConcurrentHashMap<>();

body.put("Agent", "Smith");
body2.put("Gordon", "Freeman");

document1.put("_id", "some-id");
document1.put("body", body);

document2.put("_id", "some-id2");
document2.put("body", body2);

final ArrayList<ConcurrentHashMap<String, Object>> documents = new ArrayList<>();
documents.add(document1);
documents.add(document2);

ConcurrentHashMap<String, ArrayList<Object>> result = kuzzle
.getDocumentController()
.mCreate("nyc-open-data", "yellow-taxi", documents)
.get();

/*
result =
{
successes=
[
{
result=created,
_source=
{
Agent=Smith,
_kuzzle_info={createdAt=1582892842099, author=-1}
},
_id=some-id,
_version=1,
status=201
},
{
result=created,
_source=
{
Gordon=Freeman,
_kuzzle_info={createdAt=1582892842099, author=-1}
},
_id=some-id2,
_version=1,
status=201
}
],
errors=[]
}
*/
12 changes: 12 additions & 0 deletions doc/3/controllers/document/m-create/snippets/m-create.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: document#mCreate
description: Creates multiple documents
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-array
expected:
- "id=some-id, _version=1, status=201"
- "id=some-id2, _version=1, status=201"
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,56 @@ public CompletableFuture<ConcurrentHashMap<String, Object>> create(
return this.create(index, collection, document, null);
}

/**
* Creates multiple documents in a given collection and index.
*
* @param index
* @param collection
* @param documents
* @param waitForRefresh
* @return a CompletableFuture
* @throws NotConnectedException
* @throws InternalException
*/
public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> mCreate(
final String index,
final String collection,
final ArrayList<ConcurrentHashMap<String, Object>> documents,
final Boolean waitForRefresh) throws NotConnectedException, InternalException {

final KuzzleMap query = new KuzzleMap();
query
.put("index", index)
.put("collection", collection)
.put("controller", "document")
.put("action", "mCreate")
.put("body", new KuzzleMap().put("documents", documents))
.put("waitForRefresh", waitForRefresh);

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

/**
* Creates multiple documents in a given collection and index.
*
* @param index
* @param collection
* @param documents
* @return a CompletableFuture
* @throws NotConnectedException
* @throws InternalException
*/
public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> mCreate(
final String index,
final String collection,
final ArrayList<ConcurrentHashMap<String, Object>> documents) throws NotConnectedException, InternalException {

return this.mCreate(index, collection, documents, null);
}

/**
* Deletes multiple documents.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,107 @@ public void createDocumentThrowWhenNotConnected() throws NotConnectedException,
}


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

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

ConcurrentHashMap<String, Object> document1 = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> document2 = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> body1 = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> body2 = new ConcurrentHashMap<>();

document1.put("_id", "some-id1");
body1.put("key1", "value1");
document1.put("body", body1);

document2.put("_id", "some-id2");
body2.put("key2", "value2");
document2.put("body", body2);

final ArrayList<ConcurrentHashMap<String, Object>> documents = new ArrayList<>();
documents.add(document1);
documents.add(document2);

ArgumentCaptor<KuzzleMap> arg = ArgumentCaptor.forClass(KuzzleMap.class);

kuzzleMock.getDocumentController().mCreate(index, collection, documents);
Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture());

assertEquals((arg.getValue()).getString("controller"), "document");
assertEquals((arg.getValue()).getString("action"), "mCreate");
assertEquals((arg.getValue()).getString("index"), "nyc-open-data");
assertEquals((arg.getValue()).getBoolean("waitForRefresh"), null);
assertEquals(((ArrayList<ConcurrentHashMap<String, Object>>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(0).get("_id").toString(), "some-id1");
assertEquals(((ArrayList<ConcurrentHashMap<String, Object>>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(1).get("_id").toString(), "some-id2");
}

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

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

ConcurrentHashMap<String, Object> document1 = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> document2 = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> body = new ConcurrentHashMap<>();

document1.put("_id", "some-id1");
body.put("key1", "value1");
document1.put("body", body);

document2.put("_id", "some-id2");
body.put("key2", "value2");
document2.put("body", body);

final ArrayList<ConcurrentHashMap<String, Object>> documents = new ArrayList<>();
documents.add(document1);
documents.add(document2);

ArgumentCaptor<KuzzleMap> arg = ArgumentCaptor.forClass(KuzzleMap.class);

kuzzleMock.getDocumentController().mCreate(index, collection, documents, false);
Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture());

assertEquals((arg.getValue()).getString("controller"), "document");
assertEquals((arg.getValue()).getString("action"), "mCreate");
assertEquals((arg.getValue()).getString("index"), "nyc-open-data");
assertEquals((arg.getValue()).getBoolean("waitForRefresh"), false);
assertEquals(((ArrayList<ConcurrentHashMap<String, Object>>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(0).get("_id").toString(), "some-id1");
assertEquals(((ArrayList<ConcurrentHashMap<String, Object>>)(((KuzzleMap)(arg.getValue()).get("body"))).get("documents")).get(1).get("_id").toString(), "some-id2");
}

@Test(expected = NotConnectedException.class)
public void mCreateDocumentShouldThrowWhenNotConnected() 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> document1 = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> document2 = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> body = new ConcurrentHashMap<>();

document1.put("_id", "some-id1");
body.put("key1", "value1");
document1.put("body", body);

document2.put("_id", "some-id2");
body.put("key2", "value2");
document2.put("body", body);

final ArrayList<ConcurrentHashMap<String, Object>> documents = new ArrayList<>();
documents.add(document1);
documents.add(document2);

kuzzleMock.getDocumentController().mCreate(index, collection, documents);
}

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

Expand Down