-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathFunctionContractSpec.groovy
206 lines (185 loc) · 8.4 KB
/
FunctionContractSpec.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
/*
*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package springfox.test.contract.swaggertests
import com.fasterxml.classmate.TypeResolver
import groovy.json.JsonSlurper
import org.skyscreamer.jsonassert.JSONAssert
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.RequestEntity
import org.springframework.test.context.ContextConfiguration
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration
import springfox.documentation.schema.AlternateTypeRuleConvention
import springfox.documentation.spring.web.plugins.JacksonSerializerConvention
import static org.skyscreamer.jsonassert.JSONCompareMode.*
import static org.springframework.boot.test.context.SpringBootTest.*
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = Config)
class FunctionContractSpec extends Specification implements FileAccess {
@Shared
def http = new TestRestTemplate()
@Value('${local.server.port}')
int port
@Unroll
def 'should honor swagger v2 resource listing #groupName'() {
given:
RequestEntity<Void> request = RequestEntity.get(
new URI("http://localhost:$port/v2/api-docs?group=$groupName"))
.accept(MediaType.APPLICATION_JSON)
.build()
String contract = fileContents("/contract/swagger2/$contractFile")
when:
def response = http.exchange(request, String)
then:
String raw = response.body
response.statusCode == HttpStatus.OK
def withPortReplaced = contract.replaceAll("__PORT__", "$port")
JSONAssert.assertEquals(withPortReplaced, raw, NON_EXTENSIBLE)
where:
contractFile | groupName
'swagger.json' | 'petstore'
'swaggerTemplated.json' | 'petstoreTemplated'
'declaration-bugs-service.json' | 'bugs'
'declaration-bugs-different-service.json' | 'bugsDifferent'
'declaration-business-service.json' | 'businessService'
'declaration-concrete-controller.json' | 'concrete'
'declaration-controller-with-no-request-mapping-service.json' | 'noRequestMapping'
'declaration-fancy-pet-service.json' | 'fancyPetstore'
'declaration-feature-demonstration-service.json' | 'featureService'
'declaration-feature-demonstration-service-codeGen.json' | 'featureService-codeGen'
'declaration-inherited-service-impl.json' | 'inheritedService'
'declaration-pet-grooming-service.json' | 'petGroomingService'
'declaration-pet-service.json' | 'petService'
'declaration-groovy-service.json' | 'groovyService'
'declaration-enum-service.json' | 'enumService'
'declaration-spring-data-rest.json' | 'spring-data-rest'
}
def "should list swagger resources for swagger 2.0"() {
given:
def http = new TestRestTemplate()
RequestEntity<Void> request = RequestEntity.get(new URI("http://localhost:$port/swagger-resources"))
.accept(MediaType.APPLICATION_JSON)
.build()
when:
def response = http.exchange(request, String)
def slurper = new JsonSlurper()
def result = slurper.parseText(response.body)
then:
result.find {
it.name == 'petstore' &&
it.location == '/v2/api-docs?group=petstore' &&
it.swaggerVersion == '2.0'
}
result.find {
it.name == 'businessService' &&
it.location == '/v2/api-docs?group=businessService' &&
it.swaggerVersion == '2.0'
}
result.find {
it.name == 'concrete' &&
it.location == '/v2/api-docs?group=concrete' &&
it.swaggerVersion == '2.0'
}
}
def 'should honor swagger resource listing'() {
given:
def http = new TestRestTemplate()
RequestEntity<Void> request = RequestEntity.get(new URI("http://localhost:$port/api-docs"))
.accept(MediaType.APPLICATION_JSON)
.build()
String contract = fileContents('/contract/swagger/resource-listing.json')
when:
def response = http.exchange(request, String)
then:
response.statusCode == HttpStatus.OK
JSONAssert.assertEquals(contract, response.body, NON_EXTENSIBLE)
}
@Unroll
def 'should honor api v1.2 contract [#contractFile] at endpoint [#declarationPath]'() {
given:
def http = new TestRestTemplate()
RequestEntity<Void> request = RequestEntity.get(new URI("http://localhost:$port/api-docs${declarationPath}"))
.accept(MediaType.APPLICATION_JSON)
.build()
String contract = fileContents("/contract/swagger/$contractFile")
when:
def response = http.exchange(request, String)
then:
String raw = response.body
response.statusCode == HttpStatus.OK
JSONAssert.assertEquals(contract, raw, NON_EXTENSIBLE)
where:
contractFile | declarationPath
'declaration-business-service.json' | '/default/business-service'
'declaration-concrete-controller.json' | '/default/concrete-controller'
'declaration-controller-with-no-request-mapping-service.json' | '/default/controller-with-no-request-mapping-service'
'declaration-fancy-pet-service.json' | '/default/fancy-pet-service'
'declaration-feature-demonstration-service.json' | '/default/feature-demonstration-service'
'declaration-inherited-service-impl.json' | '/default/inherited-service-impl'
'declaration-pet-grooming-service.json' | '/default/pet-grooming-service'
'declaration-pet-service.json' | '/default/pet-service'
'declaration-root-controller.json' | '/default/root-controller'
'declaration-groovy-service.json' | '/default/groovy-service'
}
def "should list swagger resources for swagger 1.2"() {
given:
def http = new TestRestTemplate()
RequestEntity<Void> request = RequestEntity.get(new URI("http://localhost:$port/swagger-resources"))
.accept(MediaType.APPLICATION_JSON)
.build()
when:
def response = http.exchange(request, String)
def slurper = new JsonSlurper()
def result = slurper.parseText(response.body)
then:
result.find {
it.name == 'default' &&
it.location == '/api-docs' &&
it.swaggerVersion == '1.2'
}
}
@Configuration
@ComponentScan([
"springfox.documentation.spring.web.dummy.controllers",
"springfox.test.contract.swagger.data.rest",
"springfox.test.contract.swagger",
"springfox.petstore.controller"
])
@Import([
SecuritySupport,
Swagger12TestConfig,
Swagger2TestConfig,
BeanValidatorPluginsConfiguration])
static class Config {
@Bean
AlternateTypeRuleConvention jacksonSerializerConvention(TypeResolver resolver) {
new JacksonSerializerConvention(resolver, "springfox.documentation.spring.web.dummy.models")
}
}
}