@@ -21,6 +21,9 @@ import spock.lang.Specification
21
21
import spock.lang.Unroll
22
22
23
23
import ru.mystamps.web.dao.JdbcCollectionDao
24
+ import ru.mystamps.web.dao.dto.AddCollectionDbDto
25
+ import ru.mystamps.web.service.dto.UrlEntityDto
26
+ import ru.mystamps.web.util.SlugUtils
24
27
25
28
class CollectionServiceImplTest extends Specification {
26
29
private JdbcCollectionDao collectionDao = Mock ()
@@ -31,6 +34,240 @@ class CollectionServiceImplTest extends Specification {
31
34
service = new CollectionServiceImpl (collectionDao)
32
35
}
33
36
37
+ //
38
+ // Tests for createCollection()
39
+ //
40
+
41
+ def " createCollection() should throw exception when owner id is null" () {
42
+ when :
43
+ service. createCollection(null , ' test-owner-login' )
44
+ then :
45
+ thrown IllegalArgumentException
46
+ }
47
+
48
+ def " createCollection() should throw exception when owner login is null" () {
49
+ when :
50
+ service. createCollection(123 , null )
51
+ then :
52
+ thrown IllegalArgumentException
53
+ }
54
+
55
+ def " createCollection() should throw exception when owner login can't be converted to slug" () {
56
+ when :
57
+ service. createCollection(123 , ' ' )
58
+ then :
59
+ thrown IllegalArgumentException
60
+ }
61
+
62
+ def " createCollection() should pass owner id to dao" () {
63
+ given :
64
+ Integer expectedOwnerId = 123
65
+ when :
66
+ service. createCollection(expectedOwnerId, ' test' )
67
+ then :
68
+ 1 * collectionDao. add({ AddCollectionDbDto collection ->
69
+ assert collection?. ownerId == expectedOwnerId
70
+ return true
71
+ }) >> 100
72
+ }
73
+
74
+ def " createCollection() should pass slugified owner login to dao" () {
75
+ given :
76
+ String ownerLogin = ' Test User'
77
+ and :
78
+ String expectedSlug = SlugUtils . slugify(ownerLogin)
79
+ when :
80
+ service. createCollection(123 , ownerLogin)
81
+ then :
82
+ 1 * collectionDao. add({ AddCollectionDbDto collection ->
83
+ assert collection?. slug == expectedSlug
84
+ return true
85
+ }) >> 200
86
+ }
87
+
88
+ //
89
+ // Tests for addToCollection()
90
+ //
91
+
92
+ def " addToCollection() should throw exception when user id is null" () {
93
+ when :
94
+ service. addToCollection(null , 456 )
95
+ then :
96
+ thrown IllegalArgumentException
97
+ }
98
+
99
+ def " addToCollection() should throw exception when series id is null" () {
100
+ when :
101
+ service. addToCollection(123 , null )
102
+ then :
103
+ thrown IllegalArgumentException
104
+ }
105
+
106
+ def " addToCollection() should find collection by user id" () {
107
+ given :
108
+ Integer expectedUserId = 123
109
+ when :
110
+ service. addToCollection(expectedUserId, 321 )
111
+ then :
112
+ 1 * collectionDao. findCollectionUrlEntityByUserId({ Integer userId ->
113
+ assert userId == expectedUserId
114
+ return true
115
+ }) >> TestObjects . createUrlEntityDto()
116
+ }
117
+
118
+ def " addToCollection() should add series to collection" () {
119
+ given :
120
+ Integer expectedSeriesId = 456
121
+ and :
122
+ UrlEntityDto url = TestObjects . createUrlEntityDto()
123
+ and :
124
+ Integer expectedCollectionId = url. getId()
125
+ and :
126
+ collectionDao. findCollectionUrlEntityByUserId(_ as Integer ) >> url
127
+ when :
128
+ service. addToCollection(123 , expectedSeriesId)
129
+ then :
130
+ 1 * collectionDao. addSeriesToCollection({ Integer collectionId ->
131
+ assert collectionId == expectedCollectionId
132
+ return true
133
+ }, { Integer seriesId ->
134
+ assert seriesId == expectedSeriesId
135
+ return true
136
+ })
137
+ }
138
+
139
+ def " addToCollection() should return result from dao" () {
140
+ given :
141
+ UrlEntityDto expectedUrl = TestObjects . createUrlEntityDto()
142
+ and :
143
+ collectionDao. findCollectionUrlEntityByUserId(_ as Integer ) >> expectedUrl
144
+ when :
145
+ UrlEntityDto serviceResult = service. addToCollection(123 , 456 )
146
+ then :
147
+ serviceResult == expectedUrl
148
+ }
149
+
150
+ //
151
+ // Tests for removeFromCollection()
152
+ //
153
+
154
+ def " removeFromCollection() should throw exception when user id is null" () {
155
+ when :
156
+ service. removeFromCollection(null , 123 )
157
+ then :
158
+ thrown IllegalArgumentException
159
+ }
160
+
161
+ def " removeFromCollection() should throw exception when series id is null" () {
162
+ when :
163
+ service. removeFromCollection(456 , null )
164
+ then :
165
+ thrown IllegalArgumentException
166
+ }
167
+
168
+
169
+ def " removeFromCollection() should find collection by user id" () {
170
+ given :
171
+ Integer expectedUserId = 123
172
+ when :
173
+ service. removeFromCollection(expectedUserId, 321 )
174
+ then :
175
+ 1 * collectionDao. findCollectionUrlEntityByUserId({ Integer userId ->
176
+ assert userId == expectedUserId
177
+ return true
178
+ }) >> TestObjects . createUrlEntityDto()
179
+ }
180
+
181
+ def " removeFromCollection() should remove series from collection" () {
182
+ given :
183
+ Integer expectedSeriesId = 456
184
+ and :
185
+ UrlEntityDto url = TestObjects . createUrlEntityDto()
186
+ and :
187
+ Integer expectedCollectionId = url. getId()
188
+ and :
189
+ collectionDao. findCollectionUrlEntityByUserId(_ as Integer ) >> url
190
+ when :
191
+ service. removeFromCollection(123 , expectedSeriesId)
192
+ then :
193
+ 1 * collectionDao. removeSeriesFromCollection({ Integer collectionId ->
194
+ assert collectionId == expectedCollectionId
195
+ return true
196
+ }, { Integer seriesId ->
197
+ assert seriesId == expectedSeriesId
198
+ return true
199
+ })
200
+ }
201
+
202
+ def " removeFromCollection() should return result from dao" () {
203
+ given :
204
+ UrlEntityDto expectedResult = TestObjects . createUrlEntityDto()
205
+ and :
206
+ collectionDao. findCollectionUrlEntityByUserId(_ as Integer ) >> expectedResult
207
+ when :
208
+ UrlEntityDto serviceResult = service. removeFromCollection(123 , 456 )
209
+ then :
210
+ serviceResult == expectedResult
211
+ }
212
+
213
+ //
214
+ // Tests for isSeriesInCollection()
215
+ //
216
+
217
+ def " isSeriesInCollection() should throw exception when series id is null" () {
218
+ when :
219
+ service. isSeriesInCollection(100 , null )
220
+ then :
221
+ thrown IllegalArgumentException
222
+ }
223
+
224
+ def " isSeriesInCollection() should return false for anonymous" () {
225
+ given :
226
+ Integer anonymousUserId = null
227
+ when :
228
+ boolean serviceResult = service. isSeriesInCollection(anonymousUserId, 456 )
229
+ then :
230
+ serviceResult == false
231
+ and :
232
+ 0 * collectionDao. isSeriesInUserCollection(_ as Integer , _ as Integer )
233
+ }
234
+
235
+ def " isSeriesInCollection() should pass arguments to dao" () {
236
+ given :
237
+ Integer expectedUserId = 123
238
+ and :
239
+ Integer expectedSeriesId = 456
240
+ and :
241
+ boolean expectedResult = true
242
+ when :
243
+ boolean serviceResult = service. isSeriesInCollection(expectedUserId, expectedSeriesId)
244
+ then :
245
+ 1 * collectionDao. isSeriesInUserCollection({ Integer userId ->
246
+ assert userId == expectedUserId
247
+ return true
248
+ }, { Integer seriesId ->
249
+ assert seriesId == expectedSeriesId
250
+ return true
251
+ }) >> expectedResult
252
+ and :
253
+ serviceResult == expectedResult
254
+ }
255
+
256
+ //
257
+ // Tests for countCollectionsOfUsers()
258
+ //
259
+
260
+ def " countCollectionsOfUsers() should call dao and return result" () {
261
+ given :
262
+ long expectedResult = Long . MAX_VALUE
263
+ when :
264
+ long serviceResult = service. countCollectionsOfUsers()
265
+ then :
266
+ 1 * collectionDao. countCollectionsOfUsers() >> expectedResult
267
+ and :
268
+ serviceResult == expectedResult
269
+ }
270
+
34
271
//
35
272
// Tests for findRecentlyCreated()
36
273
//
0 commit comments