Skip to content
This repository was archived by the owner on Mar 14, 2021. It is now read-only.

Commit a76e4a7

Browse files
committed
improved sample (additional endpoint, tests)
1 parent d20cf95 commit a76e4a7

File tree

7 files changed

+171
-29
lines changed

7 files changed

+171
-29
lines changed

build.gradle

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
buildscript {
2-
dependencies {
3-
// add generatr-spring
4-
classpath "com.github.hauner.openapi:openapi-generatr-spring"
5-
}
2+
repositories {
3+
maven {
4+
url "https://dl.bintray.com/hauner/openapi-generatr"
5+
}
6+
}
7+
8+
dependencies {
9+
// add generatr-spring
10+
classpath "com.github.hauner.openapi:openapi-generatr-spring"
11+
}
612
}
713

814
plugins {
915
id 'org.springframework.boot' version '2.1.7.RELEASE'
1016
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
1117
id 'java'
18+
id 'groovy'
19+
id 'org.unbroken-dome.test-sets' version '2.1.1'
1220

1321
// add generatr-gradle plugin
14-
// note: currently this works only inside the openapi-generatr-dev project because the
15-
// plugin is not yet published to https://plugins.gradle.org/
16-
id 'com.github.hauner.openapi.openapiGeneratr' version '0.1-SNAPSHOT'
22+
id 'com.github.hauner.openapi-generatr' version '1.0.0.B2'
1723
}
1824

1925
group = 'com.github.hauner'
@@ -24,13 +30,21 @@ repositories {
2430
mavenCentral ()
2531
}
2632

33+
testSets {
34+
testInt
35+
}
36+
37+
check.dependsOn testInt
38+
2739
dependencies {
2840
implementation 'org.springframework.boot:spring-boot-starter-web'
2941
testImplementation 'org.springframework.boot:spring-boot-starter-test'
42+
testImplementation 'org.spockframework:spock-core:1.3-groovy-2.5'
43+
testImplementation 'org.spockframework:spock-spring:1.3-groovy-2.5'
3044
}
3145

3246
// options of openapi-generatr-spring, this maps directly on the options class of the generatr.
33-
// In this case: com.github.hauner.openapi.spring.generatr.ApiOptions
47+
// In this case: com.github.hauner.openapi.spring.generatr.SpringGeneratrOptions
3448
generatrSpring {
3549
// the path to the open api yaml file.
3650
apiPath = "$projectDir/src/api/openapi.yaml"
@@ -39,6 +53,10 @@ generatrSpring {
3953
// {packageName} folder tree.
4054
targetDir = "$projectDir/build/openapi"
4155

56+
// either a type mapping yaml as string or a file name to a type mapping yaml. Note that the yaml
57+
// file name must end with either {@code .yaml} or {@code .yml}.
58+
// typeMappings = "$projectDir/mapping.yaml"
59+
4260
// the root package of the generated interfaces/model. The package folder tree will be created
4361
// inside {targetDir}. Interfaces and models will be placed into the "api" and "model"
4462
// subpackages of packageName:
@@ -60,4 +78,4 @@ sourceSets {
6078
}
6179

6280
// generate api before compiling
63-
classes.dependsOn ('generateSpringApi')
81+
compileJava.dependsOn ('generateSpring')

src/api/openapi.yaml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,36 @@ info:
55

66
paths:
77
/ping:
8-
98
get:
109
tags:
1110
- ping
1211
summary: returns a single "pong" string.
1312
description: very simple sample endpoint
1413
responses:
1514
'200':
15+
description: pong
16+
content:
17+
text/plain:
18+
schema:
19+
type: string
20+
21+
/echo:
22+
get:
23+
tags:
24+
- echo
25+
summary: echos the given parameter string.
26+
description: sample endpoint with parameter
27+
parameters:
28+
- name: source
29+
description: query, required, string
30+
in: query
31+
required: true
32+
schema:
33+
type: string
34+
responses:
35+
'200':
36+
description: echo of the source parameter
1637
content:
1738
text/plain:
1839
schema:
1940
type: string
20-
description: "pong"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2019 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;
18+
19+
import com.github.hauner.openapi.api.EchoApi;
20+
import org.springframework.http.ResponseEntity;
21+
import org.springframework.stereotype.Controller;
22+
23+
/**
24+
* Implementation of the echo api defined in src/api/openapi.yaml.
25+
*
26+
* @author Martin Hauner
27+
*/
28+
@Controller
29+
public class EchoController implements EchoApi {
30+
31+
@Override
32+
public ResponseEntity<String> getEcho (String source) {
33+
return ResponseEntity.ok (source);
34+
}
35+
36+
}

src/main/java/com/github/hauner/openapi/PingController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.springframework.stereotype.Controller;
2222

2323
/**
24-
* Implementation of the api defined in src/api/openapi.yaml.
24+
* Implementation of the ping api defined in src/api/openapi.yaml.
2525
*
2626
* @author Martin Hauner
2727
*/

src/test/java/com/github/hauner/openapi/SpringMvcSampleApplicationTests.java

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2019 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
18+
19+
import org.springframework.beans.factory.annotation.Autowired
20+
import org.springframework.boot.test.context.SpringBootTest
21+
import org.springframework.boot.test.web.client.TestRestTemplate
22+
import org.springframework.http.HttpEntity
23+
import org.springframework.http.HttpHeaders
24+
import org.springframework.http.HttpMethod
25+
import org.springframework.http.MediaType
26+
import org.springframework.web.util.UriComponentsBuilder
27+
import spock.lang.Specification
28+
29+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
30+
class EchoControllerSpec extends Specification {
31+
32+
@Autowired
33+
TestRestTemplate restTemplate
34+
35+
void "echo returns 'pong'" () {
36+
def headers = new HttpHeaders()
37+
headers.setAccept ([MediaType.TEXT_PLAIN])
38+
39+
def entity = new HttpEntity(headers)
40+
41+
def uri = UriComponentsBuilder.fromUriString ('/echo')
42+
.queryParam ('source', 'pong')
43+
.build ()
44+
.toUri ()
45+
46+
expect:
47+
restTemplate.exchange (uri, HttpMethod.GET, entity, String).body == 'pong'
48+
}
49+
50+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2019 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
18+
19+
import org.springframework.beans.factory.annotation.Autowired
20+
import org.springframework.boot.test.context.SpringBootTest
21+
import org.springframework.boot.test.web.client.TestRestTemplate
22+
import spock.lang.Specification
23+
24+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
25+
class PingControllerSpec extends Specification {
26+
27+
@Autowired
28+
TestRestTemplate restTemplate
29+
30+
void "ping returns pong" () {
31+
expect:
32+
restTemplate.getForEntity ('/ping', String).body == 'pong'
33+
}
34+
35+
}

0 commit comments

Comments
 (0)