Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.atlassian.oai.validator.pact;
import au.com.dius.pact.core.model.BrokerUrlSource;
import au.com.dius.pact.core.model.DefaultPactReader;
import au.com.dius.pact.core.model.FileSource;
import au.com.dius.pact.core.model.Pact;
import au.com.dius.pact.core.model.RequestResponseInteraction;
import au.com.dius.pact.core.model.UrlSource;
import au.com.dius.pact.core.model.V4Pact;
import au.com.dius.pact.core.pactbroker.PactBrokerClient;
import au.com.dius.pact.core.pactbroker.PactBrokerClientConfig;
import au.com.dius.pact.core.pactbroker.PactBrokerResult;
import au.com.dius.pact.provider.ConsumerInfo;
import com.atlassian.oai.validator.OpenApiInteractionValidator;
import com.atlassian.oai.validator.report.ValidationReport;
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
/**
* A validator that can be used on the Provider side to validate Consumer Pacts against the
* Provider's OpenAPI / Swagger specification.
*
* The validator can be used to validate against all Consumers with Pacts registered in a broker, and/or
* against individual Consumer Pact files sourced from other locations (the file system etc.)
*
* To validate against all Consumers in a broker:
*
*
* @see Pact broker
* @see OpenApiInteractionValidator
* @see ValidatedPactProviderRule
*/
public class PactProviderValidator {
private static final Logger log = LoggerFactory.getLogger(PactProviderValidator.class);
private final OpenApiInteractionValidator validator;
private final Collection consumers = new ArrayList<>();
private PactProviderValidator(@Nonnull final OpenApiInteractionValidator validator,
final Collection consumers) {
this.validator = requireNonNull(validator, "A validator is required");
if (consumers != null) {
this.consumers.addAll(consumers);
}
}
/**
* Create a new {@link PactProviderValidator} that validates Consumers against the given OpenAPI / Swagger specification.
*
* The provided param can be either a URL to the API specification, or an inline specification.
* Supports both JSON and YAML formats.
*
* @param specUrlOrPayload The URL of the OpenAPI / Swagger specification to use, or an inline specification
*
* @return A builder that can create configured {@link PactProviderValidator} instances.
*/
public static Builder createFor(@Nonnull final String specUrlOrPayload) {
return new Builder().withApiSpecification(specUrlOrPayload);
}
/**
* Create a new {@link PactProviderValidator} that validates Consumers against the given OpenAPI / Swagger specification.
*
* @param validator The pre-configured validator instance to use
*
* @return A builder that can create configured {@link PactProviderValidator} instances.
*/
public static Builder createFor(@Nonnull final OpenApiInteractionValidator validator) {
return new Builder().withValidator(validator);
}
/**
* Perform the validation of Consumer Pacts against the configured OpenAPI / Swagger specification.
*
* @return The results of validation for each Consumer.
*/
public PactProviderValidationResults validate() {
log.debug("Validating {} consumers against API spec", consumers.size());
final PactProviderValidationResults result = new PactProviderValidationResults();
if (consumers.isEmpty()) {
log.warn("No consumers supplied. No validation will be performed.");
return result;
}
result.addConsumerResults(
consumers.stream()
.filter(Objects::nonNull)
.map(this::doValidate)
.collect(toList()));
return result;
}
private PactProviderValidationResults.ConsumerResult doValidate(@Nonnull final ConsumerInfo consumer) {
log.debug("Validating consumer '{}' against API spec", consumer.getName());
final PactProviderValidationResults.ConsumerResult result =
new PactProviderValidationResults.ConsumerResult(consumer.getName(), getPactSourceLocation(consumer));
final Map options = new HashMap<>();
final List