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

ContractsController(ServiceContractsRepository,ContractsMapper)   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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.List;
15
16
import static com.google.common.collect.Maps.newHashMap;
17
import static java.util.stream.Collectors.toList;
18
19
@RestController
20
@Transactional
21
public class ContractsController {
22
23
    private ServiceContractsRepository serviceContractsRepository;
24
    private ContractsMapper mapper;
25
26
    @Autowired
27
    public ContractsController(ServiceContractsRepository serviceContractsRepository, ContractsMapper mapper) {
28
        this.serviceContractsRepository = serviceContractsRepository;
29
        this.mapper = mapper;
30
    }
31
32
    @PostMapping(value = "/contracts/{provider}/{version:.+}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
33
    @ApiOperation(value = "Register contracts for a version of a service", nickname = "register contracts")
34
    @ApiResponses(value = {
35
        @ApiResponse(code = 200, message = "Success", response = ServiceContractsDto.class),
36
        @ApiResponse(code = 400, message = "Bad Request"),
37
        @ApiResponse(code = 500, message = "Failure")})
38
    public ServiceContractsDto create(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version, @RequestBody ServiceContractsForm form) {
39
        return mapper.toDto(this.serviceContractsRepository.persist(
40
            new ServiceContracts(
41
                provider,
42
                version,
43
                mapper.mapCapabilitiesForm(form.getCapabilities()),
44
                mapper.mapExpectationsForm(form.getExpectations())
45
            )
46
        ));
47
    }
48
49
    @GetMapping(value = "/contracts", produces = MediaType.APPLICATION_JSON_VALUE)
50
    @ApiOperation(value = "Get names of services with registered contracts", nickname = "get names of services")
51
    @ApiResponses(value = {
52
        @ApiResponse(code = 200, message = "Success", response = String.class, responseContainer = "list"),
53
        @ApiResponse(code = 400, message = "Bad Request"),
54
        @ApiResponse(code = 500, message = "Failure")})
55
    public List<String> getAvailableServiceNames() {
56
        return serviceContractsRepository.getServiceNames();
57
    }
58
59
    @GetMapping(value = "/contracts/{serviceName}", produces = MediaType.APPLICATION_JSON_VALUE)
60
    @ApiOperation(value = "Get versions of a service with registered contracts", nickname = "get versions of a service")
61
    @ApiResponses(value = {
62
        @ApiResponse(code = 200, message = "Success", response = String.class, responseContainer = "list"),
63
        @ApiResponse(code = 400, message = "Bad Request"),
64
        @ApiResponse(code = 500, message = "Failure")})
65
    public List<String> getServiceVersions(@PathVariable(name = "serviceName") String serviceName) {
66
        return serviceContractsRepository.find(serviceName)
67
            .stream()
68
            .map(ServiceContracts::getVersion).sorted()
69
            .collect(toList());
70
    }
71
72
    @GetMapping(value = "/contracts/{provider}/{version:.+}", produces = MediaType.APPLICATION_JSON_VALUE)
73
    @ApiOperation(value = "Get contracts for a version of a service", nickname = "get contracts")
74
    @ApiResponses(value = {
75
        @ApiResponse(code = 200, message = "Success", response = ServiceContractsDto.class),
76
        @ApiResponse(code = 400, message = "Bad Request"),
77
        @ApiResponse(code = 500, message = "Failure")})
78
    public ServiceContractsDto get(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version) {
79
        return mapper.toDto(this.serviceContractsRepository.find(provider, version).orElseThrow(ResourceNotFoundException::new));
80
    }
81
82
}
83