Passed
Push — master ( 60fd5a...4bedd1 )
by Tomasz
04:04
created

toDtos(List,ServiceVersion)   B

Complexity

Conditions 3

Size

Total Lines 54
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 42
c 0
b 0
f 0
dl 0
loc 54
rs 8.872

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