Passed
Push — master ( 93a836...a92af1 )
by Tomasz
03:51
created

toDtos(List,String,String)   B

Complexity

Conditions 3

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 54
c 1
b 0
f 0
rs 8.5054
cc 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package dev.hltech.dredd.interfaces.rest.validation;
2
3
import com.google.common.collect.Ordering;
4
import dev.hltech.dredd.domain.validation.EnvironmentValidatorResult;
5
6
import java.util.Comparator;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.stream.Collectors;
10
11
import static com.google.common.collect.Maps.newHashMap;
12
13
public class Converters {
14
15
    private Converters() {
16
    }
17
18
    public static List<ContractValidationReportDto> toDtos(List<EnvironmentValidatorResult> validationResults, String serviceName, String serviceVersion) {
19
        Map<ConsumerAndProviderDto, ContractValidationReportDto> result = newHashMap();
20
        validationResults
21
            .stream()
22
            .forEach(environmentValidatorResult -> {
23
                environmentValidatorResult.getCapabilitiesValidationResults()
24
                    .stream()
25
                    .forEach(cvr -> {
26
                        ConsumerAndProviderDto key = new ConsumerAndProviderDto(cvr.getConsumerName(), cvr.getConsumerVersion(), serviceName, serviceVersion);
27
                        if (!result.containsKey(key)) {
28
                            result.put(
29
                                key,
30
                                new ContractValidationReportDto(key)
31
                            );
32
                        }
33
                        result.get(key).addInteractions(
34
                            cvr.getInteractionValidationResults()
35
                                .stream()
36
                                .map(ivr -> new InteractionValidationReportDto(
37
                                    environmentValidatorResult.getCommunicationInterface(),
38
                                    ivr.getName(),
39
                                    ivr.getStatus(),
40
                                    ivr.getErrors()
41
                                ))
42
                                .collect(Collectors.toList())
43
                        );
44
                    });
45
                environmentValidatorResult.getExpectationValidationResults()
46
                    .stream()
47
                    .forEach(evr -> {
48
                        ConsumerAndProviderDto key = new ConsumerAndProviderDto(serviceName, serviceVersion, evr.getProviderName(), evr.getProviderVersion());
49
                        if (!result.containsKey(key)) {
50
                            result.put(
51
                                key,
52
                                new ContractValidationReportDto(key)
53
                            );
54
                        }
55
                        result.get(key).addInteractions(
56
                            evr.getInteractionValidationResults()
57
                                .stream()
58
                                .map(ivr -> new InteractionValidationReportDto(
59
                                    environmentValidatorResult.getCommunicationInterface(),
60
                                    ivr.getName(),
61
                                    ivr.getStatus(),
62
                                    ivr.getErrors()
63
                                ))
64
                                .collect(Collectors.toList())
65
                        );
66
                    });
67
            });
68
69
        return Ordering
70
            .from((Comparator<ContractValidationReportDto>) (o1, o2) -> o1.getConsumerAndProvider().toString().compareTo(o2.getConsumerAndProvider().toString()))
71
            .sortedCopy(result.values());
72
    }
73
}
74