Skip to content

Commit f357be4

Browse files
authored
[gradle-plugin] enhance unit test cases (#18285)
1 parent 2a39b29 commit f357be4

4 files changed

Lines changed: 163 additions & 4 deletions

File tree

modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ class GenerateTaskDslTest : TestBase() {
401401
fun `openApiValidate should fail on invalid spec`() {
402402
// Arrange
403403
val projectFiles = mapOf(
404-
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0-invalid.yaml")
404+
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0-invalid-due-to-missing-info-attribute.yaml")
405405
)
406406

407407
withProject(defaultBuildGradle, projectFiles)
@@ -423,7 +423,7 @@ class GenerateTaskDslTest : TestBase() {
423423
fun `openApiValidate should ok skip spec validation`() {
424424
// Arrange
425425
val projectFiles = mapOf(
426-
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0-invalid.yaml")
426+
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0-invalid-due-to-missing-info-attribute.yaml")
427427
)
428428

429429
withProject("""

modules/openapi-generator-gradle-plugin/src/test/kotlin/ValidateTaskDslTest.kt

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class ValidateTaskDslTest : TestBase() {
113113
fun `openApiValidate should fail on invalid spec`(gradleVersion: String?) {
114114
// Arrange
115115
val projectFiles = mapOf(
116-
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0-invalid.yaml")
116+
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0-invalid-due-to-missing-info-attribute.yaml")
117117
)
118118
withProject(
119119
"""
@@ -139,6 +139,50 @@ class ValidateTaskDslTest : TestBase() {
139139
result.output.contains("Spec is invalid."),
140140
"Unexpected/no message presented to the user for an invalid spec."
141141
)
142+
assertTrue(
143+
result.output.contains("attribute info is missing"),
144+
"Spec validation detail"
145+
)
146+
assertEquals(
147+
FAILED, result.task(":openApiValidate")?.outcome,
148+
"Expected a failed run, but found ${result.task(":openApiValidate")?.outcome}"
149+
)
150+
}
151+
152+
@Test(dataProvider = "gradle_version_provider")
153+
fun `openApiValidate should fail on invalid spec with duplicate 200 status code`(gradleVersion: String?) {
154+
// Arrange
155+
val projectFiles = mapOf(
156+
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0-invalid-due-to-duplicate-200-status-code.yaml")
157+
)
158+
withProject(
159+
"""
160+
| plugins {
161+
| id 'org.openapi.generator'
162+
| }
163+
|
164+
| openApiValidate {
165+
| inputSpec = file('spec.yaml').absolutePath
166+
| }
167+
""".trimMargin(), projectFiles
168+
)
169+
170+
// Act
171+
val result = getGradleRunner(gradleVersion)
172+
.withProjectDir(temp)
173+
.withArguments("openApiValidate")
174+
.withPluginClasspath()
175+
.buildAndFail()
176+
177+
// Assert
178+
assertTrue(
179+
result.output.contains("Spec is invalid."),
180+
"Unexpected/no message presented to the user for an invalid spec."
181+
)
182+
assertTrue(
183+
result.output.contains("Duplicate field 200"),
184+
"Spec validation detail"
185+
)
142186
assertEquals(
143187
FAILED, result.task(":openApiValidate")?.outcome,
144188
"Expected a failed run, but found ${result.task(":openApiValidate")?.outcome}"
@@ -186,7 +230,7 @@ class ValidateTaskDslTest : TestBase() {
186230
fun `validateBadSpec as defined task should fail on invalid spec`(gradleVersion: String?) {
187231
// Arrange
188232
val projectFiles = mapOf(
189-
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0-invalid.yaml")
233+
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0-invalid-due-to-missing-info-attribute.yaml")
190234
)
191235
withProject(
192236
"""
@@ -212,6 +256,10 @@ class ValidateTaskDslTest : TestBase() {
212256
result.output.contains("Spec is invalid."),
213257
"Unexpected/no message presented to the user for an invalid spec."
214258
)
259+
assertTrue(
260+
result.output.contains("attribute info is missing"),
261+
"Unexpected/no message presented to the user for an invalid spec."
262+
)
215263
assertEquals(
216264
FAILED, result.task(":validateBadSpec")?.outcome,
217265
"Expected a failed run, but found ${result.task(":validateBadSpec")?.outcome}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/pets:
11+
get:
12+
summary: List all pets
13+
operationId: listPets
14+
tags:
15+
- pets
16+
parameters:
17+
- name: limit
18+
in: query
19+
description: How many items to return at one time (max 100)
20+
required: false
21+
schema:
22+
type: integer
23+
format: int32
24+
responses:
25+
'200':
26+
description: A paged array of pets
27+
headers:
28+
x-next:
29+
description: A link to the next page of responses
30+
schema:
31+
type: string
32+
content:
33+
application/json:
34+
schema:
35+
$ref: "#/components/schemas/Pets"
36+
'200':
37+
description: duplicate 200
38+
default:
39+
description: unexpected error
40+
content:
41+
application/json:
42+
schema:
43+
$ref: "#/components/schemas/Error"
44+
post:
45+
summary: Create a pet
46+
operationId: createPets
47+
tags:
48+
- pets
49+
responses:
50+
'201':
51+
description: Null response
52+
default:
53+
description: unexpected error
54+
content:
55+
application/json:
56+
schema:
57+
$ref: "#/components/schemas/Error"
58+
/pets/{petId}:
59+
get:
60+
summary: Info for a specific pet
61+
operationId: showPetById
62+
tags:
63+
- pets
64+
parameters:
65+
- name: petId
66+
in: path
67+
required: true
68+
description: The id of the pet to retrieve
69+
schema:
70+
type: string
71+
responses:
72+
'200':
73+
description: Expected response to a valid request
74+
content:
75+
application/json:
76+
schema:
77+
$ref: "#/components/schemas/Pets"
78+
default:
79+
description: unexpected error
80+
content:
81+
application/json:
82+
schema:
83+
$ref: "#/components/schemas/Error"
84+
components:
85+
schemas:
86+
Pet:
87+
required:
88+
- id
89+
- name
90+
properties:
91+
id:
92+
type: integer
93+
format: int64
94+
name:
95+
type: string
96+
tag:
97+
type: string
98+
Pets:
99+
type: array
100+
items:
101+
$ref: "#/components/schemas/Pet"
102+
Error:
103+
required:
104+
- code
105+
- message
106+
properties:
107+
code:
108+
type: integer
109+
format: int32
110+
message:
111+
type: string

modules/openapi-generator-gradle-plugin/src/test/resources/specs/petstore-v3.0-invalid.yaml renamed to modules/openapi-generator-gradle-plugin/src/test/resources/specs/petstore-v3.0-invalid-due-to-missing-info-attribute.yaml

File renamed without changes.

0 commit comments

Comments
 (0)