|
| 1 | +package io.getstream.android.core.api.model.config |
| 2 | + |
| 3 | +import io.getstream.android.core.api.model.event.StreamClientWsEvent |
| 4 | +import io.getstream.android.core.api.serialization.StreamEventSerialization |
| 5 | +import io.getstream.android.core.api.serialization.StreamJsonSerialization |
| 6 | +import kotlin.test.Test |
| 7 | +import kotlin.test.assertEquals |
| 8 | +import kotlin.test.assertNull |
| 9 | +import kotlin.test.assertSame |
| 10 | + |
| 11 | +class StreamClientSerializationConfigTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + fun `json builder injects json implementation`() { |
| 15 | + val json = FakeJsonSerialization() |
| 16 | + val productEvents = FakeEventSerialization<Any>() |
| 17 | + val alsoExternal = setOf("custom:event") |
| 18 | + |
| 19 | + val config = StreamClientSerializationConfig.json(json, productEvents, alsoExternal) |
| 20 | + |
| 21 | + assertSame(json, config.json) |
| 22 | + assertNull(config.eventParser) |
| 23 | + assertSame(productEvents, config.productEventSerializers) |
| 24 | + assertEquals(alsoExternal, config.alsoExternal) |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + fun `event builder injects event parser`() { |
| 29 | + val eventParser = FakeEventSerialization<StreamClientWsEvent>() |
| 30 | + val productEvents = FakeEventSerialization<Any>() |
| 31 | + val alsoExternal = setOf("product:event") |
| 32 | + |
| 33 | + val config = StreamClientSerializationConfig.event(eventParser, productEvents, alsoExternal) |
| 34 | + |
| 35 | + assertSame(eventParser, config.eventParser) |
| 36 | + assertNull(config.json) |
| 37 | + assertSame(productEvents, config.productEventSerializers) |
| 38 | + assertEquals(alsoExternal, config.alsoExternal) |
| 39 | + } |
| 40 | + |
| 41 | + private class FakeJsonSerialization : StreamJsonSerialization { |
| 42 | + override fun toJson(any: Any): Result<String> = Result.success("{}") |
| 43 | + |
| 44 | + override fun <T : Any> fromJson(raw: String, clazz: Class<T>): Result<T> = |
| 45 | + Result.failure(UnsupportedOperationException()) |
| 46 | + } |
| 47 | + |
| 48 | + private class FakeEventSerialization<T> : StreamEventSerialization<T> { |
| 49 | + override fun serialize(data: T): Result<String> = Result.success("event") |
| 50 | + |
| 51 | + override fun deserialize(raw: String): Result<T> = |
| 52 | + Result.failure(UnsupportedOperationException()) |
| 53 | + } |
| 54 | +} |
0 commit comments