Skip to content

Commit

Permalink
Merge pull request #192 from Countly/move_functions
Browse files Browse the repository at this point in the history
feat: move convenient functions to utils
  • Loading branch information
arifBurakDemiray authored Dec 19, 2023
2 parents 7da3d55 + 3efa3bf commit 9120c8d
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]",
Expand Down Expand Up @@ -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)
)));
}

Expand All @@ -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());
}

/**
Expand All @@ -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))
));
}
Expand All @@ -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)
)));
Expand All @@ -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))
));
}
Expand Down Expand Up @@ -193,7 +193,7 @@ public void pullPush_base(String op, BiConsumer<String, Object> 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], "")
)
Expand All @@ -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]))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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);
}

/**
Expand Down
53 changes: 53 additions & 0 deletions sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> entries) {
return jsonObj(entries).toString();
}

/**
* Converts a map to json object
*
* @param entries map to convert
* @return json string
*/
protected static JSONObject jsonObj(Map<String, Object> 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<String, Object> map(Object... args) {
Map<String, Object> 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;
}
}
Loading

0 comments on commit 9120c8d

Please sign in to comment.