Passed
Push — master ( fa687d...bd5bb4 )
by Tomasz
04:10
created

getAvailableServiceNames()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
dl 0
loc 8
rs 10
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 jdk.nashorn.internal.objects.annotations.Getter;
0 ignored issues
show
Unused Code introduced by
Remove this unused import 'jdk.nashorn.internal.objects.annotations.Getter'.
Loading history...
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.http.MediaType;
12
import org.springframework.web.bind.annotation.*;
13
14
import java.util.List;
15
16
import static java.util.stream.Collectors.*;
17
18
@RestController
19
public class ContractsController {
20
21
    private ServiceContractsRepository serviceContractsRepository;
22
23
    @Autowired
24
    public ContractsController(ServiceContractsRepository serviceContractsRepository) {
25
        this.serviceContractsRepository = serviceContractsRepository;
26
    }
27
28
    @PostMapping(value = "/contracts/{provider}/{version}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
29
    @ApiOperation(value = "Register contracts for a version of a service", nickname = "register contracts")
30
    @ApiResponses(value = {
31
        @ApiResponse(code = 200, message = "Success", response = ServiceContractsDto.class),
32
        @ApiResponse(code = 400, message = "Bad Request"),
33
        @ApiResponse(code = 500, message = "Failure")})
34
    public ServiceContractsDto create(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version, @RequestBody ServiceContractsForm form) {
35
        return toDto(this.serviceContractsRepository.persist(
36
            new ServiceContracts(
37
                provider,
38
                version,
39
                form.getCapabilities(),
40
                form.getExpectations()
41
            )
42
        ));
43
    }
44
45
    @GetMapping(value = "/contracts", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
46
    @ApiOperation(value = "Get names of services with registered contracts", nickname = "get names of services")
47
    @ApiResponses(value = {
48
        @ApiResponse(code = 200, message = "Success", response = String.class, responseContainer = "list"),
49
        @ApiResponse(code = 400, message = "Bad Request"),
50
        @ApiResponse(code = 500, message = "Failure")})
51
    public List<String> getAvailableServiceNames() {
52
        return serviceContractsRepository.getServiceNames();
53
    }
54
55
    @GetMapping(value = "/contracts/{serviceName}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
56
    @ApiOperation(value = "Get versions of a service with registered contracts", nickname = "get versions of a service")
57
    @ApiResponses(value = {
58
        @ApiResponse(code = 200, message = "Success", response = String.class, responseContainer = "list"),
59
        @ApiResponse(code = 400, message = "Bad Request"),
60
        @ApiResponse(code = 500, message = "Failure")})
61
    public List<String> getServiceVersions(@PathVariable(name = "serviceName") String serviceName) {
62
        return serviceContractsRepository.find(serviceName)
63
            .stream()
64
            .map(sv -> sv.getVersion()).sorted()
65
            .collect(toList());
66
    }
67
68
69
    @GetMapping(value = "/contracts/{provider}/{version}", produces = MediaType.APPLICATION_JSON_VALUE)
70
    @ApiOperation(value = "Register contracts for a version of a service", nickname = "register contracts")
71
    @ApiResponses(value = {
72
        @ApiResponse(code = 200, message = "Success", response = ServiceContractsDto.class),
73
        @ApiResponse(code = 400, message = "Bad Request"),
74
        @ApiResponse(code = 500, message = "Failure")})
75
    public ServiceContractsDto create(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version) {
76
        return toDto(this.serviceContractsRepository.find(provider, version).orElseThrow(() -> new ResourceNotFoundException()));
77
    }
78
79
    private ServiceContractsDto toDto(ServiceContracts serviceContracts) {
80
        ServiceContractsDto dto = new ServiceContractsDto();
81
        dto.setName(serviceContracts.getName());
82
        dto.setVersion(serviceContracts.getVersion());
83
84
        dto.setCapabilities(serviceContracts.getCapabilities());
85
        dto.setExpectations(serviceContracts.getExpectations());
86
        return dto;
87
    }
88
89
90
}
91