Skip to content

Commit cb5d27d

Browse files
committed
Added edit action specification tests
1 parent cba2253 commit cb5d27d

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

springfox-grails/src/main/java/springfox/documentation/grails/EditActionSpecificationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public ActionSpecification create(GrailsActionContext context) {
3131
handlerMethod,
3232
new ArrayList<>(Arrays.asList(
3333
pathParameter(1, "id", resolver.resolve(idType(context.getDomainClass()))),
34-
bodyParameter(1, resolver.resolve(domainClass(context.getDomainClass()))))),
34+
bodyParameter(2, resolver.resolve(domainClass(context.getDomainClass()))))),
3535
resolver.resolve(domainClass(context.getDomainClass())));
3636

3737
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package springfox.documentation.grails
2+
3+
import com.fasterxml.classmate.TypeResolver
4+
import grails.core.GrailsControllerClass
5+
import grails.core.GrailsDomainClass
6+
import grails.core.GrailsDomainClassProperty
7+
import org.springframework.http.MediaType
8+
import org.springframework.web.bind.annotation.RequestMethod
9+
import spock.lang.Specification
10+
11+
class EditActionSpecificationFactorySpec extends Specification {
12+
def controller = Mock(GrailsControllerClass)
13+
def domain = Mock(GrailsDomainClass)
14+
def identifierProperty = Mock(GrailsDomainClassProperty)
15+
16+
def setup() {
17+
controller.clazz >> AController
18+
domain.clazz >> ADomain
19+
domain.identifier >> identifierProperty
20+
domain.identifier.type >> Integer
21+
}
22+
23+
def "Create action produces action specification" () {
24+
given:
25+
def resolver = new TypeResolver()
26+
def sut = new EditActionSpecificationFactory(resolver)
27+
when:
28+
def spec = sut.create(new GrailsActionContext(controller, domain, "update"))
29+
then:
30+
spec.consumes == [MediaType.APPLICATION_JSON] as Set
31+
spec.produces == [MediaType.APPLICATION_JSON] as Set
32+
spec.supportedMethods == [RequestMethod.PUT, RequestMethod.POST] as Set
33+
spec.parameters.size() == 2
34+
spec.parameters[0].parameterType == resolver.resolve(Integer)
35+
spec.parameters[0].parameterIndex == 1
36+
spec.parameters[0].defaultName().isPresent()
37+
spec.parameters[0].defaultName().get() == "id"
38+
39+
spec.parameters[1].parameterType == resolver.resolve(ADomain)
40+
spec.parameters[1].parameterIndex == 2
41+
spec.parameters[1].defaultName().isPresent()
42+
spec.parameters[1].defaultName().get() == "body"
43+
44+
spec.returnType == resolver.resolve(ADomain)
45+
spec.handlerMethod.method == AController.methods.find {it.name == "update" }
46+
}
47+
48+
def "Index action throws exception when action is not found" () {
49+
given:
50+
def resolver = new TypeResolver()
51+
def sut = new EditActionSpecificationFactory(resolver)
52+
when:
53+
sut.create(new GrailsActionContext(controller, domain, "unknown"))
54+
then:
55+
def exception = thrown(NullPointerException)
56+
exception.message.contains("Handler method is null")
57+
}
58+
}

0 commit comments

Comments
 (0)