Skip to content

Commit 920d78e

Browse files
committed
Added companion default for RoomOptions, added tests for the same
1 parent bdccd57 commit 920d78e

File tree

12 files changed

+131
-46
lines changed

12 files changed

+131
-46
lines changed

chat-android/src/main/java/com/ably/chat/Room.kt

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
package com.ably.chat
44

5-
import io.ably.lib.types.AblyException
65
import io.ably.lib.types.ErrorInfo
76
import io.ably.lib.util.Log.LogHandler
87
import kotlinx.coroutines.CoroutineName
@@ -144,13 +143,7 @@ internal class DefaultRoom(
144143
override val presence: Presence
145144
get() {
146145
if (_presence == null) {
147-
throw AblyException.fromErrorInfo(
148-
ErrorInfo(
149-
"Presence is not enabled for this room",
150-
ErrorCodes.BadRequest.errorCode,
151-
HttpStatusCodes.BadRequest,
152-
),
153-
)
146+
throw ablyException("Presence is not enabled for this room", ErrorCodes.BadRequest)
154147
}
155148
return _presence as Presence
156149
}
@@ -159,13 +152,7 @@ internal class DefaultRoom(
159152
override val reactions: RoomReactions
160153
get() {
161154
if (_reactions == null) {
162-
throw AblyException.fromErrorInfo(
163-
ErrorInfo(
164-
"Reactions are not enabled for this room",
165-
ErrorCodes.BadRequest.errorCode,
166-
HttpStatusCodes.BadRequest,
167-
),
168-
)
155+
throw ablyException("Reactions are not enabled for this room", ErrorCodes.BadRequest)
169156
}
170157
return _reactions as RoomReactions
171158
}
@@ -174,13 +161,7 @@ internal class DefaultRoom(
174161
override val typing: Typing
175162
get() {
176163
if (_typing == null) {
177-
throw AblyException.fromErrorInfo(
178-
ErrorInfo(
179-
"Typing is not enabled for this room",
180-
ErrorCodes.BadRequest.errorCode,
181-
HttpStatusCodes.BadRequest,
182-
),
183-
)
164+
throw ablyException("Typing is not enabled for this room", ErrorCodes.BadRequest)
184165
}
185166
return _typing as Typing
186167
}
@@ -189,13 +170,7 @@ internal class DefaultRoom(
189170
override val occupancy: Occupancy
190171
get() {
191172
if (_occupancy == null) {
192-
throw AblyException.fromErrorInfo(
193-
ErrorInfo(
194-
"Occupancy is not enabled for this room",
195-
ErrorCodes.BadRequest.errorCode,
196-
HttpStatusCodes.BadRequest,
197-
),
198-
)
173+
throw ablyException("Occupancy is not enabled for this room", ErrorCodes.BadRequest)
199174
}
200175
return _occupancy as Occupancy
201176
}

chat-android/src/main/java/com/ably/chat/RoomOptions.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.ably.chat
22

3-
import io.ably.lib.types.AblyException
4-
import io.ably.lib.types.ErrorInfo
5-
63
/**
74
* Represents the options for a given chat room.
85
*/
@@ -31,7 +28,19 @@ data class RoomOptions(
3128
* {@link RoomOptionsDefaults.occupancy} to enable occupancy with default options.
3229
*/
3330
val occupancy: OccupancyOptions? = null,
34-
)
31+
) {
32+
companion object {
33+
/**
34+
* Supports all room options with default values
35+
*/
36+
val default = RoomOptions(
37+
typing = TypingOptions(),
38+
presence = PresenceOptions(),
39+
reactions = RoomReactionsOptions,
40+
occupancy = OccupancyOptions,
41+
)
42+
}
43+
}
3544

3645
/**
3746
* Represents the presence options for a chat room.
@@ -81,12 +90,6 @@ object OccupancyOptions
8190
*/
8291
fun RoomOptions.validateRoomOptions() {
8392
if (typing != null && typing.timeoutMs <= 0) {
84-
throw AblyException.fromErrorInfo(
85-
ErrorInfo(
86-
"Typing timeout must be greater than 0",
87-
ErrorCodes.InvalidRequestBody.errorCode,
88-
HttpStatusCodes.BadRequest,
89-
),
90-
)
93+
throw ablyException("Typing timeout must be greater than 0", ErrorCodes.InvalidRequestBody)
9194
}
9295
}

chat-android/src/main/java/com/ably/chat/Utils.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,10 @@ internal class DeferredValue<T> {
195195
return result
196196
}
197197
}
198+
199+
fun ablyException(
200+
errorMessage: String,
201+
code: ErrorCodes,
202+
statusCode: Int = HttpStatusCodes.BadRequest,
203+
cause: Throwable? = null,
204+
): AblyException = AblyException.fromErrorInfo(cause, ErrorInfo(errorMessage, statusCode, code.errorCode))
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.ably.chat.room
2+
3+
import com.ably.chat.ChatApi
4+
import com.ably.chat.DefaultRoom
5+
import com.ably.chat.RoomOptions
6+
import com.ably.chat.RoomStatus
7+
import com.ably.chat.TypingOptions
8+
import com.ably.utils.createMockRealtimeClient
9+
import io.ably.lib.types.AblyException
10+
import io.mockk.mockk
11+
import kotlinx.coroutines.test.runTest
12+
import org.junit.Assert
13+
import org.junit.Assert.assertThrows
14+
import org.junit.Test
15+
16+
/**
17+
* Chat rooms are configurable, so as to enable or disable certain features.
18+
* When requesting a room, options as to which features should be enabled, and
19+
* the configuration they should take, must be provided
20+
* Spec: CHA-RC2
21+
*/
22+
class ConfigureRoomOptionsTest {
23+
24+
@Test
25+
fun `(CHA-RC2a) If a room is requested with a negative typing timeout, an ErrorInfo with code 40001 must be thrown`() = runTest {
26+
val mockRealtimeClient = createMockRealtimeClient()
27+
val chatApi = mockk<ChatApi>(relaxed = true)
28+
29+
// Room success when positive typing timeout
30+
val room = DefaultRoom("1234", RoomOptions(typing = TypingOptions(timeoutMs = 100)), mockRealtimeClient, chatApi, null)
31+
Assert.assertNotNull(room)
32+
Assert.assertEquals(RoomStatus.Initialized, room.status)
33+
34+
// Room failure when negative timeout
35+
val exception = assertThrows(AblyException::class.java) {
36+
DefaultRoom("1234", RoomOptions(typing = TypingOptions(timeoutMs = -1)), mockRealtimeClient, chatApi, null)
37+
}
38+
Assert.assertEquals("Typing timeout must be greater than 0", exception.errorInfo.message)
39+
Assert.assertEquals(40_001, exception.errorInfo.code)
40+
Assert.assertEquals(400, exception.errorInfo.statusCode)
41+
}
42+
43+
@Test
44+
fun `(CHA-RC2b) Attempting to use disabled feature must result in an ErrorInfo with code 40000 being thrown`() = runTest {
45+
val mockRealtimeClient = createMockRealtimeClient()
46+
val chatApi = mockk<ChatApi>(relaxed = true)
47+
48+
// Room only supports messages feature, since by default other features are turned off
49+
val room = DefaultRoom("1234", RoomOptions(), mockRealtimeClient, chatApi, null)
50+
Assert.assertNotNull(room)
51+
Assert.assertEquals(RoomStatus.Initialized, room.status)
52+
53+
// Access presence throws exception
54+
var exception = assertThrows(AblyException::class.java) {
55+
room.presence
56+
}
57+
Assert.assertEquals("Presence is not enabled for this room", exception.errorInfo.message)
58+
Assert.assertEquals(40_000, exception.errorInfo.code)
59+
Assert.assertEquals(400, exception.errorInfo.statusCode)
60+
61+
// Access reactions throws exception
62+
exception = assertThrows(AblyException::class.java) {
63+
room.reactions
64+
}
65+
Assert.assertEquals("Reactions are not enabled for this room", exception.errorInfo.message)
66+
Assert.assertEquals(40_000, exception.errorInfo.code)
67+
Assert.assertEquals(400, exception.errorInfo.statusCode)
68+
69+
// Access presence throws exception
70+
exception = assertThrows(AblyException::class.java) {
71+
room.typing
72+
}
73+
Assert.assertEquals("Typing is not enabled for this room", exception.errorInfo.message)
74+
Assert.assertEquals(40_000, exception.errorInfo.code)
75+
Assert.assertEquals(400, exception.errorInfo.statusCode)
76+
77+
// Access presence throws exception
78+
exception = assertThrows(AblyException::class.java) {
79+
room.occupancy
80+
}
81+
Assert.assertEquals("Occupancy is not enabled for this room", exception.errorInfo.message)
82+
Assert.assertEquals(40_000, exception.errorInfo.code)
83+
Assert.assertEquals(400, exception.errorInfo.statusCode)
84+
85+
// room with all features
86+
val roomWithAllFeatures = DefaultRoom("1234", RoomOptions.default, mockRealtimeClient, chatApi, null)
87+
Assert.assertNotNull(roomWithAllFeatures.presence)
88+
Assert.assertNotNull(roomWithAllFeatures.reactions)
89+
Assert.assertNotNull(roomWithAllFeatures.typing)
90+
Assert.assertNotNull(roomWithAllFeatures.occupancy)
91+
}
92+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.ably.chat.room
2+
3+
class RoomGetTest
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.ably.chat.room
2+
3+
class RoomReleaseTest

chat-android/src/test/java/com/ably/chat/room/AttachTest.kt renamed to chat-android/src/test/java/com/ably/chat/room/lifecycle/AttachTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.ably.chat.room
1+
package com.ably.chat.room.lifecycle
22

33
import com.ably.chat.ContributesToRoomLifecycle
44
import com.ably.chat.DefaultRoomLifecycle

chat-android/src/test/java/com/ably/chat/room/DetachTest.kt renamed to chat-android/src/test/java/com/ably/chat/room/lifecycle/DetachTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.ably.chat.room
1+
package com.ably.chat.room.lifecycle
22

33
import com.ably.chat.ContributesToRoomLifecycle
44
import com.ably.chat.DefaultRoomLifecycle

chat-android/src/test/java/com/ably/chat/room/PrecedenceTest.kt renamed to chat-android/src/test/java/com/ably/chat/room/lifecycle/PrecedenceTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.ably.chat.room
1+
package com.ably.chat.room.lifecycle
22

33
import com.ably.chat.ContributesToRoomLifecycle
44
import com.ably.chat.DefaultRoomAttachmentResult

chat-android/src/test/java/com/ably/chat/room/ReleaseTest.kt renamed to chat-android/src/test/java/com/ably/chat/room/lifecycle/ReleaseTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.ably.chat.room
1+
package com.ably.chat.room.lifecycle
22

33
import com.ably.chat.DefaultRoomLifecycle
44
import com.ably.chat.RoomLifecycleManager

0 commit comments

Comments
 (0)