diff --git a/README.md b/README.md index f88c14b..912a2f4 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ A backend software, self-hostable and ready to use to power modern apps. You can access the Kuzzle repository on [Github](https://github.com/kuzzleio/kuzzle) - ## SDK Documentation The complete SDK documentation is available [here](http://docs.kuzzle.io/sdk-reference) @@ -24,7 +23,7 @@ https://github.com/kuzzleio/kuzzle-sdk/issues ## Installation -You can configure your android project to get the Kuzzle's android SDK from jcenter in your build.gradle: +You can configure your Android project to get Kuzzle's Android SDK from jcenter in your build.gradle: buildscript { repositories { @@ -40,6 +39,94 @@ You can configure your android project to get the Kuzzle's android SDK from jcen compile 'io.kuzzle:sdk-android:2.2.0' } +## Basic usage + +```java +Kuzzle kuzzle = new Kuzzle("host", new ResponseListener() { +@Override +public void onSuccess(Void object) { + // Handle success + KuzzleDocument doc = new KuzzleDocument(dataCollection); + doc.setContent("foo", "bar").save(); +} + +@Override +public void onError(JSONObject error) { + // Handle error +} +}); +``` + +## KuzzleDocument + +KuzzleDocument is an encapsulation of a JSONObject. + +```java +KuzzleDataCollection myCollection = new KuzzleDataCollection(kuzzle, "myNewCollection"); +KuzzleDocument myDocument = new KuzzleDocument(myCollection); +// Add properties to the body +myDocument.setContent("foo", "bar"); +// Persist the document +myDocument.save(); +// Send it on real time (not persistent) +myDocument.publish(); +``` + +## Adding metadata + +As stated [here](http://kuzzle.io/api-reference/#sending-metadata) you can add metadata to a subscription. + +```java +KuzzleOptions options = new KuzzleOptions(); +JSONObject metadata = new JSONObject(); +metadata.put("foo", "bar"); +options.setMetadata(metadata); +myCollection.subscribe(options); +``` + +# Login + +## Prerequisite + +To login using kuzzle you need at least one authentication plugin. You can refer [here](https://github.com/kuzzleio/kuzzle-plugin-auth-passport-local) for a local authentication plugin +or [here](https://github.com/kuzzleio/kuzzle-plugin-auth-passport-oauth) to refer to our OAuth2 plugin. + +To know more about how to log in with a Kuzzle SDK, please refer to our [documentation](http://docs.kuzzle.io/sdk-reference/kuzzle/login/) + +If you have the kuzzle-plugin-auth-passport-local installed you can login using either the Kuzzle's constructor or the login method. + +### Login with an OAuth strategy + +If you have an OAUTH plugin like kuzzle-plugin-auth-passport-oauth, you may use the KuzzleWebViewClient class to handle the second authentication phase: + +```java +Handler handler = new Handler(); +WebView webView = (WebView) findViewById(R.id.webView); +webView.setWebViewClient(kuzzle.getKuzzleWebViewClient()); +kuzzle.login("github", new KuzzleResponseListener() { + @Override + public void onSuccess(final JSONObject object) { + handler.post(new Runnable() { + @Override + public void run() { + try { + if (object.has("headers")) { + webView.loadUrl(object.getJSONObject("headers").getString("Location")); + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + }); + } + + @Override + public void onError(JSONObject error) { + Log.e("error", error.toString()); + } + }); +``` + ## License -[Apache 2](LICENSE.md) +[Apache 2](LICENSE) diff --git a/build.gradle b/build.gradle index 2a6a4b5..7a9339a 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +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.0" +version = "3.0.1" buildscript { repositories { jcenter() diff --git a/src/main/java/io/kuzzle/sdk/core/Collection.java b/src/main/java/io/kuzzle/sdk/core/Collection.java index 10df392..5844533 100644 --- a/src/main/java/io/kuzzle/sdk/core/Collection.java +++ b/src/main/java/io/kuzzle/sdk/core/Collection.java @@ -176,7 +176,7 @@ public void scroll(String scrollId, final Options options, final JSONObject filt options.setScrollId(scrollId); try { - this.kuzzle.query(makeQueryArgs("document", "scroll"), request, options, new OnQueryDoneListener() { + this.kuzzle.query(this.kuzzle.buildQueryArgs("document", "scroll"), request, options, new OnQueryDoneListener() { @Override public void onSuccess(JSONObject object) { try { diff --git a/src/main/java/io/kuzzle/sdk/core/Document.java b/src/main/java/io/kuzzle/sdk/core/Document.java index a8b240a..5a3c660 100644 --- a/src/main/java/io/kuzzle/sdk/core/Document.java +++ b/src/main/java/io/kuzzle/sdk/core/Document.java @@ -170,7 +170,7 @@ public void onError(JSONObject error) { /** * {@link #exists(Options, ResponseListener)} */ - public void exists(@NonNull final ResponseListener listener) { + public void exists(@NonNull final ResponseListener listener) { this.exists(null, listener); } @@ -180,7 +180,7 @@ public void exists(@NonNull final ResponseListener listener) { * @param options - Request options * @param listener - Response callback listener */ - public void exists(final Options options, @NonNull final ResponseListener listener) { + public void exists(final Options options, @NonNull final ResponseListener listener) { if (this.id == null) { throw new IllegalStateException("Document.exists: cannot check if the document exists if no id has been provided"); } @@ -193,8 +193,11 @@ public void exists(final Options options, @NonNull final ResponseListener= 0) { - body.put("expiresIn", expiresIn); + query.put("expiresIn", expiresIn); } query.put("strategy", strategy); @@ -1303,7 +1303,7 @@ public Kuzzle removeListener(Event event, EventListener listener) { /** * Renew all registered subscriptions. Usually called after: - * - a connection, if subscriptions occured before + * - a connection, if subscriptions occurred before * - a reconnection * - after a successful login attempt, to subscribe with the new credentials */ @@ -1430,7 +1430,7 @@ private void reconnect() { */ protected Socket createSocket() throws URISyntaxException { IO.Options opt = new IO.Options(); - opt.forceNew = true; + opt.forceNew = false; opt.reconnection = this.autoReconnect; opt.reconnectionDelay = this.reconnectionDelay; return IO.socket("http://" + host + ":" + this.port, opt); @@ -2152,7 +2152,7 @@ public void whoAmI(@NonNull final ResponseListener listener) { public void onSuccess(JSONObject response) { try { JSONObject result = response.getJSONObject("result"); - listener.onSuccess(new User(Kuzzle.this, result.getString("_id"), result.getJSONObject("_source"))); + listener.onSuccess(new User(Kuzzle.this, result.getString("_id"), result.getJSONObject("_source"), result.getJSONObject("_meta"))); } catch (JSONException e) { throw new RuntimeException(e); } diff --git a/src/main/java/io/kuzzle/sdk/enums/Scope.java b/src/main/java/io/kuzzle/sdk/enums/Scope.java index c38e6fa..04b1cc9 100644 --- a/src/main/java/io/kuzzle/sdk/enums/Scope.java +++ b/src/main/java/io/kuzzle/sdk/enums/Scope.java @@ -4,5 +4,6 @@ public enum Scope { IN, OUT, ALL, - NONE + NONE, + UNKNOWN } diff --git a/src/main/java/io/kuzzle/sdk/enums/Users.java b/src/main/java/io/kuzzle/sdk/enums/Users.java index 63f37fc..b8f38fa 100644 --- a/src/main/java/io/kuzzle/sdk/enums/Users.java +++ b/src/main/java/io/kuzzle/sdk/enums/Users.java @@ -1,8 +1,5 @@ package io.kuzzle.sdk.enums; -/** - * Created by kblondel on 11/12/15. - */ public enum Users { IN, OUT, diff --git a/src/main/java/io/kuzzle/sdk/responses/NotificationResponse.java b/src/main/java/io/kuzzle/sdk/responses/NotificationResponse.java index 1c72ef1..522eb91 100644 --- a/src/main/java/io/kuzzle/sdk/responses/NotificationResponse.java +++ b/src/main/java/io/kuzzle/sdk/responses/NotificationResponse.java @@ -44,8 +44,12 @@ public NotificationResponse(final Kuzzle kuzzle, final JSONObject object) { this.scope = (object.isNull("scope") ? null : Scope.valueOf(object.getString("scope").toUpperCase())); this.users = (object.isNull("user") ? null : Users.valueOf(object.getString("user").toUpperCase())); if (!object.getJSONObject("result").isNull("_source")) { - this.document = new Document(new Collection(kuzzle, this.collection, this.index), object.getJSONObject("result")); - this.document.setId(this.result.getString("_id")); + JSONObject content = object.getJSONObject("result"); + String id = content.getString("_id"); + JSONObject meta = content.isNull("_meta") ? new JSONObject() : content.getJSONObject("_meta"); + content.remove("_id"); + content.remove("_meta"); + this.document = new Document(new Collection(kuzzle, this.collection, this.index), id, content, meta); } } catch (JSONException e) { throw new RuntimeException(e); diff --git a/src/main/java/io/kuzzle/sdk/responses/SearchResult.java b/src/main/java/io/kuzzle/sdk/responses/SearchResult.java index 5dde0e1..7ab14d0 100644 --- a/src/main/java/io/kuzzle/sdk/responses/SearchResult.java +++ b/src/main/java/io/kuzzle/sdk/responses/SearchResult.java @@ -2,6 +2,7 @@ import android.support.annotation.NonNull; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -99,6 +100,7 @@ public JSONObject getFilters() { public void fetchNext(ResponseListener listener) { JSONObject filters; Options options; + try { options = new Options(this.options); } catch (JSONException e) { @@ -106,9 +108,11 @@ public void fetchNext(ResponseListener listener) { } options.setPrevious(this); + // retrieve next results with scroll if original search use it if (options.getScrollId() != null) { - if(this.fetchedDocument >= this.getTotal()) { + if (this.fetchedDocument >= this.getTotal()) { listener.onSuccess(null); + return; } @@ -125,6 +129,37 @@ public void fetchNext(ResponseListener listener) { return; } + // retrieve next results using ES's search_after + if (options.getSize() != null && this.filters.has("sort")) { + if (this.fetchedDocument >= this.getTotal()) { + listener.onSuccess(null); + + return; + } + + if (options.getFrom() != null) { + options.setFrom(null); + } + + try { + JSONArray searchAfter = new JSONArray(); + + for (int i = 0; i < this.filters.getJSONArray("sort").length(); i++) { + Document doc = this.getDocuments().get(this.getDocuments().size() - 1); + searchAfter.put(doc.getContent().get(this.filters.getJSONArray("sort").getJSONObject(i).keys().next())); + } + + this.filters.put("search_after", searchAfter); + } catch (JSONException e) { + throw new RuntimeException(e); + } + + this.collection.search(this.filters, options, listener); + + return; + } + + // retrieve next results with from/size if original search use it if (options.getFrom() != null && options.getSize() != null) { try { filters = new JSONObject(this.filters.toString()); diff --git a/src/main/java/io/kuzzle/sdk/security/AbstractSecurityDocument.java b/src/main/java/io/kuzzle/sdk/security/AbstractSecurityDocument.java index e62db43..f56cb96 100644 --- a/src/main/java/io/kuzzle/sdk/security/AbstractSecurityDocument.java +++ b/src/main/java/io/kuzzle/sdk/security/AbstractSecurityDocument.java @@ -20,6 +20,7 @@ public class AbstractSecurityDocument { protected String updateActionName; public final String id; public JSONObject content; + public JSONObject meta; /** * Instantiates a new Abstract kuzzle security document. @@ -29,7 +30,7 @@ public class AbstractSecurityDocument { * @param content Security document content * @throws JSONException */ - public AbstractSecurityDocument(final Kuzzle kuzzle, @NonNull final String id, final JSONObject content) throws JSONException { + public AbstractSecurityDocument(final Kuzzle kuzzle, @NonNull final String id, final JSONObject content, final JSONObject meta) throws JSONException { if (id == null) { throw new IllegalArgumentException("Cannot initialize with a null ID"); } @@ -43,6 +44,12 @@ public AbstractSecurityDocument(final Kuzzle kuzzle, @NonNull final String id, f } else { this.content = new JSONObject(); } + + if (meta != null) { + setMeta(meta); + } else { + this.meta = new JSONObject(); + } } /** @@ -62,6 +69,23 @@ public AbstractSecurityDocument setContent(@NonNull final JSONObject content) th return this; } + /** + * Sets the metadata of this object + * + * @param meta New metadata + * @return this + * @throws JSONException + */ + public AbstractSecurityDocument setMeta(@NonNull final JSONObject meta) throws JSONException { + if (meta == null) { + throw new IllegalArgumentException("AbstractSecurityDocument.setMeta: cannot set null metadata"); + } + + this.meta = new JSONObject(meta.toString()); + + return this; + } + /** * Serializes this object to a plain-old JSON object * @@ -149,6 +173,15 @@ public JSONObject getContent() { return this.content; } + /** + * Getter for the "meta" property + * + * @return the document metadata + */ + public JSONObject getMeta() { + return this.meta; + } + /** * {@link #update(JSONObject, Options, ResponseListener)} */ diff --git a/src/main/java/io/kuzzle/sdk/security/Profile.java b/src/main/java/io/kuzzle/sdk/security/Profile.java index 5c5a8fe..7cb58dd 100644 --- a/src/main/java/io/kuzzle/sdk/security/Profile.java +++ b/src/main/java/io/kuzzle/sdk/security/Profile.java @@ -26,10 +26,11 @@ public class Profile extends AbstractSecurityDocument { * @param kuzzle Kuzzle instance to attach * @param id Profile unique ID * @param content Profile content + * @param meta Profile metadata * @throws JSONException */ - public Profile(final Kuzzle kuzzle, @NonNull final String id, final JSONObject content) throws JSONException { - super(kuzzle, id, null); + public Profile(final Kuzzle kuzzle, @NonNull final String id, final JSONObject content, final JSONObject meta) throws JSONException { + super(kuzzle, id, null, meta); this.deleteActionName = "deleteProfile"; this.updateActionName = "updateProfile"; diff --git a/src/main/java/io/kuzzle/sdk/security/Role.java b/src/main/java/io/kuzzle/sdk/security/Role.java index 8dd5e94..a6b2300 100644 --- a/src/main/java/io/kuzzle/sdk/security/Role.java +++ b/src/main/java/io/kuzzle/sdk/security/Role.java @@ -1,6 +1,5 @@ package io.kuzzle.sdk.security; - import android.support.annotation.NonNull; import org.json.JSONException; @@ -21,10 +20,11 @@ public class Role extends AbstractSecurityDocument { * @param kuzzle Kuzzle instance to attach * @param id Role unique identifier * @param content Role content + * @param meta Role metadata * @throws JSONException */ - public Role(final Kuzzle kuzzle, @NonNull final String id, final JSONObject content) throws JSONException { - super(kuzzle, id, content); + public Role(final Kuzzle kuzzle, @NonNull final String id, final JSONObject content, final JSONObject meta) throws JSONException { + super(kuzzle, id, content, meta); this.deleteActionName = "deleteRole"; this.updateActionName = "updateRole"; } diff --git a/src/main/java/io/kuzzle/sdk/security/Security.java b/src/main/java/io/kuzzle/sdk/security/Security.java index d778b42..c691f26 100644 --- a/src/main/java/io/kuzzle/sdk/security/Security.java +++ b/src/main/java/io/kuzzle/sdk/security/Security.java @@ -17,7 +17,6 @@ import io.kuzzle.sdk.responses.SecurityDocumentList; import io.kuzzle.sdk.util.Scroll; - /** * Kuzzle security API */ @@ -75,12 +74,12 @@ public void fetchRole(@NonNull final String id, Options options, @NonNull final try { data = new JSONObject().put("_id", id); - this.kuzzle.query(buildQueryArgs("fetchRole"), data, options, new OnQueryDoneListener() { + this.kuzzle.query(buildQueryArgs("getRole"), data, options, new OnQueryDoneListener() { @Override public void onSuccess(JSONObject response) { try { JSONObject result = response.getJSONObject("result"); - listener.onSuccess(new Role(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"))); + listener.onSuccess(new Role(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"), result.getJSONObject("_meta"))); } catch (JSONException e) { throw new RuntimeException(e); @@ -135,7 +134,7 @@ public void onSuccess(JSONObject response) { for (int i = 0; i < documentsLength; i++) { JSONObject document = documents.getJSONObject(i); - roles.add(new Role(Security.this.kuzzle, document.getString("_id"), document.getJSONObject("_source"))); + roles.add(new Role(Security.this.kuzzle, document.getString("_id"), document.getJSONObject("_source"), document.getJSONObject("_meta"))); } listener.onSuccess(new SecurityDocumentList(roles, result.getLong("total"))); @@ -188,7 +187,7 @@ public void createRole(@NonNull final String id, @NonNull final JSONObject conte public void onSuccess(JSONObject response) { try { JSONObject result = response.getJSONObject("result"); - listener.onSuccess(new Role(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"))); + listener.onSuccess(new Role(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"), result.getJSONObject("_meta"))); } catch(JSONException e) { throw new RuntimeException(e); @@ -312,7 +311,7 @@ public Security updateRole(@NonNull final String id, final JSONObject content, f @Override public void onSuccess(JSONObject response) { try { - listener.onSuccess(new Role(Security.this.kuzzle, response.getJSONObject("result").getString("_id"), response.getJSONObject("result").getJSONObject("_source"))); + listener.onSuccess(new Role(Security.this.kuzzle, response.getJSONObject("result").getString("_id"), response.getJSONObject("result").getJSONObject("_source"), response.getJSONObject("result").getJSONObject("_meta"))); } catch(JSONException e) { throw new RuntimeException(e); @@ -359,18 +358,26 @@ public Security updateRole(@NonNull final String id, final JSONObject content) t * * @param id Role unique identifier * @param content Role content + * @param meta Role metadata * @return new Role object * @throws JSONException */ + public Role role(@NonNull final String id, final JSONObject content, final JSONObject meta) throws JSONException { + return new Role(this.kuzzle, id, content, meta); + } + + /** + * {@link #role(String, JSONObject, JSONObject)} + */ public Role role(@NonNull final String id, final JSONObject content) throws JSONException { - return new Role(this.kuzzle, id, content); + return new Role(this.kuzzle, id, content, null); } /** - * {@link #role(String, JSONObject)} + * {@link #role(String, JSONObject, JSONObject)} */ public Role role(@NonNull final String id) throws JSONException { - return new Role(this.kuzzle, id, null); + return new Role(this.kuzzle, id, null, null); } /** @@ -392,7 +399,7 @@ public void fetchProfile(@NonNull final String id, final Options options, @NonNu JSONObject data = new JSONObject().put("_id", id); - this.kuzzle.query(buildQueryArgs("fetchProfile"), data, options, new OnQueryDoneListener() { + this.kuzzle.query(buildQueryArgs("getProfile"), data, options, new OnQueryDoneListener() { @Override public void onSuccess(JSONObject response) { try { @@ -415,7 +422,7 @@ public void onSuccess(JSONObject response) { result.getJSONObject("_source").remove("policies"); result.getJSONObject("_source").put("policies", formattedPolicies); - listener.onSuccess(new Profile(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"))); + listener.onSuccess(new Profile(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"), result.getJSONObject("_meta"))); } catch (JSONException e) { throw new RuntimeException(e); } @@ -465,7 +472,7 @@ public void onSuccess(JSONObject response) { for (int i = 0; i < documentsLength; i++) { JSONObject document = documents.getJSONObject(i); - profiles.add(new Profile(Security.this.kuzzle, document.getString("_id"), document.getJSONObject("_source"))); + profiles.add(new Profile(Security.this.kuzzle, document.getString("_id"), document.getJSONObject("_source"), document.getJSONObject("_meta"))); } Scroll scroll = new Scroll(); @@ -526,7 +533,7 @@ public void createProfile(@NonNull final String id, @NonNull final JSONObject[] public void onSuccess(JSONObject response) { try { JSONObject result = response.getJSONObject("result"); - listener.onSuccess(new Profile(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"))); + listener.onSuccess(new Profile(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"), result.getJSONObject("_meta"))); } catch(JSONException e) { throw new RuntimeException(e); @@ -665,7 +672,7 @@ public void onSuccess(JSONObject object) { for (int i = 0; i < hits.length(); i++) { JSONObject hit = hits.getJSONObject(i); - Profile profile = new Profile(Security.this.kuzzle, hit.getString("_id"), hit.getJSONObject("_source")); + Profile profile = new Profile(Security.this.kuzzle, hit.getString("_id"), hit.getJSONObject("_source"), hit.getJSONObject("_meta")); profiles.add(profile); } @@ -718,7 +725,7 @@ public Security updateProfile(@NonNull final String id, final JSONObject[] polic @Override public void onSuccess(JSONObject response) { try { - listener.onSuccess(new Profile(Security.this.kuzzle, response.getJSONObject("result").getString("_id"), response.getJSONObject("result").getJSONObject("_source"))); + listener.onSuccess(new Profile(Security.this.kuzzle, response.getJSONObject("result").getString("_id"), response.getJSONObject("result").getJSONObject("_source"), response.getJSONObject("result").getJSONObject("_meta"))); } catch(JSONException e) { throw new RuntimeException(e); @@ -765,18 +772,26 @@ public Security updateProfile(@NonNull final String id, final JSONObject[] polic * * @param id Profile unique identifier * @param content Profile content + * @param meta Profile metadata * @return new Profile object * @throws JSONException */ + public Profile profile(@NonNull final String id, final JSONObject content, final JSONObject meta) throws JSONException { + return new Profile(this.kuzzle, id, content, meta); + } + + /** + * {@link #profile(String, JSONObject, JSONObject)} + */ public Profile profile(@NonNull final String id, final JSONObject content) throws JSONException { - return new Profile(this.kuzzle, id, content); + return new Profile(this.kuzzle, id, content, null); } /** - * {@link #profile(String, JSONObject)} + * {@link #profile(String, JSONObject, JSONObject)} */ public Profile profile(@NonNull final String id) throws JSONException { - return new Profile(this.kuzzle, id, null); + return new Profile(this.kuzzle, id, null, null); } /** @@ -798,12 +813,12 @@ public void fetchUser(@NonNull final String id, final Options options, @NonNull JSONObject data = new JSONObject().put("_id", id); - this.kuzzle.query(buildQueryArgs("fetchUser"), data, options, new OnQueryDoneListener() { + this.kuzzle.query(buildQueryArgs("getUser"), data, options, new OnQueryDoneListener() { @Override public void onSuccess(JSONObject response) { try { JSONObject result = response.getJSONObject("result"); - listener.onSuccess(new User(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"))); + listener.onSuccess(new User(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"), result.getJSONObject("_meta"))); } catch (JSONException e) { throw new RuntimeException(e); } @@ -849,7 +864,7 @@ public void replaceUser(@NonNull final String id, @NonNull final JSONObject cont public void onSuccess(JSONObject response) { try { JSONObject result = response.getJSONObject("result"); - listener.onSuccess(new User(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"))); + listener.onSuccess(new User(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"), result.getJSONObject("_meta"))); } catch (JSONException e) { throw new RuntimeException(e); @@ -919,7 +934,7 @@ public void onSuccess(JSONObject response) { for (int i = 0; i < documentsLength; i++) { JSONObject document = documents.getJSONObject(i); - users.add(new User(Security.this.kuzzle, document.getString("_id"), document.getJSONObject("_source"))); + users.add(new User(Security.this.kuzzle, document.getString("_id"), document.getJSONObject("_source"), document.getJSONObject("_meta"))); } Scroll scroll = new Scroll(); @@ -971,7 +986,7 @@ public void createUser(@NonNull final String id, @NonNull final JSONObject conte public void onSuccess(JSONObject response) { try { JSONObject result = response.getJSONObject("result"); - listener.onSuccess(new User(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"))); + listener.onSuccess(new User(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"), result.getJSONObject("_meta"))); } catch (JSONException e) { throw new RuntimeException(e); @@ -1039,7 +1054,7 @@ public void createRestrictedUser(@NonNull final String id, @NonNull final JSONOb public void onSuccess(JSONObject response) { try { JSONObject result = response.getJSONObject("result"); - listener.onSuccess(new User(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"))); + listener.onSuccess(new User(Security.this.kuzzle, result.getString("_id"), result.getJSONObject("_source"), result.getJSONObject("_meta"))); } catch (JSONException e) { throw new RuntimeException(e); @@ -1178,7 +1193,7 @@ public void onSuccess(JSONObject object) { for (int i = 0; i < hits.length(); i++) { JSONObject hit = hits.getJSONObject(i); - User user = new User(Security.this.kuzzle, hit.getString("_id"), hit.getJSONObject("_source")); + User user = new User(Security.this.kuzzle, hit.getString("_id"), hit.getJSONObject("_source"), hit.getJSONObject("_meta")); users.add(user); } @@ -1231,7 +1246,7 @@ public Security updateUser(@NonNull final String id, final JSONObject content, f @Override public void onSuccess(JSONObject response) { try { - listener.onSuccess(new User(Security.this.kuzzle, response.getJSONObject("result").getString("_id"), response.getJSONObject("result").getJSONObject("_source"))); + listener.onSuccess(new User(Security.this.kuzzle, response.getJSONObject("result").getString("_id"), response.getJSONObject("result").getJSONObject("_source"), response.getJSONObject("result").getJSONObject("_meta"))); } catch(JSONException e) { throw new RuntimeException(e); @@ -1278,18 +1293,26 @@ public Security updateUser(@NonNull final String id, final JSONObject content) t * * @param id User unique identifier * @param content User content + * @param meta User metadata * @return new User object * @throws JSONException */ + public User user(@NonNull final String id, final JSONObject content, final JSONObject meta) throws JSONException { + return new User(this.kuzzle, id, content, meta); + } + + /** + * {@link #user(String, JSONObject, JSONObject)} + */ public User user(@NonNull final String id, final JSONObject content) throws JSONException { - return new User(this.kuzzle, id, content); + return new User(this.kuzzle, id, content, null); } /** - * {@link #user(String, JSONObject)} + * {@link #user(String, JSONObject, JSONObject)} */ public User user(@NonNull final String id) throws JSONException { - return new User(this.kuzzle, id, null); + return new User(this.kuzzle, id, null, null); } /** diff --git a/src/main/java/io/kuzzle/sdk/security/User.java b/src/main/java/io/kuzzle/sdk/security/User.java index 4b2b845..70db813 100644 --- a/src/main/java/io/kuzzle/sdk/security/User.java +++ b/src/main/java/io/kuzzle/sdk/security/User.java @@ -29,10 +29,11 @@ public class User extends AbstractSecurityDocument { * @param kuzzle Kuzzle instance to attach * @param id User unique identifier * @param content User content + * @param meta User metadata * @throws JSONException */ - public User(final Kuzzle kuzzle, @NonNull final String id, final JSONObject content) throws JSONException { - super(kuzzle, id, null); + public User(final Kuzzle kuzzle, @NonNull final String id, final JSONObject content, final JSONObject meta) throws JSONException { + super(kuzzle, id, null, meta); this.deleteActionName = "deleteUser"; this.updateActionName = "updateUser"; diff --git a/src/test/java/io/kuzzle/test/core/Kuzzle/whoAmiTest.java b/src/test/java/io/kuzzle/test/core/Kuzzle/whoAmiTest.java index b660edb..fc8485e 100644 --- a/src/test/java/io/kuzzle/test/core/Kuzzle/whoAmiTest.java +++ b/src/test/java/io/kuzzle/test/core/Kuzzle/whoAmiTest.java @@ -64,7 +64,12 @@ public Object answer(InvocationOnMock invocation) throws Throwable { .put("_source", new JSONObject() .put("profile", new JSONObject() .put("_id", "admin") - .put("roles", ""))))); + .put("roles", "") + ) + ) + .put("_meta", new JSONObject()) + ) + ); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(mock(JSONObject.class)); return null; diff --git a/src/test/java/io/kuzzle/test/core/KuzzleDocument/existsTest.java b/src/test/java/io/kuzzle/test/core/KuzzleDocument/existsTest.java index b82e4b9..a3ee18e 100644 --- a/src/test/java/io/kuzzle/test/core/KuzzleDocument/existsTest.java +++ b/src/test/java/io/kuzzle/test/core/KuzzleDocument/existsTest.java @@ -77,12 +77,12 @@ public void testExistsException() throws JSONException { @Override public Object answer(InvocationOnMock invocation) throws Throwable { JSONObject result = new JSONObject() - .put("result", true); + .put("result", true); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(result); return null; } }).when(k).query(any(io.kuzzle.sdk.core.Kuzzle.QueryArgs.class), any(JSONObject.class), any(Options.class), any(OnQueryDoneListener.class)); - doThrow(JSONException.class).when(mockListener).onSuccess(any(Document.class)); + doThrow(JSONException.class).when(mockListener).onSuccess(any(JSONObject.class)); doc.exists(mockListener); } @@ -97,7 +97,7 @@ public void testExists() throws JSONException { @Override public Object answer(InvocationOnMock invocation) throws Throwable { JSONObject response = new JSONObject() - .put("result", true); + .put("result", true); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(null); return null; @@ -105,10 +105,10 @@ public Object answer(InvocationOnMock invocation) throws Throwable { }).when(k).query(any(io.kuzzle.sdk.core.Kuzzle.QueryArgs.class), any(JSONObject.class), any(Options.class), any(OnQueryDoneListener.class)); doc.setId("42"); doc.setContent("foo", "baz"); - doc.exists(new ResponseListener() { + doc.exists(new ResponseListener() { @Override - public void onSuccess(JSONObject object) { - assertNotEquals(doc, object); + public void onSuccess(Boolean exists) { + assertEquals(exists, true); } @Override diff --git a/src/test/java/io/kuzzle/test/responses/SearchResultTest.java b/src/test/java/io/kuzzle/test/responses/SearchResultTest.java index 29c039c..f77c515 100644 --- a/src/test/java/io/kuzzle/test/responses/SearchResultTest.java +++ b/src/test/java/io/kuzzle/test/responses/SearchResultTest.java @@ -1,9 +1,11 @@ package io.kuzzle.test.responses; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import java.net.URISyntaxException; import java.util.ArrayList; @@ -15,7 +17,6 @@ import io.kuzzle.sdk.enums.Mode; import io.kuzzle.sdk.listeners.ResponseListener; import io.kuzzle.sdk.responses.SearchResult; -import io.kuzzle.sdk.security.Profile; import io.kuzzle.sdk.state.States; import io.kuzzle.test.testUtils.KuzzleExtend; import io.socket.client.Socket; @@ -34,8 +35,6 @@ public class SearchResultTest { private ResponseListener listener; private long total; private ArrayList documents; - private String documentId; - private JSONObject documentContent; private Options options; private String scrollId; private String scroll; @@ -53,13 +52,12 @@ public void setUp() throws URISyntaxException { collection = new Collection(kuzzle, "test", "index"); listener = mock(ResponseListener.class); total = (long) 42; - documentId = "someDocumentId"; - documents = new ArrayList(); + documents = new ArrayList<>(); scrollId = "someScrollId"; scroll = "someScroll"; try { - documentContent = new JSONObject().put("some", "content"); - documents.add(new Document(collection, documentId, documentContent)); + documents.add(new Document(collection, "foo", new JSONObject().put("name", "John").put("age", 42))); + documents.add(new Document(collection, "bar", new JSONObject().put("name", "Michael").put("age", 36))); } catch(JSONException e) { throw new RuntimeException(e); } @@ -74,7 +72,48 @@ public void checkConstructorArguments() { assertEquals(searchResult.getAggregations(), null); assertEquals(searchResult.getOptions(), options); assertEquals(searchResult.getFilters(), null); - assertEquals(searchResult.getFetchedDocument(), (long) 1); + assertEquals(searchResult.getFetchedDocument(), (long) 2); + } + + @Test + public void fetchNextBySearchAfter() throws JSONException { + Options localOptions = new Options(options); + localOptions + .setSize((long) 2); + + JSONObject filters = new JSONObject() + .put("sort", new JSONArray() + .put(new JSONObject() + .put("age", "desc") + ) + .put(new JSONObject() + .put("name", "asc") + ) + ); + + collection = spy(collection); + SearchResult searchResult = new SearchResult(collection, total, documents, null, localOptions, filters); + + searchResult.fetchNext(listener); + + JSONObject expectedFilters = new JSONObject() + .put("search_after", new JSONArray() + .put(36) + .put("Michael") + ) + .put("sort", new JSONArray() + .put(new JSONObject() + .put("age", "desc") + ) + .put(new JSONObject() + .put("name", "asc") + ) + ); + + ArgumentCaptor argument = ArgumentCaptor.forClass(io.kuzzle.sdk.core.Kuzzle.QueryArgs.class); + verify(collection).search((JSONObject) argument.capture(), any(Options.class), eq(listener)); + + assertEquals(argument.getValue().toString(), expectedFilters.toString()); } @Test @@ -89,7 +128,7 @@ public void fetchNextByScroll() throws JSONException { searchResult.fetchNext(listener); - verify(collection).scroll(eq(scrollId), any(Options.class), eq((JSONObject) null), eq(listener)); + verify(collection).scroll(eq(scrollId), any(Options.class), any(JSONObject.class), eq(listener)); } @Test diff --git a/src/test/java/io/kuzzle/test/security/KuzzleProfileTest.java b/src/test/java/io/kuzzle/test/security/KuzzleProfileTest.java index e91d1a8..677587a 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleProfileTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleProfileTest.java @@ -34,12 +34,12 @@ public class KuzzleProfileTest { public void setUp() throws JSONException { kuzzle = mock(Kuzzle.class); kuzzle.security = new Security(kuzzle); - stubProfile = new Profile(kuzzle, "foo", null); + stubProfile = new Profile(kuzzle, "foo", null, null); } @Test public void testConstructorNoContent() throws JSONException { - Profile profile = new Profile(kuzzle, "foo", null); + Profile profile = new Profile(kuzzle, "foo", null, null); assertEquals(profile.id, "foo"); assertEquals(profile.getPolicies().length, 0); assertThat(profile.content, instanceOf(JSONObject.class)); @@ -53,7 +53,7 @@ public void testConstructorContentWithIDs() throws JSONException { "\"policies\": [{\"roleId\": \"foo\"}, {\"roleId\": \"bar\"}, {\"roleId\": \"baz\"}]" + "}" ); - Profile profile = new Profile(kuzzle, "foo", content); + Profile profile = new Profile(kuzzle, "foo", content, null); assertEquals(profile.id, "foo"); assertEquals(profile.getPolicies().length, 3); assertEquals(profile.getPolicies()[2].getString("roleId"), "baz"); @@ -72,7 +72,7 @@ public void testConstructorContentWithRoles() throws JSONException { "]" + "}" ); - Profile profile = new Profile(kuzzle, "foo", content); + Profile profile = new Profile(kuzzle, "foo", content, null); assertEquals(profile.id, "foo"); assertEquals(profile.getPolicies().length, 3); assertEquals(profile.getPolicies()[2].getString("roleId"), "baz"); @@ -80,6 +80,19 @@ public void testConstructorContentWithRoles() throws JSONException { assertEquals(profile.content.length(), 0); } + @Test + public void testConstructorMeta() throws JSONException { + JSONObject meta = new JSONObject() + .put("createdAt", "0123456789") + .put("author", "-1"); + Profile profile = new Profile(kuzzle, "foo", null, meta); + assertEquals(profile.id, "foo"); + assertEquals(profile.getMeta().length(), 2); + assertEquals(profile.getMeta().getString("createdAt"), "0123456789"); + assertEquals(profile.getMeta().getString("author"), "-1"); + assertThat(profile.meta, instanceOf(JSONObject.class)); + } + @Test public void testAddPolicyObject() throws JSONException { stubProfile.addPolicy(new JSONObject().put("roleId", "some role")); diff --git a/src/test/java/io/kuzzle/test/security/KuzzleRoleTest.java b/src/test/java/io/kuzzle/test/security/KuzzleRoleTest.java index 260e933..00af0d4 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleRoleTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleRoleTest.java @@ -15,7 +15,9 @@ import io.kuzzle.sdk.security.Role; import io.kuzzle.sdk.security.Security; +import static org.hamcrest.core.IsInstanceOf.instanceOf; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; @@ -30,7 +32,20 @@ public class KuzzleRoleTest { public void setUp() throws JSONException { kuzzle = mock(Kuzzle.class); kuzzle.security = new Security(kuzzle); - stubRole = new Role(kuzzle, "foo", null); + stubRole = new Role(kuzzle, "foo", null, null); + } + + @Test + public void testConstructorMeta() throws JSONException { + JSONObject meta = new JSONObject() + .put("createdAt", "0123456789") + .put("author", "-1"); + Role role = new Role(kuzzle, "foo", null, meta); + assertEquals(role.id, "foo"); + assertEquals(role.getMeta().length(), 2); + assertEquals(role.getMeta().getString("createdAt"), "0123456789"); + assertEquals(role.getMeta().getString("author"), "-1"); + assertThat(role.meta, instanceOf(JSONObject.class)); } @Test diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/AbstractKuzzleSecurityDocumentTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/AbstractKuzzleSecurityDocumentTest.java index bace713..b07aaa9 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/AbstractKuzzleSecurityDocumentTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/AbstractKuzzleSecurityDocumentTest.java @@ -37,7 +37,7 @@ public void setUp() throws JSONException { kuzzle = mock(Kuzzle.class); kuzzle.security = new Security(kuzzle); listener = mock(ResponseListener.class); - stubRole = new Role(kuzzle, "foo", new JSONObject("{\"foo\":\"bar\"}")); + stubRole = new Role(kuzzle, "foo", new JSONObject("{\"foo\":\"bar\"}"), null); } @Test(expected = IllegalArgumentException.class) diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createProfileTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createProfileTest.java index 07784f7..824d78c 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createProfileTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createProfileTest.java @@ -73,12 +73,13 @@ public Object answer(InvocationOnMock invocation) throws Throwable { JSONObject response = new JSONObject( "{" + "\"result\": {" + - "\"_id\": \"foobar\"," + - "\"_source\": {" + - "\"indexes\": {}" + + "\"_id\": \"foobar\"," + + "\"_source\": {" + + "\"indexes\": {}" + + "}," + + "\"_meta\": {}" + "}" + - "}" + - "}"); + "}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createRestrictedUserTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createRestrictedUserTest.java index 2a9a615..00ea3bb 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createRestrictedUserTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createRestrictedUserTest.java @@ -78,12 +78,13 @@ public Object answer(InvocationOnMock invocation) throws Throwable { JSONObject response = new JSONObject( "{" + "\"result\": {" + - "\"_id\": \"foobar\"," + - "\"_source\": {" + - "\"indexes\": {}" + + "\"_id\": \"foobar\"," + + "\"_source\": {" + + "\"indexes\": {}" + + "}," + + "\"_meta\": {}" + "}" + - "}" + - "}"); + "}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createRoleTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createRoleTest.java index 054bd9e..3021ee1 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createRoleTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createRoleTest.java @@ -72,12 +72,13 @@ public Object answer(InvocationOnMock invocation) throws Throwable { JSONObject response = new JSONObject( "{" + "\"result\": {" + - "\"_id\": \"foobar\"," + - "\"_source\": {" + - "\"indexes\": {}" + + "\"_id\": \"foobar\"," + + "\"_source\": {" + + "\"indexes\": {}" + + "}," + + "\"_meta\": {}" + "}" + - "}" + - "}"); + "}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createUserTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createUserTest.java index 2a5dc39..f347135 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createUserTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/createUserTest.java @@ -61,12 +61,13 @@ public Object answer(InvocationOnMock invocation) throws Throwable { JSONObject response = new JSONObject( "{" + "\"result\": {" + - "\"_id\": \"foobar\"," + - "\"_source\": {" + - "\"indexes\": {}" + + "\"_id\": \"foobar\"," + + "\"_source\": {" + + "\"indexes\": {}" + + "}," + + "\"_meta\": {}" + "}" + - "}" + - "}"); + "}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/getProfileTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/getProfileTest.java index 618da05..3c40c38 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/getProfileTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/getProfileTest.java @@ -39,7 +39,7 @@ public void testGetProfileNoListener() throws JSONException { } @Test(expected = RuntimeException.class) - public void testgetProfileBadResponse() throws JSONException { + public void testGetProfileBadResponse() throws JSONException { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { @@ -55,11 +55,11 @@ public Object answer(InvocationOnMock invocation) throws Throwable { } @Test - public void testgetProfileGoodFullResponse() throws JSONException { + public void testGetProfileGoodFullResponse() throws JSONException { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - JSONObject response = new JSONObject("{\"result\":{\"_id\":\"foobar\",\"_source\":{\"policies\":[{\"roleId\":\"baz\",\"restrictedTo\":[{\"index\":\"qux\"}],\"allowInternalIndex\":true}]}}}"); + JSONObject response = new JSONObject("{\"result\":{\"_id\":\"foobar\",\"_source\":{\"policies\":[{\"roleId\":\"baz\",\"restrictedTo\":[{\"index\":\"qux\"}],\"allowInternalIndex\":true}]},\"_meta\":{}}}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); @@ -72,11 +72,11 @@ public Object answer(InvocationOnMock invocation) throws Throwable { } @Test - public void testgetProfileGoodMinimalResponse() throws JSONException { + public void testGetProfileGoodMinimalResponse() throws JSONException { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - JSONObject response = new JSONObject("{\"result\":{\"_id\":\"foobar\",\"_source\":{\"policies\":[{\"roleId\":\"baz\"}]}}}"); + JSONObject response = new JSONObject("{\"result\":{\"_id\":\"foobar\",\"_source\":{\"policies\":[{\"roleId\":\"baz\"}]},\"_meta\":{}}}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); @@ -89,11 +89,11 @@ public Object answer(InvocationOnMock invocation) throws Throwable { } @Test - public void testgetProfileGoodWithRestrictedToResponse() throws JSONException { + public void testGetProfileGoodWithRestrictedToResponse() throws JSONException { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - JSONObject response = new JSONObject("{\"result\":{\"_id\":\"foobar\",\"_source\":{\"policies\":[{\"roleId\":\"baz\"}],\"restrictedTo\":[{\"index\":\"qux\"}]}}}"); + JSONObject response = new JSONObject("{\"result\":{\"_id\":\"foobar\",\"_source\":{\"policies\":[{\"roleId\":\"baz\"}],\"restrictedTo\":[{\"index\":\"qux\"}]},\"_meta\":{}}}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); @@ -106,11 +106,11 @@ public Object answer(InvocationOnMock invocation) throws Throwable { } @Test - public void testgetProfileGoodWithAllowInternalIndexResponse() throws JSONException { + public void testGetProfileGoodWithAllowInternalIndexResponse() throws JSONException { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - JSONObject response = new JSONObject("{\"result\":{\"_id\":\"foobar\",\"_source\":{\"policies\":[{\"roleId\":\"baz\"}],\"allowInternalIndex\":true}}}"); + JSONObject response = new JSONObject("{\"result\":{\"_id\":\"foobar\",\"_source\":{\"policies\":[{\"roleId\":\"baz\"}],\"allowInternalIndex\":true},\"_meta\":{}}}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/getRoleTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/getRoleTest.java index 47041b8..f82c5d0 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/getRoleTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/getRoleTest.java @@ -53,12 +53,13 @@ public Object answer(InvocationOnMock invocation) throws Throwable { JSONObject response = new JSONObject( "{" + "\"result\": {" + - "\"_id\": \"foobar\"," + - "\"_source\": {" + - "\"indexes\": {}" + + "\"_id\": \"foobar\"," + + "\"_source\": {" + + "\"indexes\": {}" + + "}," + + "\"_meta\": {}" + "}" + - "}" + - "}"); + "}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); @@ -86,7 +87,7 @@ public void onError(JSONObject error) { ArgumentCaptor argument = ArgumentCaptor.forClass(io.kuzzle.sdk.core.Kuzzle.QueryArgs.class); verify(kuzzle, times(1)).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, "security"); - assertEquals(((io.kuzzle.sdk.core.Kuzzle.QueryArgs) argument.getValue()).action, "fetchRole"); + assertEquals(((io.kuzzle.sdk.core.Kuzzle.QueryArgs) argument.getValue()).action, "getRole"); } @Test(expected = RuntimeException.class) diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/getUserTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/getUserTest.java index 3ab119a..9cd063c 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/getUserTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/getUserTest.java @@ -45,17 +45,18 @@ public void testGetUserNoListener() throws JSONException { } @Test - public void testgetUserValidResponse() throws JSONException { + public void testGetUserValidResponse() throws JSONException { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { JSONObject response = new JSONObject( "{" + "\"result\": {" + - "\"_id\": \"foobar\"," + - "\"_source\": {}" + + "\"_id\": \"foobar\"," + + "\"_source\": {}," + + "\"_meta\": {}" + "}" + - "}"); + "}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); @@ -82,11 +83,11 @@ public void onError(JSONObject error) { ArgumentCaptor argument = ArgumentCaptor.forClass(io.kuzzle.sdk.core.Kuzzle.QueryArgs.class); verify(kuzzle, times(1)).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, "security"); - assertEquals(((io.kuzzle.sdk.core.Kuzzle.QueryArgs) argument.getValue()).action, "fetchUser"); + assertEquals(((io.kuzzle.sdk.core.Kuzzle.QueryArgs) argument.getValue()).action, "getUser"); } @Test(expected = RuntimeException.class) - public void testgetUserBadResponse() throws JSONException { + public void testGetUserBadResponse() throws JSONException { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/replaceUserTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/replaceUserTest.java index ffe95e7..c542fd9 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/replaceUserTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/replaceUserTest.java @@ -55,7 +55,8 @@ public Object answer(InvocationOnMock invocation) throws Throwable { "{" + "\"result\": {" + "\"_id\": \"foo\"," + - "\"_source\": {}" + + "\"_source\": {}," + + "\"_meta\": {}" + "}" + "}"); diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/scrollProfilesTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/scrollProfilesTest.java index fb2d42f..28a2e8c 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/scrollProfilesTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/scrollProfilesTest.java @@ -128,6 +128,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable { " \"status\": \"idle\",\n" + " \"type\": \"customer\"\n" + " },\n" + + " \"_meta\": {},\n" + " \"_type\": \"users\"\n" + " },\n" + " {\n" + @@ -143,6 +144,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable { " \"status\": \"idle\",\n" + " \"type\": \"cab\"\n" + " },\n" + + " \"_meta\": {},\n" + " \"_type\": \"users\"\n" + " }\n" + " ],\n" + diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/scrollUsersTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/scrollUsersTest.java index 9c1cf43..50505b6 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/scrollUsersTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/scrollUsersTest.java @@ -128,6 +128,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable { " \"status\": \"idle\",\n" + " \"type\": \"customer\"\n" + " },\n" + + " \"_meta\": {},\n" + " \"_type\": \"users\"\n" + " },\n" + " {\n" + @@ -143,6 +144,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable { " \"status\": \"idle\",\n" + " \"type\": \"cab\"\n" + " },\n" + + " \"_meta\": {},\n" + " \"_type\": \"users\"\n" + " }\n" + " ],\n" + diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/searchProfilesTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/searchProfilesTest.java index 917d93d..68f3fee 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/searchProfilesTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/searchProfilesTest.java @@ -52,18 +52,19 @@ public Object answer(InvocationOnMock invocation) throws Throwable { JSONObject response = new JSONObject( "{" + "\"result\": {" + - "\"hits\": [" + - "{" + - "\"_id\": \"foobar\"," + - "\"_source\": {" + - "\"_id\": \"foobar\"," + - "\"indexes\": {}" + + "\"hits\": [" + + "{" + + "\"_id\": \"foobar\"," + + "\"_source\": {" + + "\"_id\": \"foobar\"," + + "\"indexes\": {}" + + "}," + + "\"_meta\": {}" + + "}" + + "]," + + "\"total\": 1" + "}" + - "}" + - "]," + - "\"total\": 1" + - "}" + - "}"); + "}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/searchRolesTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/searchRolesTest.java index 8a3c6f4..831fbff 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/searchRolesTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/searchRolesTest.java @@ -52,18 +52,19 @@ public Object answer(InvocationOnMock invocation) throws Throwable { JSONObject response = new JSONObject( "{" + "\"result\": {" + - "\"hits\": [" + - "{" + - "\"_id\": \"foobar\"," + - "\"_source\": {" + - "\"_id\": \"foobar\"," + - "\"indexes\": {}" + + "\"hits\": [" + + "{" + + "\"_id\": \"foobar\"," + + "\"_source\": {" + + "\"_id\": \"foobar\"," + + "\"indexes\": {}" + + "}," + + "\"_meta\": {}" + + "}" + + "]," + + "\"total\": 1" + "}" + - "}" + - "]," + - "\"total\": 1" + - "}" + - "}"); + "}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/searchUsersTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/searchUsersTest.java index 913dfb0..547273c 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/searchUsersTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/searchUsersTest.java @@ -52,18 +52,19 @@ public Object answer(InvocationOnMock invocation) throws Throwable { JSONObject response = new JSONObject( "{" + "\"result\": {" + - "\"hits\": [" + - "{" + - "\"_id\": \"foobar\"," + - "\"_source\": {" + - "\"_id\": \"foobar\"," + - "\"indexes\": {}" + + "\"hits\": [" + + "{" + + "\"_id\": \"foobar\"," + + "\"_source\": {" + + "\"_id\": \"foobar\"," + + "\"indexes\": {}" + + "}," + + "\"_meta\": {}" + + "}" + + "]," + + "\"total\": 1" + "}" + - "}" + - "]," + - "\"total\": 1" + - "}" + - "}"); + "}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/updateProfileTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/updateProfileTest.java index 1fa3a4b..0341d0a 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/updateProfileTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/updateProfileTest.java @@ -53,10 +53,11 @@ public Object answer(InvocationOnMock invocation) throws Throwable { JSONObject response = new JSONObject( "{" + "\"result\": {" + - "\"_id\": \"foobar\"," + - "\"_source\": {}" + + "\"_id\": \"foobar\"," + + "\"_source\": {}," + + "\"_meta\": {}" + "}" + - "}"); + "}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/updateRoleTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/updateRoleTest.java index a6d620d..6af81e3 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/updateRoleTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/updateRoleTest.java @@ -52,12 +52,13 @@ public void testUpdateRoleValidResponse() throws JSONException { @Override public Object answer(InvocationOnMock invocation) throws Throwable { JSONObject response = new JSONObject( - "{" + - "\"result\": {" + - "\"_id\": \"foobar\"," + - "\"_source\": {}" + - "}" + - "}"); + "{" + + "\"result\": {" + + "\"_id\": \"foobar\"," + + "\"_source\": {}," + + "\"_meta\": {}" + + "}" + + "}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); diff --git a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/updateUserTest.java b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/updateUserTest.java index 749d09b..922e180 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleSecurity/updateUserTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleSecurity/updateUserTest.java @@ -52,12 +52,13 @@ public void testUpdateUserValidResponse() throws JSONException { @Override public Object answer(InvocationOnMock invocation) throws Throwable { JSONObject response = new JSONObject( - "{" + - "\"result\": {" + - "\"_id\": \"foobar\"," + - "\"_source\": {}" + - "}" + - "}"); + "{" + + "\"result\": {" + + "\"_id\": \"foobar\"," + + "\"_source\": {}," + + "\"_meta\": {}" + + "}" + + "}"); ((OnQueryDoneListener) invocation.getArguments()[3]).onSuccess(response); ((OnQueryDoneListener) invocation.getArguments()[3]).onError(new JSONObject().put("error", "stub")); diff --git a/src/test/java/io/kuzzle/test/security/KuzzleUserTest.java b/src/test/java/io/kuzzle/test/security/KuzzleUserTest.java index 7e877c7..cc80034 100644 --- a/src/test/java/io/kuzzle/test/security/KuzzleUserTest.java +++ b/src/test/java/io/kuzzle/test/security/KuzzleUserTest.java @@ -37,12 +37,12 @@ public class KuzzleUserTest { public void setUp() throws JSONException { kuzzle = mock(Kuzzle.class); kuzzle.security = new Security(kuzzle); - stubUser = new User(kuzzle, "foo", null); + stubUser = new User(kuzzle, "foo", null, null); } @Test public void testKuzzleUserConstructorNoContent() throws JSONException { - User user = new User(kuzzle, "foo", null); + User user = new User(kuzzle, "foo", null, null); assertEquals(user.id, "foo"); assertEquals(user.getProfileIds().length, 0); assertThat(user.content, instanceOf(JSONObject.class)); @@ -56,7 +56,7 @@ public void testKuzzleUserConstructorWithEmptyProfile() throws JSONException { "\"someuseless\": \"field\"" + "}" ); - User user = new User(kuzzle, "foo", stubProfile); + User user = new User(kuzzle, "foo", stubProfile, null); assertEquals(user.id, "foo"); assertEquals(user.getProfileIds()[0], "bar"); assertThat(user.content, instanceOf(JSONObject.class)); @@ -66,13 +66,26 @@ public void testKuzzleUserConstructorWithEmptyProfile() throws JSONException { @Test public void testKuzzleUserConstructorProfileWithContent() throws JSONException { JSONObject stubProfile = new JSONObject("{\"profileIds\": [\"bar\"]}"); - User user = new User(kuzzle, "foo", stubProfile); + User user = new User(kuzzle, "foo", stubProfile, null); assertEquals(user.id, "foo"); assertThat(user.getProfileIds(), instanceOf(String[].class)); assertEquals(user.getProfileIds()[0], "bar"); assertThat(user.content, instanceOf(JSONObject.class)); } + @Test + public void testKuzzleUserConstructorMeta() throws JSONException { + JSONObject meta = new JSONObject() + .put("createdAt", "0123456789") + .put("author", "-1"); + User user = new User(kuzzle, "foo", null, meta); + assertEquals(user.id, "foo"); + assertEquals(user.getMeta().length(), 2); + assertEquals(user.getMeta().getString("createdAt"), "0123456789"); + assertEquals(user.getMeta().getString("author"), "-1"); + assertThat(user.meta, instanceOf(JSONObject.class)); + } + @Test public void testSetProfileWithKuzzleProfile() throws JSONException { String[] ids = new String[1]; @@ -254,7 +267,7 @@ public void testGetProfileIds() throws JSONException { JSONObject stubProfile = new JSONObject( "{\"profileIds\": [\"bar\"]}" ); - User user = new User(kuzzle, "foo", stubProfile); + User user = new User(kuzzle, "foo", stubProfile, null); assertEquals(user.getProfileIds()[0], "bar"); } @@ -263,7 +276,7 @@ public void testAddProfile() throws JSONException { JSONObject stubProfile = new JSONObject( "{\"profileIds\": [\"bar\"]}" ); - User user = new User(kuzzle, "foo", stubProfile); + User user = new User(kuzzle, "foo", stubProfile, null); user.addProfile("new profile"); assertEquals(user.getProfileIds()[1], "new profile"); } @@ -273,20 +286,20 @@ public void testAddNullProfile() throws JSONException { JSONObject stubProfile = new JSONObject( "{\"profileIds\": [\"bar\"]}" ); - User user = new User(kuzzle, "foo", stubProfile); + User user = new User(kuzzle, "foo", stubProfile, null); user.addProfile(null); doThrow(IllegalArgumentException.class).when(user).addProfile(eq((String)null)); } @Test(expected = IllegalArgumentException.class) public void testGetProfilesException() throws JSONException { - User user = new User(kuzzle, "foo", new JSONObject()); + User user = new User(kuzzle, "foo", new JSONObject(), null); user.getProfiles(null); } @Test public void testGetProfilesEmpty() throws JSONException { - User user = new User(kuzzle, "foo", new JSONObject()); + User user = new User(kuzzle, "foo", new JSONObject(), null); user.getProfiles(new ResponseListener() { @Override @@ -304,7 +317,7 @@ public void onError(JSONObject error) { @Test public void testGetProfiles() throws JSONException { JSONArray profiles = new JSONArray().put("foo").put("bar").put("baz"); - User user = new User(kuzzle, "foo", new JSONObject().put("profileIds", profiles)); + User user = new User(kuzzle, "foo", new JSONObject().put("profileIds", profiles), null); Answer mockAnswer = new Answer() { @Override @@ -317,6 +330,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable { .put("_source", new JSONObject() .put("policies", new JSONArray()) ) + .put("_meta", new JSONObject()) ) ); return null; @@ -351,7 +365,7 @@ public void onError(JSONObject error) { @Test public void testGetProfilesError() throws JSONException { JSONArray profiles = new JSONArray().put("foo").put("bar").put("baz"); - User user = new User(kuzzle, "foo", new JSONObject().put("profileIds", profiles)); + User user = new User(kuzzle, "foo", new JSONObject().put("profileIds", profiles), null); final boolean[] invoked = {false}; Answer mockAnswer = new Answer() { diff --git a/src/test/java/io/kuzzle/test/testUtils/RoomExtend.java b/src/test/java/io/kuzzle/test/testUtils/RoomExtend.java index 34edfc0..4b463f7 100644 --- a/src/test/java/io/kuzzle/test/testUtils/RoomExtend.java +++ b/src/test/java/io/kuzzle/test/testUtils/RoomExtend.java @@ -10,9 +10,6 @@ import io.kuzzle.sdk.core.RoomOptions; import io.kuzzle.sdk.listeners.ResponseListener; -/** - * Created by scottinet on 19/02/16. - */ public class RoomExtend extends Room { public RoomExtend(Collection kuzzleDataCollection) {