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
|
|
|
|