1717 */
1818package ru.mystamps.web.feature.image
1919
20- import static io.qala.datagen.RandomShortApi.bool
21-
2220import org.slf4j.helpers.NOPLogger
2321import org.springframework.web.multipart.MultipartFile
2422import ru.mystamps.web.feature.image.ImageDb.Images
25- import ru.mystamps.web.service.TestObjects
2623import ru.mystamps.web.tests.Random
2724import spock.lang.Specification
2825import spock.lang.Unroll
@@ -45,51 +42,12 @@ class ImageServiceImplTest extends Specification {
4542 multipartFile. size >> 1024L
4643 multipartFile. contentType >> ' image/png'
4744 multipartFile. originalFilename >> ' super-image.png'
48- imageDao. add(_ as String , _ as String ) >> 17
4945 }
5046
5147 //
5248 // Tests for save()
5349 //
5450
55- def " save() should throw exception if file is null" () {
56- when :
57- service. save(null )
58- then :
59- IllegalArgumentException ex = thrown()
60- ex. message == ' File must be non null'
61- }
62-
63- def " save() should throw exception if file has zero size" () {
64- when :
65- service. save(multipartFile)
66- then :
67- multipartFile. size >> 0L
68- and :
69- IllegalArgumentException ex = thrown()
70- ex. message == ' Image size must be greater than zero'
71- }
72-
73- def " save() should throw exception if content type is null" () {
74- when :
75- service. save(multipartFile)
76- then :
77- multipartFile. contentType >> null
78- and :
79- IllegalArgumentException ex = thrown()
80- ex. message == ' File type must be non null'
81- }
82-
83- def " save() should throw exception for unsupported content type" () {
84- when :
85- service. save(multipartFile)
86- then :
87- multipartFile. contentType >> ' image/tiff'
88- and :
89- IllegalStateException ex = thrown()
90- ex. message == " File type must be PNG or JPEG image, but 'image/tiff' (tiff) were passed"
91- }
92-
9351 @Unroll
9452 def " save() should pass content type '#contentType' to image dao" (String contentType, String expectedType) {
9553 when :
@@ -109,30 +67,6 @@ class ImageServiceImplTest extends Specification {
10967 ' image/png; charset=UTF8' || ' PNG'
11068 }
11169
112- @Unroll
113- def ' save() should pass filename "#filename" to image dao' (String filename, String expectedFilename) {
114- when :
115- service. save(multipartFile)
116- then :
117- multipartFile. originalFilename >> filename
118- and :
119- 1 * imageDao. add(
120- _ as String ,
121- { String actualFilename ->
122- assert actualFilename == expectedFilename
123- return true
124- }
125- ) >> Random . id()
126- where :
127- filename || expectedFilename
128- null || null
129- ' ' || null
130- ' ' || null
131- ' test.png' || ' test.png'
132- ' test.png ' || ' test.png'
133- ' http://example/pic.jpeg' || ' http://example/pic.jpeg'
134- }
135-
13670 def ' save() should pass abbreviated filename when it is too long' () {
13771 given :
13872 String longFilename = ' /long/url/' + (' x' * Images . FILENAME_LENGTH )
@@ -151,258 +85,4 @@ class ImageServiceImplTest extends Specification {
15185 ) >> Random . id()
15286 }
15387
154- def " save() should throw exception when image dao returned null" () {
155- when :
156- service. save(multipartFile)
157- then :
158- imageDao. add(_ as String , _ as String ) >> null
159- and :
160- 0 * imagePersistenceStrategy. save(_ as MultipartFile , _ as ImageInfoDto )
161- and :
162- ImagePersistenceException ex = thrown()
163- ex. message == " Can't save image"
164- }
165-
166- def " save() should call strategy" () {
167- given :
168- ImageInfoDto image = TestObjects . createImageInfoDto()
169- when :
170- service. save(multipartFile)
171- then :
172- imageDao. add(_ as String , _ as String ) >> image. id
173- and :
174- 1 * imagePersistenceStrategy. save({ MultipartFile passedFile ->
175- assert passedFile == multipartFile
176- return true
177- }, { ImageInfoDto passedImage ->
178- assert passedImage?. id == image. id
179- assert passedImage?. type == image. type. toString()
180- return true
181- })
182- }
183-
184- def " save() should return saved image" () {
185- given :
186- Integer expectedImageId = 17
187- and :
188- ImageInfoDto expectedImageInfo = new ImageInfoDto (expectedImageId, ' PNG' )
189- when :
190- ImageInfoDto actualImageInfo = service. save(multipartFile)
191- then :
192- imageDao. add(_ as String , _ as String ) >> expectedImageId
193- and :
194- actualImageInfo == expectedImageInfo
195- }
196-
197- //
198- // Tests for get()
199- //
200-
201- def ' get() should throw exception if image id is null' () {
202- when :
203- service. get(null )
204- then :
205- IllegalArgumentException ex = thrown()
206- ex. message == ' Image id must be non null'
207- }
208-
209- @Unroll
210- def " get() should throw exception if image id is #imageId" (Integer imageId) {
211- when :
212- service. get(imageId)
213- then :
214- IllegalArgumentException ex = thrown()
215- ex. message == ' Image id must be greater than zero'
216- where :
217- imageId | _
218- -1 | _
219- 0 | _
220- }
221-
222- def " get() should pass argument to image dao" () {
223- when :
224- service. get(7 )
225- then :
226- 1 * imageDao. findById({ Integer imageId ->
227- assert imageId == 7
228- return true
229- })
230- }
231-
232- def " get() should not call strategy when image dao returned null" () {
233- when :
234- ImageDto image = service. get(9 )
235- then :
236- imageDao. findById(_ as Integer ) >> null
237- and :
238- 0 * imagePersistenceStrategy. get(_ as ImageInfoDto )
239- and :
240- image == null
241- }
242-
243- def " get() should pass argument to strategy and return result from it" () {
244- given :
245- ImageInfoDto expectedImage = TestObjects . createImageInfoDto()
246- and :
247- imageDao. findById(_ as Integer ) >> expectedImage
248- and :
249- ImageDto expectedImageDto = TestObjects . createImageDto()
250- when :
251- ImageDto actualImageDto = service. get(7 )
252- then :
253- 1 * imagePersistenceStrategy. get({ ImageInfoDto passedImage ->
254- assert passedImage?. id == expectedImage. id
255- assert passedImage?. type == expectedImage. type. toString()
256- return true
257- }) >> expectedImageDto
258- and :
259- actualImageDto == expectedImageDto
260- }
261-
262- def " get() should return null when strategy returned null" () {
263- given :
264- imageDao. findById(_ as Integer ) >> TestObjects . createImageInfoDto()
265- and :
266- imagePersistenceStrategy. get(_ as ImageInfoDto ) >> null
267- when :
268- ImageDto image = service. get(8 )
269- then :
270- image == null
271- }
272-
273- //
274- // Tests for getOrCreatePreview()
275- //
276-
277- def ' getOrCreatePreview() should throw exception if image id is null' () {
278- when :
279- service. getOrCreatePreview(null )
280- then :
281- IllegalArgumentException ex = thrown()
282- ex. message == ' Image id must be non null'
283- }
284-
285- @Unroll
286- def " getOrCreatePreview() should throw exception if image id is #imageId" (Integer imageId) {
287- when :
288- service. getOrCreatePreview(imageId)
289- then :
290- IllegalArgumentException ex = thrown()
291- ex. message == ' Image id must be greater than zero'
292- where :
293- imageId | _
294- -1 | _
295- 0 | _
296- }
297-
298- def " getOrCreatePreview() should pass argument to strategy and return result from it" () {
299- given :
300- Integer expectedImageId = 7
301- String expectedImageType = ' jpeg'
302- and :
303- ImageDto expectedImageDto = TestObjects . createImageDto()
304- when :
305- ImageDto actualImageDto = service. getOrCreatePreview(expectedImageId)
306- then :
307- 1 * imagePersistenceStrategy. getPreview({ ImageInfoDto passedImage ->
308- assert passedImage?. id == expectedImageId
309- assert passedImage?. type == expectedImageType
310- return true
311- }) >> expectedImageDto
312- and :
313- actualImageDto == expectedImageDto
314- }
315-
316- //
317- // Tests for addToSeries()
318- //
319-
320- def " addToSeries() should throw exception when series id is null" () {
321- when :
322- service. addToSeries(null , 1 )
323- then :
324- IllegalArgumentException ex = thrown()
325- ex. message == ' Series id must be non null'
326- }
327-
328- def " addToSeries() should throw exception when image id is null" () {
329- when :
330- service. addToSeries(1 , null )
331- then :
332- IllegalArgumentException ex = thrown()
333- ex. message == ' Image id must be non null'
334- }
335-
336- def " addToSeries() should invoke dao, pass argument and return result from dao" () {
337- given :
338- Integer expectedSeriesId = 14
339- Integer expectedImageId = 15
340- when :
341- service. addToSeries(expectedSeriesId, expectedImageId)
342- then :
343- 1 * imageDao. addToSeries({ Integer seriesId ->
344- assert seriesId == expectedSeriesId
345- return true
346- }, { Integer imageId ->
347- assert imageId == expectedImageId
348- return true
349- })
350- }
351-
352- //
353- // Tests for findBySeriesId()
354- //
355-
356- def " findBySeriesId() should throw exception when series id is null" () {
357- when :
358- service. findBySeriesId(null , bool())
359- then :
360- IllegalArgumentException ex = thrown()
361- ex. message == ' Series id must be non null'
362- }
363-
364- def " findBySeriesId() should invoke dao, pass argument and return result from dao" () {
365- given :
366- Integer expectedSeriesId = 14
367- boolean expectedHidden = bool()
368- and :
369- List<Integer > expectedResult = [ 1 , 2 ]
370- when :
371- List<Integer > result = service. findBySeriesId(expectedSeriesId, expectedHidden)
372- then :
373- 1 * imageDao. findBySeriesId({ Integer seriesId ->
374- assert seriesId == expectedSeriesId
375- return true
376- }, { boolean hidden ->
377- assert hidden == expectedHidden
378- return true
379- }) >> expectedResult
380- and :
381- result == expectedResult
382- }
383-
384- //
385- // Tests for removeIfPossible()
386- //
387-
388- def " removeIfPossible() should throw exception when image info is null" () {
389- when :
390- service. removeIfPossible(null )
391- then :
392- IllegalArgumentException ex = thrown()
393- ex. message == ' Image info must be non null'
394- }
395-
396- def " removeIfPossible() should pass argument to strategy" () {
397- given :
398- ImageInfoDto expectedImageInfo = TestObjects . createImageInfoDto()
399- when :
400- service. removeIfPossible(expectedImageInfo)
401- then :
402- 1 * imagePersistenceStrategy. removeIfPossible({ ImageInfoDto imageInfo ->
403- assert imageInfo == expectedImageInfo
404- return true
405- })
406- }
407-
40888}
0 commit comments