From 3e2ce25806bb29fec3311967e20cdf949eb63302 Mon Sep 17 00:00:00 2001 From: Chryssa Aliferi Date: Thu, 18 Jul 2024 19:00:38 +0300 Subject: [PATCH] feat: applies changes in test classes --- .../com/rudderstack/core/AnalyticsTest.kt | 37 +++++----- .../internal/plugins/CoreInputsPluginTest.kt | 19 ----- .../DestinationConfigurationPluginTest.kt | 44 ++++++------ .../plugins/EventSizeFilterPluginTest.kt | 21 ++++-- .../core/internal/plugins/GDPRPluginTest.kt | 22 +++--- .../plugins/RudderOptionPluginTest.kt | 69 +++++++++---------- .../testcommon/MockConfigDownloadService.kt | 5 +- .../testcommon/TestAnalyticsProvider.kt | 11 +-- .../testcommon/TestDataUploadService.kt | 6 +- 9 files changed, 116 insertions(+), 118 deletions(-) diff --git a/core/src/test/java/com/rudderstack/core/AnalyticsTest.kt b/core/src/test/java/com/rudderstack/core/AnalyticsTest.kt index ff3068ac..6a592d08 100644 --- a/core/src/test/java/com/rudderstack/core/AnalyticsTest.kt +++ b/core/src/test/java/com/rudderstack/core/AnalyticsTest.kt @@ -521,20 +521,21 @@ abstract class AnalyticsTest { } val isDone = AtomicBoolean(false) var msgCounter = 1 - val assertionPlugin = Plugin { - val msg = it.message() - assertThat( - msg, allOf( - Matchers.isA(TrackMessage::class.java), - hasProperty("eventName", `is`("event:${msgCounter++}")) + val assertPlugin = object : Plugin { + override lateinit var analytics: Analytics + override fun intercept(chain: Plugin.Chain): Message { + assertThat( + chain.message(), allOf( + Matchers.isA(TrackMessage::class.java), + hasProperty("eventName", `is`("event:${msgCounter++}")) + ) ) - ) - Thread.sleep(20L) //a minor delay - if (events.size < msgCounter) isDone.set(true) - it.proceed(it.message()) + Thread.sleep(20L) //a minor delay + if (events.size < msgCounter) isDone.set(true) + return chain.proceed(chain.message()) + } } - - analytics.addPlugin(assertionPlugin) + analytics.addPlugin(assertPlugin) for (i in events) { analytics.track(i) } @@ -784,11 +785,15 @@ abstract class AnalyticsTest { fun `test custom plugin`() { println("running test test custom plugin") val isDone = AtomicBoolean(false) - val customPlugin = Plugin { - println("inside custom plugin") - isDone.set(true) - it.proceed(it.message()) + val customPlugin = object : Plugin { + override lateinit var analytics: Analytics + override fun intercept(chain: Plugin.Chain): Message { + println("inside custom plugin") + isDone.set(true) + return chain.proceed(chain.message()) + } } + val waitUntil = AtomicBoolean(false) analytics.addCallback(object : Callback { override fun success(message: Message?) { diff --git a/core/src/test/java/com/rudderstack/core/internal/plugins/CoreInputsPluginTest.kt b/core/src/test/java/com/rudderstack/core/internal/plugins/CoreInputsPluginTest.kt index 96b7a9a9..1af9669a 100644 --- a/core/src/test/java/com/rudderstack/core/internal/plugins/CoreInputsPluginTest.kt +++ b/core/src/test/java/com/rudderstack/core/internal/plugins/CoreInputsPluginTest.kt @@ -42,25 +42,6 @@ class CoreInputsPluginTest { analytics.shutdown() } - @Test - fun `intercept method proceeds without modification when storage is null`() { - // Arrange - val mockChain = mock() - val mockMessage = TrackMessage.create("ev_name", RudderUtils.timeStamp) - `when`(mockChain.message()).thenReturn(mockMessage) - whenever(mockChain.proceed(any())) doAnswer { - it.getArgument(0) - } - - // Act - val result = coreInputsPlugin.intercept(mockChain) - - // Assert - assertThat(result, `is`(mockMessage)) - verify(mockChain).proceed(mockMessage) - } - - @Test fun `intercept method adds library context to message context when storage is not null`() { // Arrange diff --git a/core/src/test/java/com/rudderstack/core/internal/plugins/DestinationConfigurationPluginTest.kt b/core/src/test/java/com/rudderstack/core/internal/plugins/DestinationConfigurationPluginTest.kt index 522d2233..ab0663d6 100644 --- a/core/src/test/java/com/rudderstack/core/internal/plugins/DestinationConfigurationPluginTest.kt +++ b/core/src/test/java/com/rudderstack/core/internal/plugins/DestinationConfigurationPluginTest.kt @@ -1,10 +1,12 @@ package com.rudderstack.core.internal.plugins +import com.rudderstack.core.Analytics import com.rudderstack.core.BaseDestinationPlugin import com.rudderstack.core.DestinationPlugin import com.rudderstack.core.Plugin import com.rudderstack.core.RudderUtils import com.rudderstack.core.internal.CentralPluginChain +import com.rudderstack.core.models.Message import com.rudderstack.core.models.RudderServerConfig import com.rudderstack.core.models.TrackMessage import org.hamcrest.MatcherAssert.assertThat @@ -91,19 +93,18 @@ class DestinationConfigurationPluginTest { val centralPluginChain = defaultPluginChain!!.copy( plugins = defaultPluginChain!!.plugins.toMutableList().also { //after destination config plugin - it.add(0, Plugin { - //after processing the chain should be devoid of d-2 - assertThat( - it.plugins, allOf( - Matchers.hasItems(*(destinations.toMutableList().also { - it.removeIf { - it is DestinationPlugin<*> && it.name == "d-2" - } - }.toTypedArray())), - everyItem(not(destinations[1]/*isIn(shouldNotBeInList)*/)) + it.add(0, object : Plugin { + override lateinit var analytics: Analytics + override fun intercept(chain: Plugin.Chain): Message { + assertThat( + chain.plugins, allOf( + Matchers.hasItems(*(destinations.toMutableList().also { + it.removeIf { it is DestinationPlugin<*> && it.name == "d-2" } + }.toTypedArray())), everyItem(not(destinations[1]/*isIn(shouldNotBeInList)*/)) + ) ) - ) - it.proceed(it.message()) + return chain.proceed(chain.message()) + } }) } ) @@ -117,16 +118,17 @@ class DestinationConfigurationPluginTest { val centralPluginChain = defaultPluginChain!!.copy( plugins = defaultPluginChain!!.plugins.toMutableList().also { //after destination config plugin - it.add(0, Plugin { - //after processing the chain should be devoid of d-2 - assertThat( - it.plugins, allOf( - iterableWithSize(1), //the test plugin - //check there should be no destination plugin - everyItem(not(isA(DestinationPlugin::class.java))) + it.add(0, object : Plugin { + override lateinit var analytics: Analytics + override fun intercept(chain: Plugin.Chain): Message { + assertThat( + chain.plugins, allOf( + iterableWithSize(1), + everyItem(not(isA(DestinationPlugin::class.java))) + ) ) - ) - it.proceed(it.message()) + return chain.proceed(chain.message()) + } }) } ) diff --git a/core/src/test/java/com/rudderstack/core/internal/plugins/EventSizeFilterPluginTest.kt b/core/src/test/java/com/rudderstack/core/internal/plugins/EventSizeFilterPluginTest.kt index e9dae4fc..4f859489 100644 --- a/core/src/test/java/com/rudderstack/core/internal/plugins/EventSizeFilterPluginTest.kt +++ b/core/src/test/java/com/rudderstack/core/internal/plugins/EventSizeFilterPluginTest.kt @@ -1,14 +1,15 @@ package com.rudderstack.core.internal.plugins +import com.rudderstack.core.Analytics import com.rudderstack.core.Configuration import com.rudderstack.core.Plugin import com.rudderstack.core.RudderUtils import com.rudderstack.core.RudderUtils.MAX_EVENT_SIZE import com.rudderstack.core.RudderUtils.getUTF8Length import com.rudderstack.core.internal.CentralPluginChain -import com.rudderstack.gsonrudderadapter.GsonAdapter import com.rudderstack.core.models.Message import com.rudderstack.core.models.TrackMessage +import com.rudderstack.gsonrudderadapter.GsonAdapter import org.junit.Test class EventSizeFilterPluginTest { @@ -19,9 +20,12 @@ class EventSizeFilterPluginTest { @Test fun `given event size does not exceed the maximum size, then the next plugin in the chain should be called`() { var isCalled = false - val testPlugin = Plugin { - isCalled = true - it.proceed(it.message()) + val testPlugin = object : Plugin { + override lateinit var analytics: Analytics + override fun intercept(chain: Plugin.Chain): Message { + isCalled = true + return chain.proceed(chain.message()) + } } val message = getMessageUnderMaxSize() val eventSizeFilterTestChain = CentralPluginChain( @@ -39,9 +43,12 @@ class EventSizeFilterPluginTest { @Test fun `given event size exceeds the maximum size, then the next plugin in the chain should not be called`() { var isCalled = false - val testPlugin = Plugin { - isCalled = true - it.proceed(it.message()) + val testPlugin = object : Plugin { + override lateinit var analytics: Analytics + override fun intercept(chain: Plugin.Chain): Message { + isCalled = true + return chain.proceed(chain.message()) + } } val message = getMessageOverMaxSize() val eventSizeFilterTestChain = CentralPluginChain( diff --git a/core/src/test/java/com/rudderstack/core/internal/plugins/GDPRPluginTest.kt b/core/src/test/java/com/rudderstack/core/internal/plugins/GDPRPluginTest.kt index 3b4e1783..db5ff20b 100644 --- a/core/src/test/java/com/rudderstack/core/internal/plugins/GDPRPluginTest.kt +++ b/core/src/test/java/com/rudderstack/core/internal/plugins/GDPRPluginTest.kt @@ -4,6 +4,7 @@ import com.rudderstack.core.Analytics import com.rudderstack.core.Plugin import com.rudderstack.core.RudderUtils import com.rudderstack.core.internal.CentralPluginChain +import com.rudderstack.core.models.Message import com.rudderstack.core.models.TrackMessage import io.mockk.every import io.mockk.mockk @@ -30,10 +31,12 @@ class GDPRPluginTest { fun `test gdpr with opt out`() { val analytics = mockk(relaxed = true) every { analytics.storage.isOptedOut } returns true - val testPluginForOptOut = Plugin { - //should not be called - assert(false) - it.proceed(it.message()) + val testPluginForOptOut = object : Plugin { + override lateinit var analytics: Analytics + override fun intercept(chain: Plugin.Chain): Message { + assert(false) + return chain.proceed(chain.message()) + } } val optOutTestChain = CentralPluginChain( message, listOf(gdprPlugin, testPluginForOptOut), originalMessage = message @@ -51,11 +54,14 @@ class GDPRPluginTest { every { analytics.storage.isOptedOut } returns false var isCalled = false - val testPluginForOptIn = Plugin { - //should be called - isCalled = true - it.proceed(it.message()) + val testPluginForOptIn = object : Plugin { + override lateinit var analytics: Analytics + override fun intercept(chain: Plugin.Chain): Message { + isCalled = true + return chain.proceed(chain.message()) + } } + val optInTestChain = CentralPluginChain( message, listOf(gdprPlugin, testPluginForOptIn), originalMessage = message diff --git a/core/src/test/java/com/rudderstack/core/internal/plugins/RudderOptionPluginTest.kt b/core/src/test/java/com/rudderstack/core/internal/plugins/RudderOptionPluginTest.kt index b7aae00b..fb9ae613 100644 --- a/core/src/test/java/com/rudderstack/core/internal/plugins/RudderOptionPluginTest.kt +++ b/core/src/test/java/com/rudderstack/core/internal/plugins/RudderOptionPluginTest.kt @@ -1,10 +1,12 @@ package com.rudderstack.core.internal.plugins +import com.rudderstack.core.Analytics import com.rudderstack.core.BaseDestinationPlugin import com.rudderstack.core.Plugin import com.rudderstack.core.RudderOption import com.rudderstack.core.RudderUtils import com.rudderstack.core.internal.CentralPluginChain +import com.rudderstack.core.models.Message import com.rudderstack.core.models.TrackMessage import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.allOf @@ -45,16 +47,14 @@ class RudderOptionPluginTest { @Test fun `test all true for empty integrations`() { //assertion plugin - val assertPlugin = Plugin { - //must contain all plugins - assertThat( - it.plugins, allOf( - iterableWithSize(5), - hasItems(dest1, dest2, dest3) - ) - ) - return@Plugin it.proceed(it.message()) + val assertPlugin = object : Plugin { + override lateinit var analytics: Analytics + override fun intercept(chain: Plugin.Chain): Message { + assertThat(chain.plugins, allOf(iterableWithSize(5), hasItems(dest1, dest2, dest3))) + return chain.proceed(chain.message()) + } } + val chain = CentralPluginChain( message, listOf( RudderOptionPlugin(RudderOption()), assertPlugin, dest1, dest2, dest3 @@ -66,15 +66,12 @@ class RudderOptionPluginTest { @Test fun `test all false for integrations`() { //assertion plugin - val assertPlugin = Plugin { - //must contain all plugins - assertThat( - it.plugins, allOf( - iterableWithSize(2), - everyItem(not(`in`(arrayOf(dest1, dest2, dest3)))) - ) - ) - return@Plugin it.proceed(it.message()) + val assertPlugin = object : Plugin { + override lateinit var analytics: Analytics + override fun intercept(chain: Plugin.Chain): Message { + assertThat(chain.plugins, allOf(iterableWithSize(2), everyItem(not(`in`(arrayOf(dest1, dest2, dest3)))))) + return chain.proceed(chain.message()) + } } val chain = CentralPluginChain( message, listOf( @@ -90,16 +87,18 @@ class RudderOptionPluginTest { @Test fun `test custom integrations with false`() { //assertion plugin - val assertPlugin = Plugin { - //must contain all plugins - assertThat( - it.plugins, allOf( - iterableWithSize(3), - everyItem(not(`in`(arrayOf(dest2, dest3)))), - hasItem(dest1) + val assertPlugin = object : Plugin { + override lateinit var analytics: Analytics + override fun intercept(chain: Plugin.Chain): Message { + assertThat( + chain.plugins, allOf( + iterableWithSize(3), + everyItem(not(`in`(arrayOf(dest2, dest3)))), + hasItem(dest1) + ) ) - ) - return@Plugin it.proceed(it.message()) + return chain.proceed(chain.message()) + } } val chain = CentralPluginChain( message, listOf( @@ -116,16 +115,14 @@ class RudderOptionPluginTest { @Test fun `test custom integrations with true`() { //assertion plugin - val assertPlugin = Plugin { - //must contain all plugins - assertThat( - it.plugins, allOf( - iterableWithSize(3), - everyItem(not(`in`(arrayOf(dest1, dest3)))), - hasItem(dest2) + val assertPlugin = object : Plugin { + override lateinit var analytics: Analytics + override fun intercept(chain: Plugin.Chain): Message { + assertThat( + chain.plugins, allOf(iterableWithSize(3), everyItem(not(`in`(arrayOf(dest1, dest3)))), hasItem(dest2)) ) - ) - return@Plugin it.proceed(it.message()) + return chain.proceed(chain.message()) + } } val chain = CentralPluginChain( message, listOf( diff --git a/libs/test-common/src/main/java/com/vagabond/testcommon/MockConfigDownloadService.kt b/libs/test-common/src/main/java/com/vagabond/testcommon/MockConfigDownloadService.kt index 4d16f1fb..d98140c9 100644 --- a/libs/test-common/src/main/java/com/vagabond/testcommon/MockConfigDownloadService.kt +++ b/libs/test-common/src/main/java/com/vagabond/testcommon/MockConfigDownloadService.kt @@ -12,6 +12,7 @@ class MockConfigDownloadService( ) ) : ConfigDownloadService { + override lateinit var analytics: Analytics override fun download(callback: (success: Boolean, RudderServerConfig?, lastErrorMsg: String?) -> Unit) { callback(mockConfigDownloadSuccess, mockConfig, mockLastErrorMsg) @@ -25,10 +26,6 @@ class MockConfigDownloadService( // Not-required } - override fun setup(analytics: Analytics) { - // Not-required - } - override fun shutdown() { // Not-required } diff --git a/libs/test-common/src/main/java/com/vagabond/testcommon/TestAnalyticsProvider.kt b/libs/test-common/src/main/java/com/vagabond/testcommon/TestAnalyticsProvider.kt index c7c4038d..6c1de413 100644 --- a/libs/test-common/src/main/java/com/vagabond/testcommon/TestAnalyticsProvider.kt +++ b/libs/test-common/src/main/java/com/vagabond/testcommon/TestAnalyticsProvider.kt @@ -14,10 +14,13 @@ import com.rudderstack.rudderjsonadapter.JsonAdapter private const val DUMMY_WRITE_KEY = "DUMMY_WRITE_KEY" private var currentTestPlugin: Plugin? = null private var inputs = listOf() -val inputVerifyPlugin = Plugin { chain -> - chain.proceed(chain.message().also { - inputs += it.copy() - }) +val inputVerifyPlugin = object : Plugin { + override lateinit var analytics: Analytics + override fun intercept(chain: Plugin.Chain): Message { + return chain.proceed(chain.message().also { + inputs += it.copy() + }) + } } fun generateTestAnalytics(jsonAdapter: JsonAdapter): Analytics { diff --git a/libs/test-common/src/main/java/com/vagabond/testcommon/TestDataUploadService.kt b/libs/test-common/src/main/java/com/vagabond/testcommon/TestDataUploadService.kt index bce4866c..b0d4f1e6 100644 --- a/libs/test-common/src/main/java/com/vagabond/testcommon/TestDataUploadService.kt +++ b/libs/test-common/src/main/java/com/vagabond/testcommon/TestDataUploadService.kt @@ -6,11 +6,13 @@ import com.rudderstack.core.models.Message import com.rudderstack.web.HttpResponse class TestDataUploadService : DataUploadService { + + override lateinit var analytics: Analytics private var headers = mutableMapOf() var mockUploadStatus = 200 var mockUploadBody = "OK" - var errorBody : String? = null + var errorBody: String? = null var error: Throwable? = null override fun addHeaders(headers: Map) { this.headers += headers @@ -30,8 +32,6 @@ class TestDataUploadService : DataUploadService { return HttpResponse(mockUploadStatus, mockUploadBody, errorBody, error) } - override fun setup(analytics: Analytics) {} - override fun shutdown() { headers = mutableMapOf() }