Skip to content

Commit

Permalink
fix: InteractionFilter ByRequestPath was using concrete class and did…
Browse files Browse the repository at this point in the history
… not work with V4 interactions #1673
  • Loading branch information
rholshausen committed Mar 6, 2023
1 parent 67a6670 commit 92018c0
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 33 deletions.
27 changes: 27 additions & 0 deletions core/model/src/main/kotlin/au/com/dius/pact/core/model/V4Pact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ sealed class V4Interaction(
interactionMarkup, transport),
SynchronousRequestResponse {

@JvmOverloads
constructor(
description: String,
providerStates: List<ProviderState> = listOf(),
request: HttpRequest = HttpRequest(),
response: HttpResponse = HttpResponse(),
interactionId: String? = null
): this("", description, providerStates, request, response, interactionId)

override fun toString(): String {
val pending = if (pending) " [PENDING]" else ""
return "Interaction: $description$pending\n\tin states ${displayState()}\n" +
Expand Down Expand Up @@ -290,6 +299,14 @@ sealed class V4Interaction(
interactionMarkup, transport),
MessageInteraction {

@JvmOverloads
constructor(
description: String,
providerStates: List<ProviderState> = listOf(),
contents: MessageContents = MessageContents(),
interactionId: String? = null
): this("", description, contents, interactionId, providerStates)

override fun toString(): String {
val pending = if (pending) " [PENDING]" else ""
return "Interaction: $description$pending\n\tin states ${displayState()}\n" +
Expand Down Expand Up @@ -388,6 +405,16 @@ sealed class V4Interaction(
transport: String? = null
) : V4Interaction(key, description, interactionId, providerStates, comments, pending, pluginConfiguration,
interactionMarkup, transport), MessageInteraction {

@JvmOverloads
constructor(
description: String,
providerStates: List<ProviderState> = listOf(),
request: MessageContents = MessageContents(),
response: MutableList<MessageContents> = mutableListOf(),
interactionId: String? = null
): this("", description, interactionId, providerStates, mutableMapOf(), false, request, response)

override fun withGeneratedKey(): V4Interaction {
return SynchronousMessages(
generateKey(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import au.com.dius.pact.core.model.Interaction;
import au.com.dius.pact.core.model.RequestResponseInteraction;
import au.com.dius.pact.core.model.SynchronousRequestResponse;

import java.util.Arrays;
import java.util.function.Predicate;
Expand Down Expand Up @@ -34,9 +35,9 @@ class ByRequestPath<I extends Interaction> implements InteractionFilter<I> {
@Override
public Predicate<I> buildPredicate(String[] values) {
return interaction -> {
if (interaction instanceof RequestResponseInteraction) {
if (interaction instanceof SynchronousRequestResponse) {
return Arrays.stream(values).anyMatch(value ->
((RequestResponseInteraction) interaction).getRequest().getPath().matches(value)
((SynchronousRequestResponse) interaction).getRequest().getPath().matches(value)
);
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package au.com.dius.pact.provider.junit.filter;

import au.com.dius.pact.core.model.HttpRequest;
import au.com.dius.pact.core.model.Interaction;
import au.com.dius.pact.core.model.ProviderState;
import au.com.dius.pact.core.model.Request;
import au.com.dius.pact.core.model.RequestResponseInteraction;
import au.com.dius.pact.core.model.V4Interaction;
import au.com.dius.pact.core.model.messaging.Message;
import au.com.dius.pact.provider.junitsupport.filter.InteractionFilter;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class InteractionFilterTest {

Expand All @@ -25,34 +31,27 @@ class ByProviderState {
ByProviderState() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
}

@Test
public void filterRequestResponseInteraction() {
RequestResponseInteraction interaction = new RequestResponseInteraction(
@ParameterizedTest
@ValueSource(classes = {
RequestResponseInteraction.class,
Message.class,
V4Interaction.SynchronousHttp.class,
V4Interaction.SynchronousMessages.class,
V4Interaction.AsynchronousMessage.class
})
public void filterInteraction(Class interactionClass)
throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Interaction interaction = (Interaction) interactionClass.getDeclaredConstructor(String.class, List.class).newInstance(
"test",
Arrays.asList(new ProviderState("state1"), new ProviderState("state2"))
);

Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"state1"}).test(interaction));
Assert.assertFalse(interactionFilter.buildPredicate(new String[]{"noop"}).test(interaction));
Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"state1", "state2"}).test(interaction));
Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"noop", "state2"}).test(interaction));
Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"state1", "state2"}).test(interaction));
Assert.assertFalse(interactionFilter.buildPredicate(new String[]{""}).test(interaction));
}

@Test
public void filterMessageInteraction() {
Message interaction = new Message(
"test",
Arrays.asList(new ProviderState("state1"), new ProviderState("state2"))
);

Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"state1"}).test(interaction));
Assert.assertFalse(interactionFilter.buildPredicate(new String[]{"noop"}).test(interaction));
Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"state1", "state2"}).test(interaction));
Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"noop", "state2"}).test(interaction));
Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"state1", "state2"}).test(interaction));
Assert.assertFalse(interactionFilter.buildPredicate(new String[]{""}).test(interaction));
Assertions.assertTrue(interactionFilter.buildPredicate(new String[]{"state1"}).test(interaction));
Assertions.assertFalse(interactionFilter.buildPredicate(new String[]{"noop"}).test(interaction));
Assertions.assertTrue(interactionFilter.buildPredicate(new String[]{"state1", "state2"}).test(interaction));
Assertions.assertTrue(interactionFilter.buildPredicate(new String[]{"noop", "state2"}).test(interaction));
Assertions.assertTrue(interactionFilter.buildPredicate(new String[]{"state1", "state2"}).test(interaction));
Assertions.assertFalse(interactionFilter.buildPredicate(new String[]{""}).test(interaction));
}
}

Expand All @@ -73,16 +72,41 @@ public void filterRequestResponseInteraction() {
new Request("GET", "/some-path")
);

Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"\\/some-path"}).test(interaction));
Assert.assertFalse(interactionFilter.buildPredicate(new String[]{"other"}).test(interaction));
Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"\\/some-path.*"}).test(interaction));
Assert.assertTrue(interactionFilter.buildPredicate(new String[]{".*some-path"}).test(interaction));
Assert.assertFalse(interactionFilter.buildPredicate(new String[]{""}).test(interaction));
Assertions.assertTrue(interactionFilter.buildPredicate(new String[]{"\\/some-path"}).test(interaction));
Assertions.assertFalse(interactionFilter.buildPredicate(new String[]{"other"}).test(interaction));
Assertions.assertTrue(interactionFilter.buildPredicate(new String[]{"\\/some-path.*"}).test(interaction));
Assertions.assertTrue(interactionFilter.buildPredicate(new String[]{".*some-path"}).test(interaction));
Assertions.assertFalse(interactionFilter.buildPredicate(new String[]{""}).test(interaction));
}

@Test
public void filterMessageInteraction() {
Message interaction = new Message("test", Collections.emptyList());
public void filterSynchronousHttpInteraction() {
V4Interaction.SynchronousHttp interaction = new V4Interaction.SynchronousHttp(
"key",
"test",
Collections.emptyList(),
new HttpRequest("GET", "/some-path")
);

Assertions.assertTrue(interactionFilter.buildPredicate(new String[]{"\\/some-path"}).test(interaction));
Assertions.assertFalse(interactionFilter.buildPredicate(new String[]{"other"}).test(interaction));
Assertions.assertTrue(interactionFilter.buildPredicate(new String[]{"\\/some-path.*"}).test(interaction));
Assertions.assertTrue(interactionFilter.buildPredicate(new String[]{".*some-path"}).test(interaction));
Assertions.assertFalse(interactionFilter.buildPredicate(new String[]{""}).test(interaction));
}

@ParameterizedTest
@ValueSource(classes = {
Message.class,
V4Interaction.SynchronousMessages.class,
V4Interaction.AsynchronousMessage.class
})
public void filterMessageInteraction(Class interactionClass)
throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Interaction interaction = (Interaction) interactionClass.getDeclaredConstructor(String.class, List.class).newInstance(
"test",
Collections.emptyList()
);
Assert.assertFalse(interactionFilter.buildPredicate(new String[]{".*"}).test(interaction));
}
}
Expand Down

0 comments on commit 92018c0

Please sign in to comment.