Passed
Pull Request — master (#78)
by Paweł
05:48
created

com.hltech.pact.gen.domain.client.jaxrs.JaxRsMethodRepresentationExtractor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 42
loc 42
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A extractRequestProperties(Method) 5 5 1
A JaxRsMethodRepresentationExtractor(Collection) 2 2 1
A extractResponseProperties(Method) 19 19 2
A extractClientMethodRepresentation(Method) 6 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
package com.hltech.pact.gen.domain.client.jaxrs;
2
3
import com.google.common.collect.Lists;
4
import com.hltech.pact.gen.domain.client.ClientMethodRepresentationExtractor;
5
import com.hltech.pact.gen.domain.client.annotation.handlers.AnnotatedMethodHandler;
6
import com.hltech.pact.gen.domain.client.feign.InteractionInfo;
7
import com.hltech.pact.gen.domain.client.model.ClientMethodRepresentation;
8
import com.hltech.pact.gen.domain.client.model.RequestRepresentation;
9
import com.hltech.pact.gen.domain.client.model.ResponseRepresentation;
10
import com.hltech.pact.gen.domain.client.util.RawHeadersParser;
11
12
import java.lang.reflect.Method;
13
import java.util.Arrays;
14
import java.util.Collection;
15
import java.util.List;
16
import java.util.stream.Collectors;
17
18 View Code Duplication
public class JaxRsMethodRepresentationExtractor implements ClientMethodRepresentationExtractor {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
19
20
    private final Collection<AnnotatedMethodHandler> annotatedMethodHandlers;
21
22
    public JaxRsMethodRepresentationExtractor(Collection<AnnotatedMethodHandler> annotatedMethodHandlers) {
23
        this.annotatedMethodHandlers = annotatedMethodHandlers;
24
    }
25
26
    @Override
27
    public ClientMethodRepresentation extractClientMethodRepresentation(Method clientMethod) {
28
        return ClientMethodRepresentation.builder()
29
                .requestRepresentation(extractRequestProperties(clientMethod))
30
                .responseRepresentationList(extractResponseProperties(clientMethod))
31
                .build();
32
    }
33
34
    private RequestRepresentation extractRequestProperties(Method clientMethod) {
35
        return this.annotatedMethodHandlers.stream()
36
                .filter(annotationHandler -> annotationHandler.isSupported(clientMethod))
37
                .findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown HTTP method"))
38
                .handleRequest(clientMethod);
39
    }
40
41
    private List<ResponseRepresentation> extractResponseProperties(Method method) {
42
        String[] responseHeaders = this.annotatedMethodHandlers.stream()
43
            .filter(annotationHandler -> annotationHandler.isSupported(method))
44
            .findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown HTTP method"))
45
            .getResponseMediaHeaders(method);
46
47
        List<ResponseRepresentation> results = Arrays
48
            .stream(method.getDeclaredAnnotationsByType(InteractionInfo.class))
49
            .map(annotation -> ResponseRepresentation.from(
50
                annotation.responseStatus(),
51
                RawHeadersParser.parseAll(annotation.responseHeaders()),
52
                method,
53
                annotation.description(),
54
                annotation.emptyBodyExpected()))
55
            .collect(Collectors.toList());
56
57
        return !results.isEmpty()
58
            ? results
59
            : Lists.newArrayList(ResponseRepresentation.getDefaultForMethod(method, responseHeaders));
60
    }
61
}
62