Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix pacts loading from broker #130

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -21,12 +21,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -73,7 +68,15 @@ protected List<Pact> getPacts(BeforeClass test) {
contractsSource.setProviderName(serviceName);
final List<URI> contractsDirectory = contractsSource.retrieve();

pacts = loadContractFiles(contractsDirectory, serviceName).stream()
Map<String, Object> options = new HashMap<>();
if (algeronProviderConfigurationInstance.get() != null && algeronProviderConfigurationInstance.get().getRetrieverConfiguration() != null) {
Map<String, Object> config = algeronProviderConfigurationInstance.get().getRetrieverConfiguration();
// if pact retrieve specifies username, we need to create Pact options with authentication flag
if (config.containsKey("username")) {
options.put("authentication", Arrays.asList("Basic", config.get("username"), config.get("password")));
}
}
pacts = loadContractFiles(contractsDirectory, serviceName, options).stream()
.filter(p -> consumerName == null || p.getConsumer().getName().equals(consumerName))
.collect(toList());
} catch (IOException e) {
Expand All @@ -82,25 +85,27 @@ protected List<Pact> getPacts(BeforeClass test) {
return pacts;
}

protected List<Pact> loadContractFiles(List<URI> contracts, String providerName) {
protected List<Pact> loadContractFiles(List<URI> contracts, String providerName, Map<String, ?> options) {
if (contracts != null) {
List<URI> contractFiles = contracts.stream()
.filter(uri -> uri.toString().endsWith(".json"))
.collect(toList());

List<Pact> result = new ArrayList<>();
if (contractFiles != null) {
return contractFiles.stream()
.map(URI::toString)
.map(PactReader::loadPact)
.map(uri -> PactReader.loadPact(options, uri))
.filter(pact -> pact.getProvider().getName().equals(providerName))
.collect(Collectors.toList());

}
}
return new ArrayList<>();
}

protected ContractsRetriever getContractsSource(final TestClass testClass,
AlgeronProviderConfiguration algeronProviderConfiguration) {
AlgeronProviderConfiguration algeronProviderConfiguration) {

// Gets a test annotated directly with PactSource annotation
final ContractsSource pactSource = testClass.getAnnotation(ContractsSource.class);
Expand Down
Expand Up @@ -9,7 +9,7 @@
* Annotation to set consumer in test
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
public @interface Consumer {
/**
* @return consumer name for pact test running
Expand Down