diff --git a/event-bridge/build.gradle b/event-bridge/build.gradle index a1a673c9..d59edf81 100644 --- a/event-bridge/build.gradle +++ b/event-bridge/build.gradle @@ -6,4 +6,11 @@ plugins { java { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 -} \ No newline at end of file +} +test { + useJUnitPlatform() +} +dependencies { + testImplementation platform("org.junit:junit-bom:5.7.2") + testImplementation "org.junit.jupiter:junit-jupiter" +} diff --git a/event-bridge/src/main/java/com/amplitude/eventbridge/EventBridge.kt b/event-bridge/src/main/java/com/amplitude/eventbridge/EventBridge.kt index 35a829fe..08a4f755 100644 --- a/event-bridge/src/main/java/com/amplitude/eventbridge/EventBridge.kt +++ b/event-bridge/src/main/java/com/amplitude/eventbridge/EventBridge.kt @@ -18,6 +18,9 @@ interface EventReceiver { fun receive(channel: EventChannel, event: Event) } +/** + * Bridges to transfer data between modules, like analytics and experiment + */ interface EventBridge { fun sendEvent(channel: EventChannel, event: Event) fun setEventReceiver(channel: EventChannel, receiver: EventReceiver) diff --git a/event-bridge/src/main/java/com/amplitude/eventbridge/EventBridgeContainer.kt b/event-bridge/src/main/java/com/amplitude/eventbridge/EventBridgeContainer.kt index b7ccd7c2..d14cf8eb 100644 --- a/event-bridge/src/main/java/com/amplitude/eventbridge/EventBridgeContainer.kt +++ b/event-bridge/src/main/java/com/amplitude/eventbridge/EventBridgeContainer.kt @@ -1,5 +1,8 @@ package com.amplitude.eventbridge +/** + * Container of EventBridge + */ class EventBridgeContainer { companion object { diff --git a/event-bridge/src/test/kotlin/com/amplitude/eventbridge/EventBridgeContainerTest.kt b/event-bridge/src/test/kotlin/com/amplitude/eventbridge/EventBridgeContainerTest.kt new file mode 100644 index 00000000..d6f54363 --- /dev/null +++ b/event-bridge/src/test/kotlin/com/amplitude/eventbridge/EventBridgeContainerTest.kt @@ -0,0 +1,16 @@ +package com.amplitude.eventbridge + +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class EventBridgeContainerTest { + + @Test + fun `test getInstance return same instance for certain name`() { + val eventBridge1 = EventBridgeContainer.getInstance("testInstance") + val eventBridge2 = EventBridgeContainer.getInstance("testInstance") + Assertions.assertEquals(eventBridge1, eventBridge2) + } +} diff --git a/event-bridge/src/test/kotlin/com/amplitude/eventbridge/EventBridgeTest.kt b/event-bridge/src/test/kotlin/com/amplitude/eventbridge/EventBridgeTest.kt new file mode 100644 index 00000000..63380652 --- /dev/null +++ b/event-bridge/src/test/kotlin/com/amplitude/eventbridge/EventBridgeTest.kt @@ -0,0 +1,60 @@ +package com.amplitude.eventbridge + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class EventBridgeTest { + + @Test + fun `test addEventListener, sendEvent, listener called`() { + val testEvent = Event("test") + val identifyEvent = Event("identify") + val eventBridge = EventBridgeImpl() + var wasEventReceiverCalled = false + eventBridge.setEventReceiver( + EventChannel.EVENT, + object : EventReceiver { + override fun receive(channel: EventChannel, event: Event) { + assertEquals(event, testEvent) + wasEventReceiverCalled = true + } + } + ) + eventBridge.setEventReceiver( + EventChannel.IDENTIFY, + object : EventReceiver { + override fun receive(channel: EventChannel, event: Event) { + assertEquals(event, identifyEvent) + wasEventReceiverCalled = true + } + } + ) + eventBridge.sendEvent(EventChannel.EVENT, testEvent) + eventBridge.sendEvent(EventChannel.IDENTIFY, identifyEvent) + assertTrue(wasEventReceiverCalled) + } + + @Test + fun `test sendEvent first, add listener later and listener receive event`() { + val testEvent1 = Event("test1") + val testEvent2 = Event("test2") + val testEvent3 = Event("test3") + val eventBridge = EventBridgeImpl() + eventBridge.sendEvent(EventChannel.EVENT, testEvent1) + eventBridge.sendEvent(EventChannel.EVENT, testEvent2) + eventBridge.sendEvent(EventChannel.EVENT, testEvent3) + var eventCount = 0 + eventBridge.setEventReceiver( + EventChannel.EVENT, + object : EventReceiver { + override fun receive(channel: EventChannel, event: Event) { + eventCount++ + } + } + ) + assertEquals(3, eventCount) + } +}