From 83da230a4d3aa91dd52cfa69616cf4e9c3a414ed Mon Sep 17 00:00:00 2001
From: wojciechbulaty
Date: Thu, 15 Sep 2016 16:36:25 +0100
Subject: [PATCH] Created a VersionedPactUrl to be used to reference versioned
pacts.
---
.../junit/loader/VersionedPactUrl.java | 35 ++++++++++++
.../junit/loader/VersionedPactUrlLoader.java | 54 +++++++++++++++++++
.../loader/VersionedPactUrlLoaderTest.java | 48 +++++++++++++++++
3 files changed, 137 insertions(+)
create mode 100644 pact-jvm-provider-junit/src/main/java/au/com/dius/pact/provider/junit/loader/VersionedPactUrl.java
create mode 100644 pact-jvm-provider-junit/src/main/java/au/com/dius/pact/provider/junit/loader/VersionedPactUrlLoader.java
create mode 100644 pact-jvm-provider-junit/src/test/java/au/com/dius/pact/provider/junit/loader/VersionedPactUrlLoaderTest.java
diff --git a/pact-jvm-provider-junit/src/main/java/au/com/dius/pact/provider/junit/loader/VersionedPactUrl.java b/pact-jvm-provider-junit/src/main/java/au/com/dius/pact/provider/junit/loader/VersionedPactUrl.java
new file mode 100644
index 0000000000..6cc561fb42
--- /dev/null
+++ b/pact-jvm-provider-junit/src/main/java/au/com/dius/pact/provider/junit/loader/VersionedPactUrl.java
@@ -0,0 +1,35 @@
+package au.com.dius.pact.provider.junit.loader;
+
+import au.com.dius.pact.provider.junit.PactRunner;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Used to point {@link PactRunner} to a versioned source of pacts for contract tests.
+ *
+ * Use ${any.variable} in the url and specify any.variable as a system property.
+ *
+ *
+ * For example, when you annotate a provider test class with:
+ *
{@literal @}VersionedPactUrl(urls = {"http://artifactory:8081/artifactory/consumercontracts/foo-bar/${foo.version}/foo-bar-${foo.version}.json"})
+ * And pass a system property foo.version to the JVM, for example -Dfoo.version=123
+ *
+ *
+ * Then the pact tests will fetch the following contract:
+ *
http://artifactory:8081/artifactory/consumercontracts/foo-bar/123/foo-bar-123.json
+ *
+ *
+ * @see VersionedPactUrlLoader pact loader
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+@PactSource(VersionedPactUrlLoader.class)
+public @interface VersionedPactUrl {
+ /**
+ * @return a list of urls to pact files
+ */
+ String[] urls();
+}
diff --git a/pact-jvm-provider-junit/src/main/java/au/com/dius/pact/provider/junit/loader/VersionedPactUrlLoader.java b/pact-jvm-provider-junit/src/main/java/au/com/dius/pact/provider/junit/loader/VersionedPactUrlLoader.java
new file mode 100644
index 0000000000..75ff9063fe
--- /dev/null
+++ b/pact-jvm-provider-junit/src/main/java/au/com/dius/pact/provider/junit/loader/VersionedPactUrlLoader.java
@@ -0,0 +1,54 @@
+package au.com.dius.pact.provider.junit.loader;
+
+import au.com.dius.pact.model.Pact;
+import com.google.common.annotations.VisibleForTesting;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static java.lang.String.format;
+import static java.util.Arrays.stream;
+
+/**
+ * Implementation of {@link PactLoader} that downloads pacts from given urls containing versions to be filtered in from system properties.
+ *
+ * @see VersionedPactUrl usage instructions
+ */
+public class VersionedPactUrlLoader implements PactLoader {
+ private final String[] urls;
+
+ public VersionedPactUrlLoader(String[] urls) {
+ this.urls = urls;
+ }
+
+ @SuppressWarnings("unused")
+ public VersionedPactUrlLoader(VersionedPactUrl pactUrl) {
+ this(pactUrl.urls());
+ }
+
+ @Override
+ public List load(String providerName) throws IOException {
+ return new PactUrlLoader(expandVariables(urls)).load(providerName);
+ }
+
+ @VisibleForTesting
+ static String[] expandVariables(String[] urls) throws IOException {
+ return stream(urls)
+ .map(VersionedPactUrlLoader::expandVariables)
+ .collect(Collectors.toList())
+ .toArray(new String[urls.length]);
+ }
+
+ private static String expandVariables(String urlWithVariables) {
+ String urlWithVersions = urlWithVariables;
+ for (Map.Entry