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

Dynamic container junit5 #943

Merged
merged 7 commits into from
Sep 30, 2024

Conversation

vchaitanya
Copy link
Collaborator

@vchaitanya vchaitanya commented Sep 27, 2024

AbstractFlocessor flows() provides all chained transactions grouped together in the flattened list.
After the change, these chained transactions are grouped together under a dynamic container

@vchaitanya vchaitanya marked this pull request as ready for review September 27, 2024 14:15
@therealryan
Copy link
Collaborator

I've taken the liberty of writing a test that dumps out the container/test structure:

/**
 * A simple sequence of flows with no chains
 */
@Test
void simple() {
	expectNodes( model( null, null, null, null, null ),
			"test : 0 []",
			"test : 1 []",
			"test : 2 []",
			"test : 3 []",
			"test : 4 []" );
}

/**
 * A single chain of a single flow
 */
@Test
void link() {
	expectNodes( model( null, "a", null ),
			"test : 0 []",
			"container : Chain: 1 [chain:a]",
			"  test : 1 [chain:a]",
			"test : 2 []" );
}

/**
 * Consecutive single-flow chains
 */
@Test
void links() {
	expectNodes( model( "a", "b", "c" ),
			"container : Chain: 0 [chain:a]",
			"  test : 0 [chain:a]",
			"container : Chain: 1 [chain:b]",
			"  test : 1 [chain:b]",
			"container : Chain: 2 [chain:c]",
			"  test : 2 [chain:c]" );
}

/**
 * A single multi-flow chain
 */
@Test
void chain() {
	// in the middle
	expectNodes( model( null, "a", "a", "a", null ),
			"test : 0 []",
			"container : Chain: 1 [chain:a]",
			"  test : 1 [chain:a]",
			"  test : 2 [chain:a]",
			"  test : 3 [chain:a]",
			"test : 4 []" );

	// at the start
	expectNodes( model( "a", "a", "a", null ),
			"container : Chain: 0 [chain:a]",
			"  test : 0 [chain:a]",
			"  test : 1 [chain:a]",
			"  test : 2 [chain:a]",
			"test : 3 []" );

	// at the end
	expectNodes( model( null, "a", "a", "a" ),
			"test : 0 []",
			"container : Chain: 1 [chain:a]",
			"  test : 1 [chain:a]",
			"  test : 2 [chain:a]",
			"  test : 3 [chain:a]" );
}

/**
 * Multiple multi-flow chains
 */
@Test
void chains() {
	expectNodes( model( "a", "a", null, "b", "b", "c" ),
			"container : Chain: 0 [chain:a]",
			"  test : 0 [chain:a]",
			"  test : 1 [chain:a]",
			"test : 2 []",
			"container : Chain: 3 [chain:b]",
			"  test : 3 [chain:b]",
			"  test : 4 [chain:b]",
			"container : Chain: 5 [chain:c]",
			"  test : 5 [chain:c]" );
}

private static Model model( String... chains ) {
	List<Flow> flows = new ArrayList<>();
	for( int i = 0; i < chains.length; i++ ) {
		int idx = i;
		Flow flow = Creator
				.build( f -> f.meta( data -> data
						.description( String.valueOf( idx ) )
						.tags( Tags.add( Optional.ofNullable( chains[idx] )
								.map( v -> Chain.PREFIX + v )
								.orElse( "" ) ) ) ) );
		flows.add( flow );
	}
	Model model = mock( Model.class );
	when( model.flows( anySet(), anySet() ) )
			.thenReturn( flows.stream() );

	return model;
}

private static void expectNodes( Model model, String... expected ) {
	Flocessor flocessor = new Flocessor( "", model );
	List<String> actual = new ArrayList<>();
	flocessor.tests()
			.forEach( node -> stringify( node, "", actual ) );
	assertEquals(
			copypasta( Stream.of( expected ) ),
			copypasta( actual.stream() ) );
}

private static void stringify( DynamicNode node, String prefix, List<String> lines ) {
	if( node instanceof DynamicTest ) {
		DynamicTest test = (DynamicTest) node;
		lines.add( prefix + "test : " + test.getDisplayName() );
	}
	else if( node instanceof DynamicContainer ) {
		DynamicContainer container = (DynamicContainer) node;
		lines.add( prefix + "container : " + container.getDisplayName() );
		container.getChildren()
				.forEach( child -> stringify( child, prefix + "  ", lines ) );
	}
	else {
		throw new IllegalStateException( "unexpected node " + node.getClass() );
	}
}

/**
 * @param content Some strings
 * @return A string that can be trivially copy/pasted into java source
 */
private static String copypasta( Stream<String> content ) {
	return content
			.map( s -> s.replaceAll( "\r", "" ) )
			.flatMap( s -> Stream.of( s.split( "\n" ) ) )
			.map( s -> s.replaceAll( "\"", "'" ) )
			.collect( Collectors.joining( "\",\n\"", "\"", "\"" ) );
}

The flow structure looks good, but I think we should change the display name of the DynamicContainers - they should just be the chain:foobar tag rather than the name of the first flow in the chain.

@therealryan
Copy link
Collaborator

Looks great!

@therealryan therealryan merged commit 02d3cf2 into Mastercard:main Sep 30, 2024
5 of 6 checks passed
@vchaitanya
Copy link
Collaborator Author

Resolves #896

@therealryan therealryan added enhancement New feature or request java Pull requests that update Java code labels Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request java Pull requests that update Java code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants