Skip to content

Commit

Permalink
Implemented validator for jms
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe444 committed May 8, 2019
1 parent d234278 commit 10a817b
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package dev.hltech.dredd.domain.validation.jms;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.hltech.dredd.domain.validation.InterfaceContractValidator;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class JmsContractValidator extends InterfaceContractValidator<List<Contract>, List<Contract>> {

public static final String COMMUNICATION_INTERFACE = "jms";

private final ObjectMapper objectMapper;

public JmsContractValidator() {
super(COMMUNICATION_INTERFACE);
objectMapper = new ObjectMapper();
}

@Override
public List<Contract> asCapabilities(String rawCapabilities) {
try {
return objectMapper.readValue(rawCapabilities, new TypeReference<List<Contract>>(){});
} catch (IOException e) {
e.printStackTrace();
}
return new ArrayList<>();
}

@Override
public List<Contract> asExpectations(String rawExpectations) {
try {
return objectMapper.readValue(rawExpectations, new TypeReference<List<Contract>>(){});
} catch (IOException e) {
e.printStackTrace();
}
return new ArrayList<>();
}

@Override
public List<InteractionValidationResult> validate(List<Contract> expectations, List<Contract> capabilities) {
VauntValidator vauntValidator = new VauntValidator();

List<ValidationResult> validationResults = vauntValidator.validate(expectations, capabilities);

return validationResults.stream()
.map(this::toInteractionValidationResult)
.collect(Collectors.toList());
}

private InteractionValidationResult toInteractionValidationResult(ValidationResult validationResult) {
if (!validationResult.isValid()) {
List<String> errors = validationResult.getErrors().stream()
.map(ValidationError::getDescription)
.collect(Collectors.toList());
return InteractionValidationResult.fail(validationResult.toString(), errors);
}
return InteractionValidationResult.success(validationResult.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package dev.hltech.dredd.domain.validation.jms

import com.fasterxml.jackson.module.jsonSchema.types.NullSchema
import com.fasterxml.jackson.module.jsonSchema.types.NumberSchema
import com.fasterxml.jackson.module.jsonSchema.types.StringSchema
import dev.hltech.dredd.domain.validation.InterfaceContractValidator
import spock.lang.Specification
import spock.lang.Subject

class JmsContractValidatorUT extends Specification {

@Subject
def validator = new JmsContractValidator()

def "Should map raw capabilities"() {
given:
def dstType = DestinationType.QUEUE
def dstName = 'dst'
def rawCapabilities = """
[
{
"destinationType":"${dstType}",
"destinationName":"${dstName}",
"body": {
"type":"string"
}
}
]
"""

expect:
validator.asCapabilities(rawCapabilities) == [new Contract(dstType, dstName, new StringSchema())]
}

def "Should map raw expectations"() {
given:
def dstType = DestinationType.QUEUE
def dstName = 'dst'
def rawExpectations = """
[
{
"destinationType":"${dstType}",
"destinationName":"${dstName}",
"body": {
"type":"string"
}
}
]
"""

expect:
validator.asExpectations(rawExpectations) == [new Contract(dstType, dstName, new StringSchema())]
}

def "If there are no expectations and capabilities - results are empty"() {
given:
def expectations = []

and:
def capabilities = []

when:
def results = validator.validate(expectations, capabilities)

then:
results.size() == 0
}

def "If there are no expectations - results are empty"() {
given:
def expectations = []

and:
def capabilities = [new Contract(DestinationType.QUEUE, 'dst', new StringSchema())]

when:
def results = validator.validate(expectations, capabilities)

then:
results.size() == 0
}

def "If there is matching capability for each expectations - validation results with success"() {
given:
def expectations = [new Contract(DestinationType.QUEUE, 'dst', new StringSchema()),
new Contract(DestinationType.TOPIC, 'dst', new StringSchema()),
new Contract(DestinationType.QUEUE, 'dst2', new StringSchema()),
new Contract(DestinationType.QUEUE, 'dst', new NumberSchema())]

and:
def capabilities = [new Contract(DestinationType.QUEUE, 'dst', new NumberSchema()),
new Contract(DestinationType.TOPIC, 'dst', new StringSchema()),
new Contract(DestinationType.QUEUE, 'dst2', new StringSchema()),
new Contract(DestinationType.QUEUE, 'dst', new StringSchema()),
new Contract(DestinationType.QUEUE, 'weirdDst', new NumberSchema())]

when:
def results = validator.validate(expectations, capabilities)

then:
results.size() == 4
results.every { result ->
result.status == InterfaceContractValidator.InteractionValidationStatus.OK
result.errors.size() == 0
}
}

def "If there are some umnatched expectatuions - validation results with success"() {
given:
def expectations = [new Contract(DestinationType.QUEUE, 'dst', new StringSchema()),
new Contract(DestinationType.TOPIC, 'dst', new StringSchema()),
new Contract(DestinationType.QUEUE, 'dst2', new StringSchema()),
new Contract(DestinationType.QUEUE, 'dst', new NumberSchema()),
new Contract(DestinationType.QUEUE, 'weirdDst', new NumberSchema()),
new Contract(DestinationType.QUEUE, 'dst', new NullSchema())]

and:
def capabilities = [new Contract(DestinationType.QUEUE, 'dst', new NumberSchema()),
new Contract(DestinationType.TOPIC, 'dst', new StringSchema()),
new Contract(DestinationType.QUEUE, 'dst2', new StringSchema()),
new Contract(DestinationType.QUEUE, 'dst', new StringSchema())]

when:
def results = validator.validate(expectations, capabilities)

then:
results.size() == 6
results.any { result ->
result.status == InterfaceContractValidator.InteractionValidationStatus.FAILED
result.errors.size() == 1
result.errors[0] == 'Missing endpoint required by consumer'
}
results.any { result ->
result.status == InterfaceContractValidator.InteractionValidationStatus.FAILED
result.errors.size() == 1
result.errors[0] == 'Wrong schema of the message'
}
results.count { result ->
result.status == InterfaceContractValidator.InteractionValidationStatus.OK
result.errors.size() == 0
} == 4
}
}

0 comments on commit 10a817b

Please sign in to comment.