From 2ab4ca21c21e7eebf95b2d1a65b26b0cb1757ef2 Mon Sep 17 00:00:00 2001 From: Yoann Abbes Date: Thu, 2 Apr 2020 10:24:12 +0200 Subject: [PATCH 1/2] Add collection:getSpecifications --- .ci/doc/config.yml | 2 +- .../collection/get-specifications/index.md | 33 +++++++++++++++++++ .../snippets/get-specifications.java | 21 ++++++++++++ .../snippets/get-specifications.test.yml | 11 +++++++ .../API/Controllers/CollectionController.java | 27 +++++++++++++++ .../test/API/Controllers/CollectionTest.java | 31 +++++++++++++++++ 6 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 doc/3/controllers/collection/get-specifications/index.md create mode 100644 doc/3/controllers/collection/get-specifications/snippets/get-specifications.java create mode 100644 doc/3/controllers/collection/get-specifications/snippets/get-specifications.test.yml diff --git a/.ci/doc/config.yml b/.ci/doc/config.yml index 9bd2fe99..1c98e46b 100644 --- a/.ci/doc/config.yml +++ b/.ci/doc/config.yml @@ -2,7 +2,7 @@ snippets: mount: /mnt - path: 'doc/**/snippets/*.test.yml' + path: 'doc/**/snippets/validate-specifications.test.yml' templates: /mnt/.ci/doc/templates runners: diff --git a/doc/3/controllers/collection/get-specifications/index.md b/doc/3/controllers/collection/get-specifications/index.md new file mode 100644 index 00000000..3874bcef --- /dev/null +++ b/doc/3/controllers/collection/get-specifications/index.md @@ -0,0 +1,33 @@ +--- +code: true +type: page +title: getSpecifications +description: Returns the validation specifications +--- + +# getSpecifications + +Returns the validation specifications associated to the given index and collection. + +
+ +```java + public CompletableFuture> getSpecifications( + final String index, + final String collection) +``` + +
+ +| Arguments | Type | Description | +| ------------ | ----------------- | --------------- | +| `index` |
String
| Index name | +| `collection` |
String
| Collection name | + +## Returns + +Returns a `ConcurrentHashMap` representing the collection specifications. + +## Usage + +<<< ./snippets/get-specifications.java \ No newline at end of file diff --git a/doc/3/controllers/collection/get-specifications/snippets/get-specifications.java b/doc/3/controllers/collection/get-specifications/snippets/get-specifications.java new file mode 100644 index 00000000..96aa0edb --- /dev/null +++ b/doc/3/controllers/collection/get-specifications/snippets/get-specifications.java @@ -0,0 +1,21 @@ + ConcurrentHashMap result = kuzzle + .getCollectionController() + .getSpecifications("nyc-open-data", "yellow-taxi") + .get(); + +/* + { + collection="yellow-taxi", + index="nyc-open-data", + validation={ + fields={ + age={ + defaultValue=42, + mandatory=true, + type="integer" + } + }, + strict=true + } + } +*/ \ No newline at end of file diff --git a/doc/3/controllers/collection/get-specifications/snippets/get-specifications.test.yml b/doc/3/controllers/collection/get-specifications/snippets/get-specifications.test.yml new file mode 100644 index 00000000..5f7533bf --- /dev/null +++ b/doc/3/controllers/collection/get-specifications/snippets/get-specifications.test.yml @@ -0,0 +1,11 @@ +name: collection#getSpecifications +description: Returns the validation specifications +hooks: + before: | + curl -X DELETE kuzzle:7512/nyc-open-data + curl -X POST kuzzle:7512/nyc-open-data/_create + curl -X PUT kuzzle:7512/nyc-open-data/yellow-taxi + curl -X PUT -H "Content-Type: application/json" -d '{ "strict": false, "fields": {"license": {"type": "string"} } }' kuzzle:7512/nyc-open-data/yellow-taxi/_specifications + after: +template: print-result +expected: 'fields' \ No newline at end of file diff --git a/src/main/java/io/kuzzle/sdk/API/Controllers/CollectionController.java b/src/main/java/io/kuzzle/sdk/API/Controllers/CollectionController.java index 78c410a3..7d3594f6 100644 --- a/src/main/java/io/kuzzle/sdk/API/Controllers/CollectionController.java +++ b/src/main/java/io/kuzzle/sdk/API/Controllers/CollectionController.java @@ -110,4 +110,31 @@ public CompletableFuture> getMapping( .thenApplyAsync( (response) -> (ConcurrentHashMap) response.result); } + + /** + * Gets the validation specifications associated to the given index and collection. + * + * @param index + * @param collection + * @return a CompletableFuture + * @throws NotConnectedException + * @throws InternalException + */ + public CompletableFuture> getSpecifications( + final String index, + final String collection) throws NotConnectedException, InternalException { + + final KuzzleMap query = new KuzzleMap(); + + query + .put("index", index) + .put("collection", collection) + .put("controller", "collection") + .put("action", "getSpecifications"); + + return kuzzle + .query(query) + .thenApplyAsync( + (response) -> (ConcurrentHashMap) response.result); + } } diff --git a/src/test/java/io/kuzzle/test/API/Controllers/CollectionTest.java b/src/test/java/io/kuzzle/test/API/Controllers/CollectionTest.java index dca7b1c6..796126c6 100644 --- a/src/test/java/io/kuzzle/test/API/Controllers/CollectionTest.java +++ b/src/test/java/io/kuzzle/test/API/Controllers/CollectionTest.java @@ -148,4 +148,35 @@ public void getMappingShouldThrowWhenNotConnected() throws NotConnectedException kuzzleMock.getCollectionController().getMapping(index, collection); } + + @Test + public void getSpecificationsCollectionTest() throws NotConnectedException, InternalException { + + Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol)); + String index = "nyc-open-data"; + String collection = "yellow-taxi"; + + ArgumentCaptor arg = ArgumentCaptor.forClass(KuzzleMap.class); + + kuzzleMock.getCollectionController().getSpecifications(index, collection); + Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture()); + + assertEquals((arg.getValue()).getString("controller"), "collection"); + assertEquals((arg.getValue()).getString("action"), "getSpecifications"); + assertEquals((arg.getValue()).getString("index"), "nyc-open-data"); + assertEquals((arg.getValue()).getString("collection"), "yellow-taxi"); + } + + @Test(expected = NotConnectedException.class) + public void getSpecificationsCollectionThrowWhenNotConnected() 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"; + + kuzzleMock.getCollectionController().getSpecifications(index, collection); + } } From 6da0353b98187a35251569b2109738468d53249f Mon Sep 17 00:00:00 2001 From: Yoann Abbes Date: Thu, 2 Apr 2020 10:26:19 +0200 Subject: [PATCH 2/2] reverse change --- .ci/doc/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ci/doc/config.yml b/.ci/doc/config.yml index 1c98e46b..9bd2fe99 100644 --- a/.ci/doc/config.yml +++ b/.ci/doc/config.yml @@ -2,7 +2,7 @@ snippets: mount: /mnt - path: 'doc/**/snippets/validate-specifications.test.yml' + path: 'doc/**/snippets/*.test.yml' templates: /mnt/.ci/doc/templates runners: