|
| 1 | +package au.com.dius.pact.provider.junit.filter; |
| 2 | + |
| 3 | +import au.com.dius.pact.core.model.Interaction; |
| 4 | +import au.com.dius.pact.core.model.ProviderState; |
| 5 | +import au.com.dius.pact.core.model.Request; |
| 6 | +import au.com.dius.pact.core.model.RequestResponseInteraction; |
| 7 | +import au.com.dius.pact.core.model.messaging.Message; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.jupiter.api.Nested; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import java.lang.reflect.InvocationTargetException; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.Collections; |
| 15 | + |
| 16 | +class InteractionFilterTest { |
| 17 | + |
| 18 | + @Nested |
| 19 | + class ByProviderState { |
| 20 | + |
| 21 | + InteractionFilter<? super Interaction> interactionFilter = |
| 22 | + InteractionFilter.ByProviderState.class.getDeclaredConstructor().newInstance(); |
| 23 | + |
| 24 | + ByProviderState() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void filterRequestResponseInteraction() { |
| 29 | + RequestResponseInteraction interaction = new RequestResponseInteraction( |
| 30 | + "test", |
| 31 | + Arrays.asList(new ProviderState("state1"), new ProviderState("state2")) |
| 32 | + ); |
| 33 | + |
| 34 | + Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"state1"}).test(interaction)); |
| 35 | + Assert.assertFalse(interactionFilter.buildPredicate(new String[]{"noop"}).test(interaction)); |
| 36 | + Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"state1", "state2"}).test(interaction)); |
| 37 | + Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"noop", "state2"}).test(interaction)); |
| 38 | + Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"state1", "state2"}).test(interaction)); |
| 39 | + Assert.assertFalse(interactionFilter.buildPredicate(new String[]{""}).test(interaction)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void filterMessageInteraction() { |
| 44 | + Message interaction = new Message( |
| 45 | + "test", |
| 46 | + Arrays.asList(new ProviderState("state1"), new ProviderState("state2")) |
| 47 | + ); |
| 48 | + |
| 49 | + Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"state1"}).test(interaction)); |
| 50 | + Assert.assertFalse(interactionFilter.buildPredicate(new String[]{"noop"}).test(interaction)); |
| 51 | + Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"state1", "state2"}).test(interaction)); |
| 52 | + Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"noop", "state2"}).test(interaction)); |
| 53 | + Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"state1", "state2"}).test(interaction)); |
| 54 | + Assert.assertFalse(interactionFilter.buildPredicate(new String[]{""}).test(interaction)); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Nested |
| 59 | + class ByRequestPath { |
| 60 | + |
| 61 | + InteractionFilter<? super Interaction> interactionFilter = |
| 62 | + InteractionFilter.ByRequestPath.class.getDeclaredConstructor().newInstance(); |
| 63 | + |
| 64 | + ByRequestPath() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void filterRequestResponseInteraction() { |
| 69 | + RequestResponseInteraction interaction = new RequestResponseInteraction( |
| 70 | + "test", |
| 71 | + Collections.emptyList(), |
| 72 | + new Request("GET", "/some-path") |
| 73 | + ); |
| 74 | + |
| 75 | + Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"\\/some-path"}).test(interaction)); |
| 76 | + Assert.assertFalse(interactionFilter.buildPredicate(new String[]{"other"}).test(interaction)); |
| 77 | + Assert.assertTrue(interactionFilter.buildPredicate(new String[]{"\\/some-path.*"}).test(interaction)); |
| 78 | + Assert.assertTrue(interactionFilter.buildPredicate(new String[]{".*some-path"}).test(interaction)); |
| 79 | + Assert.assertFalse(interactionFilter.buildPredicate(new String[]{""}).test(interaction)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void filterMessageInteraction() { |
| 84 | + Message interaction = new Message("test", Collections.emptyList()); |
| 85 | + Assert.assertFalse(interactionFilter.buildPredicate(new String[]{".*"}).test(interaction)); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments