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
64 changes: 64 additions & 0 deletions doc/3/controllers/collection/create/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
code: true
type: page
title: create
description: Creates a new collection
---

# create

Creates a new [collection](/core/2/guides/essentials/store-access-data) in Kuzzle via the persistence engine, in the provided index.

You can also provide an optional data mapping that allows you to exploit the full capabilities of our
persistent data storage layer, [ElasticSearch](https://www.elastic.co/elastic-stack) (check here the [mapping capabilities of ElasticSearch](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/mapping.html)).

This method will only update the mapping if the collection already exists.

<br/>

---

## Arguments

```java
public CompletableFuture<Void> create(
final String index,
final String collection,
final ConcurrentHashMap<String, Object> mapping)
throws NotConnectedException, InternalException

public CompletableFuture<Void> create(
final String index,
final String collection)
throws NotConnectedException, InternalException
```

| Arguments | Type | Description |
| ------------------ | -------------------------------------------- | --------------------------------- |
| `index` | <pre>String</pre> | Index |
| `collection` | <pre>String</pre> | Collection |
| `mapping` | <pre>ConcurrentHashMap<String, Object></pre> | Describes the data mapping to associate to the new collection, using Elasticsearch [mapping format](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/mapping.html) |

---

### mapping

A `ConcurrentHashMap<String, Object>` representing the data mapping of the collection.

The mapping must have a root field `properties` that contain the mapping definition:

```java
ConcurrentHashMap<String, Object> mapping = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> properties = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> field = new ConcurrentHashMap<>();

field.put("type", "keyword");
properties.put("field", field);
mapping.put("properties", properties);
```

More information about database mappings [here](/core/2/guides/essentials/database-mappings).

## Usage

<<< ./snippets/create.java
10 changes: 10 additions & 0 deletions doc/3/controllers/collection/create/snippets/create.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ConcurrentHashMap<String, Object> mapping = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> properties = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> license = new ConcurrentHashMap<>();

license.put("type", "keyword");
properties.put("license", license);
mapping.put("properties", properties);

kuzzle.getCollectionController().create("nyc-open-data", "yellow-taxi", mapping)
.get();
9 changes: 9 additions & 0 deletions doc/3/controllers/collection/create/snippets/create.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: collection#create
description: Creates a new collection
hooks:
before: |
curl -XDELETE kuzzle:7512/nyc-open-data
curl -XPOST kuzzle:7512/nyc-open-data/_create
after:
template: default
expected: Success
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ hooks:
after:
template: print-result-array
expected:
- "id=some-id, status=200"
- "id=some-id2, status=200"
- "id=some-id, _version=2, status=200"
- "id=some-id2, _version=2, status=200"
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,51 @@ public CompletableFuture<Boolean> exists(
(response) -> (Boolean) response.result);
}

/**
* Creates a collection in a given index.
*
* @param index
* @param collection
* @param mappings
* @return a CompletableFuture
* @throws NotConnectedException
* @throws InternalException
*/
public CompletableFuture<Void> create(
final String index,
final String collection,
final ConcurrentHashMap<String, Object> mappings) throws NotConnectedException, InternalException {

final KuzzleMap query = new KuzzleMap();

query
.put("index", index)
.put("collection", collection)
.put("controller", "collection")
.put("action", "create")
.put("body", mappings != null ? new KuzzleMap(mappings) : null);

return kuzzle
.query(query)
.thenApplyAsync((response) -> null);
}

/**
* Creates a collection in a given index.
*
* @param index
* @param collection
* @return a CompletableFuture
* @throws NotConnectedException
* @throws InternalException
*/
public CompletableFuture<Void> create(
final String index,
final String collection) throws NotConnectedException, InternalException {

return this.create(index, collection, null);
}

/**
* Get collection mapping
*
Expand Down
68 changes: 68 additions & 0 deletions src/test/java/io/kuzzle/test/API/Controllers/CollectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;

import java.util.concurrent.ConcurrentHashMap;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.spy;

Expand Down Expand Up @@ -50,6 +52,72 @@ public void existsCollectionShouldThrowWhenNotConnected() throws NotConnectedExc
kuzzleMock.getCollectionController().exists(index, collection);
}

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

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

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

kuzzleMock.getCollectionController().create(index, collection);
Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture());

assertEquals((arg.getValue()).getString("controller"), "collection");
assertEquals((arg.getValue()).getString("action"), "create");
assertEquals((arg.getValue()).getString("collection"), "yellow-taxi");
assertEquals((arg.getValue()).getString("index"), "nyc-open-data");
}

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

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

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

ConcurrentHashMap<String, Object> mapping = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> properties = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> license = new ConcurrentHashMap<>();

license.put("type", "keyword");
properties.put("license", license);
mapping.put("properties", properties);

kuzzleMock.getCollectionController().create(index, collection, mapping);
Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture());

assertEquals((arg.getValue()).getString("controller"), "collection");
assertEquals((arg.getValue()).getString("action"), "create");
assertEquals((arg.getValue()).getString("index"), "nyc-open-data");
assertEquals((arg.getValue()).getString("collection"), "yellow-taxi");
assertEquals((
(ConcurrentHashMap<String, Object>) (
(ConcurrentHashMap<String, Object>) (
((ConcurrentHashMap<String, Object>)
((arg.getValue())
.get("body")))
.get("properties")))
.get("license"))
.get("type").toString(), "keyword");
}

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

kuzzleMock.getCollectionController().create(index, collection);
}

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

Expand Down