|
1 | 1 | /* |
2 | | - * Copyright 2019 the original authors |
| 2 | + * Copyright 2019-2020 the original authors |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
@@ -95,7 +95,8 @@ class ApiWriterSpec extends Specification { |
95 | 95 | ]) |
96 | 96 |
|
97 | 97 | when: |
98 | | - new ApiWriter (opts, interfaceWriter, null, null).write (api) |
| 98 | + new ApiWriter (opts, interfaceWriter, null, null, false) |
| 99 | + .write (api) |
99 | 100 |
|
100 | 101 | then: |
101 | 102 | def fooSource = new File(getApiPath (opts.targetDir, 'FooApi.java')) |
@@ -130,7 +131,8 @@ Bar interface! |
130 | 131 | def api = new Api(dt) |
131 | 132 |
|
132 | 133 | when: |
133 | | - new ApiWriter (opts, Stub(InterfaceWriter), dataTypeWriter, Stub(StringEnumWriter)).write (api) |
| 134 | + new ApiWriter (opts, Stub(InterfaceWriter), dataTypeWriter, Stub(StringEnumWriter), false) |
| 135 | + .write (api) |
134 | 136 |
|
135 | 137 | then: |
136 | 138 | def fooSource = new File(getModelPath (opts.targetDir, 'Foo.java')) |
@@ -165,7 +167,8 @@ Bar class! |
165 | 167 | def api = new Api(dt) |
166 | 168 |
|
167 | 169 | when: |
168 | | - new ApiWriter (opts, Stub(InterfaceWriter), Stub(DataTypeWriter), enumWriter).write (api) |
| 170 | + new ApiWriter (opts, Stub(InterfaceWriter), Stub(DataTypeWriter), enumWriter, false) |
| 171 | + .write (api) |
169 | 172 |
|
170 | 173 | then: |
171 | 174 | def fooSource = new File(getModelPath (opts.targetDir, 'Foo.java')) |
@@ -202,18 +205,107 @@ Bar enum! |
202 | 205 | def api = new Api(dt) |
203 | 206 |
|
204 | 207 | when: |
205 | | - new ApiWriter (opts, Stub(InterfaceWriter), dataTypeWriter, Stub(StringEnumWriter)).write (api) |
| 208 | + new ApiWriter (opts, Stub(InterfaceWriter), dataTypeWriter, Stub(StringEnumWriter), false) |
| 209 | + .write (api) |
206 | 210 |
|
207 | 211 | then: |
208 | 212 | 0 * dataTypeWriter.write (_, dt.find ('simple')) |
209 | 213 | 0 * dataTypeWriter.write (_, dt.find ('Type')) |
210 | 214 | } |
211 | 215 |
|
| 216 | + void "re-formats interface sources"() { |
| 217 | + def interfaceWriter = Stub (InterfaceWriter) { |
| 218 | + write (_ as Writer, _ as Interface) >> { |
| 219 | + Writer writer = it.get(0) |
| 220 | + writer.write (' interface Foo { }\n') |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + def opts = new ApiOptions( |
| 225 | + packageName: 'com.github.hauner.openapi', |
| 226 | + targetDir: [target.root.toString (), 'java', 'src'].join (File.separator) |
| 227 | + ) |
| 228 | + |
| 229 | + def api = new Api(interfaces: [ |
| 230 | + new Interface(pkg: "${opts.packageName}.api", name: 'Foo') |
| 231 | + ]) |
| 232 | + |
| 233 | + when: |
| 234 | + new ApiWriter (opts, interfaceWriter, null, null) |
| 235 | + .write (api) |
| 236 | + |
| 237 | + then: |
| 238 | + def fooSource = new File(getApiPath (opts.targetDir, 'FooApi.java')) |
| 239 | + fooSource.text == """\ |
| 240 | +interface Foo { |
| 241 | +} |
| 242 | +""" |
| 243 | + } |
| 244 | + |
| 245 | + void "re-formats model sources"() { |
| 246 | + def dataTypeWriter = Stub (DataTypeWriter) { |
| 247 | + write (_ as Writer, _ as ObjectDataType) >> { |
| 248 | + Writer writer = it.get(0) |
| 249 | + writer.write (' class Foo { }') |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + def opts = new ApiOptions( |
| 254 | + packageName: 'com.github.hauner.openapi', |
| 255 | + targetDir: [target.root.toString (), 'java', 'src'].join (File.separator) |
| 256 | + ) |
| 257 | + |
| 258 | + def dt = new DataTypes() |
| 259 | + dt.add (new ObjectDataType(pkg: "${opts.packageName}.model", type: 'Foo')) |
| 260 | + def api = new Api(dt) |
| 261 | + |
| 262 | + when: |
| 263 | + new ApiWriter (opts, Stub(InterfaceWriter), dataTypeWriter, Stub(StringEnumWriter)) |
| 264 | + .write (api) |
| 265 | + |
| 266 | + then: |
| 267 | + def fooSource = new File(getModelPath (opts.targetDir, 'Foo.java')) |
| 268 | + fooSource.text == """\ |
| 269 | +class Foo { |
| 270 | +} |
| 271 | +""" |
| 272 | + } |
| 273 | + |
| 274 | + void "re-formats model enum sources"() { |
| 275 | + def enumWriter = Stub (StringEnumWriter) { |
| 276 | + write (_ as Writer, _ as StringEnumDataType) >> { |
| 277 | + Writer writer = it.get(0) |
| 278 | + writer.write (' enum Foo { }') |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 282 | + def opts = new ApiOptions( |
| 283 | + packageName: 'com.github.hauner.openapi', |
| 284 | + targetDir: [target.root.toString (), 'java', 'src'].join (File.separator) |
| 285 | + ) |
| 286 | + |
| 287 | + def dt = new DataTypes() |
| 288 | + dt.add (new StringEnumDataType(pkg: "${opts.packageName}.model", type: 'Foo')) |
| 289 | + def api = new Api(dt) |
| 290 | + |
| 291 | + when: |
| 292 | + new ApiWriter (opts, Stub(InterfaceWriter), Stub(DataTypeWriter), enumWriter) |
| 293 | + .write (api) |
| 294 | + |
| 295 | + then: |
| 296 | + def fooSource = new File(getModelPath (opts.targetDir, 'Foo.java')) |
| 297 | + fooSource.text == """\ |
| 298 | +enum Foo { |
| 299 | +} |
| 300 | +""" |
| 301 | + } |
| 302 | + |
212 | 303 | String getApiPath(String targetFolder, String clazzName) { |
213 | 304 | ([targetFolder] + apiPkgPath + [clazzName]).join(File.separator) |
214 | 305 | } |
215 | 306 |
|
216 | 307 | String getModelPath(String targetFolder, String clazzName) { |
217 | 308 | ([targetFolder] + apiModelPath + [clazzName]).join(File.separator) |
218 | 309 | } |
| 310 | + |
219 | 311 | } |
0 commit comments