Skip to content

Commit

Permalink
Update arquillian-algeron to Pact 3.6.15 (#193)
Browse files Browse the repository at this point in the history
* update pact version to 3.6.15
* support provider state params
* fix code style, restore wildcard imports to explicit imports
  • Loading branch information
kubinator authored Nov 1, 2022
1 parent ec79372 commit f03d8f1
Show file tree
Hide file tree
Showing 18 changed files with 129 additions and 59 deletions.
2 changes: 1 addition & 1 deletion pact/consumer/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
</dependency>
<dependency>
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-consumer_2.11</artifactId>
<artifactId>pact-jvm-consumer_2.12</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private ConsumerProviderPair executePactFragment(EventContext<Test> testEventCon
}

private PactVerificationResult runPactTest(EventContext<Test> base, RequestResponsePact requestResponsePact) {
return runConsumerTest(requestResponsePact, mockProviderConfigInstance.get(), mockServer -> base.proceed());
return runConsumerTest(requestResponsePact, mockProviderConfigInstance.get(), (mockServer, executionContext) -> base.proceed());
}

private void validateResult(PactVerificationResult result, PactVerification pactVerification) throws Throwable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Archive<?> createAuxiliaryArchive() {
arquillianPactConsumer.add(new StringAsset(configuration), "/pact-consumer-configuration.properties");

final JavaArchive[] pactConsumerDeps = Maven.resolver()
.resolve("au.com.dius:pact-jvm-consumer_2.11:" + getVersion())
.resolve("au.com.dius:pact-jvm-consumer_2.12:" + getVersion())
.withTransitivity().as(JavaArchive.class);

final JavaArchive merge = merge(arquillianPactConsumer, pactConsumerDeps);
Expand All @@ -79,6 +79,8 @@ private JavaArchive addSunHttpServer(JavaArchive arquillianPactConsumer) {
.addClass(Class.forName("sun.net.httpserver.TimeSource"))
.addClass(Class.forName("sun.net.httpserver.ServerImpl$ServerTimerTask"))
.addClass(Class.forName("sun.net.httpserver.ServerImpl$ServerTimerTask1"))
.addClass(Class.forName("sun.net.httpserver.ServerImpl$1"))
.addClass(Class.forName("sun.net.httpserver.ServerImpl$2"))
.addClass(Class.forName("sun.net.httpserver.ServerConfig"))
.addClass(Class.forName("sun.net.httpserver.ServerConfig$1"))
.addClass(Class.forName("sun.net.httpserver.ServerConfig$2"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class PactConsumerVersionExtractorTest {

@Test
public void should_extract_version_of_pact_from_manifest() {
assertThat(PactConsumerVersionExtractor.fromClassPath()).startsWith("3.5");
assertThat(PactConsumerVersionExtractor.fromClassPath()).startsWith("3.6");
}
}
2 changes: 1 addition & 1 deletion pact/consumer/ftest-container/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</dependency>
<dependency>
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-consumer_2.11</artifactId>
<artifactId>pact-jvm-consumer_2.12</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion pact/consumer/ftest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</dependency>
<dependency>
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-consumer_2.11</artifactId>
<artifactId>pact-jvm-consumer_2.12</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion pact/consumer/pact-broker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<dependency>
<!-- This dependency should be removed when https://github.com/DiUS/pact-jvm/issues/408 fixed -->
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-provider_2.11</artifactId>
<artifactId>pact-jvm-provider_2.12</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
7 changes: 6 additions & 1 deletion pact/provider/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</dependency>
<dependency>
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-provider_2.11</artifactId>
<artifactId>pact-jvm-provider_2.12</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -91,6 +91,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>1.0</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import au.com.dius.pact.model.RequestResponseInteraction;
import au.com.dius.pact.model.RequestResponsePact;
import org.apache.http.HttpRequest;
import org.arquillian.algeron.pact.provider.spi.Target;
import org.arquillian.algeron.pact.provider.api.Pacts;
import org.arquillian.algeron.pact.provider.spi.ArquillianTestClassAwareTarget;
import org.arquillian.algeron.pact.provider.spi.CurrentConsumer;
import org.arquillian.algeron.pact.provider.spi.CurrentInteraction;
import org.arquillian.algeron.pact.provider.spi.PactProviderExecutionAwareTarget;
import org.arquillian.algeron.pact.provider.spi.ProviderContextAwareTarget;
import org.arquillian.algeron.pact.provider.spi.State;
import org.arquillian.algeron.pact.provider.spi.Target;
import org.arquillian.algeron.pact.provider.spi.TargetRequestFilter;
import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
Expand Down Expand Up @@ -90,7 +91,7 @@ private void executePacts(EventContext<Test> test, final Pacts pacts, final Fiel
}

for (final RequestResponseInteraction interaction : requestResponsePact.getInteractions()) {
executeStateChanges(interaction, testClass, testInstance);
Map<String, ?> stateParams = executeStateChanges(interaction, testClass, testInstance);

Target target = targetInstance.get();

Expand All @@ -106,6 +107,10 @@ private void executePacts(EventContext<Test> test, final Pacts pacts, final Fiel
pactProviderExecutionAwareTarget.setConsumer(pact.getConsumer());
pactProviderExecutionAwareTarget.setRequestResponseInteraction(interaction);
}
if (target instanceof ProviderContextAwareTarget) {
ProviderContextAwareTarget providerContextAwareTarget = (ProviderContextAwareTarget) target;
providerContextAwareTarget.setStateParams(stateParams);
}

// Inject current interaction to test
if (interactionField != null) {
Expand Down Expand Up @@ -181,8 +186,8 @@ private void validatePublicVoidMethods(Method method, final List<Throwable> erro
logger.log(Level.SEVERE, publicError);
errors.add(new IllegalArgumentException(publicError));
}
if (!returnsVoid(method)) {
String voidError = "Method %s should return void";
if (!(returnsVoid(method) || returnsMap(method))) {
String voidError = "Method %s should return void or Map";
logger.log(Level.SEVERE, voidError);
errors.add(new IllegalArgumentException(voidError));
}
Expand Down Expand Up @@ -213,30 +218,36 @@ private Field validateAndGetResourceField(TestClass testClass, Class<?> fieldTyp
return null;
}

protected void executeStateChanges(final RequestResponseInteraction interaction, final TestClass testClass,
protected Map<String, ?> executeStateChanges(final RequestResponseInteraction interaction, final TestClass testClass,
final Object target) {
Map<String, ?> statesParams = new HashMap<>();
if (!interaction.getProviderStates().isEmpty()) {
for (final ProviderState state : interaction.getProviderStates()) {
Arrays.stream(testClass.getMethods(State.class))
.filter(method -> ArrayMatcher.matches(
method.getAnnotation(State.class).value(), state.getName()))
.forEach(method -> {
Object stateParams;
if (isStateMethodWithMapParameter(method)) {
executeMethod(method, target, state.getParams());
stateParams = executeMethod(method, target, state.getParams());
} else {
if (method.getParameterCount() > 0) {
// Use regular expressions to pass parameters.
executeStateMethodWithRegExp(method, state, target);
stateParams = executeStateMethodWithRegExp(method, state, target);
} else {
executeMethod(method, target);
stateParams = executeMethod(method, target);
}
}
if (stateParams instanceof Map) {
statesParams.putAll((Map)stateParams);
}
});
}
}
return statesParams;
}

private void executeStateMethodWithRegExp(Method stateMethod, ProviderState state, Object target) {
private Object executeStateMethodWithRegExp(Method stateMethod, ProviderState state, Object target) {
final Optional<String> matchingStateOptional = ArrayMatcher.firstMatch(
stateMethod.getAnnotation(State.class).value(), state.getName());
// We are sure that at this point, an state is present
Expand All @@ -259,17 +270,17 @@ private void executeStateMethodWithRegExp(Method stateMethod, ProviderState stat
instances[i] = StateTypeConverter.convert(arguments.get(i), parameter);
}

executeMethod(stateMethod, target, instances);
return executeMethod(stateMethod, target, instances);
}

private boolean isStateMethodWithMapParameter(Method method) {
return method.getParameterCount() == 1 &&
Map.class.isAssignableFrom(method.getParameterTypes()[0]);
}

private void executeMethod(Method method, Object target, Object... params) {
private Object executeMethod(Method method, Object target, Object... params) {
try {
method.invoke(target, params);
return method.invoke(target, params);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalArgumentException(e);
}
Expand Down Expand Up @@ -304,4 +315,8 @@ private boolean isPublic(Method method) {
private boolean returnsVoid(Method method) {
return Void.TYPE.equals(method.getReturnType());
}

private boolean returnsMap(Method method) {
return Map.class.equals(method.getReturnType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -25,13 +26,15 @@
import org.arquillian.algeron.pact.provider.spi.ArquillianTestClassAwareTarget;
import org.arquillian.algeron.pact.provider.spi.PactProviderExecutionAwareTarget;
import org.arquillian.algeron.pact.provider.spi.Provider;
import org.arquillian.algeron.pact.provider.spi.ProviderContextAwareTarget;
import org.arquillian.algeron.pact.provider.spi.Target;
import org.arquillian.algeron.pact.provider.spi.TargetRequestFilter;
import org.arquillian.algeron.pact.provider.spi.VerificationReports;
import org.jboss.arquillian.core.api.Injector;
import org.jboss.arquillian.test.spi.TestClass;

public class HttpTarget implements Target, ArquillianTestClassAwareTarget, PactProviderExecutionAwareTarget {
public class HttpTarget implements Target, ArquillianTestClassAwareTarget, PactProviderExecutionAwareTarget,
ProviderContextAwareTarget {

private String path;
private String host;
Expand All @@ -47,6 +50,7 @@ public class HttpTarget implements Target, ArquillianTestClassAwareTarget, PactP
private au.com.dius.pact.model.Consumer currentConsumer;
private RequestResponseInteraction currentRequestResponseInteraction;
private Injector injector;
private Map<String, ?> currentStateParams;

/**
* @param host
Expand Down Expand Up @@ -179,6 +183,7 @@ public void testInteraction(URL url) {
private void resetCurrentFields() {
this.currentConsumer = null;
this.currentRequestResponseInteraction = null;
this.currentStateParams = null;
}

@Override
Expand All @@ -199,15 +204,16 @@ public void testInteraction(String consumerName, RequestResponseInteraction inte

Map<String, Object> failures = new HashMap<>();
ProviderClient client = new ProviderClient(provider, new HttpClientFactory());
verifier.verifyResponseFromProvider(provider, interaction, interaction.getDescription(), failures, client);
verifier.verifyResponseFromProvider(provider, interaction, interaction.getDescription(), failures, client,
currentStateParams == null ? Collections.emptyMap() : currentStateParams);

try {
if (!failures.isEmpty()) {
verifier.displayFailures(failures);
throw getAssertionError(failures);
}
} finally {
verifier.finialiseReports();
verifier.finaliseReports();
}
}

Expand All @@ -220,9 +226,7 @@ private ProviderVerifier setupVerifier(RequestResponseInteraction interaction, P
verifier.initialiseReporters(provider);
verifier.reportVerificationForConsumer(consumer, provider);

if (interaction.getProviderState() != null) {
verifier.reportStateForInteraction(interaction.getProviderState(), provider, consumer, true);
}
verifier.reportStateForInteraction(interaction.displayState(), provider, consumer, true);

verifier.reportInteractionDescription(interaction);

Expand Down Expand Up @@ -393,4 +397,9 @@ public void setConsumer(au.com.dius.pact.model.Consumer consumer) {
public void setRequestResponseInteraction(RequestResponseInteraction requestResponseInteraction) {
this.currentRequestResponseInteraction = requestResponseInteraction;
}

@Override
public void setStateParams(Map<String, ?> params) {
this.currentStateParams = Collections.singletonMap("providerState", params);
}
}
Loading

0 comments on commit f03d8f1

Please sign in to comment.