/
IMSessionManagerImplTestCase.groovy
174 lines (149 loc) · 5.36 KB
/
IMSessionManagerImplTestCase.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.beolnix.marvin.im
import com.beolnix.marvin.config.api.BotSettings
import com.beolnix.marvin.config.api.ConfigurationProvider
import com.beolnix.marvin.config.api.error.ConfigurationException
import com.beolnix.marvin.im.api.IMSession
import com.beolnix.marvin.im.api.IMSessionProvider
import com.beolnix.marvin.im.api.IMSessionState
import com.beolnix.marvin.im.api.model.IMOutgoingMessage
import com.beolnix.marvin.im.api.model.IMOutgoingMessageBuilder
import com.beolnix.marvin.plugins.api.PluginsManager
import org.junit.Test
import java.util.concurrent.Executor
import static org.junit.Assert.assertFalse
import static org.junit.Assert.assertNotNull
import static org.junit.Assert.assertTrue
/**
* Created by DAtmakin on 11/5/2015.
*/
class IMSessionManagerImplTestCase {
public static final String PROTOCOL = "test"
@Test
public void testRegisterIMSessionProvider() {
def imSessionProvider = [
getProtocol: {
PROTOCOL
}
] as IMSessionProvider
def imSessionManager = new IMSessionManagerImpl(null, null)
imSessionManager.registerIMSessionProvider(imSessionProvider)
assertTrue(imSessionManager.sessionProvidersMap.containsKey(PROTOCOL))
assertNotNull(imSessionManager.sessionProvidersMap.get(PROTOCOL))
}
@Test
public void testCreateSessionsPositive() {
def isConnectExecuted = false
// Prepare mocks for the test
def botSettings = [
getProtocol: {
PROTOCOL
}
] as BotSettings
def configProvider = [
getBotSettings: {
["testBot" : botSettings]
}
] as ConfigurationProvider
def pluginManager = [] as PluginsManager
def imSession = [
connect: {
isConnectExecuted = true
}
] as IMSession
def imSessionProvider = [
getProtocol: {
PROTOCOL
},
createNewSession: { botConfig, pluginsManager ->
imSession
}
] as IMSessionProvider
// doing the test:
// 1. registered provider must be used to create the session
// 2. connect method of newly created session must be executed
// 3. session must be placed to the sessions map
def imSessionManager = new IMSessionManagerImpl(configProvider, null)
imSessionManager.registerIMSessionProvider(imSessionProvider)
imSessionManager.createSessions(pluginManager)
assertTrue(imSessionManager.sessionsMap.containsKey("testBot"))
assertTrue(isConnectExecuted)
}
/**
* Session must not be created because session provider for the protocol specified in botSettings has not been provided
*/
@Test
public void testCreateSessionsNegative() {
def pluginManager = [] as PluginsManager
def botSettings = [
getProtocol: {
PROTOCOL
}
] as BotSettings
def configProvider = [
getBotSettings: {
["testBot" : botSettings]
}
] as ConfigurationProvider
def imSessionManager = new IMSessionManagerImpl(configProvider, null)
imSessionManager.createSessions(pluginManager)
assertFalse(imSessionManager.sessionsMap.containsKey("testBot"))
}
/**
* Message should be passed to IMSession
*/
@Test
public void testSendMessagePositive() {
def isSendMessageExecuted = false
def executor = [
execute: { runnable ->
runnable.run()
}
] as Executor
def imSessionManager = new IMSessionManagerImpl(null, executor)
def imSession = [
getState : {
IMSessionState.CONNECTED
},
sendMessage : {
isSendMessageExecuted = true
}
] as IMSession
imSessionManager.sessionsMap["test bot"] = imSession
def msg = new IMOutgoingMessageBuilder()
.withBotName("test bot")
.withProtocol(PROTOCOL)
.withRawMessageBody("msg text")
.build()
imSessionManager.sendMessage(msg)
assertTrue(isSendMessageExecuted)
}
/**
* Message should be passed to IMSession if IMSession is not in CONNECTED state
*/
@Test
public void testSendMessageNegative1() {
def isSendMessageExecuted = false
def executor = [
execute: { runnable ->
runnable.run()
}
] as Executor
def imSessionManager = new IMSessionManagerImpl(null, executor)
def imSession = [
getState : {
IMSessionState.DISCONNECTED
},
sendMessage : {
isSendMessageExecuted = true
}
] as IMSession
imSessionManager.sessionsMap["test bot"] = imSession
def msg = new IMOutgoingMessageBuilder()
.withBotName("test bot")
.withProtocol(PROTOCOL)
.withRawMessageBody("msg text")
.build()
imSessionManager.sendMessage(msg)
assertFalse(isSendMessageExecuted)
}
}