Skip to content

Commit

Permalink
feat: applies changes in test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ChryssaAliferi committed Jul 18, 2024
1 parent f61648d commit 3e2ce25
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 118 deletions.
37 changes: 21 additions & 16 deletions core/src/test/java/com/rudderstack/core/AnalyticsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,6 @@ class CoreInputsPluginTest {
analytics.shutdown()
}

@Test
fun `intercept method proceeds without modification when storage is null`() {
// Arrange
val mockChain = mock<Plugin.Chain>()
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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())
}
})
}
)
Expand All @@ -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())
}
})
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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(
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,10 +31,12 @@ class GDPRPluginTest {
fun `test gdpr with opt out`() {
val analytics = mockk<Analytics>(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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -25,10 +26,6 @@ class MockConfigDownloadService(
// Not-required
}

override fun setup(analytics: Analytics) {
// Not-required
}

override fun shutdown() {
// Not-required
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message>()
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 {
Expand Down
Loading

0 comments on commit 3e2ce25

Please sign in to comment.