Passed
Push — master ( ea107c...296ba7 )
by Filip
04:54
created

getServiceVersions(String)   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
c 0
b 0
f 0
dl 0
loc 11
rs 9.85
1
package dev.hltech.dredd.interfaces.rest.contracts;
2
3
import dev.hltech.dredd.domain.contracts.ServiceContracts;
4
import dev.hltech.dredd.domain.contracts.ServiceContractsRepository;
5
import dev.hltech.dredd.interfaces.rest.ResourceNotFoundException;
6
import io.swagger.annotations.ApiOperation;
7
import io.swagger.annotations.ApiResponse;
8
import io.swagger.annotations.ApiResponses;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.http.MediaType;
11
import org.springframework.transaction.annotation.Transactional;
12
import org.springframework.web.bind.annotation.*;
13
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.stream.Collectors;
18
19
import static com.google.common.collect.Maps.newHashMap;
20
import static java.util.stream.Collectors.toList;
21
22
@RestController
23
@Transactional
24
public class ContractsController {
25
26
    private ServiceContractsRepository serviceContractsRepository;
27
28
    @Autowired
29
    public ContractsController(ServiceContractsRepository serviceContractsRepository) {
30
        this.serviceContractsRepository = serviceContractsRepository;
31
    }
32
33
    @PostMapping(value = "/contracts/{provider}/{version:.+}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
34
    @ApiOperation(value = "Register contracts for a version of a service", nickname = "register contracts")
35
    @ApiResponses(value = {
36
        @ApiResponse(code = 200, message = "Success", response = ServiceContractsDto.class),
37
        @ApiResponse(code = 400, message = "Bad Request"),
38
        @ApiResponse(code = 500, message = "Failure")})
39
    public ServiceContractsDto create(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version, @RequestBody ServiceContractsForm form) {
40
        return toDto(this.serviceContractsRepository.persist(
41
            new ServiceContracts(
42
                provider,
43
                version,
44
                map(form.getCapabilities()),
45
                form.getExpectations().entrySet().stream()
46
                    .collect(Collectors.toMap(
47
                        Map.Entry::getKey,
48
                        entry -> map(entry.getValue())
49
                        )
50
                    )
51
            )
52
        ));
53
    }
54
55
    @PostMapping(value = "/new/contracts/{provider}/{version:.+}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
56
    @ApiOperation(value = "Register contracts for a version of a service", nickname = "register contracts")
57
    @ApiResponses(value = {
58
        @ApiResponse(code = 200, message = "Success", response = ServiceContractsDto.class),
59
        @ApiResponse(code = 400, message = "Bad Request"),
60
        @ApiResponse(code = 500, message = "Failure")})
61
    public NewServiceContractsDto newCreate(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version, @RequestBody NewServiceContractsForm form) {
62
        return newToDto(this.serviceContractsRepository.persist(
63
            new ServiceContracts(
64
                provider,
65
                version,
66
                mapToEntity(form.getCapabilities()),
67
                form.getExpectations().entrySet().stream()
68
                    .collect(Collectors.toMap(
69
                        Map.Entry::getKey,
70
                        entry -> mapToEntity(entry.getValue())
71
                        )
72
                    )
73
            )
74
        ));
75
    }
76
77
    @GetMapping(value = "/contracts", produces = MediaType.APPLICATION_JSON_VALUE)
78
    @ApiOperation(value = "Get names of services with registered contracts", nickname = "get names of services")
79
    @ApiResponses(value = {
80
        @ApiResponse(code = 200, message = "Success", response = String.class, responseContainer = "list"),
81
        @ApiResponse(code = 400, message = "Bad Request"),
82
        @ApiResponse(code = 500, message = "Failure")})
83
    public List<String> getAvailableServiceNames() {
84
        return serviceContractsRepository.getServiceNames();
85
    }
86
87
    @GetMapping(value = "/contracts/{serviceName}", produces = MediaType.APPLICATION_JSON_VALUE)
88
    @ApiOperation(value = "Get versions of a service with registered contracts", nickname = "get versions of a service")
89
    @ApiResponses(value = {
90
        @ApiResponse(code = 200, message = "Success", response = String.class, responseContainer = "list"),
91
        @ApiResponse(code = 400, message = "Bad Request"),
92
        @ApiResponse(code = 500, message = "Failure")})
93
    public List<String> getServiceVersions(@PathVariable(name = "serviceName") String serviceName) {
94
        return serviceContractsRepository.find(serviceName)
95
            .stream()
96
            .map(sv -> sv.getVersion()).sorted()
97
            .collect(toList());
98
    }
99
100
    @GetMapping(value = "/contracts/{provider}/{version:.+}", produces = MediaType.APPLICATION_JSON_VALUE)
101
    @ApiOperation(value = "Get contracts for a version of a service", nickname = "get contracts")
102
    @ApiResponses(value = {
103
        @ApiResponse(code = 200, message = "Success", response = ServiceContractsDto.class),
104
        @ApiResponse(code = 400, message = "Bad Request"),
105
        @ApiResponse(code = 500, message = "Failure")})
106
    public ServiceContractsDto get(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version) {
107
        return toDto(this.serviceContractsRepository.find(provider, version).orElseThrow(() -> new ResourceNotFoundException()));
108
    }
109
110
    @GetMapping(value = "/new/contracts/{provider}/{version:.+}", produces = MediaType.APPLICATION_JSON_VALUE)
111
    @ApiOperation(value = "Get contracts for a version of a service", nickname = "get contracts")
112
    @ApiResponses(value = {
113
        @ApiResponse(code = 200, message = "Success", response = ServiceContractsDto.class),
114
        @ApiResponse(code = 400, message = "Bad Request"),
115
        @ApiResponse(code = 500, message = "Failure")})
116
    public NewServiceContractsDto newGet(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version) {
117
        return newToDto(this.serviceContractsRepository.find(provider, version).orElseThrow(() -> new ResourceNotFoundException()));
118
    }
119
120
    private Map<String, ServiceContracts.Contract> map(Map<String, String> protocolToContractStrings) {
121
        return protocolToContractStrings.entrySet().stream()
122
            .collect(Collectors.toMap(
123
                Map.Entry::getKey,
124
                entry -> new ServiceContracts.Contract(entry.getValue(), MediaType.APPLICATION_JSON_VALUE)
125
            ));
126
    }
127
128
    private Map<String, ServiceContracts.Contract> mapToEntity(Map<String, NewServiceContractsForm.ContractForm> protocolToContractForms) {
129
        return protocolToContractForms.entrySet().stream()
130
            .collect(Collectors.toMap(
131
                Map.Entry::getKey,
132
                entry -> new ServiceContracts.Contract(entry.getValue().getValue(), entry.getValue().getMimeType())
133
            ));
134
    }
135
136
    private Map<String, NewServiceContractsDto.ContractDto> mapCapabilitiesToDto(Map<String, ServiceContracts.Contract> capabilities) {
137
        return capabilities.entrySet().stream()
138
            .collect(Collectors.toMap(
139
                Map.Entry::getKey,
140
                entry -> new NewServiceContractsDto.ContractDto(entry.getValue().getValue(), entry.getValue().getMimeType())
141
            ));
142
    }
143
144
    private Map<String, Map<String, NewServiceContractsDto.ContractDto>> mapExpectationsToDto(Map<ServiceContracts.ProviderProtocol, ServiceContracts.Contract> expectations) {
145
        HashMap<String, Map<String, NewServiceContractsDto.ContractDto>> result = newHashMap();
146
        for (Map.Entry<ServiceContracts.ProviderProtocol, ServiceContracts.Contract> e : expectations.entrySet()) {
147
            ServiceContracts.ProviderProtocol pp = e.getKey();
148
            ServiceContracts.Contract contract = e.getValue();
149
            if (!result.containsKey(pp.getProvider())) {
150
                result.put(pp.getProvider(), newHashMap());
151
            }
152
            result.get(pp.getProvider()).put(pp.getProtocol(), new NewServiceContractsDto.ContractDto(contract.getValue(), contract.getMimeType()));
153
        }
154
        return result;
155
    }
156
157
    private ServiceContractsDto toDto(ServiceContracts serviceContracts) {
158
        return new ServiceContractsDto(
159
            serviceContracts.getName(),
160
            serviceContracts.getVersion(),
161
            serviceContracts.getMappedCapabilities(),
162
            serviceContracts.getMappedExpectations()
163
        );
164
    }
165
166
    private NewServiceContractsDto newToDto(ServiceContracts serviceContracts) {
167
        return new NewServiceContractsDto(
168
            serviceContracts.getName(),
169
            serviceContracts.getVersion(),
170
            mapCapabilitiesToDto(serviceContracts.getCapabilitiesPerProtocol()),
171
            mapExpectationsToDto(serviceContracts.getExpectations())
172
        );
173
    }
174
}
175