1
|
|
|
package dev.hltech.dredd.interfaces.rest.interrelationship; |
2
|
|
|
|
3
|
|
|
import dev.hltech.dredd.domain.contracts.ServiceContracts; |
4
|
|
|
import dev.hltech.dredd.domain.contracts.ServiceContractsRepository; |
5
|
|
|
import dev.hltech.dredd.domain.environment.EnvironmentAggregate; |
6
|
|
|
import dev.hltech.dredd.domain.environment.EnvironmentRepository; |
7
|
|
|
import dev.hltech.dredd.interfaces.rest.contracts.ContractsMapper; |
8
|
|
|
import dev.hltech.dredd.interfaces.rest.contracts.ServiceContractsDto; |
9
|
|
|
import io.swagger.annotations.ApiOperation; |
10
|
|
|
import io.swagger.annotations.ApiResponse; |
11
|
|
|
import io.swagger.annotations.ApiResponses; |
12
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
13
|
|
|
import org.springframework.http.MediaType; |
14
|
|
|
import org.springframework.web.bind.annotation.GetMapping; |
15
|
|
|
import org.springframework.web.bind.annotation.PathVariable; |
16
|
|
|
import org.springframework.web.bind.annotation.RestController; |
17
|
|
|
|
18
|
|
|
import java.util.HashMap; |
19
|
|
|
import java.util.Set; |
20
|
|
|
import java.util.stream.Collectors; |
21
|
|
|
|
22
|
|
|
@RestController |
23
|
|
|
public class InterrelationshipController { |
24
|
|
|
|
25
|
|
|
private final EnvironmentRepository environmentRepository; |
26
|
|
|
private final ServiceContractsRepository serviceContractsRepository; |
27
|
|
|
private final ContractsMapper contractsMapper; |
28
|
|
|
|
29
|
|
|
@Autowired |
30
|
|
|
public InterrelationshipController(EnvironmentRepository environmentRepository, |
31
|
|
|
ServiceContractsRepository serviceContractsRepository, |
32
|
|
|
ContractsMapper contractsMapper) { |
33
|
|
|
this.environmentRepository = environmentRepository; |
34
|
|
|
this.serviceContractsRepository = serviceContractsRepository; |
35
|
|
|
this.contractsMapper = contractsMapper; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
@GetMapping(value = "/interrelationship/{environment}", produces = MediaType.APPLICATION_JSON_VALUE) |
39
|
|
|
@ApiOperation(value = "Get interrelationship between services in given environment", nickname = "Validate against environment") |
40
|
|
|
@ApiResponses(value = { |
41
|
|
|
@ApiResponse(code = 200, message = "Success", response = String.class, responseContainer = "list"), |
42
|
|
|
@ApiResponse(code = 500, message = "Failure") |
43
|
|
|
}) |
44
|
|
|
public InterrelationshipDto getInterrelationship (@PathVariable("environment") String env) { |
45
|
|
|
|
46
|
|
|
Set<ServiceContractsDto> serviceContractsSet = environmentRepository.get(env).getAllServices().stream() |
47
|
|
|
.map(this::getServiceContracts) |
48
|
|
|
.map(contractsMapper::toDto) |
49
|
|
|
.collect(Collectors.toSet()); |
50
|
|
|
|
51
|
|
|
return new InterrelationshipDto(env, serviceContractsSet); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private ServiceContracts getServiceContracts(EnvironmentAggregate.ServiceVersion service) { |
55
|
|
|
return serviceContractsRepository.find(service.getName(), service.getVersion()) |
56
|
|
|
.orElseGet(() -> |
57
|
|
|
new ServiceContracts(service.getName(), service.getVersion(), new HashMap<>(), new HashMap<>())); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|