Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving dependency inheritance convenience #835

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -57,11 +58,36 @@ public static Flow build( Flow basis, Consumer<? super Deriver>... steps ) {
* @return <code>this</code>
*/
public Deriver inheritDependencies( Flow oldDep, Flow newDep ) {
return inheritDependencies( d -> d.source().flow() == oldDep, newDep );
}

/**
* <p>
* Imports all dependencies from the basis {@link Flow}. The {@link Dependency}
* endpoint {@link Flow}s will be updated:
* </p>
* <ul>
* <li>{@link Dependency#source()} updated per the arguments to this method</li>
* <li>{@link Dependency#sink()} updated to the flow under construction</li>
* </ul>
* <p>
* All other aspects of the imported {@link Dependency} will remain the same.
* </p>
*
* @param newDep The {@link Flow} upon which this {@link Flow} will depend in
* the same way as the basis {@link Flow} did
* @return <code>this</code>
*/
public Deriver inheritDependencies( Flow newDep ) {
return inheritDependencies( d -> true, newDep );
}

private Deriver inheritDependencies( Predicate<Dependency> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
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;

import org.junit.jupiter.api.Assertions;
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;
Expand Down Expand Up @@ -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']",
"<response bloop location>",
derivedSink,
"Msg[Child^2 of 'I need brie, Ben says to say 'bloop'' {<request bloop location>=retrived value of '<response bloop location>'}]",
"<request bloop location>",
"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<Dependency> 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']",
"<response bloop location>",
derivedSink,
"Msg[Child^2 of 'I need brie, Ben says to say 'bloop'' {<request bloop location>=retrived value of '<response bloop location>'}]",
"<request bloop location>",
"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<Dependency> 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']",
"<response bloop location>",
derivedSink,
"Msg[Child^2 of 'I need brie, Ben says to say 'bloop'' {<request bloop location>=retrived value of '<response bloop location>'}]",
"<request bloop location>",
"value", "VALUE" );

assertDependency( "order", deps.next(),
derivedSource, null, null,
derivedSink, null, null );

assertFalse( deps.hasNext() );
}
}

/**
Expand Down
Loading