|
| 1 | +package com.nylas.util |
| 2 | + |
| 3 | +import com.nylas.models.ConferencingProvider |
| 4 | +import com.squareup.moshi.Moshi |
| 5 | +import okio.Buffer |
| 6 | +import kotlin.test.Test |
| 7 | +import kotlin.test.assertEquals |
| 8 | +import kotlin.test.assertNull |
| 9 | + |
| 10 | +class ConferencingProviderAdapterTest { |
| 11 | + |
| 12 | + private val moshi = Moshi.Builder() |
| 13 | + .add(ConferencingProviderAdapter()) |
| 14 | + .build() |
| 15 | + |
| 16 | + private val adapter = moshi.adapter(ConferencingProvider::class.java) |
| 17 | + |
| 18 | + @Test |
| 19 | + fun `deserializes current JSON values correctly`() { |
| 20 | + assertEquals(ConferencingProvider.GOOGLE_MEET, adapter.fromJson("\"Google Meet\"")) |
| 21 | + assertEquals(ConferencingProvider.GOTOMEETING, adapter.fromJson("\"GoToMeeting\"")) |
| 22 | + assertEquals(ConferencingProvider.MICROSOFT_TEAMS, adapter.fromJson("\"Microsoft Teams\"")) |
| 23 | + assertEquals(ConferencingProvider.SKYPE_FOR_BUSINESS, adapter.fromJson("\"Skype for Business\"")) |
| 24 | + assertEquals(ConferencingProvider.SKYPE_FOR_CONSUMER, adapter.fromJson("\"Skype for Consumer\"")) |
| 25 | + assertEquals(ConferencingProvider.TEAMS_FOR_BUSINESS, adapter.fromJson("\"Teams for Business\"")) |
| 26 | + assertEquals(ConferencingProvider.WEBEX, adapter.fromJson("\"WebEx\"")) |
| 27 | + assertEquals(ConferencingProvider.ZOOM_MEETING, adapter.fromJson("\"Zoom Meeting\"")) |
| 28 | + assertEquals(ConferencingProvider.UNKNOWN, adapter.fromJson("\"unknown\"")) |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun `deserializes legacy deprecated JSON values correctly`() { |
| 33 | + // These should map to the same enum values as their current counterparts |
| 34 | + assertEquals(ConferencingProvider.SKYPE_FOR_CONSUMER, adapter.fromJson("\"skypeForConsumer\"")) |
| 35 | + assertEquals(ConferencingProvider.SKYPE_FOR_BUSINESS, adapter.fromJson("\"skypeForBusiness\"")) |
| 36 | + assertEquals(ConferencingProvider.TEAMS_FOR_BUSINESS, adapter.fromJson("\"teamsForBusiness\"")) |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun `deserializes unknown values to UNKNOWN enum`() { |
| 41 | + assertEquals(ConferencingProvider.UNKNOWN, adapter.fromJson("\"unknownProvider\"")) |
| 42 | + assertEquals(ConferencingProvider.UNKNOWN, adapter.fromJson("\"someRandomValue\"")) |
| 43 | + assertEquals(ConferencingProvider.UNKNOWN, adapter.fromJson("\"\"")) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `deserializes null values correctly`() { |
| 48 | + assertNull(adapter.fromJson("null")) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `serializes to current JSON format`() { |
| 53 | + assertEquals("\"Google Meet\"", adapter.toJson(ConferencingProvider.GOOGLE_MEET)) |
| 54 | + assertEquals("\"GoToMeeting\"", adapter.toJson(ConferencingProvider.GOTOMEETING)) |
| 55 | + assertEquals("\"Microsoft Teams\"", adapter.toJson(ConferencingProvider.MICROSOFT_TEAMS)) |
| 56 | + assertEquals("\"Skype for Business\"", adapter.toJson(ConferencingProvider.SKYPE_FOR_BUSINESS)) |
| 57 | + assertEquals("\"Skype for Consumer\"", adapter.toJson(ConferencingProvider.SKYPE_FOR_CONSUMER)) |
| 58 | + assertEquals("\"Teams for Business\"", adapter.toJson(ConferencingProvider.TEAMS_FOR_BUSINESS)) |
| 59 | + assertEquals("\"WebEx\"", adapter.toJson(ConferencingProvider.WEBEX)) |
| 60 | + assertEquals("\"Zoom Meeting\"", adapter.toJson(ConferencingProvider.ZOOM_MEETING)) |
| 61 | + assertEquals("\"unknown\"", adapter.toJson(ConferencingProvider.UNKNOWN)) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun `serializes null values correctly`() { |
| 66 | + assertEquals("null", adapter.toJson(null)) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun `round trip serialization with current values`() { |
| 71 | + val values = ConferencingProvider.values() |
| 72 | + for (value in values) { |
| 73 | + val json = adapter.toJson(value) |
| 74 | + val deserialized = adapter.fromJson(json) |
| 75 | + assertEquals(value, deserialized, "Round trip failed for $value") |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun `backward compatibility roundtrip test`() { |
| 81 | + // Test that legacy JSON values can be deserialized and then serialized to current format |
| 82 | + val legacyToCurrentMappings = mapOf( |
| 83 | + "skypeForConsumer" to "Skype for Consumer", |
| 84 | + "skypeForBusiness" to "Skype for Business", |
| 85 | + "teamsForBusiness" to "Teams for Business" |
| 86 | + ) |
| 87 | + |
| 88 | + for ((legacyJson, expectedCurrentJson) in legacyToCurrentMappings) { |
| 89 | + val deserialized = adapter.fromJson("\"$legacyJson\"") |
| 90 | + val serialized = adapter.toJson(deserialized) |
| 91 | + assertEquals("\"$expectedCurrentJson\"", serialized, |
| 92 | + "Legacy value $legacyJson should serialize to current format $expectedCurrentJson") |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun `integration test with JSON objects containing conferencing provider`() { |
| 98 | + // Test within a larger JSON context using full JsonHelper setup |
| 99 | + val moshiWithAllAdapters = JsonHelper.moshi() |
| 100 | + val eventAdapter = moshiWithAllAdapters.adapter(TestEvent::class.java) |
| 101 | + |
| 102 | + val eventJson = """ |
| 103 | + { |
| 104 | + "conferencing": { |
| 105 | + "provider": "skypeForConsumer", |
| 106 | + "details": { |
| 107 | + "url": "https://example.com" |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + """.trimIndent() |
| 112 | + |
| 113 | + val event = eventAdapter.fromJson(eventJson) |
| 114 | + assertEquals(ConferencingProvider.SKYPE_FOR_CONSUMER, event?.conferencing?.provider) |
| 115 | + |
| 116 | + // When serialized back, it should use the current format |
| 117 | + val serializedJson = eventAdapter.toJson(event) |
| 118 | + val deserializedAgain = eventAdapter.fromJson(serializedJson) |
| 119 | + assertEquals(ConferencingProvider.SKYPE_FOR_CONSUMER, deserializedAgain?.conferencing?.provider) |
| 120 | + } |
| 121 | + |
| 122 | + // Test data classes for integration testing |
| 123 | + data class TestEvent( |
| 124 | + val conferencing: TestConferencing? |
| 125 | + ) |
| 126 | + |
| 127 | + data class TestConferencing( |
| 128 | + val provider: ConferencingProvider?, |
| 129 | + val details: Map<String, Any>? |
| 130 | + ) |
| 131 | +} |
0 commit comments