@@ -25,12 +25,14 @@ import ru.mystamps.web.entity.Image
25
25
import ru.mystamps.web.service.dto.ImageDto
26
26
import ru.mystamps.web.service.exception.ImagePersistenceException
27
27
28
+ import java.nio.file.Path
29
+
28
30
class FilesystemImagePersistenceStrategyTest extends Specification {
29
31
private static final STORAGE_DIR = " /tmp"
30
32
31
33
private MultipartFile multipartFile = Mock ()
32
34
private Image image = TestObjects . createImage()
33
- private File mockFile = Mock (File , constructorArgs : [ " /fake/path " ] )
35
+ private Path mockFile = Mock (Path )
34
36
35
37
private ImagePersistenceStrategy strategy = Spy (FilesystemImagePersistenceStrategy , constructorArgs : [STORAGE_DIR ])
36
38
@@ -42,7 +44,7 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
42
44
when :
43
45
strategy. save(multipartFile, image)
44
46
then :
45
- 1 * strategy. writeToFile(_ as MultipartFile , _ as File ) >> {}
47
+ 1 * strategy. writeToFile(_ as MultipartFile , _ as Path ) >> {}
46
48
}
47
49
48
50
def " save() should saves file onto the configured directory" () {
@@ -51,8 +53,8 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
51
53
when :
52
54
strategy. save(multipartFile, image)
53
55
then :
54
- 1 * strategy. writeToFile(_ as MultipartFile , { File file ->
55
- assert file . parent == expectedDirectoryName
56
+ 1 * strategy. writeToFile(_ as MultipartFile , { Path path ->
57
+ assert path . parent. toString() == expectedDirectoryName
56
58
return true
57
59
}) >> {}
58
60
}
@@ -65,15 +67,15 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
65
67
when :
66
68
strategy. save(multipartFile, image)
67
69
then :
68
- 1 * strategy. writeToFile(_ as MultipartFile , { File file ->
69
- assert file . name == expectedFileName
70
+ 1 * strategy. writeToFile(_ as MultipartFile , { Path path ->
71
+ assert path . fileName . toString() == expectedFileName
70
72
return true
71
73
}) >> {}
72
74
}
73
75
74
76
def " save() should converts IOException to ImagePersistenceException" () {
75
77
given :
76
- strategy. writeToFile(_ as MultipartFile , _ as File ) >> { throw new IOException () }
78
+ strategy. writeToFile(_ as MultipartFile , _ as Path ) >> { throw new IOException () }
77
79
when :
78
80
strategy. save(multipartFile, image)
79
81
then :
@@ -88,7 +90,7 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
88
90
89
91
def " get() should returns null when file doesn't exist" () {
90
92
given :
91
- mockFile . exists() >> false
93
+ strategy . exists(_ as Path ) >> false
92
94
and :
93
95
strategy. createFile(_ as Image ) >> mockFile
94
96
when :
@@ -99,11 +101,11 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
99
101
100
102
def " get() should converts IOException to ImagePersistenceException" () {
101
103
given :
102
- mockFile . exists() >> true
104
+ strategy . exists(_ as Path ) >> true
103
105
and :
104
106
strategy. createFile(_ as Image ) >> mockFile
105
107
and :
106
- strategy. toByteArray(_ as File ) >> { throw new IOException () }
108
+ strategy. toByteArray(_ as Path ) >> { throw new IOException () }
107
109
when :
108
110
strategy. get(image)
109
111
then :
@@ -118,11 +120,11 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
118
120
and :
119
121
byte [] expectedData = ' any data' . bytes
120
122
and :
121
- mockFile . exists() >> true
123
+ strategy . exists(_ as Path ) >> true
122
124
and :
123
125
strategy. createFile(_ as Image ) >> mockFile
124
126
and :
125
- strategy. toByteArray(_ as File ) >> expectedData
127
+ strategy. toByteArray(_ as Path ) >> expectedData
126
128
when :
127
129
ImageDto result = strategy. get(image)
128
130
then :
0 commit comments