Skip to content

Commit

Permalink
feat: simplify configuration objects (#461)
Browse files Browse the repository at this point in the history
* feat: refactor Configuration class

* feat: refactor ConfigurationAndroid class to data class

* chore: refactor builder classes

* chore: add JvmField annotation in Logger for Java interoperability

* chore: fix import

* chore(sample-app): use named argument for jsonAdapter

* chore: remove comments from Configuration class

* test: fix failing test cases
  • Loading branch information
1abhishekpandey authored Jul 12, 2024
1 parent 8e9ed09 commit 44726b2
Show file tree
Hide file tree
Showing 24 changed files with 573 additions and 823 deletions.
359 changes: 49 additions & 310 deletions android/src/main/java/com/rudderstack/android/ConfigurationAndroid.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class RudderAnalytics private constructor() {
storage: Storage = AndroidStorageImpl(
configuration.application,
writeKey = writeKey,
useContentProvider = ConfigurationAndroid.Defaults.USE_CONTENT_PROVIDER
useContentProvider = ConfigurationAndroid.USE_CONTENT_PROVIDER
),
dataUploadService: DataUploadService? = null,
configDownloadService: ConfigDownloadService? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
/*
* Creator: Debanjan Chatterjee on 02/12/23, 5:57 pm Last modified: 02/12/23, 5:57 pm
* Copyright: All rights reserved Ⓒ 2023 http://rudderstack.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain a
* copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.rudderstack.android.compat;

import android.app.Application;

import com.rudderstack.android.AndroidUtils;
import com.rudderstack.android.ConfigurationAndroid;
import com.rudderstack.android.internal.AndroidLogger;
import com.rudderstack.core.compat.ConfigurationBuilder;
import com.rudderstack.rudderjsonadapter.JsonAdapter;
import com.rudderstack.core.Logger;
Expand All @@ -30,19 +15,19 @@
public class ConfigurationAndroidBuilder extends ConfigurationBuilder {
private final Application application ;
private String anonymousId;
private Boolean trackLifecycleEvents = ConfigurationAndroid.Defaults.TRACK_LIFECYCLE_EVENTS;
private Boolean recordScreenViews = ConfigurationAndroid.Defaults.RECORD_SCREEN_VIEWS;
private Boolean isPeriodicFlushEnabled = ConfigurationAndroid.Defaults.IS_PERIODIC_FLUSH_ENABLED;
private Boolean autoCollectAdvertId = ConfigurationAndroid.Defaults.AUTO_COLLECT_ADVERT_ID;
private Boolean multiProcessEnabled = ConfigurationAndroid.Defaults.MULTI_PROCESS_ENABLED;
private String defaultProcessName= ConfigurationAndroid.Defaults.INSTANCE.getDEFAULT_PROCESS_NAME();
private Boolean trackLifecycleEvents = ConfigurationAndroid.TRACK_LIFECYCLE_EVENTS;
private Boolean recordScreenViews = ConfigurationAndroid.RECORD_SCREEN_VIEWS;
private Boolean isPeriodicFlushEnabled = ConfigurationAndroid.IS_PERIODIC_FLUSH_ENABLED;
private Boolean autoCollectAdvertId = ConfigurationAndroid.AUTO_COLLECT_ADVERT_ID;
private Boolean multiProcessEnabled = ConfigurationAndroid.MULTI_PROCESS_ENABLED;
private String defaultProcessName= ConfigurationAndroid.DEFAULT_PROCESS_NAME;
private String advertisingId = null;
private String deviceToken = null;
private boolean collectDeviceId = ConfigurationAndroid.Defaults.COLLECT_DEVICE_ID;
private boolean collectDeviceId = ConfigurationAndroid.COLLECT_DEVICE_ID;
private ExecutorService advertisingIdFetchExecutor = Executors.newCachedThreadPool();
private boolean trackAutoSession = ConfigurationAndroid.Defaults.AUTO_SESSION_TRACKING;
private long sessionTimeoutMillis = ConfigurationAndroid.Defaults.SESSION_TIMEOUT;
private Logger logger = new AndroidLogger();
private boolean trackAutoSession = ConfigurationAndroid.AUTO_SESSION_TRACKING;
private long sessionTimeoutMillis = ConfigurationAndroid.SESSION_TIMEOUT;
private Logger.LogLevel logLevel = Logger.DEFAULT_LOG_LEVEL;

public ConfigurationAndroidBuilder(Application application, JsonAdapter jsonAdapter) {
super(jsonAdapter);
Expand Down Expand Up @@ -99,7 +84,7 @@ public ConfigurationBuilder withSessionTimeoutMillis(long sessionTimeoutMillis)
}

public ConfigurationBuilder withLogLevel(Logger.LogLevel logLevel) {
this.logger = new AndroidLogger(logLevel);
this.logLevel = logLevel;
return this;
}

Expand All @@ -110,7 +95,7 @@ public ConfigurationBuilder withCollectDeviceId(boolean collectDeviceId) {

@Override
public ConfigurationAndroid build() {
return ConfigurationAndroid.Companion.invoke(super.build(),
return new ConfigurationAndroid(
application,
anonymousId,
trackLifecycleEvents,
Expand All @@ -121,11 +106,12 @@ public ConfigurationAndroid build() {
defaultProcessName,
advertisingId,
deviceToken,
logger,
logLevel,
collectDeviceId,
advertisingIdFetchExecutor,
trackAutoSession,
sessionTimeoutMillis
sessionTimeoutMillis,
jsonAdapter
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
/*
* Creator Debanjan Chatterjee on 10/10/22, 528 PM Last modified 10/10/22, 528 PM
* Copyright All rights reserved Ⓒ 2022 http//rudderstack.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain a
* copy of the License at http//www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.rudderstack.android.compat;


Expand Down Expand Up @@ -40,7 +26,7 @@ public final class RudderAnalyticsBuilderCompat {
private ConfigDownloadService configDownloadService = null;
private InitializationListener initializationListener = null;
private AndroidStorage storage = new AndroidStorageImpl(configuration.getApplication(),
ConfigurationAndroid.Defaults.USE_CONTENT_PROVIDER,
ConfigurationAndroid.USE_CONTENT_PROVIDER,
writeKey,
Executors.newSingleThreadExecutor());

Expand Down
32 changes: 20 additions & 12 deletions android/src/test/java/com/rudderstack/android/AndroidStorageTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package com.rudderstack.android
import android.os.Build
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.rudderstack.android.utils.TestExecutor
import com.rudderstack.android.utils.busyWait
import com.rudderstack.android.storage.AndroidStorage
import com.rudderstack.android.storage.AndroidStorageImpl
import com.rudderstack.android.utils.TestExecutor
import com.rudderstack.android.utils.busyWait
import com.rudderstack.core.Analytics
import com.rudderstack.core.Logger
import com.rudderstack.core.RudderUtils
import com.rudderstack.core.Storage
import com.rudderstack.core.models.TrackMessage
import com.rudderstack.gsonrudderadapter.GsonAdapter
import com.rudderstack.jacksonrudderadapter.JacksonAdapter
import com.rudderstack.core.models.TrackMessage
import com.rudderstack.rudderjsonadapter.JsonAdapter
import com.vagabond.testcommon.generateTestAnalytics
import junit.framework.TestSuite
Expand All @@ -28,7 +28,7 @@ import org.mockito.MockitoAnnotations
import org.mockito.junit.MockitoJUnitRunner
import org.mockito.kotlin.mock
import org.robolectric.annotation.Config
import java.util.*
import java.util.Date

/**
* Test class for testing the AndroidStorageImpl class
Expand All @@ -49,13 +49,20 @@ abstract class AndroidStorageTest {
false, writeKey = "test_writeKey",
storageExecutor = TestExecutor()
)
mockConfig = ConfigurationAndroid(ApplicationProvider.getApplicationContext(),
jsonAdapter, shouldVerifySdk = false, analyticsExecutor = TestExecutor(),
networkExecutor = TestExecutor(), flushQueueSize = 200, maxFlushInterval = 1000,
mockConfig = ConfigurationAndroid(
application = ApplicationProvider.getApplicationContext(),
jsonAdapter = jsonAdapter,
shouldVerifySdk = false,
analyticsExecutor = TestExecutor(),
networkExecutor = TestExecutor(),
flushQueueSize = 200,
maxFlushInterval = 1000,
logLevel = Logger.LogLevel.DEBUG,
)
analytics = generateTestAnalytics( mockConfig, storage = storage,
dataUploadService = mock(), configDownloadService = mock())
)
analytics = generateTestAnalytics(
mockConfig, storage = storage,
dataUploadService = mock(), configDownloadService = mock()
)
}

@After
Expand Down Expand Up @@ -150,7 +157,7 @@ abstract class AndroidStorageTest {

@Test
fun `test save and retrieve sessionId`() {
val storage = analytics.storage as AndroidStorage
val storage = analytics.storage as AndroidStorage
storage.clearStorage()
MatcherAssert.assertThat(storage.sessionId, Matchers.nullValue())
val sessionId = 123456L
Expand All @@ -159,8 +166,9 @@ abstract class AndroidStorageTest {
storage.clearSessionId()
MatcherAssert.assertThat(storage.sessionId, Matchers.nullValue())
}

@Test
fun `test delete sync`(){
fun `test delete sync`() {
val storage = analytics.storage as AndroidStorage
storage.clearStorage()
val events = (1..20).map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class RudderAnalyticsTest {
fun `when writeKey and configuration is passed, then getInstance should return Analytics instance`() {
val analytics = getInstance(
writeKey, ConfigurationAndroid(
ApplicationProvider.getApplicationContext(),
JacksonAdapter(),
application = ApplicationProvider.getApplicationContext(),
jsonAdapter = JacksonAdapter(),
logLevel = Logger.LogLevel.DEBUG,
)
)
Expand All @@ -36,16 +36,16 @@ class RudderAnalyticsTest {
val writeKey2 = "writeKey2"
val analytics = getInstance(
writeKey, ConfigurationAndroid(
ApplicationProvider.getApplicationContext(),
JacksonAdapter(),
application = ApplicationProvider.getApplicationContext(),
jsonAdapter = JacksonAdapter(),
logLevel = Logger.LogLevel.DEBUG,
)
)

val analytics2 = getInstance(
writeKey2, ConfigurationAndroid(
ApplicationProvider.getApplicationContext(),
JacksonAdapter(),
application = ApplicationProvider.getApplicationContext(),
jsonAdapter = JacksonAdapter(),
logLevel = Logger.LogLevel.DEBUG,
)
)
Expand All @@ -59,16 +59,16 @@ class RudderAnalyticsTest {
fun `given that the SDK supports a singleton instance, when an attempt is made to create multiple instance with the same writeKey, then both instances should remain the same`() {
val analytics = getInstance(
writeKey, ConfigurationAndroid(
ApplicationProvider.getApplicationContext(),
JacksonAdapter(),
application = ApplicationProvider.getApplicationContext(),
jsonAdapter = JacksonAdapter(),
logLevel = Logger.LogLevel.DEBUG,
)
)

val analytics2 = getInstance(
writeKey, ConfigurationAndroid(
ApplicationProvider.getApplicationContext(),
JacksonAdapter(),
application = ApplicationProvider.getApplicationContext(),
jsonAdapter = JacksonAdapter(),
logLevel = Logger.LogLevel.DEBUG,
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.rudderstack.android.internal.extensions

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.Test
import org.hamcrest.Matchers.hasEntry
import org.hamcrest.Matchers.hasKey
import org.hamcrest.Matchers.not
import org.hamcrest.Matchers.nullValue
import org.hamcrest.Matchers.sameInstance
import org.junit.Test


private const val CONTEXT_SESSION_ID_KEY = "sessionId"
private const val CONTEXT_SESSION_START_KEY = "sessionStart"
class UserSessionExtensionTest {



class UserSessionExtensionTest {
@Test
fun `withSessionId should add sessionId to MessageContext`() {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import android.app.Application
import android.os.Build
import androidx.test.core.app.ApplicationProvider
import com.rudderstack.android.ConfigurationAndroid
import com.rudderstack.android.utils.TestExecutor
import com.rudderstack.android.storage.AndroidStorage
import com.rudderstack.android.storage.AndroidStorageImpl
import com.rudderstack.android.utils.TestExecutor
import com.rudderstack.core.Analytics
import com.rudderstack.core.Logger
import com.rudderstack.gsonrudderadapter.GsonAdapter
import com.rudderstack.core.models.TrackMessage
import com.rudderstack.gsonrudderadapter.GsonAdapter
import com.rudderstack.rudderjsonadapter.JsonAdapter
import com.vagabond.testcommon.generateTestAnalytics
import org.hamcrest.MatcherAssert
import org.hamcrest.Matchers
import org.junit.After
import org.junit.Before
import org.junit.Test

import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
Expand Down Expand Up @@ -57,7 +56,8 @@ class AppInstallUpdateTrackerPluginTest {
storageExecutor = TestExecutor(),
)
val mockConfig = ConfigurationAndroid(
application, jsonAdapter,
application = application,
jsonAdapter = jsonAdapter,
shouldVerifySdk = false,
analyticsExecutor = TestExecutor(),
trackLifecycleEvents = trackLifecycleEvents,
Expand Down
Loading

0 comments on commit 44726b2

Please sign in to comment.