Skip to content

Commit 25bce5e

Browse files
committed
[ECO-5338[Integration test] Created unittest setup class for initial setup
1. Updated LiveObjectTest to extend UnitTest class 2. Marked test utility methods as internal
1 parent af9deda commit 25bce5e

File tree

5 files changed

+61
-11
lines changed

5 files changed

+61
-11
lines changed

live-objects/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ tasks.withType<Test>().configureEach {
3434
tasks.register<Test>("runLiveObjectUnitTests") {
3535
filter {
3636
includeTestsMatching("io.ably.lib.objects.unit.*")
37+
exclude("**/UnitTest.class")
3738
}
3839
}
3940

live-objects/src/test/kotlin/io/ably/lib/objects/integration/setup/IntegrationTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package io.ably.lib.objects.integration.setup
22

33
import io.ably.lib.realtime.AblyRealtime
44
import io.ably.lib.realtime.Channel
5-
import io.ably.lib.types.ClientOptions
65
import kotlinx.coroutines.runBlocking
76
import org.junit.After
87
import org.junit.AfterClass
@@ -34,7 +33,7 @@ open class IntegrationTest {
3433
* @return The attached realtime channel.
3534
* @throws Exception If the channel fails to attach or the client fails to connect.
3635
*/
37-
suspend fun getRealtimeChannel(channelName: String, clientId: String = "client1"): Channel {
36+
internal suspend fun getRealtimeChannel(channelName: String, clientId: String = "client1"): Channel {
3837
val client = realtimeClients.getOrPut(clientId) {
3938
sandbox.createRealtimeClient {
4039
this.clientId = clientId
@@ -51,7 +50,7 @@ open class IntegrationTest {
5150
* Generates a unique channel name for testing purposes.
5251
* This is mainly to avoid channel name/state/history collisions across tests in same file.
5352
*/
54-
fun generateChannelName(): String {
53+
internal fun generateChannelName(): String {
5554
return "test-channel-${UUID.randomUUID()}"
5655
}
5756

live-objects/src/test/kotlin/io/ably/lib/objects/integration/setup/Sandbox.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Sandbox private constructor(val appId: String, val apiKey: String) {
4545
return JsonParser.parseString(fileContent).asJsonObject.get("post_apps")
4646
}
4747

48-
suspend fun createInstance(): Sandbox {
48+
internal suspend fun createInstance(): Sandbox {
4949
val response: HttpResponse = client.post("https://sandbox.realtime.ably-nonprod.net/apps") {
5050
contentType(ContentType.Application.Json)
5151
setBody(loadAppCreationJson().toString())
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package io.ably.lib.objects.unit
22

3-
import org.junit.Assert.assertTrue
3+
import io.ably.lib.objects.unit.setup.UnitTest
4+
import kotlinx.coroutines.test.runTest
45
import org.junit.Test
6+
import kotlin.test.assertNotNull
57

6-
class LiveObjectTest {
7-
8+
class LiveObjectTest : UnitTest() {
89
@Test
9-
fun testLiveObjectCreationUnitTest() {
10-
// This is a placeholder for the actual test implementation.
11-
// You would typically create instances of LiveObjects and perform assertions here.
12-
assertTrue(true)
10+
fun testChannelObjectGetterTest() = runTest {
11+
val channel = getMockRealtimeChannel("test-channel")
12+
val objects = channel.objects
13+
assertNotNull(objects)
1314
}
1415
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.ably.lib.objects.unit.setup
2+
3+
import io.ably.lib.objects.integration.setup.ensureAttached
4+
import io.ably.lib.realtime.AblyRealtime
5+
import io.ably.lib.realtime.Channel
6+
import io.ably.lib.realtime.ChannelState
7+
import io.ably.lib.types.ClientOptions
8+
import io.mockk.every
9+
import io.mockk.mockk
10+
import io.mockk.spyk
11+
import org.junit.After
12+
import org.junit.runner.RunWith
13+
import org.junit.runners.BlockJUnit4ClassRunner
14+
15+
// This class serves as a base for unit tests related to Live Objects.
16+
// It can be extended to include common setup or utility methods for unit tests.
17+
@RunWith(BlockJUnit4ClassRunner::class)
18+
open class UnitTest {
19+
20+
private val realtimeClients = mutableMapOf<String, AblyRealtime>()
21+
internal fun getMockRealtimeChannel(channelName: String, clientId: String = "client1"): Channel {
22+
val client = realtimeClients.getOrPut(clientId) {
23+
AblyRealtime(ClientOptions().apply {
24+
autoConnect = false
25+
key = "keyName:Value"
26+
this.clientId = clientId
27+
})
28+
}
29+
val channel = client.channels.get(channelName)
30+
return spyk(channel) {
31+
every { attach() } answers {
32+
state = ChannelState.attached
33+
}
34+
every { detach() } answers {
35+
state = ChannelState.detached
36+
}
37+
every { subscribe(any<String>(), any()) } returns mockk(relaxUnitFun = true)
38+
every { subscribe(any<Array<String>>(), any()) } returns mockk(relaxUnitFun = true)
39+
every { subscribe(any()) } returns mockk(relaxUnitFun = true)
40+
}.apply {
41+
state = ChannelState.attached
42+
}
43+
}
44+
45+
@After
46+
fun afterEach() {
47+
realtimeClients.clear()
48+
}
49+
}

0 commit comments

Comments
 (0)