@@ -21,11 +21,13 @@ import com.github.difflib.UnifiedDiffUtils
2121import com.github.hauner.openapi.spring.processor.MappingReader
2222import com.github.hauner.openapi.spring.processor.SpringProcessor
2323import com.github.hauner.openapi.spring.processor.mapping.Mapping
24- import groovy.io.FileType
2524import org.junit.Rule
26- import org.junit.Test
2725import org.junit.rules.TemporaryFolder
2826
27+ import java.nio.file.FileSystem
28+ import java.nio.file.Files
29+ import java.nio.file.Path
30+ import java.util.stream.Stream
2931
3032import static org.junit.Assert.assertEquals
3133
4345 to: org.springframework.http.ResponseEntity
4446 """
4547
48+ Path project = Path . of (" " ). toAbsolutePath ()
4649 TestSet testSet
4750
4851 ProcessorTestBase (TestSet testSet) {
4952 this . testSet = testSet
5053 }
5154
52- @Test
53- void " processor creates expected files for api set " () {
55+ protected runOnNativeFileSystem () {
5456 def source = testSet. name
5557
5658 def processor = new SpringProcessor ()
5759 def options = [
5860 parser : testSet. parser. toString (),
59- apiPath : " . /src/testInt/resources/${ source} /openapi.yaml" ,
61+ apiPath : " ${ project.toString () } /src/testInt/resources/${ source} /openapi.yaml" ,
6062 targetDir : folder. root
6163 ]
6264
63- def mappingYaml = new File ( " ./ src/testInt/resources/${ source} /mapping.yaml" )
64- if (mappingYaml . exists ()) {
65- options. mapping = mappingYaml
65+ def mappingYaml = project . resolve ( " src/testInt/resources/${ source} /mapping.yaml" )
66+ if (Files . exists (mappingYaml )) {
67+ options. mapping = mappingYaml. toString ()
6668 } else {
6769 options. mapping = DEFAULT_OPTIONS
6870 }
7173 Mapping mapping = reader. read (options. mapping as String )
7274
7375 def packageName = mapping. options. packageName
74- def expectedPath = [ ' . ' , ' src' , ' testInt' , ' resources' , source, packageName] . join( File . separator )
75- def generatedPath = [ folder. root. absolutePath, packageName] . join( File . separator )
76+ def expectedPath = project . resolve ( " src/ testInt/ resources/ ${ source} / ${ packageName} " )
77+ def generatedPath = Path . of ( folder. root. toString()) . resolve (packageName )
7678
7779 when :
7880 processor. run (options)
7981
8082 then :
81- def generatedFiles = collectGenerated(generatedPath )
82- def expectedFiles = collectExpected(expectedPath )
83- assert generatedFiles == expectedFiles
83+ def expectedFiles = collectPaths (expectedPath )
84+ def generatedFiles = collectPaths (generatedPath )
85+ assert expectedFiles == generatedFiles
8486
8587 expectedFiles. each {
86- def expected = new File ([ expectedPath, it] . join ( ' / ' ) )
87- def generated = new File ([ generatedPath, it] . join ( ' / ' ) )
88+ def expected = expectedPath. resolve (it )
89+ def generated = generatedPath. resolve (it )
8890
8991 printUnifiedDiff (expected, generated)
9092 assertEquals (
9597 }
9698 }
9799
98- void printUnifiedDiff (File expected , File generated ) {
100+ protected void runOnCustomFileSystem (FileSystem fs ) {
101+ def source = testSet. name
102+
103+ Path root = Files . createDirectory (fs. getPath (" source" ))
104+ Path files = Path . of (" ./src/testInt/resources/${ source} " )
105+ copy (files, root)
106+
107+ Path api = root. resolve (' openapi.yaml' )
108+ Path target = fs. getPath (' target' )
109+
110+ def processor = new SpringProcessor ()
111+ def options = [
112+ parser : ' OPENAPI4J' , // swagger-parser does not work with fs
113+ apiPath : api. toUri (). toURL (). toString (),
114+ targetDir : target. toUri (). toURL (). toString ()
115+ ]
116+
117+ def mappingYaml = root. resolve (' mapping.yaml' )
118+ if (Files . exists (mappingYaml)) {
119+ options. mapping = mappingYaml. toUri (). toURL (). toString ()
120+ } else {
121+ options. mapping = DEFAULT_OPTIONS
122+ }
123+
124+ def reader = new MappingReader ()
125+ Mapping mapping = reader. read (options. mapping as String )
126+
127+ def packageName = mapping. options. packageName
128+ def expectedPath = root. resolve (packageName)
129+ def generatedPath = target. resolve (packageName)
130+
131+ when :
132+ processor. run (options)
133+
134+ then :
135+ def expectedFiles = collectPaths (expectedPath)
136+ def generatedFiles = collectPaths (generatedPath)
137+ assert expectedFiles == generatedFiles
138+
139+ expectedFiles. each {
140+ def expected = expectedPath. resolve (it)
141+ def generated = generatedPath. resolve (it)
142+
143+ printUnifiedDiff (expected, generated)
144+ assertEquals (
145+ // ignore cr (ie. crlf vs lf)
146+ expected. text. replace(' \r ' ,' ' ),
147+ generated. text. replace(' \r ' ,' ' )
148+ )
149+ }
150+ }
151+
152+ private void copy (Path source , Path target ) {
153+ Stream<Path > paths = Files . walk (source)
154+ .filter ({f -> ! Files . isDirectory (f)})
155+
156+ paths. forEach { p ->
157+ Path relativePath = source. relativize (p)
158+ Path targetPath = target. resolve (relativePath. toString ())
159+ Files . createDirectories (targetPath. getParent ())
160+
161+ InputStream src = Files . newInputStream (p)
162+ OutputStream dst = Files . newOutputStream (targetPath)
163+ src. transferTo (dst)
164+ }
165+
166+ paths. close ()
167+ }
168+
169+ private List<String > collectPaths (Path source ) {
170+ def files = []
171+
172+ def found = Files . walk (source)
173+ .filter ({ f ->
174+ ! Files . isDirectory (f)
175+ })
176+
177+ found. forEach ({f ->
178+ files << source. relativize (f). toString ()
179+ })
180+ found. close ()
181+
182+ return files
183+ }
184+
185+ void printUnifiedDiff (Path expected , Path generated ) {
99186 def patch = DiffUtils . diff (
100187 expected. readLines (),
101188 generated. readLines ())
102189
103190 def diff = UnifiedDiffUtils . generateUnifiedDiff (
104- expected. path ,
105- generated. path ,
191+ expected. toString () ,
192+ generated. toString () ,
106193 expected. readLines (),
107194 patch,
108195 2
@@ -113,20 +200,4 @@ map:
113200 }
114201 }
115202
116- List<String > collectGenerated (String generatedPath ) {
117- def generated = []
118- new File (generatedPath). eachFileRecurse FileType . FILES , {
119- generated << it. absolutePath. replace (generatedPath, ' ' )
120- }
121- generated
122- }
123-
124- List<String > collectExpected (String expectedPath ) {
125- def expected = []
126- new File (expectedPath). eachFileRecurse FileType . FILES , {
127- expected << it. path. replace (expectedPath, ' ' )
128- }
129- expected
130- }
131-
132203}
0 commit comments