From 78a07426f014c3e7bb5ebc5b461caa42fd595b38 Mon Sep 17 00:00:00 2001 From: Ryan McNally Date: Thu, 6 Jun 2024 16:51:47 +0100 Subject: [PATCH] Improving dependency inheritance convenience --- .../mastercard/test/flow/builder/Deriver.java | 30 ++++++++- .../test/flow/builder/DeriverTest.java | 65 +++++++++++++++---- 2 files changed, 80 insertions(+), 15 deletions(-) diff --git a/builder/src/main/java/com/mastercard/test/flow/builder/Deriver.java b/builder/src/main/java/com/mastercard/test/flow/builder/Deriver.java index 7610a2299f..bc8f27b29b 100644 --- a/builder/src/main/java/com/mastercard/test/flow/builder/Deriver.java +++ b/builder/src/main/java/com/mastercard/test/flow/builder/Deriver.java @@ -1,6 +1,7 @@ package com.mastercard.test.flow.builder; import java.util.function.Consumer; +import java.util.function.Predicate; import com.mastercard.test.flow.Dependency; import com.mastercard.test.flow.Flow; @@ -57,11 +58,36 @@ public static Flow build( Flow basis, Consumer... steps ) { * @return this */ public Deriver inheritDependencies( Flow oldDep, Flow newDep ) { + return inheritDependencies( d -> d.source().flow() == oldDep, newDep ); + } + + /** + *

+ * Imports all dependencies from the basis {@link Flow}. The {@link Dependency} + * endpoint {@link Flow}s will be updated: + *

+ * + *

+ * All other aspects of the imported {@link Dependency} will remain the same. + *

+ * + * @param newDep The {@link Flow} upon which this {@link Flow} will depend in + * the same way as the basis {@link Flow} did + * @return this + */ + public Deriver inheritDependencies( Flow newDep ) { + return inheritDependencies( d -> true, newDep ); + } + + private Deriver inheritDependencies( Predicate target, Flow newSource ) { flow.basis().dependencies() - .filter( d -> d.source().flow() == oldDep ) + .filter( target ) .map( MutableDependency::new ) .map( d -> d - .source( s -> s.flow( newDep ) ) + .source( s -> s.flow( newSource ) ) .sink( s -> s.flow( SELF ) ) ) .forEach( flow::dependency ); return this; diff --git a/builder/src/test/java/com/mastercard/test/flow/builder/DeriverTest.java b/builder/src/test/java/com/mastercard/test/flow/builder/DeriverTest.java index 3f2e86ec15..4c76c23109 100644 --- a/builder/src/test/java/com/mastercard/test/flow/builder/DeriverTest.java +++ b/builder/src/test/java/com/mastercard/test/flow/builder/DeriverTest.java @@ -8,7 +8,9 @@ import static com.mastercard.test.flow.builder.mock.Actrs.EFA; import static java.util.stream.Collectors.toList; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import java.util.Iterator; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; @@ -16,6 +18,7 @@ import org.junit.jupiter.api.Test; import com.mastercard.test.flow.Context; +import com.mastercard.test.flow.Dependency; import com.mastercard.test.flow.Flow; import com.mastercard.test.flow.Interaction; import com.mastercard.test.flow.Residue; @@ -214,19 +217,55 @@ void dependency() { "value", "VALUE" ); Flow derivedSource = Deriver.build( originalSource ); - Flow derivedSink = Deriver.build( originalSink, - flw -> flw.inheritDependencies( originalSource, derivedSource ) - .meta( data -> data.description( "inherit" ) ) ); - - assertEquals( 1, derivedSink.dependencies().count() ); - assertMutatingDependency( "data", derivedSink.dependencies().iterator().next(), - derivedSource, - "Msg[Child^2 of 'Efa can hook you up. Tell her 'bloop' so she knows I sent you']", - "", - derivedSink, - "Msg[Child^2 of 'I need brie, Ben says to say 'bloop'' {=retrived value of ''}]", - "", - "value", "VALUE" ); + + // Using the two-arg inheritDependencies allows you to target a specific + // dependency relationships + { + Flow derivedSink = Deriver.build( originalSink, + flw -> flw.inheritDependencies( originalSource, derivedSource ) + .meta( data -> data.description( "inherit" ) ) ); + + assertEquals( 1, derivedSink.dependencies().count(), + "only one of the two available deps copied" ); + Iterator deps = derivedSink.dependencies().iterator(); + + assertMutatingDependency( "data", deps.next(), + derivedSource, + "Msg[Child^2 of 'Efa can hook you up. Tell her 'bloop' so she knows I sent you']", + "", + derivedSink, + "Msg[Child^2 of 'I need brie, Ben says to say 'bloop'' {=retrived value of ''}]", + "", + "value", "VALUE" ); + + assertFalse( deps.hasNext() ); + } + + // using the 1-arg inheritDependencies will bring over every dependency + { + Flow derivedSink = Deriver.build( originalSink, + flw -> flw.inheritDependencies( derivedSource ) + .meta( data -> data.description( "inherit" ) ) ); + + assertEquals( 2, derivedSink.dependencies().count(), + "Both dependencies copied" ); + Iterator deps = derivedSink.dependencies().iterator(); + + assertMutatingDependency( "data", deps.next(), + derivedSource, + "Msg[Child^2 of 'Efa can hook you up. Tell her 'bloop' so she knows I sent you']", + "", + derivedSink, + "Msg[Child^2 of 'I need brie, Ben says to say 'bloop'' {=retrived value of ''}]", + "", + "value", "VALUE" ); + + assertDependency( "order", deps.next(), + derivedSource, null, null, + derivedSink, null, null ); + + assertFalse( deps.hasNext() ); + } } /**