Skip to content

Commit

Permalink
fix: after conflicts and merge
Browse files Browse the repository at this point in the history
  • Loading branch information
arifBurakDemiray committed May 23, 2024
1 parent e6dea3b commit 1332cd4
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public void testOnStop_reallyStopping_emptyEventQueue() {

assertEquals(0, mCountly.getActivityCount());
assertTrue(mCountly.getPrevSessionDurationStartTime() > 0);
verify(requestQueueProvider).endSession(0, null);
verify(requestQueueProvider).endSession(0);
verify(requestQueueProvider, times(0)).recordEvents(anyString());
}

Expand All @@ -415,7 +415,7 @@ public void testOnStop_reallyStopping_nonEmptyEventQueue() {

assertEquals(0, mCountly.getActivityCount());
assertTrue(mCountly.getPrevSessionDurationStartTime() > 0);
verify(requestQueueProvider).endSession(0, null);
verify(requestQueueProvider).endSession(0);
verify(requestQueueProvider).recordEvents(eventStr);
}

Expand Down
15 changes: 11 additions & 4 deletions sdk/src/androidTest/java/ly/count/android/sdk/DeviceIdTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public void sessionDurationScenario_1() throws InterruptedException {
config.lifecycleObserver = () -> true;

Countly countly = new Countly().init(config);
ModuleSessionsTests.validateSessionRequest(0, null, null, false);
ModuleSessionsTests.validateSessionRequest(0, null, null, false, true);

countly.userProfile().setProperty("prop1", "string");
countly.userProfile().setProperty("prop2", 123);
Expand All @@ -300,8 +300,10 @@ public void sessionDurationScenario_1() throws InterruptedException {

countly.deviceId().changeWithoutMerge("ff"); // this will generate a request with "end_session", "session_duration" fields and reset duration
assertEquals(4, TestUtils.getCurrentRQ().length);
ModuleSessionsTests.validateSessionRequest(1, 1, "ff_merge", false);
ModuleSessionsTests.validateSessionRequest(3, 2, null, true);

TestUtils.validateRequest("ff_merge", TestUtils.map("old_device_id", "1234"), 1);
TestUtils.validateRequest("ff_merge", TestUtils.map("user_details", "{\"custom\":{\"prop2\":\"123\",\"prop1\":\"string\",\"prop3\":\"false\"}}"), 2);
ModuleSessionsTests.validateSessionRequest(3, 3, "ff_merge", true, false);

Thread.sleep(1000);

Expand Down Expand Up @@ -330,6 +332,11 @@ public void sessionDurationScenario_1() throws InterruptedException {
countly.deviceId().changeWithMerge("ff_merge"); // this will generate a request with "session_duration" field and reset duration

assertEquals(8, TestUtils.getCurrentRQ().length);
ModuleSessionsTests.validateSessionRequest(7, 4, "ff_merge", false);

TestUtils.validateRequest("ff", TestUtils.map("user_details", "{\"custom\":{\"prop4\":\"[Ljava.lang.String;@f500e8a\"}}"), 4);
TestUtils.validateRequest("ff", TestUtils.map("user_details", "{\"custom\":{\"prop6\":\"{key=123}\",\"prop5\":\"{key=value}\",\"prop7\":\"{key=false}\"}}"), 5);
TestUtils.validateRequest("ff", TestUtils.map("user_details", "{\"custom\":{\"prop2\":\"456\",\"prop1\":\"string_a\",\"prop3\":\"true\"}}"), 6);

TestUtils.validateRequest("ff_merge", TestUtils.map("old_device_id", "ff"), 7);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,6 @@ private void appendIfNotNull(StringBuilder sb, String key, String value) {
}

void validateRequestsAreEqual(String required, String gotten) {
Assert.assertEquals(Utils.splitIntoParams(required), Utils.splitIntoParams(gotten));
Assert.assertEquals(Utils.splitIntoParams(required, mockLog), Utils.splitIntoParams(gotten, mockLog));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.mockito.Mockito;

@RunWith(AndroidJUnit4.class)
public class ModuleSessionsTests {
Expand All @@ -30,7 +26,7 @@ public void manualSessionBegin() {
Countly mCountly = new Countly().init(config);
mCountly.sessions().beginSession();

validateSessionRequest(0, null, null, false);
validateSessionRequest(0, null, null, false, true);
}

@Test
Expand All @@ -40,15 +36,15 @@ public void manualSessionBeginUpdateEnd() throws InterruptedException {

Assert.assertEquals(0, TestUtils.getCurrentRQ().length);
mCountly.sessions().beginSession();
validateSessionRequest(0, null, null, false);
validateSessionRequest(0, null, null, false, true);

Thread.sleep(1000);
mCountly.sessions().updateSession();
validateSessionRequest(1, 1, null, false);
validateSessionRequest(1, 1, null, false, false);

Thread.sleep(2000);
mCountly.sessions().endSession();
validateSessionRequest(2, 2, null, true);
validateSessionRequest(2, 2, null, true, false);
}

@Test
Expand Down Expand Up @@ -95,13 +91,13 @@ public void automaticSessionBeginEndWithManualDisabled() throws InterruptedExcep

Assert.assertEquals(0, TestUtils.getCurrentRQ().length);
mCountly.onStartInternal(null);
validateSessionRequest(0, null, null, false);
validateSessionRequest(0, null, null, false, true);

Thread.sleep(1000);

mCountly.onStopInternal();

validateSessionRequest(1, 1, null, true);
validateSessionRequest(1, 1, null, true, false);
}

/**
Expand All @@ -124,7 +120,6 @@ public void consentNotGivenNothingHappens() {
mCountly.onStart(Mockito.mock(TestUtils.Activity2.class));
mCountly.onStopInternal();


Assert.assertEquals(2, TestUtils.getCurrentRQ().length);
mCountly.sessions().beginSession();
Assert.assertEquals(2, TestUtils.getCurrentRQ().length);
Expand Down Expand Up @@ -168,7 +163,7 @@ public void manualSessionsNoReactionStartingSessionAgain() {

mCountly.sessions().beginSession();

validateSessionRequest(0, null, null, false);
validateSessionRequest(0, null, null, false, true);
mCountly.sessions().beginSession();

Assert.assertEquals(1, TestUtils.getCurrentRQ().length);
Expand Down Expand Up @@ -201,7 +196,7 @@ public void sessionBeginEndConsentChanges() throws InterruptedException {
RQ = TestUtils.getCurrentRQ();
Assert.assertEquals(4, RQ.length);

validateSessionRequest(2, null, null, false);
validateSessionRequest(2, null, null, false, true);

TestUtils.validateRequiredParams(RQ[3]);
Assert.assertEquals(consentForSession(true), RQ[3].get("consent"));
Expand All @@ -219,16 +214,16 @@ public void sessionBeginEndConsentChanges() throws InterruptedException {
RQ = TestUtils.getCurrentRQ();
Assert.assertEquals(6, RQ.length);

validateSessionRequest(4, 1, null, true);
validateSessionRequest(4, 1, null, true, false);
TestUtils.validateRequiredParams(RQ[5]); // this is consent request
Assert.assertEquals(consentForSession(false), RQ[0].get("consent"));
}

private String consentForSession(boolean consent) {
return "{\"sessions\":" + consent + ",\"crashes\":false,\"users\":false,\"push\":false,\"feedback\":false,\"scrolls\":false,\"remote-config\":false,\"attribution\":false,\"clicks\":false,\"location\":false,\"star-rating\":false,\"events\":false,\"views\":false,\"apm\":false}";
}
static void validateSessionRequest(int idx, Integer duration, String deviceId, boolean endSession) {

static void validateSessionRequest(int idx, Integer duration, String deviceId, boolean endSession, boolean beginSession) {
Map<String, String> request = TestUtils.getCurrentRQ()[idx];

if (deviceId != null) {
Expand All @@ -243,7 +238,9 @@ static void validateSessionRequest(int idx, Integer duration, String deviceId, b

if (duration != null) {
Assert.assertEquals(duration, Integer.valueOf(request.get("session_duration")));
} else {
}

if (beginSession) {
Assert.assertTrue(request.containsKey("begin_session"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Intent;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.util.Map;
import org.json.JSONException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -72,12 +71,12 @@ public void SE_200_CR_CG_M() throws InterruptedException {
flowManualSessions(countly);

Assert.assertEquals(6, TestUtils.getCurrentRQ().length);
validateConsentRequest(0, true);
validateConsentRequest(0, true, TestUtils.commonDeviceId);
validateRequest(TestUtils.map("location", ""), 1);
validateSessionBeginRequest(2);
validateSessionUpdateRequest(3, 2);
validateSessionUpdateRequest(4, 2);
validateSessionEndRequest(5, 2);
validateSessionBeginRequest(2, TestUtils.commonDeviceId);
validateSessionUpdateRequest(3, 2, TestUtils.commonDeviceId);
validateSessionUpdateRequest(4, 2, TestUtils.commonDeviceId);
validateSessionEndRequest(5, 2, TestUtils.commonDeviceId);
}

/**
Expand All @@ -98,10 +97,10 @@ public void SE_201_CNR_M() throws InterruptedException {
flowManualSessions(countly);

Assert.assertEquals(4, TestUtils.getCurrentRQ().length);
validateSessionBeginRequest(0);
validateSessionUpdateRequest(1, 2);
validateSessionUpdateRequest(2, 2);
validateSessionEndRequest(3, 2);
validateSessionBeginRequest(0, TestUtils.commonDeviceId);
validateSessionUpdateRequest(1, 2, TestUtils.commonDeviceId);
validateSessionUpdateRequest(2, 2, TestUtils.commonDeviceId);
validateSessionEndRequest(3, 2, TestUtils.commonDeviceId);
}

/**
Expand All @@ -123,7 +122,7 @@ public void SE_202_CR_CNG_M() throws InterruptedException {
flowManualSessions(countly);

Assert.assertEquals(2, TestUtils.getCurrentRQ().length);
validateConsentRequest(0, false);
validateConsentRequest(0, false, TestUtils.commonDeviceId);
validateRequest(TestUtils.map("location", ""), 1);
}

Expand Down Expand Up @@ -174,12 +173,12 @@ public void SE_203_CR_CG_M_id_change() throws InterruptedException {
countly.sessions().endSession();

Assert.assertEquals(6, TestUtils.getCurrentRQ().length);
validateConsentRequest(0, true);
validateConsentRequest(0, true, TestUtils.commonDeviceId);
validateRequest(TestUtils.map("location", ""), 1);
validateRequest(TestUtils.map("device_id", "newID", "session_duration", "2"), 2);
validateSessionBeginRequest(3);
validateSessionEndRequest(4, 2);
validateConsentRequest(5, false);
TestUtils.validateRequest("newID", TestUtils.map("session_duration", "2"), 2);
validateSessionBeginRequest(3, "newID");
validateSessionEndRequest(4, 2, "newID");
validateConsentRequest(5, false, TestUtils.commonDeviceId);
// TODO when RQ migration added add validation for device id change request

}
Expand Down Expand Up @@ -215,12 +214,13 @@ public void SE_204_CNR_A_id_change() throws InterruptedException {

flowAutomaticSessions(countly);

Assert.assertEquals(5, TestUtils.getCurrentRQ().length);
validateSessionBeginRequest(0);
validateRequest(TestUtils.map("device_id", "newID", "session_duration", "1"), 1);
validateSessionEndRequest(2, 1);
validateRequest(TestUtils.map("device_id", "newID", "session_duration", "1"), 3);
validateRequest(TestUtils.map("device_id", "newID", "session_duration", "3"), 4);
Assert.assertEquals(6, TestUtils.getCurrentRQ().length);
TestUtils.validateRequest("newID", TestUtils.map("old_device_id", TestUtils.commonDeviceId), 0);
TestUtils.validateRequest("newID", TestUtils.map("old_device_id", "newID_2"), 1);
TestUtils.validateRequest("newID", TestUtils.map("old_device_id", "newID_2"), 2);
validateSessionBeginRequest(3, "newID");
// 4 is orientation change
validateSessionEndRequest(5, null, "newID");
}

/**
Expand All @@ -246,22 +246,23 @@ public void SE_204_CNR_A_id_change() throws InterruptedException {
* *back to foreground*
* --- Expected Requests ---
* Check request queue and verify:
* 1. too lazy to calculate just let us know and lets verify together
* 1. too lazy to calculate just let us know and let's verify together
*/
@Test
public void SE_205_CR_CG_A_id_change() throws InterruptedException, JSONException {
public void SE_205_CR_CG_A_id_change() throws InterruptedException {
CountlyConfig config = TestUtils.createBaseConfig().setRequiresConsent(true).setConsentEnabled(new String[] { "sessions" });
Countly countly = new Countly().init(config);

flowAutomaticSessions(countly);

Assert.assertEquals(6, TestUtils.getCurrentRQ().length);
validateConsentRequest(0, true);
Assert.assertEquals(7, TestUtils.getCurrentRQ().length);
validateConsentRequest(0, true, TestUtils.commonDeviceId);
validateRequest(TestUtils.map("location", ""), 1);
validateRequest(TestUtils.map("device_id", "newID", "session_duration", "1"), 2);
validateConsentRequest(3, false);
validateRequest(TestUtils.map("device_id", "newID"), 4);
validateRequest(TestUtils.map("device_id", "newID"), 5);
TestUtils.validateRequest("newID", TestUtils.map("old_device_id", TestUtils.commonDeviceId), 2);
validateConsentRequest(3, false, "newID");
TestUtils.validateRequest("newID", TestUtils.map("old_device_id", "newID_2"), 4);
validateConsentRequest(5, false, "newID");
TestUtils.validateRequest("newID", TestUtils.map("old_device_id", "newID_2"), 6);
}

/**
Expand Down Expand Up @@ -296,13 +297,14 @@ public void SE_206_CR_CNG_A_id_change() throws InterruptedException {

flowAutomaticSessions(countly);

Assert.assertEquals(6, TestUtils.getCurrentRQ().length);
validateConsentRequest(0, false);
Assert.assertEquals(7, TestUtils.getCurrentRQ().length);
validateConsentRequest(0, false, TestUtils.commonDeviceId);
validateRequest(TestUtils.map("location", ""), 1);
validateRequest(TestUtils.map("device_id", "newID"), 2);
validateConsentRequest(3, false);
validateRequest(TestUtils.map("device_id", "newID"), 4);
validateRequest(TestUtils.map("device_id", "newID"), 5);
TestUtils.validateRequest("newID", TestUtils.map("old_device_id", TestUtils.commonDeviceId), 2);
validateConsentRequest(3, false, "newID");
TestUtils.validateRequest("newID", TestUtils.map("old_device_id", "newID_2"), 4);
validateConsentRequest(5, false, "newID");
TestUtils.validateRequest("newID", TestUtils.map("old_device_id", "newID_2"), 6);
}

private void flowManualSessions(Countly countly) throws InterruptedException {
Expand Down Expand Up @@ -345,36 +347,29 @@ private void flowAutomaticSessions(Countly countly) throws InterruptedException
bringAppToForeground();
}

private void validateSessionBeginRequest(int idx) {
Map<String, String> request = TestUtils.getCurrentRQ()[idx];

TestUtils.validateRequiredParams(TestUtils.getCurrentRQ()[idx]);
Assert.assertEquals("1", request.get("begin_session"));
private void validateSessionBeginRequest(int idx, String deviceId) {
TestUtils.validateRequest(deviceId, TestUtils.map("begin_session", "1"), idx);
}

private void validateSessionEndRequest(int idx, Integer duration) {
Map<String, String> request = validateSessionUpdateRequest(idx, duration);
private void validateSessionEndRequest(int idx, Integer duration, String deviceId) {
Map<String, String> request = validateSessionUpdateRequest(idx, duration, deviceId);
Assert.assertEquals("1", request.get("end_session"));
}

private Map<String, String> validateSessionUpdateRequest(int idx, Integer duration) {
private Map<String, String> validateSessionUpdateRequest(int idx, Integer duration, String deviceId) {
Map<String, String> request = TestUtils.getCurrentRQ()[idx];

TestUtils.validateRequiredParams(TestUtils.getCurrentRQ()[idx]);
TestUtils.validateRequiredParams(TestUtils.getCurrentRQ()[idx], deviceId);
if (duration != null) {
Assert.assertEquals(duration.toString(), request.get("session_duration"));
}

return request;
}

private void validateConsentRequest(int idx, boolean consentForSession) {
Map<String, String> request = TestUtils.getCurrentRQ()[idx];

TestUtils.validateRequiredParams(TestUtils.getCurrentRQ()[idx]);
Assert.assertEquals(
"{\"sessions\":" + consentForSession + ",\"crashes\":false,\"users\":false,\"push\":false,\"feedback\":false,\"scrolls\":false,\"remote-config\":false,\"attribution\":false,\"clicks\":false,\"location\":false,\"star-rating\":false,\"events\":false,\"views\":false,\"apm\":false}",
request.get("consent"));
private void validateConsentRequest(int idx, boolean consentForSession, String deviceId) {
TestUtils.validateRequest(deviceId, TestUtils.map("consent",
"{\"sessions\":" + consentForSession + ",\"crashes\":false,\"users\":false,\"push\":false,\"feedback\":false,\"scrolls\":false,\"remote-config\":false,\"attribution\":false,\"clicks\":false,\"location\":false,\"star-rating\":false,\"events\":false,\"views\":false,\"apm\":false}"), idx);
}

private void sendAppToBackground() {
Expand All @@ -391,11 +386,6 @@ private void bringAppToForeground() {
}

private void validateRequest(Map<String, Object> expectedExtras, int idx) {
Map<String, String> request = TestUtils.getCurrentRQ()[idx];

TestUtils.validateRequiredParams(TestUtils.getCurrentRQ()[idx]);
for (Map.Entry<String, Object> entry : expectedExtras.entrySet()) {
Assert.assertEquals(entry.getValue(), request.get(entry.getKey()));
}
TestUtils.validateRequest(TestUtils.commonDeviceId, expectedExtras, idx);
}
}
9 changes: 9 additions & 0 deletions sdk/src/androidTest/java/ly/count/android/sdk/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -584,4 +584,13 @@ protected static String generateRandomString(int length) {

return new String(array, java.nio.charset.StandardCharsets.UTF_8);
}

protected static void validateRequest(String deviceId, Map<String, Object> expectedExtras, int idx) {
Map<String, String> request = TestUtils.getCurrentRQ()[idx];

TestUtils.validateRequiredParams(TestUtils.getCurrentRQ()[idx], deviceId);
for (Map.Entry<String, Object> entry : expectedExtras.entrySet()) {
Assert.assertEquals(entry.getValue(), request.get(entry.getKey()));
}
}
}
Loading

0 comments on commit 1332cd4

Please sign in to comment.