Search in sources :

Example 1 with Pact

use of au.com.dius.pact.model.Pact in project pact-jvm by DiUS.

the class AmqpTarget method getProviderInfo.

protected ProviderInfo getProviderInfo() {
    Provider provider = testClass.getAnnotation(Provider.class);
    ProviderInfo providerInfo = new ProviderInfo(provider.value());
    providerInfo.setVerificationType(PactVerification.ANNOTATED_METHOD);
    providerInfo.setPackagesToScan(packagesToScan);
    PactBroker annotation = testClass.getAnnotation(PactBroker.class);
    PactFolder folder = testClass.getAnnotation(PactFolder.class);
    if (annotation != null && annotation.host() != null) {
        List list = providerInfo.hasPactsFromPactBroker(annotation.protocol() + "://" + annotation.host() + (annotation.port() != null ? ":" + annotation.port() : ""));
        providerInfo.setConsumers(list);
    } else if (folder != null && folder.value() != null) {
        try {
            PactFolderLoader folderLoader = new PactFolderLoader(folder);
            Map<Pact, File> pactFileMap = folderLoader.loadPactsWithFiles(providerInfo.getName());
            providerInfo.setConsumers(pactFileMap.entrySet().stream().map(e -> new ConsumerInfo(e.getKey().getConsumer().getName(), e.getValue())).collect(Collectors.toList()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return providerInfo;
}
Also used : PactBroker(au.com.dius.pact.provider.junit.loader.PactBroker) Pact(au.com.dius.pact.model.Pact) ConsumerInfo(au.com.dius.pact.provider.ConsumerInfo) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) URL(java.net.URL) PactVerification(au.com.dius.pact.provider.PactVerification) IOException(java.io.IOException) HashMap(java.util.HashMap) Interaction(au.com.dius.pact.model.Interaction) PactFolderLoader(au.com.dius.pact.provider.junit.loader.PactFolderLoader) Provider(au.com.dius.pact.provider.junit.Provider) Collectors(java.util.stream.Collectors) File(java.io.File) URLClassLoader(java.net.URLClassLoader) List(java.util.List) ProviderVerifier(au.com.dius.pact.provider.ProviderVerifier) ProviderInfo(au.com.dius.pact.provider.ProviderInfo) Map(java.util.Map) PactFolder(au.com.dius.pact.provider.junit.loader.PactFolder) Collections(java.util.Collections) ConsumerInfo(au.com.dius.pact.provider.ConsumerInfo) ProviderInfo(au.com.dius.pact.provider.ProviderInfo) PactFolderLoader(au.com.dius.pact.provider.junit.loader.PactFolderLoader) PactBroker(au.com.dius.pact.provider.junit.loader.PactBroker) PactFolder(au.com.dius.pact.provider.junit.loader.PactFolder) List(java.util.List) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) Provider(au.com.dius.pact.provider.junit.Provider)

Example 2 with Pact

use of au.com.dius.pact.model.Pact in project pact-jvm by DiUS.

the class PactRunner method getPactSource.

protected PactLoader getPactSource(final TestClass clazz) throws InitializationError {
    final PactSource pactSource = clazz.getAnnotation(PactSource.class);
    final List<Annotation> pactLoaders = Arrays.stream(clazz.getAnnotations()).filter(annotation -> annotation.annotationType().getAnnotation(PactSource.class) != null).collect(toList());
    if ((pactSource == null ? 0 : 1) + pactLoaders.size() != 1) {
        throw new InitializationError("Exactly one pact source should be set");
    }
    try {
        if (pactSource != null) {
            final Class<? extends PactLoader> pactLoaderClass = pactSource.value();
            try {
                // Checks if there is a constructor with one argument of type Class.
                final Constructor<? extends PactLoader> contructorWithClass = pactLoaderClass.getDeclaredConstructor(Class.class);
                contructorWithClass.setAccessible(true);
                return contructorWithClass.newInstance(clazz.getJavaClass());
            } catch (NoSuchMethodException e) {
                LOGGER.error(e.getMessage(), e);
                return pactLoaderClass.newInstance();
            }
        } else {
            final Annotation annotation = pactLoaders.iterator().next();
            return annotation.annotationType().getAnnotation(PactSource.class).value().getConstructor(annotation.annotationType()).newInstance(annotation);
        }
    } catch (final InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        LOGGER.error("Error while creating pact source", e);
        throw new InitializationError(e);
    }
}
Also used : PactSource(au.com.dius.pact.provider.junit.loader.PactSource) Arrays(java.util.Arrays) PactBroker(au.com.dius.pact.provider.junit.loader.PactBroker) Logger(org.slf4j.Logger) Pact(au.com.dius.pact.model.Pact) LoggerFactory(org.slf4j.LoggerFactory) Description(org.junit.runner.Description) IOException(java.io.IOException) Constructor(java.lang.reflect.Constructor) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) HttpTarget(au.com.dius.pact.provider.junit.target.HttpTarget) TestTarget(au.com.dius.pact.provider.junit.target.TestTarget) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) TestClass(org.junit.runners.model.TestClass) Annotation(java.lang.annotation.Annotation) PactLoader(au.com.dius.pact.provider.junit.loader.PactLoader) ParentRunner(org.junit.runners.ParentRunner) RunNotifier(org.junit.runner.notification.RunNotifier) InitializationError(org.junit.runners.model.InitializationError) PactFolder(au.com.dius.pact.provider.junit.loader.PactFolder) PactSource(au.com.dius.pact.provider.junit.loader.PactSource) Target(au.com.dius.pact.provider.junit.target.Target) InitializationError(org.junit.runners.model.InitializationError) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with Pact

use of au.com.dius.pact.model.Pact in project pact-jvm by DiUS.

the class PactBrokerLoader method loadPactsForProvider.

private List<Pact> loadPactsForProvider(final String providerName, final String tag) throws IOException {
    LOGGER.debug("Loading pacts from pact broker for provider " + providerName + " and tag " + tag);
    URIBuilder uriBuilder = new URIBuilder().setScheme(parseExpressions(pactBrokerProtocol)).setHost(parseExpressions(pactBrokerHost)).setPort(Integer.parseInt(parseExpressions(pactBrokerPort)));
    try {
        List<ConsumerInfo> consumers;
        PactBrokerClient pactBrokerClient = newPactBrokerClient(uriBuilder.build());
        if (StringUtils.isEmpty(tag)) {
            consumers = pactBrokerClient.fetchConsumers(providerName);
        } else {
            consumers = pactBrokerClient.fetchConsumersWithTag(providerName, tag);
        }
        if (failIfNoPactsFound && consumers.isEmpty()) {
            throw new NoPactsFoundException("No consumer pacts were found for provider '" + providerName + "' and tag '" + tag + "'. (URL " + pactBrokerClient.getUrlForProvider(providerName, tag) + ")");
        }
        return consumers.stream().map(consumer -> this.loadPact(consumer, pactBrokerClient.getOptions())).collect(toList());
    } catch (URISyntaxException e) {
        throw new IOException("Was not able load pacts from broker as the broker URL was invalid", e);
    }
}
Also used : PactRunnerTagListExpressionParser.parseTagListExpressions(au.com.dius.pact.provider.junit.sysprops.PactRunnerTagListExpressionParser.parseTagListExpressions) java.util(java.util) Logger(org.slf4j.Logger) Pact(au.com.dius.pact.model.Pact) ConsumerInfo(au.com.dius.pact.provider.ConsumerInfo) URIBuilder(org.apache.http.client.utils.URIBuilder) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) PactRunnerExpressionParser.parseExpressions(au.com.dius.pact.provider.junit.sysprops.PactRunnerExpressionParser.parseExpressions) StringUtils(org.apache.commons.lang3.StringUtils) Collectors.toList(java.util.stream.Collectors.toList) PactReader(au.com.dius.pact.model.PactReader) PactBrokerClient(au.com.dius.pact.provider.broker.PactBrokerClient) URI(java.net.URI) ConsumerInfo(au.com.dius.pact.provider.ConsumerInfo) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) PactBrokerClient(au.com.dius.pact.provider.broker.PactBrokerClient) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 4 with Pact

use of au.com.dius.pact.model.Pact in project pact-jvm by DiUS.

the class PactFolderLoader method loadPactsWithFiles.

public Map<Pact, File> loadPactsWithFiles(final String providerName) throws IOException {
    Map<Pact, File> pacts = new HashMap<Pact, File>();
    File pactFolder = resolvePath();
    File[] files = pactFolder.listFiles(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".json");
        }
    });
    if (files != null) {
        for (File file : files) {
            Pact pact = PactReader.loadPact(file);
            if (pact.getProvider().getName().equals(providerName)) {
                pacts.put(pact, file);
            }
        }
    }
    return pacts;
}
Also used : FilenameFilter(java.io.FilenameFilter) HashMap(java.util.HashMap) Pact(au.com.dius.pact.model.Pact) File(java.io.File)

Example 5 with Pact

use of au.com.dius.pact.model.Pact in project pact-jvm by DiUS.

the class PactFolderLoader method load.

@Override
public List<Pact> load(final String providerName) throws IOException {
    List<Pact> pacts = new ArrayList<Pact>();
    File pactFolder = resolvePath();
    File[] files = pactFolder.listFiles(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".json");
        }
    });
    if (files != null) {
        for (File file : files) {
            Pact pact = PactReader.loadPact(file);
            if (pact.getProvider().getName().equals(providerName)) {
                pacts.add(pact);
            }
        }
    }
    return pacts;
}
Also used : FilenameFilter(java.io.FilenameFilter) Pact(au.com.dius.pact.model.Pact) ArrayList(java.util.ArrayList) File(java.io.File)

Aggregations

Pact (au.com.dius.pact.model.Pact)5 File (java.io.File)3 IOException (java.io.IOException)3 ConsumerInfo (au.com.dius.pact.provider.ConsumerInfo)2 PactBroker (au.com.dius.pact.provider.junit.loader.PactBroker)2 PactFolder (au.com.dius.pact.provider.junit.loader.PactFolder)2 FilenameFilter (java.io.FilenameFilter)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Collectors.toList (java.util.stream.Collectors.toList)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Interaction (au.com.dius.pact.model.Interaction)1 PactReader (au.com.dius.pact.model.PactReader)1 PactVerification (au.com.dius.pact.provider.PactVerification)1 ProviderInfo (au.com.dius.pact.provider.ProviderInfo)1 ProviderVerifier (au.com.dius.pact.provider.ProviderVerifier)1 PactBrokerClient (au.com.dius.pact.provider.broker.PactBrokerClient)1