-
Notifications
You must be signed in to change notification settings - Fork 5
/
DefaultDirectoryHierarchyTest.groovy
223 lines (187 loc) · 7.99 KB
/
DefaultDirectoryHierarchyTest.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* © 2021. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.io.naming
import edu.ie3.datamodel.exceptions.FileException
import edu.ie3.datamodel.io.naming.DefaultDirectoryHierarchy
import edu.ie3.util.io.FileIOUtils
import org.apache.commons.io.FilenameUtils
import org.testcontainers.shaded.org.bouncycastle.util.test.TestFailedException
import spock.lang.Shared
import spock.lang.Specification
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.stream.Collectors
import java.util.stream.Stream
class DefaultDirectoryHierarchyTest extends Specification {
@Shared
Path tmpDirectory
def setup() {
tmpDirectory = Files.createTempDirectory("psdm_default_input_hierarchy")
}
def basePathString(String gridName) {
tmpDirectory.resolve(gridName)
}
def cleanup() {
FileIOUtils.deleteRecursively(tmpDirectory)
}
def "A DefaultFileHierarchy is set up correctly"() {
given:
def gridName = "test_grid"
def basePath = basePathString(gridName)
when:
def dfh = new DefaultDirectoryHierarchy(tmpDirectory, gridName)
then:
try {
dfh.baseDirectory.get() == basePath
dfh.subDirectories.size() == 9
dfh.subDirectories.get(basePath.resolve(Path.of("input", "grid"))) == true
dfh.subDirectories.get(basePath.resolve(Path.of("input", "participants"))) == true
dfh.subDirectories.get(basePath.resolve(Path.of("input", "participants", "time_series"))) == false
dfh.subDirectories.get(basePath.resolve(Path.of("input", "global"))) == true
dfh.subDirectories.get(basePath.resolve(Path.of("input", "thermal"))) == false
dfh.subDirectories.get(basePath.resolve(Path.of("input", "graphics"))) == false
dfh.subDirectories.get(basePath.resolve(Path.of("results", "grid"))) == false
dfh.subDirectories.get(basePath.resolve(Path.of("results", "participants"))) == false
dfh.subDirectories.get(basePath.resolve(Path.of("results", "thermal"))) == false
} catch (TestFailedException e) {
FileIOUtils.deleteRecursively(tmpDirectory)
throw e
}
}
def "A DefaultFileHierarchy is able to create a correct hierarchy of mandatory directories"() {
given:
def gridName = "test_grid"
def basePath = basePathString(gridName)
def dfh = new DefaultDirectoryHierarchy(tmpDirectory, gridName)
when:
dfh.createDirs()
then:
Files.exists(basePath)
Files.isDirectory(basePath)
dfh.subDirectories.each { path, isMandatory ->
assert Files.exists(path) == isMandatory
if (isMandatory) {
assert Files.isDirectory(path)
}
}
/* Ignore the partial result and input trees, as they are not listed explicitly */
Files.list(basePath).filter { path ->
path != Paths.get(Stream.of(basePath.toString(), "input").collect(Collectors.joining(File.separator))) && path != Paths.get(Stream.of(basePath.toString(), "results").collect(Collectors.joining(File.separator)))
}.each { path ->
assert dfh.subDirectories.containsKey(path)
}
}
def "A DefaultFileHierarchy is able to create a correct hierarchy of mandatory and optional directories"() {
given:
def gridName = "test_grid"
def basePath = basePathString(gridName)
def dfh = new DefaultDirectoryHierarchy(tmpDirectory, gridName)
when:
dfh.createDirs(true)
then:
Files.exists(basePath)
Files.isDirectory(basePath)
dfh.subDirectories.each { path, isMandatory ->
assert Files.exists(path)
assert Files.isDirectory(path)
}
Files.list(basePath).filter { path ->
path != Paths.get(Stream.of(basePath.toString(), "input").collect(Collectors.joining(File.separator))) && path != Paths.get(Stream.of(basePath.toString(), "results").collect(Collectors.joining(File.separator)))
}.each { path -> assert dfh.subDirectories.containsKey(path) }
}
def "A DefaultFileHierarchy is able to validate a correct hierarchy of mandatory and optional directories"() {
given:
def gridName = "test_grid"
def dfh = new DefaultDirectoryHierarchy(tmpDirectory, gridName)
dfh.createDirs(true)
when:
dfh.validate()
then:
noExceptionThrown()
}
def "A DefaultFileHierarchy throws an exception when trying to validate a missing hierarchy of mandatory and optional directories"() {
given:
def gridName = "test_grid"
def basePath = basePathString(gridName)
def dfh = new DefaultDirectoryHierarchy(tmpDirectory, gridName)
when:
dfh.validate()
then:
def ex = thrown(FileException)
ex.message == "The path '" + basePath + "' does not exist."
}
def "A DefaultFileHierarchy throws an exception when trying to validate a file instead of a hierarchy"() {
given:
def gridName = "test_grid"
def basePath = basePathString(gridName)
def dfh = new DefaultDirectoryHierarchy(tmpDirectory, gridName)
Files.createFile(basePath)
when:
dfh.validate()
then:
def ex = thrown(FileException)
ex.message == "The path '" + basePath + "' has to be a directory."
}
def "A DefaultFileHierarchy throws an exception when trying to validate a hierarchy with missing mandatory directory"() {
given:
def gridName = "test_grid"
def basePath = basePathString(gridName)
def dfh = new DefaultDirectoryHierarchy(tmpDirectory, gridName)
dfh.createDirs()
def globalDirectory = dfh.subDirectories.entrySet().find { entry -> entry.key.toString().endsWith("global") }.key
Files.delete(globalDirectory)
when:
dfh.validate()
then:
def ex = thrown(FileException)
ex.message == "The mandatory directory '" + Stream.of(basePath.toString(), "input", "global").collect(Collectors.joining(File.separator)) + "' does not exist."
}
def "A DefaultFileHierarchy throws an exception when trying to validate a hierarchy with file instead of mandatory directory"() {
given:
def gridName = "test_grid"
def basePath = basePathString(gridName)
def dfh = new DefaultDirectoryHierarchy(tmpDirectory, gridName)
dfh.createDirs()
def globalDirectory = dfh.subDirectories.entrySet().find { entry -> entry.key.toString().endsWith("global") }.key
Files.delete(globalDirectory)
Files.createFile(globalDirectory)
when:
dfh.validate()
then:
def ex = thrown(FileException)
ex.message == "The mandatory directory '" + Stream.of(basePath.toString(), "input", "global").collect(Collectors.joining(File.separator)) + "' is not a directory."
}
def "A DefaultFileHierarchy throws an exception when trying to validate a hierarchy with file instead of optional directory"() {
given:
def gridName = "test_grid"
def basePath = basePathString(gridName)
def dfh = new DefaultDirectoryHierarchy(tmpDirectory, gridName)
dfh.createDirs(true)
def thermalDirectory = dfh.subDirectories.entrySet().find { entry -> entry.key.toString().endsWith("input" + File.separator + "thermal") }.key
Files.delete(thermalDirectory)
Files.createFile(thermalDirectory)
when:
dfh.validate()
then:
def ex = thrown(FileException)
ex.message == "The optional directory '" + Stream.of(basePath.toString(), "input", "thermal").collect(Collectors.joining(File.separator)) + "' is not a directory."
}
def "A DefaultFileHierarchy throws an exception when trying to validate a hierarchy with unsupported extra directory"() {
given:
def gridName = "test_grid"
def basePath = basePathString(gridName)
def fifthWheelPath = basePathString(gridName).resolve("something_on_top")
def dfh = new DefaultDirectoryHierarchy(tmpDirectory, gridName)
dfh.createDirs(true)
Files.createDirectory(fifthWheelPath)
when:
dfh.validate()
then:
def ex = thrown(FileException)
ex.message == "There is a directory '" + Stream.of(basePath.toString(), "something_on_top").collect(Collectors.joining(File.separator)) + "' apparent, that is not supported by the default directory hierarchy."
}
}