|
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
|
|
View Code Duplication |
@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
|
|
|
mapToEntity(form.getCapabilities()), |
|
45
|
|
|
form.getExpectations().entrySet().stream() |
|
46
|
|
|
.collect(Collectors.toMap( |
|
47
|
|
|
Map.Entry::getKey, |
|
48
|
|
|
entry -> mapToEntity(entry.getValue()) |
|
49
|
|
|
) |
|
50
|
|
|
) |
|
51
|
|
|
) |
|
52
|
|
|
)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
View Code Duplication |
@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 ServiceContractsDto newCreate(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version, @RequestBody ServiceContractsForm form) { |
|
62
|
|
|
return toDto(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
|
|
View Code Duplication |
@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
|
|
View Code Duplication |
@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 ServiceContractsDto newGet(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version) { |
|
117
|
|
|
return toDto(this.serviceContractsRepository.find(provider, version).orElseThrow(() -> new ResourceNotFoundException())); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private Map<String, ServiceContracts.Contract> mapToEntity(Map<String, ServiceContractsForm.ContractForm> protocolToContractForms) { |
|
121
|
|
|
return protocolToContractForms.entrySet().stream() |
|
122
|
|
|
.collect(Collectors.toMap( |
|
123
|
|
|
Map.Entry::getKey, |
|
124
|
|
|
entry -> new ServiceContracts.Contract(entry.getValue().getValue(), entry.getValue().getMimeType()) |
|
125
|
|
|
)); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
private Map<String, ServiceContractsDto.ContractDto> mapCapabilitiesToDto(Map<String, ServiceContracts.Contract> capabilities) { |
|
129
|
|
|
return capabilities.entrySet().stream() |
|
130
|
|
|
.collect(Collectors.toMap( |
|
131
|
|
|
Map.Entry::getKey, |
|
132
|
|
|
entry -> new ServiceContractsDto.ContractDto(entry.getValue().getValue(), entry.getValue().getMimeType()) |
|
133
|
|
|
)); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
private Map<String, Map<String, ServiceContractsDto.ContractDto>> mapExpectationsToDto(Map<ServiceContracts.ProviderProtocol, ServiceContracts.Contract> expectations) { |
|
137
|
|
|
HashMap<String, Map<String, ServiceContractsDto.ContractDto>> result = newHashMap(); |
|
138
|
|
|
for (Map.Entry<ServiceContracts.ProviderProtocol, ServiceContracts.Contract> e : expectations.entrySet()) { |
|
139
|
|
|
ServiceContracts.ProviderProtocol pp = e.getKey(); |
|
140
|
|
|
ServiceContracts.Contract contract = e.getValue(); |
|
141
|
|
|
if (!result.containsKey(pp.getProvider())) { |
|
142
|
|
|
result.put(pp.getProvider(), newHashMap()); |
|
143
|
|
|
} |
|
144
|
|
|
result.get(pp.getProvider()).put(pp.getProtocol(), new ServiceContractsDto.ContractDto(contract.getValue(), contract.getMimeType())); |
|
145
|
|
|
} |
|
146
|
|
|
return result; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
private ServiceContractsDto toDto(ServiceContracts serviceContracts) { |
|
150
|
|
|
return new ServiceContractsDto( |
|
151
|
|
|
serviceContracts.getName(), |
|
152
|
|
|
serviceContracts.getVersion(), |
|
153
|
|
|
mapCapabilitiesToDto(serviceContracts.getCapabilitiesPerProtocol()), |
|
154
|
|
|
mapExpectationsToDto(serviceContracts.getExpectations()) |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|