diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleLocationsTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleLocationsTests.java index 41a3fd2c..6784b085 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleLocationsTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleLocationsTests.java @@ -32,7 +32,7 @@ public void disableLocation() { Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Location)); Countly.session().begin(); Countly.instance().location().disableLocation(); - validateLocationRequestInRQ(UserEditorTests.map("location", ""), 0); + validateLocationRequestInRQ(TestUtils.map("location", ""), 0); } /** @@ -57,7 +57,7 @@ public void setLocation() { Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Location)); Countly.session().begin(); Countly.instance().location().setLocation("US", "New York", "1,2", "1.1.1.1"); - validateLocationRequestInRQ(UserEditorTests.map("country_code", "US", "city", "New York", "location", "1,2", "ip", "1.1.1.1"), 0); + validateLocationRequestInRQ(TestUtils.map("country_code", "US", "city", "New York", "location", "1,2", "ip", "1.1.1.1"), 0); } /** @@ -70,7 +70,7 @@ public void setLocation_cityOnly() { Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Location)); Countly.session().begin(); Countly.instance().location().setLocation(null, "New York", "1,2", "1.1.1.1"); - validateLocationRequestInRQ(UserEditorTests.map("city", "New York", "location", "1,2", "ip", "1.1.1.1"), 0); + validateLocationRequestInRQ(TestUtils.map("city", "New York", "location", "1,2", "ip", "1.1.1.1"), 0); } /** @@ -82,9 +82,9 @@ public void setLocation_cityOnly() { public void setLocation_notBeganSession() { Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Location, Config.Feature.Sessions)); Countly.instance().location().setLocation("US", "New York", "1,2", "1.1.1.1"); - validateLocationRequestInRQ(UserEditorTests.map("country_code", "US", "city", "New York", "location", "1,2", "ip", "1.1.1.1"), 0); + validateLocationRequestInRQ(TestUtils.map("country_code", "US", "city", "New York", "location", "1,2", "ip", "1.1.1.1"), 0); Countly.session().begin(); - validateLocationRequestInRQ(UserEditorTests.map("country_code", "US", "city", "New York", "location", "1,2", "ip", "1.1.1.1", "begin_session", "1"), 1); + validateLocationRequestInRQ(TestUtils.map("country_code", "US", "city", "New York", "location", "1,2", "ip", "1.1.1.1", "begin_session", "1"), 1); } /** @@ -97,10 +97,10 @@ public void setLocation_notBeganSession_withConfig() throws InterruptedException Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Location, Config.Feature.Sessions) .setLocation("US", "New York", "1,2", "1.1.1.1")); Thread.sleep(100); - validateLocationRequestInRQ(UserEditorTests.map("country_code", "US", "city", "New York", "location", "1,2", "ip", "1.1.1.1"), 0); + validateLocationRequestInRQ(TestUtils.map("country_code", "US", "city", "New York", "location", "1,2", "ip", "1.1.1.1"), 0); Countly.session().begin(); Thread.sleep(100); - validateLocationRequestInRQ(UserEditorTests.map("country_code", "US", "city", "New York", "location", "1,2", "ip", "1.1.1.1", "begin_session", "1"), 1); + validateLocationRequestInRQ(TestUtils.map("country_code", "US", "city", "New York", "location", "1,2", "ip", "1.1.1.1", "begin_session", "1"), 1); } /** @@ -113,9 +113,9 @@ public void disableLocation_notBeganSession_withConfig() { Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Location, Config.Feature.Sessions) .setLocation("US", "New York", "1,2", "1.1.1.1") .disableLocation()); - validateLocationRequestInRQ(UserEditorTests.map("location", ""), 0); + validateLocationRequestInRQ(TestUtils.map("location", ""), 0); Countly.session().begin(); - validateLocationRequestInRQ(UserEditorTests.map("location", "", "begin_session", "1"), 1); + validateLocationRequestInRQ(TestUtils.map("location", "", "begin_session", "1"), 1); } /** diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleUserProfileTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleUserProfileTests.java index 82949720..c9713e3b 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleUserProfileTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleUserProfileTests.java @@ -45,7 +45,7 @@ public void setUserBasics() { Countly.instance().userProfile().save(); - UserEditorTests.validateUserDetailsRequestInRQ(UserEditorTests.map("user_details", UserEditorTests.json( + UserEditorTests.validateUserDetailsRequestInRQ(TestUtils.map("user_details", TestUtils.json( "name", "Test", "username", "TestUsername", "email", "test@test.test", @@ -77,10 +77,10 @@ public void clear() { Countly.instance().userProfile().clear(); Countly.instance().userProfile().save(); - UserEditorTests.validateUserDetailsRequestInRQ(UserEditorTests.map("user_details", UserEditorTests.json( + UserEditorTests.validateUserDetailsRequestInRQ(TestUtils.map("user_details", TestUtils.json( "name", "Test", "username", "TestUsername", - "custom", UserEditorTests.map("level", 56) + "custom", TestUtils.map("level", 56) ))); } @@ -95,7 +95,7 @@ public void setProperties_empty_null() { Countly.instance().userProfile().setProperties(null); Countly.instance().userProfile().setProperties(new ConcurrentHashMap<>()); Countly.instance().userProfile().save(); - UserEditorTests.validateUserDetailsRequestInRQ(UserEditorTests.map()); + UserEditorTests.validateUserDetailsRequestInRQ(TestUtils.map()); } /** @@ -109,7 +109,7 @@ public void increment() { Countly.instance().userProfile().increment("test"); Countly.instance().userProfile().incrementBy("test", 2); Countly.instance().userProfile().save(); - UserEditorTests.validateUserDetailsRequestInRQ(UserEditorTests.map("user_details", + UserEditorTests.validateUserDetailsRequestInRQ(TestUtils.map("user_details", UserEditorTests.c(UserEditorTests.opJson("test", "$inc", 3)) )); } @@ -128,7 +128,7 @@ public void saveMax_Min() { Countly.instance().userProfile().saveMin(TestUtils.eKeys[1], 2); Countly.instance().userProfile().saveMin(TestUtils.eKeys[1], 0.002); Countly.instance().userProfile().save(); - UserEditorTests.validateUserDetailsRequestInRQ(UserEditorTests.map("user_details", UserEditorTests.c( + UserEditorTests.validateUserDetailsRequestInRQ(TestUtils.map("user_details", UserEditorTests.c( UserEditorTests.opJson(TestUtils.eKeys[1], "$min", 0.002), UserEditorTests.opJson(TestUtils.eKeys[0], "$max", 9.62) ))); @@ -144,7 +144,7 @@ public void multiply() { Countly.instance().init(TestUtils.getBaseConfig()); Countly.instance().userProfile().multiply("test", 2); Countly.instance().userProfile().save(); - UserEditorTests.validateUserDetailsRequestInRQ(UserEditorTests.map("user_details", + UserEditorTests.validateUserDetailsRequestInRQ(TestUtils.map("user_details", UserEditorTests.c(UserEditorTests.opJson("test", "$mul", 2)) )); } @@ -193,7 +193,7 @@ public void pullPush_base(String op, BiConsumer opFunction) { Countly.instance().userProfile().save(); - UserEditorTests.validateUserDetailsRequestInRQ(UserEditorTests.map("user_details", UserEditorTests.c( + UserEditorTests.validateUserDetailsRequestInRQ(TestUtils.map("user_details", UserEditorTests.c( UserEditorTests.opJson(TestUtils.eKeys[3], op, TestUtils.eKeys[2]), UserEditorTests.opJson(TestUtils.eKeys[0], op, TestUtils.eKeys[1], TestUtils.eKeys[2], 89, TestUtils.eKeys[2], "") ) @@ -211,7 +211,7 @@ public void setOnce() { Countly.instance().userProfile().setOnce(TestUtils.eKeys[0], 56); Countly.instance().userProfile().setOnce(TestUtils.eKeys[0], TestUtils.eKeys[1]); Countly.instance().userProfile().save(); - UserEditorTests.validateUserDetailsRequestInRQ(UserEditorTests.map("user_details", UserEditorTests.c( + UserEditorTests.validateUserDetailsRequestInRQ(TestUtils.map("user_details", UserEditorTests.c( UserEditorTests.opJson(TestUtils.eKeys[0], "$setOnce", TestUtils.eKeys[1])))); } } diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/ScenarioLocationTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/ScenarioLocationTests.java index c306c280..6e5e6c1f 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/ScenarioLocationTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/ScenarioLocationTests.java @@ -43,7 +43,7 @@ public void setLocation_noInitTimeConfigForLocation() throws InterruptedExceptio Countly.instance().location().setLocation(null, null, "1,2", null); Thread.sleep(200); // wait for location req to be written - validateLocationRequestInRQ(UserEditorTests.map("location", "1,2"), 1); + validateLocationRequestInRQ(TestUtils.map("location", "1,2"), 1); endAndValidateEndSessionRequest(2); beginAndValidateSessionRequest(3, "begin_session", "1", "location", "1,2"); @@ -70,13 +70,13 @@ public void setLocationOnInitAndAfterInit() throws InterruptedException { Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Location, Config.Feature.Sessions) .setLocation("TR", "Izmir", null, null)); Thread.sleep(200); // wait for first init location req to be written - validateLocationRequestInRQ(UserEditorTests.map("city", "Izmir", "country_code", "TR"), 0); + validateLocationRequestInRQ(TestUtils.map("city", "Izmir", "country_code", "TR"), 0); beginAndValidateSessionRequest(1, "begin_session", "1", "city", "Izmir", "country_code", "TR"); Countly.instance().location().setLocation(null, null, "1,2", null); Thread.sleep(200); // wait for location req to be written - validateLocationRequestInRQ(UserEditorTests.map("location", "1,2"), 2); + validateLocationRequestInRQ(TestUtils.map("location", "1,2"), 2); endAndValidateEndSessionRequest(3); beginAndValidateSessionRequest(4, "begin_session", "1", "location", "1,2"); @@ -104,20 +104,20 @@ public void setLocationOnInitAndAfterBeginSession() throws InterruptedException Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Location, Config.Feature.Sessions) .setLocation("TR", "Izmir", null, null)); Thread.sleep(200); // wait for first init location req to be written - validateLocationRequestInRQ(UserEditorTests.map("city", "Izmir", "country_code", "TR"), 0); + validateLocationRequestInRQ(TestUtils.map("city", "Izmir", "country_code", "TR"), 0); beginAndValidateSessionRequest(1, "begin_session", "1", "city", "Izmir", "country_code", "TR"); Countly.instance().location().setLocation(null, null, "1,2", null); Thread.sleep(200); // wait for location req to be written - validateLocationRequestInRQ(UserEditorTests.map("location", "1,2"), 2); + validateLocationRequestInRQ(TestUtils.map("location", "1,2"), 2); endAndValidateEndSessionRequest(3); beginAndValidateSessionRequest(4, "begin_session", "1", "location", "1,2"); Countly.instance().location().setLocation(null, null, null, "1.1.1.1"); Thread.sleep(200); // wait for location req to be written - validateLocationRequestInRQ(UserEditorTests.map("ip", "1.1.1.1"), 5); + validateLocationRequestInRQ(TestUtils.map("ip", "1.1.1.1"), 5); endAndValidateEndSessionRequest(6); beginAndValidateSessionRequest(7, "begin_session", "1", "ip", "1.1.1.1"); @@ -141,17 +141,17 @@ public void setLocationOnInitAndBeforeBeginSession() throws InterruptedException Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Location, Config.Feature.Sessions) .setLocation("TR", "Izmir", null, null)); Thread.sleep(200); // wait for first location req to be written - validateLocationRequestInRQ(UserEditorTests.map("country_code", "TR", "city", "Izmir"), 0); + validateLocationRequestInRQ(TestUtils.map("country_code", "TR", "city", "Izmir"), 0); Countly.instance().location().setLocation(null, null, "1,2", "1.1.1.1"); Thread.sleep(200); // wait for location req to be written - validateLocationRequestInRQ(UserEditorTests.map("ip", "1.1.1.1", "location", "1,2"), 1); + validateLocationRequestInRQ(TestUtils.map("ip", "1.1.1.1", "location", "1,2"), 1); beginAndValidateSessionRequest(2, "begin_session", "1", "ip", "1.1.1.1", "location", "1,2"); Countly.instance().location().setLocation("TR", "Izmir", "3,4", null); Thread.sleep(200); // wait for location req to be written - validateLocationRequestInRQ(UserEditorTests.map("country_code", "TR", "location", "3,4", "city", "Izmir"), 3); + validateLocationRequestInRQ(TestUtils.map("country_code", "TR", "location", "3,4", "city", "Izmir"), 3); endAndValidateEndSessionRequest(4); beginAndValidateSessionRequest(5, "begin_session", "1", "country_code", "TR", "location", "3,4", "city", "Izmir"); @@ -161,13 +161,13 @@ public void setLocationOnInitAndBeforeBeginSession() throws InterruptedException private void beginAndValidateSessionRequest(int rqIdx, Object... params) throws InterruptedException { Countly.session().begin(); Thread.sleep(200); // wait for begin_session req to be written - validateLocationRequestInRQ(UserEditorTests.map(params), rqIdx); + validateLocationRequestInRQ(TestUtils.map(params), rqIdx); } private void endAndValidateEndSessionRequest(int rqIdx) throws InterruptedException { Countly.session().end(); Thread.sleep(200); // wait for end_session req to be written - validateLocationRequestInRQ(UserEditorTests.map("end_session", "1"), rqIdx); + validateLocationRequestInRQ(TestUtils.map("end_session", "1"), rqIdx); } /** diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java index fda18add..567a215b 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.Map; import java.util.Scanner; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Stream; import ly.count.sdk.java.Config; import ly.count.sdk.java.Countly; @@ -518,4 +519,56 @@ public String toString() { return value; } } + + /** + * Converts a map to json string + * + * @param entries map to convert + * @return json string + */ + protected static String json(Map entries) { + return jsonObj(entries).toString(); + } + + /** + * Converts a map to json object + * + * @param entries map to convert + * @return json string + */ + protected static JSONObject jsonObj(Map entries) { + JSONObject json = new JSONObject(); + entries.forEach(json::put); + return json; + } + + /** + * Converts array of objects to json string + * Returns empty json if array is null or empty + * + * @param args array of objects + * @return json string + */ + protected static String json(Object... args) { + if (args == null || args.length == 0) { + return "{}"; + } + return json(map(args)); + } + + /** + * Converts array of objects to a 'String, Object' map + * + * @param args array of objects + * @return map + */ + protected static Map map(Object... args) { + Map map = new ConcurrentHashMap<>(); + if (args.length % 2 == 0) { + for (int i = 0; i < args.length; i += 2) { + map.put(args[i].toString(), args[i + 1]); + } + } + return map; + } } \ No newline at end of file diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/UserEditorTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/UserEditorTests.java index a4dab7af..ff352903 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/UserEditorTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/UserEditorTests.java @@ -3,7 +3,6 @@ import java.io.File; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiFunction; import java.util.function.Supplier; import ly.count.sdk.java.Config; @@ -48,7 +47,7 @@ public void setPicturePath_webUrl() { //set profile picture url and commit it sessionHandler(() -> Countly.instance().user().edit().setPicturePath(imgFileWebUrl).commit()); validatePictureAndPath(imgFileWebUrl, null); - validateUserDetailsRequestInRQ(map("user_details", "{\"picture\":\"" + imgFileWebUrl + "\"}")); + validateUserDetailsRequestInRQ(TestUtils.map("user_details", "{\"picture\":\"" + imgFileWebUrl + "\"}")); } /** @@ -64,7 +63,7 @@ public void setPicturePath_localPath() { //set profile picture url and commit it sessionHandler(() -> Countly.instance().user().edit().setPicturePath(imgFile.getAbsolutePath()).commit()); validatePictureAndPath(imgFile.getAbsolutePath(), null); - validateUserDetailsRequestInRQ(map("user_details", "{}", "picturePath", imgFile.getAbsolutePath())); + validateUserDetailsRequestInRQ(TestUtils.map("user_details", "{}", "picturePath", imgFile.getAbsolutePath())); } /** @@ -79,7 +78,7 @@ public void setPicturePath_null() { //set profile picture url and commit it sessionHandler(() -> Countly.instance().user().edit().setPicturePath(null).commit()); validatePictureAndPath(null, null); - validateUserDetailsRequestInRQ(map("user_details", "{\"picture\":null}")); + validateUserDetailsRequestInRQ(TestUtils.map("user_details", "{\"picture\":null}")); } /** @@ -94,7 +93,7 @@ public void setPicturePath_garbage() { //set profile picture url and commit it sessionHandler(() -> Countly.instance().user().edit().setPicturePath("garbage_thing/.txt").commit()); validatePictureAndPath(null, null); - validateUserDetailsRequestInRQ(map()); + validateUserDetailsRequestInRQ(TestUtils.map()); } /** @@ -112,7 +111,7 @@ public void setPicture_binaryData() { sessionHandler(() -> Countly.instance().user().edit().setPicture(imgData).commit()); validatePictureAndPath(null, imgData); Countly.session().end(); - validateUserDetailsRequestInRQ(map("user_details", "{}", "picturePath", ModuleUserProfile.PICTURE_IN_USER_PROFILE)); + validateUserDetailsRequestInRQ(TestUtils.map("user_details", "{}", "picturePath", ModuleUserProfile.PICTURE_IN_USER_PROFILE)); } /** @@ -127,7 +126,7 @@ public void setPicture_null() { //set profile picture url and commit it sessionHandler(() -> Countly.instance().user().edit().setPicture(null).commit()); validatePictureAndPath(null, null); - validateUserDetailsRequestInRQ(map("user_details", "{\"picture\":null}")); + validateUserDetailsRequestInRQ(TestUtils.map("user_details", "{\"picture\":null}")); } /** @@ -142,7 +141,7 @@ public void setOnce() { .setOnce(TestUtils.eKeys[0], 56) .setOnce(TestUtils.eKeys[0], TestUtils.eKeys[1]) .commit()); - validateUserDetailsRequestInRQ(map("user_details", c(opJson(TestUtils.eKeys[0], "$setOnce", TestUtils.eKeys[1])))); + validateUserDetailsRequestInRQ(TestUtils.map("user_details", c(opJson(TestUtils.eKeys[0], "$setOnce", TestUtils.eKeys[1])))); } /** @@ -154,7 +153,7 @@ public void setOnce() { public void setOnce_null() { Countly.instance().init(TestUtils.getBaseConfig()); sessionHandler(() -> Countly.instance().user().edit().setOnce(TestUtils.eKeys[0], null).commit()); - validateUserDetailsRequestInRQ(map()); + validateUserDetailsRequestInRQ(TestUtils.map()); } /** @@ -166,7 +165,7 @@ public void setOnce_null() { public void setOnce_empty() { Countly.instance().init(TestUtils.getBaseConfig()); sessionHandler(() -> Countly.instance().user().edit().setOnce(TestUtils.eKeys[0], "").commit()); - validateUserDetailsRequestInRQ(map("user_details", c(opJson(TestUtils.eKeys[0], "$setOnce", "")))); + validateUserDetailsRequestInRQ(TestUtils.map("user_details", c(opJson(TestUtils.eKeys[0], "$setOnce", "")))); } /** @@ -184,7 +183,7 @@ public void setLocationBasics() { .setLocation(40.7128, -74.0060) .commit()); - validateUserDetailsRequestInRQ(map( + validateUserDetailsRequestInRQ(TestUtils.map( "country_code", "US", "city", "New York", "location", "40.7128,-74.006")); @@ -205,7 +204,7 @@ public void setLocationBasics_null() { .setLocation(null) .commit()); - validateUserDetailsRequestInRQ(map( + validateUserDetailsRequestInRQ(TestUtils.map( "country_code", JSONObject.NULL, "city", JSONObject.NULL, "location", JSONObject.NULL)); @@ -226,7 +225,7 @@ public void setLocationBasics_noConsent() { .setLocation(38.4237, 27.1428) .commit()); - validateUserDetailsRequestInRQ(map("locale", "tr")); + validateUserDetailsRequestInRQ(TestUtils.map("locale", "tr")); } /** @@ -273,7 +272,7 @@ private void pullPush_base(String op, BiFunction opF return opFunction.apply(TestUtils.eKeys[0], "").commit(); }); - validateUserDetailsRequestInRQ(map("user_details", c( + validateUserDetailsRequestInRQ(TestUtils.map("user_details", c( opJson(TestUtils.eKeys[3], op, TestUtils.eKeys[2]), opJson(TestUtils.eKeys[0], op, TestUtils.eKeys[1], TestUtils.eKeys[2], 89, TestUtils.eKeys[2], "") ) @@ -300,7 +299,7 @@ public void setCustom() { .setCustom("tags", new Object[] { "tag1", "tag2", 34, 67.8, null, "" }) .commit()); - validateUserDetailsRequestInRQ(map("user_details", c(map( + validateUserDetailsRequestInRQ(TestUtils.map("user_details", c(TestUtils.map( TestUtils.eKeys[0], TestUtils.eKeys[1], TestUtils.eKeys[5], "", TestUtils.eKeys[6], 128.987, @@ -325,7 +324,7 @@ public void max() { .commit() ); - validateUserDetailsRequestInRQ(map("user_details", c( + validateUserDetailsRequestInRQ(TestUtils.map("user_details", c( opJson(TestUtils.eKeys[2], "$max", 0), opJson(TestUtils.eKeys[1], "$max", -1), opJson(TestUtils.eKeys[0], "$max", 128))) @@ -349,7 +348,7 @@ public void min() { .commit() ); - validateUserDetailsRequestInRQ(map("user_details", c( + validateUserDetailsRequestInRQ(TestUtils.map("user_details", c( opJson(TestUtils.eKeys[2], "$min", 0), opJson(TestUtils.eKeys[1], "$min", -155.9), opJson(TestUtils.eKeys[0], "$min", 122))) @@ -373,7 +372,7 @@ public void inc() { .commit() ); - validateUserDetailsRequestInRQ(map("user_details", c( + validateUserDetailsRequestInRQ(TestUtils.map("user_details", c( opJson(TestUtils.eKeys[2], "$inc", 0), opJson(TestUtils.eKeys[1], "$inc", -155), opJson(TestUtils.eKeys[0], "$inc", 0))) @@ -400,7 +399,7 @@ public void mul() { .commit() ); - validateUserDetailsRequestInRQ(map("user_details", c( + validateUserDetailsRequestInRQ(TestUtils.map("user_details", c( opJson(TestUtils.eKeys[3], "$mul", -90), opJson(TestUtils.eKeys[2], "$mul", 0), opJson(TestUtils.eKeys[1], "$mul", -5.28), @@ -427,7 +426,7 @@ public void setUserBasics() { .commit() ); - validateUserDetailsRequestInRQ(map("user_details", json( + validateUserDetailsRequestInRQ(TestUtils.map("user_details", TestUtils.json( "name", "Test", "username", "TestUsername", "email", "test@test.test", @@ -457,7 +456,7 @@ public void setUserBasics_null() { .commit() ); - validateUserDetailsRequestInRQ(map("user_details", json( + validateUserDetailsRequestInRQ(TestUtils.map("user_details", TestUtils.json( "name", JSONObject.NULL, "username", JSONObject.NULL, "email", JSONObject.NULL, @@ -475,7 +474,7 @@ public void setUserBasics_null() { */ @Test public void setBirthYear_invalidParam() { - setBirthYear_base(TestUtils.eKeys[0], map()); + setBirthYear_base(TestUtils.eKeys[0], TestUtils.map()); } /** @@ -485,7 +484,7 @@ public void setBirthYear_invalidParam() { */ @Test public void setBirthYear_stringInteger() { - setBirthYear_base("1999", map("user_details", json("byear", 1999))); + setBirthYear_base("1999", TestUtils.map("user_details", TestUtils.json("byear", 1999))); } /** @@ -495,7 +494,7 @@ public void setBirthYear_stringInteger() { */ @Test public void setBirthYear_stringNotInteger() { - setBirthYear_base("1999.0", map()); + setBirthYear_base("1999.0", TestUtils.map()); } private void setBirthYear_base(String value, Map expectedValues) { @@ -511,7 +510,7 @@ private void setBirthYear_base(String value, Map expectedValues) */ @Test public void setGender_invalid() { - setGender_base("Non-Binary", map()); + setGender_base("Non-Binary", TestUtils.map()); } /** @@ -521,7 +520,7 @@ public void setGender_invalid() { */ @Test public void setGender_number() { - setGender_base(1, map()); + setGender_base(1, TestUtils.map()); } /** @@ -531,7 +530,7 @@ public void setGender_number() { */ @Test public void setGender_string() { - setGender_base("M", map("user_details", json("gender", "M"))); + setGender_base("M", TestUtils.map("user_details", TestUtils.json("gender", "M"))); } private void setGender_base(Object gender, Map expectedValues) { @@ -549,7 +548,7 @@ private void setGender_base(Object gender, Map expectedValues) { public void setLocation_fromString() { Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Location)); sessionHandler(() -> Countly.instance().user().edit().setLocation("-40.7128, 74.0060").commit()); - validateUserDetailsRequestInRQ(map("location", "-40.7128,74.006")); + validateUserDetailsRequestInRQ(TestUtils.map("location", "-40.7128,74.006")); } /** @@ -561,7 +560,7 @@ public void setLocation_fromString() { public void setLocation_fromString_noConsent() { Countly.instance().init(TestUtils.getBaseConfig()); sessionHandler(() -> Countly.instance().user().edit().setLocation("32.78, 28.01").commit()); - validateUserDetailsRequestInRQ(map()); + validateUserDetailsRequestInRQ(TestUtils.map()); } /** @@ -573,7 +572,7 @@ public void setLocation_fromString_noConsent() { public void setLocation_fromString_invalid() { Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Location)); sessionHandler(() -> Countly.instance().user().edit().setLocation(",28.34").commit()); - validateUserDetailsRequestInRQ(map()); + validateUserDetailsRequestInRQ(TestUtils.map()); } /** @@ -585,7 +584,7 @@ public void setLocation_fromString_invalid() { public void setLocation_fromString_onePair() { Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Location)); sessionHandler(() -> Countly.instance().user().edit().setLocation("61.32,").commit()); - validateUserDetailsRequestInRQ(map()); + validateUserDetailsRequestInRQ(TestUtils.map()); } /** @@ -597,7 +596,7 @@ public void setLocation_fromString_onePair() { public void setLocation_fromString_null() { Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Location)); sessionHandler(() -> Countly.instance().user().edit().setLocation(null).commit()); - validateUserDetailsRequestInRQ(map("location", JSONObject.NULL)); + validateUserDetailsRequestInRQ(TestUtils.map("location", JSONObject.NULL)); } /** @@ -609,7 +608,7 @@ public void setLocation_fromString_null() { public void optOutFromLocationServices() { Countly.instance().init(TestUtils.getBaseConfig().setFeatures(Config.Feature.Location)); sessionHandler(() -> Countly.instance().user().edit().optOutFromLocationServices().commit()); - validateUserDetailsRequestInRQ(map("location", JSONObject.NULL, "country_code", JSONObject.NULL, "city", JSONObject.NULL)); + validateUserDetailsRequestInRQ(TestUtils.map("location", JSONObject.NULL, "country_code", JSONObject.NULL, "city", JSONObject.NULL)); } /** @@ -634,7 +633,7 @@ public void set_notAString() { //.set(ModuleUserProfile.COUNTRY_KEY, new TestUtils.AtomicString("Not a country")) //.set(ModuleUserProfile.LOCALE_KEY, new TestUtils.AtomicString("Not a locale")) .commit()); - validateUserDetailsRequestInRQ(map("user_details", json("name", "Magical", + validateUserDetailsRequestInRQ(TestUtils.map("user_details", TestUtils.json("name", "Magical", "username", "TestUsername", "email", "test@test.ly", "organization", "Magical Org", @@ -663,12 +662,12 @@ public void set_multipleCalls_sessionsEnabled() { .setBirthyear("3000") .setPicturePath("https://someurl.com") .commit()); - validateUserDetailsRequestInRQ(map("user_details", json("name", "SomeName", + validateUserDetailsRequestInRQ(TestUtils.map("user_details", TestUtils.json("name", "SomeName", "byear", 3000, "gender", "F", "picture", "https://someurl.com", "email", "SomeEmail", - "custom", jsonObj(map(TestUtils.eKeys[0], map("$push", new Object[] { 56, "TW" }), "some_custom", 56))), + "custom", TestUtils.jsonObj(TestUtils.map(TestUtils.eKeys[0], TestUtils.map("$push", new Object[] { 56, "TW" }), "some_custom", 56))), "country_code", "US", "city", "New York", "location", "40.7128,-74.006"), 1, 3); @@ -745,7 +744,7 @@ protected static String c(String... entries) { * @return wrapped json */ private String c(Map entries) { - return "{\"custom\":" + json(entries) + "}"; + return "{\"custom\":" + TestUtils.json(entries) + "}"; } /** @@ -778,56 +777,4 @@ private void sessionHandler(Supplier process) { Assert.assertNotNull(process.get()); Countly.session().end(); } - - /** - * Converts a map to json string - * - * @param entries map to convert - * @return json string - */ - protected static String json(Map entries) { - return jsonObj(entries).toString(); - } - - /** - * Converts a map to json object - * - * @param entries map to convert - * @return json string - */ - protected static JSONObject jsonObj(Map entries) { - JSONObject json = new JSONObject(); - entries.forEach(json::put); - return json; - } - - /** - * Converts array of objects to json string - * Returns empty json if array is null or empty - * - * @param args array of objects - * @return json string - */ - protected static String json(Object... args) { - if (args == null || args.length == 0) { - return "{}"; - } - return json(map(args)); - } - - /** - * Converts array of objects to a 'String, Object' map - * - * @param args array of objects - * @return map - */ - protected static Map map(Object... args) { - Map map = new ConcurrentHashMap<>(); - if (args.length % 2 == 0) { - for (int i = 0; i < args.length; i += 2) { - map.put(args[i].toString(), args[i + 1]); - } - } - return map; - } }