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;
}
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);
}
}
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);
}
}
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;
}
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;
}
Aggregations