Skip to content

Commit dfa7767

Browse files
committed
Added tests to demonstrate annotations overriding values
(6)
1 parent b480895 commit dfa7767

File tree

6 files changed

+609
-220
lines changed

6 files changed

+609
-220
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ allprojects {
3535
springPluginVersion = "1.2.0.RELEASE"
3636
swagger2Core = "1.5.12"
3737
springBoot = "1.4.4.RELEASE"
38-
springfox = "2.6.1"
38+
springfox = "2.7.0-SNAPSHOT"
3939
}
4040
}

springfox-grails-contract-tests/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ dependencies {
5858
compile "org.grails.plugins:views-json"
5959
compile "org.grails.plugins:views-json-templates"
6060
console "org.grails:grails-console"
61+
6162
profile "org.grails.profiles:rest-api"
63+
6264
provided "org.codehaus.groovy:groovy-ant"
65+
6366
runtime "com.h2database:h2"
67+
6468
testCompile "org.grails:grails-plugin-testing"
6569
testCompile "org.grails.plugins:geb"
6670
testCompile "org.grails:grails-datastore-rest-client"
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package grails.springfox.sample
2+
3+
import grails.transaction.Transactional
4+
import io.swagger.annotations.ApiImplicitParam
5+
import io.swagger.annotations.ApiImplicitParams
6+
import io.swagger.annotations.ApiOperation
7+
import springfox.documentation.annotations.ApiIgnore
8+
9+
import static org.springframework.http.HttpStatus.*
10+
11+
@Transactional(readOnly = true)
12+
class BookController {
13+
14+
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
15+
16+
@ApiOperation(value = "index", httpMethod = "GET", notes="Creates a book")
17+
def index(Integer max) {
18+
params.max = Math.min(max ?: 10, 100)
19+
respond Book.list(params), model:[bookCount: Book.count()]
20+
}
21+
22+
@ApiIgnore
23+
def show(Book book) {
24+
respond book
25+
}
26+
27+
@ApiOperation(value = "create", httpMethod = "POST", notes="Creates a book")
28+
@ApiImplicitParams([
29+
@ApiImplicitParam(name = "name", dataType = "string", required = true, paramType = "form",
30+
value = "Name of the book")
31+
])
32+
def create() {
33+
respond new Book(params)
34+
}
35+
36+
@Transactional
37+
@ApiOperation(value = "save", httpMethod = "POST", notes="Saves a book")
38+
def save(Book book) {
39+
if (book == null) {
40+
transactionStatus.setRollbackOnly()
41+
notFound()
42+
return
43+
}
44+
45+
if (book.hasErrors()) {
46+
transactionStatus.setRollbackOnly()
47+
respond book.errors, view:'create'
48+
return
49+
}
50+
51+
book.save flush:true
52+
53+
request.withFormat {
54+
form multipartForm {
55+
flash.message = message(code: 'default.created.message', args: [message(code: 'book.label', default: 'Book'), book.id])
56+
redirect book
57+
}
58+
'*' { respond book, [status: CREATED] }
59+
}
60+
}
61+
62+
@ApiIgnore
63+
def edit(Book book) {
64+
respond book
65+
}
66+
67+
@Transactional
68+
@ApiOperation(value = "update", httpMethod = "PUT", notes="Updates a book")
69+
def update(Book book) {
70+
if (book == null) {
71+
transactionStatus.setRollbackOnly()
72+
notFound()
73+
return
74+
}
75+
76+
if (book.hasErrors()) {
77+
transactionStatus.setRollbackOnly()
78+
respond book.errors, view:'edit'
79+
return
80+
}
81+
82+
book.save flush:true
83+
84+
request.withFormat {
85+
form multipartForm {
86+
flash.message = message(code: 'default.updated.message', args: [message(code: 'book.label', default: 'Book'), book.id])
87+
redirect book
88+
}
89+
'*'{ respond book, [status: OK] }
90+
}
91+
}
92+
93+
@Transactional
94+
@ApiOperation(value = "delete", httpMethod = "DELETE", notes="Deletes a book")
95+
@ApiImplicitParams([
96+
@ApiImplicitParam(name = "id", dataType = "long", required = true, paramType = "form",
97+
value = "Id of the book")
98+
])
99+
def delete(Book book) {
100+
101+
if (book == null) {
102+
transactionStatus.setRollbackOnly()
103+
notFound()
104+
return
105+
}
106+
107+
book.delete flush:true
108+
109+
request.withFormat {
110+
form multipartForm {
111+
flash.message = message(code: 'default.deleted.message', args: [message(code: 'book.label', default: 'Book'), book.id])
112+
redirect action:"index", method:"GET"
113+
}
114+
'*'{ render status: NO_CONTENT }
115+
}
116+
}
117+
118+
protected void notFound() {
119+
request.withFormat {
120+
form multipartForm {
121+
flash.message = message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])
122+
redirect action: "index", method: "GET"
123+
}
124+
'*'{ render status: NOT_FOUND }
125+
}
126+
}
127+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package grails.springfox.sample
2+
3+
class Book {
4+
Long id
5+
String name
6+
7+
static constraints = {
8+
name nullable: false
9+
}
10+
}

springfox-grails-contract-tests/src/integration-test/groovy/grails/springfox/sample/SpringFoxSpec.groovy

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,22 @@ import static io.restassured.RestAssured.get
1212

1313
@Integration
1414
class SpringFoxSpec extends Specification implements FileAccess {
15-
@LocalServerPort
16-
private int port
17-
void "test something"() {
18-
given:
19-
def expected = fileContents("/expected-service-description.json")
20-
def actual = get("http://localhost:$port/v2/api-docs").asString()
15+
@LocalServerPort
16+
private int port
2117

22-
expect:
23-
try {
24-
JSONAssert.assertEquals(
25-
expected.replaceAll("__PORT__", "$port"),
26-
actual,
27-
JSONCompareMode.NON_EXTENSIBLE)
28-
} catch (AssertionError e) {
29-
Assert.fail("${e.getMessage()}${System.getProperty("line.separator")}${JsonOutput.prettyPrint(actual)}")
30-
}
18+
void "test something"() {
19+
given:
20+
def expected = fileContents("/expected-service-description.json")
21+
def actual = get("http://localhost:$port/v2/api-docs").asString()
3122

32-
33-
}
23+
expect:
24+
try {
25+
JSONAssert.assertEquals(
26+
expected.replaceAll("__PORT__", "$port"),
27+
actual,
28+
JSONCompareMode.NON_EXTENSIBLE)
29+
} catch (AssertionError e) {
30+
Assert.fail("${e.getMessage()}${System.getProperty("line.separator")}${JsonOutput.prettyPrint(actual)}")
31+
}
32+
}
3433
}

0 commit comments

Comments
 (0)