Passed
Push — master ( 82a2f8...3af9f5 )
by Filip
03:41
created

toDto(ServiceContracts)   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
eloc 6
1
package dev.hltech.dredd.interfaces.rest.contracts;
2
3
import dev.hltech.dredd.domain.contracts.ServiceContracts;
4
import org.springframework.stereotype.Service;
5
6
import java.util.HashMap;
7
import java.util.Map;
8
import java.util.stream.Collectors;
9
10
import static com.google.common.collect.Maps.newHashMap;
11
12
@Service
13
public class ContractsMapper {
14
15
    Map<String, Map<String, ServiceContracts.Contract>> mapExpectationsForm(Map<String, Map<String, ServiceContractsForm.ContractForm>> expectations) {
16
        return expectations.entrySet().stream()
17
            .collect(Collectors.toMap(
18
                Map.Entry::getKey,
19
                entry -> mapCapabilitiesForm(entry.getValue())
20
                )
21
            );
22
    }
23
24
    Map<String, ServiceContracts.Contract> mapCapabilitiesForm(Map<String, ServiceContractsForm.ContractForm> capabilities) {
25
        return mapContractsForm(capabilities);
26
    }
27
28
    private Map<String, ServiceContracts.Contract> mapContractsForm(Map<String, ServiceContractsForm.ContractForm> protocolToContractForms) {
29
        return protocolToContractForms.entrySet().stream()
30
            .collect(Collectors.toMap(
31
                Map.Entry::getKey,
32
                entry -> new ServiceContracts.Contract(entry.getValue().getValue(), entry.getValue().getMimeType())
33
            ));
34
    }
35
36
    ServiceContractsDto toDto(ServiceContracts serviceContracts) {
37
        return new ServiceContractsDto(
38
            serviceContracts.getName(),
39
            serviceContracts.getVersion(),
40
            mapCapabilitiesToDto(serviceContracts.getCapabilitiesPerProtocol()),
41
            mapExpectationsToDto(serviceContracts.getExpectations())
42
        );
43
    }
44
45
    private Map<String, Map<String, ServiceContractsDto.ContractDto>> mapExpectationsToDto(Map<ServiceContracts.ProviderProtocol, ServiceContracts.Contract> expectations) {
46
        HashMap<String, Map<String, ServiceContractsDto.ContractDto>> result = newHashMap();
47
        for (Map.Entry<ServiceContracts.ProviderProtocol, ServiceContracts.Contract> e : expectations.entrySet()) {
48
            ServiceContracts.ProviderProtocol pp = e.getKey();
49
            ServiceContracts.Contract contract = e.getValue();
50
            if (!result.containsKey(pp.getProvider())) {
51
                result.put(pp.getProvider(), newHashMap());
52
            }
53
            result.get(pp.getProvider()).put(pp.getProtocol(), new ServiceContractsDto.ContractDto(contract.getValue(), contract.getMimeType()));
54
        }
55
        return result;
56
    }
57
58
    private Map<String, ServiceContractsDto.ContractDto> mapCapabilitiesToDto(Map<String, ServiceContracts.Contract> capabilities) {
59
        return capabilities.entrySet().stream()
60
            .collect(Collectors.toMap(
61
                Map.Entry::getKey,
62
                entry -> new ServiceContractsDto.ContractDto(entry.getValue().getValue(), entry.getValue().getMimeType())
63
            ));
64
    }
65
}
66