Passed
Push — master ( 93a836...a92af1 )
by Tomasz
03:51
created

ValidationController(JudgeD,ServiceContractsRepository,List)   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
c 1
b 0
f 0
rs 9.95
cc 1
1
package dev.hltech.dredd.interfaces.rest.validation;
2
3
import dev.hltech.dredd.domain.JudgeD;
4
import dev.hltech.dredd.domain.contracts.ServiceContracts;
5
import dev.hltech.dredd.domain.contracts.ServiceContractsRepository;
6
import dev.hltech.dredd.domain.validation.EnvironmentValidatorResult;
7
import dev.hltech.dredd.domain.validation.InterfaceContractValidator;
8
import dev.hltech.dredd.interfaces.rest.ResourceNotFoundException;
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.List;
19
import java.util.stream.Collectors;
20
21
import static dev.hltech.dredd.interfaces.rest.validation.Converters.toDtos;
22
23
@RestController
24
public class ValidationController {
25
26
    private final JudgeD judgeD;
27
    private final ServiceContractsRepository serviceContractsRepository;
28
    private final List<InterfaceContractValidator<?, ?>> validators;
29
30
    @Autowired
31
    public ValidationController(
32
        JudgeD judgeD,
33
        ServiceContractsRepository serviceContractsRepository,
34
        List<InterfaceContractValidator<?, ?>> validators
35
    ) {
36
        this.judgeD = judgeD;
37
        this.serviceContractsRepository = serviceContractsRepository;
38
        this.validators = validators;
39
    }
40
41
    @GetMapping(value = "/validation-report/environment/{environment}/service/{serviceName}:{serviceVersion:.+}", produces = MediaType.APPLICATION_JSON_VALUE)
42
    @ApiOperation(value = "Get validation report for contract between given service and given environment", nickname = "Validate against environment")
43
    @ApiResponses(value = {
44
        @ApiResponse(code = 200, message = "Success", response = String.class, responseContainer = "list"),
45
        @ApiResponse(code = 500, message = "Failure"),
46
        @ApiResponse(code = 404, message = "Service not found"),})
47
    public List<ContractValidationReportDto> validate(
48
        @PathVariable("environment") String environment,
49
        @PathVariable("serviceName") String serviceName,
50
        @PathVariable("serviceVersion") String serviceVersion
51
    ) {
52
        ServiceContracts validatedServiceContracts = this.serviceContractsRepository.find(serviceName, serviceVersion)
53
            .orElseThrow(() -> new ResourceNotFoundException());
54
55
        List<EnvironmentValidatorResult> collect = this.validators.stream()
56
            .map(validator ->
57
                this.judgeD.validateServiceAgainstEnv(
58
                    validatedServiceContracts,
59
                    environment,
60
                    validator
61
                ))
62
            .collect(Collectors.toList());
63
        return toDtos(collect, serviceName, serviceVersion);
64
    }
65
66
67
}
68