Test Setup Failed
Pull Request — master (#71)
by
unknown
02:21
created

com.hltech.pact.gen.domain.client.annotation.handlers.JaxRsGetMappingMethodsHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 69
c 1
b 0
f 0
dl 0
loc 92
rs 10
wmc 12

11 Methods

Rating   Name   Duplication   Size   Complexity  
A fromPathParam(Parameter) 0 5 1
A getResponseMediaHeaders(Method) 0 3 1
A extractBody(Method) 0 14 1
A isSupported(Method) 0 3 1
A extractPathParameters(Method) 0 5 1
A fromQueryParam(Parameter) 0 5 1
A handleRequest(Method) 0 10 1
A extractPath(Method) 0 7 2
A fromHeaderParam(HeaderParam) 0 5 1
A extractHeaders(Method) 0 6 1
A extractRequestParameters(Method) 0 5 1
1
package com.hltech.pact.gen.domain.client.annotation.handlers;
2
3
import com.hltech.pact.gen.domain.client.annotation.MappingMethodHandler;
4
import com.hltech.pact.gen.domain.client.model.Body;
5
import com.hltech.pact.gen.domain.client.model.Param;
6
import com.hltech.pact.gen.domain.client.model.RequestRepresentation;
7
import com.hltech.pact.gen.domain.client.util.TypeExtractor;
8
import org.springframework.http.HttpMethod;
9
import org.springframework.util.Assert;
10
11
import javax.ws.rs.Consumes;
12
import javax.ws.rs.GET;
13
import javax.ws.rs.HeaderParam;
14
import javax.ws.rs.Path;
15
import javax.ws.rs.PathParam;
16
import javax.ws.rs.QueryParam;
17
import java.lang.annotation.ElementType;
18
import java.lang.reflect.Method;
19
import java.lang.reflect.Parameter;
20
import java.util.Arrays;
21
import java.util.List;
22
import java.util.Optional;
23
import java.util.stream.Collectors;
24
25
@MappingMethodHandler
26
public class JaxRsGetMappingMethodsHandler implements AnnotatedMethodHandler {
27
28
    @Override
29
    public boolean isSupported(Method method) {
30
        return method.isAnnotationPresent(GET.class);
31
    }
32
33
    @Override
34
    public RequestRepresentation handleRequest(Method method) {
35
        return RequestRepresentation.builder()
36
                .httpMethod(HttpMethod.GET)
37
                .path(extractPath(method))
38
                .headers(extractHeaders(method))
39
                .body(extractBody(method))
40
                .requestParameters(extractRequestParameters(method))
41
                .pathParameters(extractPathParameters(method))
42
                .build();
43
    }
44
45
    @Override
46
    public String[] getResponseMediaHeaders(Method method) {
47
        return new String[0];
48
    }
49
50
    private List<Param> extractPathParameters(Method method) {
51
        return Arrays.stream(method.getParameters())
52
                .filter(param -> param.getAnnotation(PathParam.class) != null)
53
                .map(this::fromPathParam)
54
                .collect(Collectors.toList());
55
    }
56
57
    private Param fromPathParam(Parameter parameter) {
58
        return Param.builder()
59
                .name(parameter.getAnnotation(PathParam.class).value())
60
                .type(parameter.getType())
61
                .build();
62
    }
63
64
    private List<Param> extractRequestParameters(Method method) {
65
        return Arrays.stream(method.getParameters())
66
                .filter(param -> param.getAnnotation(QueryParam.class) != null)
67
                .map(this::fromQueryParam)
68
                .collect(Collectors.toList());
69
    }
70
71
    private Param fromQueryParam(Parameter parameter) {
72
        return Param.builder()
73
                .name(parameter.getAnnotation(QueryParam.class).value())
74
                .type(parameter.getType())
75
                .build();
76
    }
77
78
    private Body extractBody(Method method) {
79
        Optional<Parameter> requestBody = Arrays.stream(method.getParameters())
80
                .filter(p -> p.getAnnotations().length == 0 || p.getAnnotation(Consumes.class) != null)
81
                .findFirst();
82
83
        Body.BodyBuilder builder = Body.builder();
84
        requestBody
85
                .map(Parameter::getType)
86
                .ifPresent(builder::type);
87
        requestBody
88
                .map(Parameter::getParameterizedType)
89
                .map(TypeExtractor::extractParameterTypesFromType)
90
                .ifPresent(builder::genericArgumentTypes);
91
        return builder.build();
92
    }
93
94
    //TODO this method supports only headers added via annotation @HeaderParam.class
95
    private List<Param> extractHeaders(Method method) {
96
        return Arrays.stream(method.getParameters())
97
                .filter(param -> param.getAnnotation(HeaderParam.class) != null)
98
                .map(param -> param.getAnnotation(HeaderParam.class))
99
                .map(this::fromHeaderParam)
100
                .collect(Collectors.toList());
101
    }
102
103
    private Param fromHeaderParam(HeaderParam headerParam) {
104
        return Param
105
                .builder()
106
                .name(headerParam.value())
107
                .build();
108
    }
109
110
    private String extractPath(Method method) {
111
        Path annotation = method.getAnnotation(Path.class) != null
112
                ? method.getAnnotation(Path.class)
113
                : method.getDeclaringClass().getAnnotation(Path.class);
114
        Assert.notNull(annotation, () -> String.format("Cannot find annotation %s on %s or %s",
115
                Path.class.getName(), ElementType.TYPE, ElementType.METHOD));
116
        return annotation.value();
117
    }
118
}
119