Skip to content

Commit c16d98d

Browse files
committed
test with jimfs://
1 parent 282ae06 commit c16d98d

File tree

6 files changed

+201
-66
lines changed

6 files changed

+201
-66
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ dependencies {
6565
implementation 'com.google.googlejavaformat:google-java-format:1.7'
6666
compileOnly "com.github.hauner.openapi:openapi-processor-api:$processorApiVersion"
6767

68+
testImplementation 'com.google.jimfs:jimfs:1.1'
6869
testImplementation ('org.spockframework:spock-core:2.0-M2-groovy-2.5') {
6970
exclude group: 'org.codehaus.groovy'
7071
}
Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original authors
2+
* Copyright 2019-2020 the original authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,48 +17,23 @@
1717
package com.github.hauner.openapi.processor
1818

1919
import com.github.hauner.openapi.spring.parser.ParserType
20+
import org.junit.Test
2021
import org.junit.runner.RunWith
2122
import org.junit.runners.Parameterized
2223

2324
/**
2425
* using Junit so IDEA adds a "<Click to see difference>" when using assertEquals().
2526
*/
26-
2727
@RunWith(Parameterized)
2828
class ProcessorEndToEndTest extends ProcessorTestBase {
2929

30-
static def testSets = [
31-
'bean-validation',
32-
'endpoint-exclude',
33-
'method-operation-id',
34-
'no-response-content',
35-
'params-additional',
36-
'params-complex-data-types',
37-
'params-enum',
38-
'params-simple-data-types',
39-
'params-request-body',
40-
'params-request-body-multipart-form-data',
41-
'params-path-simple-data-types',
42-
'params-spring-pageable-mapping',
43-
'ref-into-another-file',
44-
'ref-loop',
45-
'response-array-data-type-mapping',
46-
'response-complex-data-types',
47-
'response-content-multiple',
48-
'response-content-single',
49-
'response-result-mapping',
50-
'response-simple-data-types',
51-
'response-single-multi-mapping',
52-
'schema-composed'
53-
]
54-
5530
@Parameterized.Parameters(name = "{0}")
5631
static Collection<TestSet> sources () {
57-
def swagger = testSets.collect {
32+
def swagger = TestSet.testSets.collect {
5833
new TestSet (name: it, parser: ParserType.SWAGGER)
5934
}
6035

61-
def openapi4j = testSets.collect {
36+
def openapi4j = TestSet.testSets.collect {
6237
new TestSet (name: it, parser: ParserType.OPENAPI4J)
6338
}
6439

@@ -69,4 +44,9 @@ class ProcessorEndToEndTest extends ProcessorTestBase {
6944
super (testSet)
7045
}
7146

47+
@Test
48+
void "native - processor creates expected files for api set "() {
49+
runOnNativeFileSystem ()
50+
}
51+
7252
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2020 the original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.hauner.openapi.processor
18+
19+
import com.github.hauner.openapi.spring.parser.ParserType
20+
import com.google.common.jimfs.Configuration
21+
import com.google.common.jimfs.Jimfs
22+
import org.junit.Test
23+
import org.junit.runner.RunWith
24+
import org.junit.runners.Parameterized
25+
26+
/**
27+
* using Junit so IDEA adds a "<Click to see difference>" when using assertEquals().
28+
*/
29+
@RunWith(Parameterized)
30+
class ProcessorJimsFileSystemTest extends ProcessorTestBase {
31+
32+
@Parameterized.Parameters(name = "{0}")
33+
static Collection<TestSet> sources () {
34+
// the swagger parser does not work with a custom FileSystem so we just run the test with
35+
// openapi4j
36+
37+
TestSet.testSets.collect {
38+
new TestSet (name: it, parser: ParserType.OPENAPI4J)
39+
}
40+
}
41+
42+
ProcessorJimsFileSystemTest (TestSet testSet) {
43+
super (testSet)
44+
}
45+
46+
@Test
47+
void "jimfs - processor creates expected files for api set "() {
48+
runOnCustomFileSystem (Jimfs.newFileSystem (Configuration.unix ()))
49+
}
50+
51+
}

src/testInt/groovy/com/github/hauner/openapi/processor/ProcessorPendingTest.groovy

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original authors
2+
* Copyright 2019-2020 the original authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,10 +18,11 @@ package com.github.hauner.openapi.processor
1818

1919
import com.github.hauner.openapi.spring.parser.ParserType
2020
import org.junit.Ignore
21+
import org.junit.Test
2122
import org.junit.runner.RunWith
2223
import org.junit.runners.Parameterized
2324

24-
//@Ignore
25+
@Ignore
2526
@RunWith(Parameterized)
2627
class ProcessorPendingTest extends ProcessorTestBase {
2728

@@ -37,4 +38,9 @@ class ProcessorPendingTest extends ProcessorTestBase {
3738
super (testSet)
3839
}
3940

41+
@Test
42+
void "native - processor creates expected files for api set "() {
43+
runOnNativeFileSystem ()
44+
}
45+
4046
}

src/testInt/groovy/com/github/hauner/openapi/processor/ProcessorTestBase.groovy

Lines changed: 105 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ import com.github.difflib.UnifiedDiffUtils
2121
import com.github.hauner.openapi.spring.processor.MappingReader
2222
import com.github.hauner.openapi.spring.processor.SpringProcessor
2323
import com.github.hauner.openapi.spring.processor.mapping.Mapping
24-
import groovy.io.FileType
2524
import org.junit.Rule
26-
import org.junit.Test
2725
import 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

3032
import static org.junit.Assert.assertEquals
3133

@@ -43,26 +45,26 @@ map:
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
}
@@ -71,20 +73,20 @@ map:
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(
@@ -95,14 +97,99 @@ map:
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
}

src/testInt/groovy/com/github/hauner/openapi/processor/TestSet.groovy

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original authors
2+
* Copyright 2019-2020 the original authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,32 @@ package com.github.hauner.openapi.processor
1919
import com.github.hauner.openapi.spring.parser.ParserType
2020

2121
class TestSet {
22+
23+
static def testSets = [
24+
'bean-validation',
25+
'endpoint-exclude',
26+
'method-operation-id',
27+
'no-response-content',
28+
'params-additional',
29+
'params-complex-data-types',
30+
'params-enum',
31+
'params-simple-data-types',
32+
'params-request-body',
33+
'params-request-body-multipart-form-data',
34+
'params-path-simple-data-types',
35+
'params-spring-pageable-mapping',
36+
'ref-into-another-file',
37+
'ref-loop',
38+
'response-array-data-type-mapping',
39+
'response-complex-data-types',
40+
'response-content-multiple',
41+
'response-content-single',
42+
'response-result-mapping',
43+
'response-simple-data-types',
44+
'response-single-multi-mapping',
45+
'schema-composed'
46+
]
47+
2248
String name
2349
ParserType parser = ParserType.SWAGGER
2450

0 commit comments

Comments
 (0)