Skip to content

Commit 978dfd6

Browse files
committed
Fixed the tests and added Restful action spec tests
1 parent fd18186 commit 978dfd6

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package springfox.documentation.grails;
22

33
import com.fasterxml.classmate.TypeResolver;
4+
import com.google.common.base.Preconditions;
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.stereotype.Component;
67

@@ -18,6 +19,10 @@ public RestfulActionSpecificationFactory(TypeResolver resolver) {
1819

1920
@Override
2021
public ActionSpecification create(GrailsActionContext context) {
22+
Preconditions.checkArgument(
23+
factoryLookup.containsKey(context.getAction()),
24+
String.format("Action %s is not a restful action", context.getAction()));
25+
2126
return factoryLookup.get(context.getAction()).create(context);
2227
}
2328

springfox-grails/src/test/groovy/springfox/documentation/grails/DeleteActionSpecificationFactorySpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class DeleteActionSpecificationFactorySpec extends Specification {
2929
then:
3030
spec.consumes == [MediaType.APPLICATION_JSON] as Set
3131
spec.produces == [MediaType.APPLICATION_JSON] as Set
32-
spec.supportedMethods == [RequestMethod.GET] as Set
32+
spec.supportedMethods == [RequestMethod.POST] as Set
3333
spec.parameters.size() == 1
3434
spec.parameters[0].parameterType == resolver.resolve(Integer)
3535
spec.parameters[0].parameterIndex == 1
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 spock.lang.Specification
8+
9+
class RestfulActionSpecificationFactorySpec extends Specification {
10+
def controller = Mock(GrailsControllerClass)
11+
def domain = Mock(GrailsDomainClass)
12+
def identifierProperty = Mock(GrailsDomainClassProperty)
13+
14+
def setup() {
15+
controller.clazz >> AController
16+
domain.clazz >> ADomain
17+
domain.identifier >> identifierProperty
18+
domain.identifier.type >> Integer
19+
}
20+
21+
def "Resolves all restful actions"() {
22+
given:
23+
def resolver = new TypeResolver()
24+
def sut = new RestfulActionSpecificationFactory(resolver)
25+
when:
26+
def actionSpec = sut.create(new GrailsActionContext(controller, domain, action.toLowerCase()))
27+
then:
28+
actionSpec.handlerMethod.method.name == action
29+
where:
30+
action << ["index", "save", "show", "edit", "update", "delete", "patch", "create"]
31+
}
32+
33+
def "Resolves ONLY restful actions"() {
34+
given:
35+
def resolver = new TypeResolver()
36+
def sut = new RestfulActionSpecificationFactory(resolver)
37+
when:
38+
sut.create(new GrailsActionContext(controller, domain, "unknown"))
39+
then:
40+
def exception = thrown(IllegalArgumentException)
41+
exception.message.contains("Action unknown is not a restful action")
42+
}
43+
}

0 commit comments

Comments
 (0)