From 1f56a8fc7c4fc73531aed9e113260f746f1ec0c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Cottinet?= Date: Fri, 27 Jul 2018 14:26:16 +0200 Subject: [PATCH] Release 3.0.4 (#161) * collection.create can be used with a mapping (#159) * collection.create can be used with a mapping * build tools version update * Release 3.0.4 --- .travis.yml | 2 +- CHANGELOG.md | 48 ------------- build.gradle | 24 +++---- gradle/wrapper/gradle-wrapper.properties | 2 +- .../java/io/kuzzle/sdk/core/Collection.java | 68 ++++++++++++++----- .../core/KuzzleDataCollection/createTest.java | 59 +++++++++++++--- 6 files changed, 115 insertions(+), 88 deletions(-) delete mode 100644 CHANGELOG.md diff --git a/.travis.yml b/.travis.yml index a0a5b4d..6885b6d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ jdk: android: components: - tools - - build-tools-26.0.2 + - build-tools-27.0.3 - platform-tools - android-23 - extra-android-support diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 1cbf796..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,48 +0,0 @@ -*__note:__ the # at the end of lines are the pull request numbers on GitHub* - -# 2.0.0 - -* https://github.com/kuzzleio/sdk-android/releases/tag/2.0.0 - -## Breaking Changes - -* Full refactoring of factory methods and class names #104 #105 - -# 2.1.0 - -* https://github.com/kuzzleio/sdk-android/releases/tag/2.1.0 - -# 2.0.0 - -* https://github.com/kuzzleio/sdk-android/releases/tag/2.0.0 - -# 1.8.0 - -* https://github.com/kuzzleio/sdk-android/releases/tag/1.8.0 - -# 1.7.0 - -* https://github.com/kuzzleio/sdk-android/releases/tag/1.7.0 - -# 1.6.1 - -* Fix roles format within profiles definition #69 -* Move the `getMyRights` method from the `KuzzleSecurity` object to the `Kuzzle` one #68 - -# 1.6.0 - -## Features - -* Implement new roles functionalities (isActionAllowed, getMyRights, getUserRights) -* Implement the elasticsearch autorefresh features (Kuzzle.refreshIndex, Kuzzle.getAutoRefresh, Kuzzle.setAutoRefresh) -* Fix issues #57 #59 #60 - -## List of merged PR - -* Merge pull request #66 from kuzzleio/fix-rights-format - _Sébastien Cottinet_ -* Merge pull request #65 from kuzzleio/kuz-576-getrights - _Kévin Blondel_ -* Merge pull request #64 from kuzzleio/kuz-489-isactionallowed _Kévin Blondel_ -* Merge pull request #63 from kuzzleio/bugfix#60 - _Sébastien Cottinet_ -* Merge pull request #62 from kuzzleio/bugfix#57 - _Sébastien Cottinet_ -* Merge pull request #61 from kuzzleio/bugfix#59 - _Sébastien Cottinet_ -* Merge pull request #58 from kuzzleio/kuz-463-ez-autorefresh - _Sébastien Cottinet_ diff --git a/build.gradle b/build.gradle index 4e3e7ca..d41ccb3 100644 --- a/build.gradle +++ b/build.gradle @@ -2,9 +2,7 @@ apply plugin: 'com.android.library' apply plugin: 'jacoco-android' apply plugin: 'com.jfrog.bintray' apply plugin: 'com.github.dcendents.android-maven' - version = "3.0.4" - buildscript { repositories { google() @@ -12,7 +10,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.0.1' + classpath 'com.android.tools.build:gradle:3.1.3' classpath 'com.dicedmelon.gradle:jacoco-android:0.1.1' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' @@ -54,18 +52,18 @@ repositories { } dependencies { - compile fileTree(include: ['*.jar'], dir: 'libs') + implementation fileTree(include: ['*.jar'], dir: 'libs') // Temporary using v21, until Travis supports v23+ - compile 'com.android.support:appcompat-v7:23.+' - compile 'io.socket:socket.io-client:1.0.0' - testCompile 'junit:junit:4.12' - testCompile 'org.hamcrest:hamcrest-junit:2.0.0.0' - testCompile 'org.mockito:mockito-core:1.9.5' - testCompile 'org.skyscreamer:jsonassert:1.5.0' + implementation 'com.android.support:appcompat-v7:23.+' + implementation 'io.socket:socket.io-client:1.0.0' + testImplementation 'junit:junit:4.12' + testImplementation 'org.hamcrest:hamcrest-junit:2.0.0.0' + testImplementation 'org.mockito:mockito-core:1.9.5' + testImplementation 'org.skyscreamer:jsonassert:1.5.0' // for not mocking JSONObject - testCompile files('libs-test/json-20140107.jar') - androidTestCompile 'junit:junit:4.12' - compile 'com.android.support:support-annotations:21.0.1' + testImplementation files('libs-test/json-20140107.jar') + androidTestImplementation 'junit:junit:4.12' + implementation 'com.android.support:support-annotations:21.0.1' javadocDeps 'com.android.support:support-annotations:22.2.0' } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index b36f392..5ae286d 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip diff --git a/src/main/java/io/kuzzle/sdk/core/Collection.java b/src/main/java/io/kuzzle/sdk/core/Collection.java index 5844533..56d717f 100644 --- a/src/main/java/io/kuzzle/sdk/core/Collection.java +++ b/src/main/java/io/kuzzle/sdk/core/Collection.java @@ -150,7 +150,7 @@ public void scroll(String scrollId, final Options options, final ResponseListene /** * Gets the next page of results from a previous search or scroll request - * + * * @param scrollId Scroll unique identifier * @param options Request options * @param listener Response callback listener @@ -424,37 +424,73 @@ public void onError(JSONObject error) { } /** - * {@link #create(Options, ResponseListener)} + * {@link #create(JSONObject, Options, ResponseListener)} */ public Collection create(final Options options) { - return this.create(options, null); + return this.create(null, options, null); } /** - * {@link #create(Options, ResponseListener)} + * {@link #create(JSONObject, Options, ResponseListener)} */ public Collection create() { - return this.create(null, null); + return this.create(null, null, null); } /** - * {@link #create(Options, ResponseListener)} + * {@link #create(JSONObject, Options, ResponseListener)} */ public Collection create(final ResponseListener listener) { - return this.create(null, listener); + return this.create(null, null, listener); + } + + /** + * {@link #create(JSONObject, Options, ResponseListener)} + */ + public Collection create(final Options options, final ResponseListener listener) { + return this.create(null, options, listener); + } + + /** + * {@link #create(JSONObject, Options, ResponseListener)} + */ + public Collection create(final JSONObject mapping) { + return this.create(mapping, null, null); + } + + /** + * {@link #create(JSONObject, Options, ResponseListener)} + */ + public Collection create(final JSONObject mapping, final Options options) { + return this.create(mapping, options, null); } /** - * Create a new empty data collection, with no associated mapping. - * Kuzzle automatically creates data collections when storing documents, but there are cases where we want to create and prepare data collections before storing documents in it. + * {@link #create(JSONObject, Options, ResponseListener)} + */ + public Collection create(final JSONObject mapping, final ResponseListener listener) { + return this.create(mapping, null, listener); + } + + /** + * Create a new empty data collection, with associated mappings. + * Kuzzle automatically creates data collections when storing documents, + * but there are cases where we want to create and prepare data collections + * before storing documents in it. * + * @param mapping Collection mapping * @param options Request options * @param listener Response callback listener * @return this */ - public Collection create(final Options options, final ResponseListener listener) { + public Collection create(final JSONObject mapping, final Options options, final ResponseListener listener) { JSONObject data = new JSONObject(); + try { + if (mapping != null) { + data.put("body", mapping); + } + this.kuzzle.addHeaders(data, this.getHeaders()); this.kuzzle.query(makeQueryArgs("collection", "create"), data, options, new OnQueryDoneListener() { @Override @@ -538,7 +574,7 @@ public Collection createDocument(@NonNull final JSONObject content, Options opti * @param options Request options * @param listener Response callback listener * @return this - * @throws JSONException + * @throws JSONException */ public Collection createDocument(final String id, @NonNull final JSONObject content, Options options, final ResponseListener listener) throws JSONException { if (content == null) { @@ -709,7 +745,7 @@ public Collection deleteDocument(@NonNull final JSONObject filters, final Option /** * Delete either a single document or multiple ones using search filters - * + * * @param documentId Document unique identifier * @param filter Search fitlers * @param options Request options @@ -853,7 +889,7 @@ public Document document(final JSONObject content) throws JSONException { * @param id Document unique identifier * @param content Document content * @return newly instantiated Document object - * @throws JSONException + * @throws JSONException */ public Document document(final String id, final JSONObject content) throws JSONException { return new Document(this, id, content); @@ -983,7 +1019,7 @@ public void getSpecifications(@NonNull final ResponseListener listen * * @param options Request options * @param listener Response callback listener - * @throws JSONException + * @throws JSONException */ public void getSpecifications(final Options options, @NonNull final ResponseListener listener) throws JSONException { JSONObject data = new JSONObject() @@ -1018,7 +1054,7 @@ public void onError(JSONObject error) { * @param options Request options * @param listener Response callback listener * @return this - * @throws JSONException + * @throws JSONException */ public Collection mCreateDocument(final Document[] documents, final Options options, final ResponseListener listener) throws JSONException { if (documents.length == 0) { @@ -1216,7 +1252,7 @@ public Collection mDeleteDocument(final String[] documentIds) throws JSONExcepti } /** - * Fetch multiple documents + * Fetch multiple documents * * @param documentIds Array of document IDs to retrieve * @param options Request options diff --git a/src/test/java/io/kuzzle/test/core/KuzzleDataCollection/createTest.java b/src/test/java/io/kuzzle/test/core/KuzzleDataCollection/createTest.java index 5456799..5ce8179 100644 --- a/src/test/java/io/kuzzle/test/core/KuzzleDataCollection/createTest.java +++ b/src/test/java/io/kuzzle/test/core/KuzzleDataCollection/createTest.java @@ -18,6 +18,7 @@ import io.kuzzle.sdk.listeners.OnQueryDoneListener; import io.kuzzle.sdk.state.States; import io.kuzzle.test.testUtils.KuzzleExtend; +import io.kuzzle.sdk.core.Kuzzle.QueryArgs; import io.socket.client.Socket; import static org.junit.Assert.assertEquals; @@ -53,15 +54,21 @@ public void setUp() throws URISyntaxException { @Test public void checkSignaturesVariants() { collection = spy(collection); + collection.create(); collection.create(mock(Options.class)); collection.create(listener); - verify(collection, times(3)).create(any(Options.class), any(ResponseListener.class)); + collection.create(mock(Options.class), listener); + collection.create(mock(JSONObject.class)); + collection.create(mock(JSONObject.class), mock(Options.class)); + collection.create(mock(JSONObject.class), listener); + + verify(collection, times(7)).create(any(JSONObject.class), any(Options.class), any(ResponseListener.class)); } @Test(expected = RuntimeException.class) public void testCreateQueryException() throws JSONException { - doThrow(JSONException.class).when(kuzzle).query(any(io.kuzzle.sdk.core.Kuzzle.QueryArgs.class), any(JSONObject.class), any(Options.class), any(OnQueryDoneListener.class)); + doThrow(JSONException.class).when(kuzzle).query(any(QueryArgs.class), any(JSONObject.class), any(Options.class), any(OnQueryDoneListener.class)); collection.create(listener); } @@ -73,13 +80,13 @@ public Object answer(InvocationOnMock invocation) throws Throwable { ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(new JSONObject().put("result", mock(JSONObject.class))); return null; } - }).when(kuzzle).query(any(io.kuzzle.sdk.core.Kuzzle.QueryArgs.class), any(JSONObject.class), any(Options.class), any(OnQueryDoneListener.class)); + }).when(kuzzle).query(any(QueryArgs.class), any(JSONObject.class), any(Options.class), any(OnQueryDoneListener.class)); doThrow(JSONException.class).when(listener).onSuccess(any(JSONObject.class)); collection.create(listener); } @Test - public void testCreate() throws JSONException { + public void testCreateWithoutMapping() throws JSONException { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { @@ -87,15 +94,49 @@ public Object answer(InvocationOnMock invocation) throws Throwable { ((OnQueryDoneListener) invocation.getArguments()[3]).onError(mock(JSONObject.class)); return null; } - }).when(kuzzle).query(any(io.kuzzle.sdk.core.Kuzzle.QueryArgs.class), any(JSONObject.class), any(Options.class), any(OnQueryDoneListener.class)); + }).when(kuzzle).query(any(QueryArgs.class), any(JSONObject.class), any(Options.class), any(OnQueryDoneListener.class)); + collection.create(new Options(), listener); collection.create(listener); collection.create(new Options()); collection.create(); - ArgumentCaptor argument = ArgumentCaptor.forClass(io.kuzzle.sdk.core.Kuzzle.QueryArgs.class); - verify(kuzzle, times(4)).query((io.kuzzle.sdk.core.Kuzzle.QueryArgs) argument.capture(), any(JSONObject.class), any(Options.class), any(OnQueryDoneListener.class)); - assertEquals(((io.kuzzle.sdk.core.Kuzzle.QueryArgs) argument.getValue()).controller, "collection"); - assertEquals(((io.kuzzle.sdk.core.Kuzzle.QueryArgs) argument.getValue()).action, "create"); + + ArgumentCaptor argument = ArgumentCaptor.forClass(QueryArgs.class); + verify(kuzzle, times(4)).query((QueryArgs) argument.capture(), any(JSONObject.class), any(Options.class), any(OnQueryDoneListener.class)); + assertEquals(((QueryArgs) argument.getValue()).controller, "collection"); + assertEquals(((QueryArgs) argument.getValue()).action, "create"); } + @Test + public void testCreateWithMapping() throws JSONException { + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(new JSONObject().put("result", mock(JSONObject.class))); + ((OnQueryDoneListener) invocation.getArguments()[3]).onError(mock(JSONObject.class)); + return null; + } + }).when(kuzzle).query(any(QueryArgs.class), any(JSONObject.class), any(Options.class), any(OnQueryDoneListener.class)); + + JSONObject mapping = new JSONObject().put("foo", "bar"); + + collection.create(mapping); + collection.create(mapping, new Options(), listener); + collection.create(mapping, listener); + collection.create(mapping, new Options()); + + ArgumentCaptor + capturedQargs = ArgumentCaptor.forClass(QueryArgs.class), + capturedMappings = ArgumentCaptor.forClass(JSONObject.class); + + verify(kuzzle, times(4)).query((QueryArgs)capturedQargs.capture(), (JSONObject)capturedMappings.capture(), any(Options.class), any(OnQueryDoneListener.class)); + + assertEquals(((QueryArgs) capturedQargs.getValue()).controller, "collection"); + assertEquals(((QueryArgs) capturedQargs.getValue()).action, "create"); + + String expected = (new JSONObject().put("body", mapping)).toString(); + for (Object m : capturedMappings.getAllValues()) { + assertEquals(expected, m.toString()); + } + } }