Passed
Push — master ( c2b3b2...3d45b9 )
by Filip
03:45
created

dev.hltech.dredd.domain.validation.jms.JmsContractValidator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 54
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A asExpectations(String) 0 9 2
A JmsContractValidator() 0 3 1
A asCapabilities(String) 0 9 2
A toInteractionValidationResult(ValidationResult) 0 8 2
A validate(List,List) 0 9 1
1
package dev.hltech.dredd.domain.validation.jms;
2
3
import com.fasterxml.jackson.core.type.TypeReference;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import dev.hltech.dredd.domain.validation.InterfaceContractValidator;
6
import lombok.extern.slf4j.Slf4j;
7
import org.springframework.stereotype.Component;
8
9
import java.io.IOException;
10
import java.util.ArrayList;
11
import java.util.List;
12
import java.util.stream.Collectors;
13
14
@Component
15
@Slf4j
16
public class JmsContractValidator extends InterfaceContractValidator<List<Contract>, List<Contract>> {
17
18
    public static final String COMMUNICATION_INTERFACE = "jms";
19
20
    private final ObjectMapper objectMapper;
21
22
    public JmsContractValidator() {
23
        super(COMMUNICATION_INTERFACE);
24
        objectMapper = new ObjectMapper();
25
    }
26
27
    @Override
28
    public List<Contract> asCapabilities(String rawCapabilities) {
29
        try {
30
            return objectMapper.readValue(rawCapabilities, new TypeReference<List<Contract>>(){});
31
        } catch (IOException e) {
32
            //TODO: after importing vaunt, provide handling
33
            log.error(e.getMessage());
34
        }
35
        return new ArrayList<>();
36
    }
37
38
    @Override
39
    public List<Contract> asExpectations(String rawExpectations) {
40
        try {
41
            return objectMapper.readValue(rawExpectations, new TypeReference<List<Contract>>(){});
42
        } catch (IOException e) {
43
            //TODO: after importing vaunt, provide handling
44
            log.error(e.getMessage());
45
        }
46
        return new ArrayList<>();
47
    }
48
49
    @Override
50
    public List<InteractionValidationResult> validate(List<Contract> expectations, List<Contract> capabilities) {
51
        VauntValidator vauntValidator = new VauntValidator();
52
53
        List<ValidationResult> validationResults = vauntValidator.validate(expectations, capabilities);
54
55
        return validationResults.stream()
56
            .map(this::toInteractionValidationResult)
57
            .collect(Collectors.toList());
58
    }
59
60
    private InteractionValidationResult toInteractionValidationResult(ValidationResult validationResult) {
61
        if (!validationResult.isValid()) {
62
            List<String> errors = validationResult.getErrors().stream()
63
                .map(ValidationError::getDescription)
64
                .collect(Collectors.toList());
65
            return InteractionValidationResult.fail(validationResult.toString(), errors);
66
        }
67
        return InteractionValidationResult.success(validationResult.toString());
68
    }
69
}
70