|
| 1 | +package springfox.documentation.grails |
| 2 | + |
| 3 | +import com.fasterxml.classmate.TypeResolver |
| 4 | +import com.google.common.base.Objects |
| 5 | +import grails.core.GrailsControllerClass |
| 6 | +import grails.core.GrailsDomainClass |
| 7 | +import grails.web.mapping.LinkGenerator |
| 8 | +import grails.web.mapping.UrlMappings |
| 9 | +import io.swagger.annotations.Api |
| 10 | +import org.springframework.http.MediaType |
| 11 | +import org.springframework.web.bind.annotation.RequestMethod |
| 12 | +import org.springframework.web.method.HandlerMethod |
| 13 | +import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition |
| 14 | +import spock.lang.Specification |
| 15 | +import springfox.documentation.RequestHandlerKey |
| 16 | +import springfox.documentation.service.ResolvedMethodParameter |
| 17 | + |
| 18 | +class GrailsRequestHandlerSpec extends Specification { |
| 19 | + def resolver = new TypeResolver() |
| 20 | + def controller = new AnotherController() |
| 21 | + |
| 22 | + def "Adapts action specification"() { |
| 23 | + given: |
| 24 | + def paths = new PatternsRequestCondition("/test") |
| 25 | + def mediaTypes = [MediaType.APPLICATION_JSON] as Set |
| 26 | + when: |
| 27 | + def sut = grailsRequestHandler() |
| 28 | + and: |
| 29 | + sut.requestMapping |
| 30 | + then: |
| 31 | + thrown(UnsupportedOperationException) |
| 32 | + and: |
| 33 | + sut.declaringClass() == AnotherController |
| 34 | + !sut.isAnnotatedWith(Api) |
| 35 | + sut.getPatternsCondition() == paths |
| 36 | + sut.findControllerAnnotation(Api).isPresent() |
| 37 | + !sut.findAnnotation(Api).isPresent() |
| 38 | + sut.consumes() == mediaTypes |
| 39 | + sut.produces() == mediaTypes |
| 40 | + sut.parameters.size() == 1 |
| 41 | + areEqual(sut.parameters[0], new ResolvedMethodParameter(0, "id", [], resolver.resolve(Integer))) |
| 42 | + sut.returnType == resolver.resolve(Pet) |
| 43 | + sut.key() == new RequestHandlerKey(paths.getPatterns(), [RequestMethod.GET] as Set, mediaTypes, mediaTypes) |
| 44 | + sut.handlerMethod.equals(handlerMethod()) |
| 45 | + } |
| 46 | + |
| 47 | + def areEqual(ResolvedMethodParameter a, ResolvedMethodParameter b) { |
| 48 | + Objects.equal(a.defaultName(), b.defaultName()) && |
| 49 | + Objects.equal(a.getParameterIndex(), b.getParameterIndex()) && |
| 50 | + Objects.equal(a.getParameterType(), b.getParameterType()) |
| 51 | + } |
| 52 | + |
| 53 | + def grailsRequestHandler(){ |
| 54 | + def attributes = new GrailsActionAttributes( |
| 55 | + linkGenerator(), |
| 56 | + urlMappings()) |
| 57 | + new GrailsRequestHandler( |
| 58 | + new GrailsActionContext( |
| 59 | + controller(), |
| 60 | + domain(), |
| 61 | + attributes, |
| 62 | + "test" |
| 63 | + ), |
| 64 | + attributes, |
| 65 | + actionSpecification() |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + ActionSpecification actionSpecification() { |
| 70 | + new ActionSpecification( |
| 71 | + [RequestMethod.GET] as Set, |
| 72 | + [MediaType.APPLICATION_JSON] as Set, |
| 73 | + [MediaType.APPLICATION_JSON] as Set, |
| 74 | + handlerMethod(), |
| 75 | + [new ResolvedMethodParameter(0, "id", [], resolver.resolve(Integer))], |
| 76 | + resolver.resolve(Pet) |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + def handlerMethod() { |
| 81 | + new HandlerMethod( |
| 82 | + controller, |
| 83 | + AnotherController.methods[0]) |
| 84 | + } |
| 85 | + |
| 86 | + UrlMappings urlMappings() { |
| 87 | + Mock(UrlMappings) |
| 88 | + } |
| 89 | + |
| 90 | + LinkGenerator linkGenerator() { |
| 91 | + def links = Mock(LinkGenerator) |
| 92 | + links.link(_) >> "/test" |
| 93 | + links.serverBaseURL >> "http://localhost:8080" |
| 94 | + links |
| 95 | + } |
| 96 | + |
| 97 | + GrailsDomainClass domain() { |
| 98 | + Mock(GrailsDomainClass) |
| 99 | + } |
| 100 | + |
| 101 | + GrailsControllerClass controller() { |
| 102 | + def controller = Mock(GrailsControllerClass) |
| 103 | + controller.getClazz() >> AnotherController |
| 104 | + controller.name >> "Another" |
| 105 | + controller |
| 106 | + } |
| 107 | + |
| 108 | + @Api |
| 109 | + class AnotherController { |
| 110 | + def show(int id) { |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments