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

External log integration in report #987

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -183,6 +183,10 @@ public enum State {
// no-op
};

private ReportCustomizer reportCustomizer = new ReportCustomizer() {
// no-op
};

/**
* @param title The title of this test
* @param model The model to process
Expand Down Expand Up @@ -368,6 +372,17 @@ public T behaviour( Consumer<Assertion> t ) {
return self();
}

/**
* Configures the report customizer behaviour
*
* @param customizer How to modify the {@link FlowData}
* @return <code>this</code> for method chaining.
*/
public T motivation( ReportCustomizer customizer ) {
reportCustomizer = customizer;
return self();
}

/**
* @return The {@link Flow}s to process, in order
*/
Expand Down Expand Up @@ -547,6 +562,7 @@ private int processInteraction( Flow flow, Interaction ntr, List<Assertion> actu
reportUpdates.add( d -> logCapture.end( flow ).forEach( d.logs::add ) );
reportUpdates.add( d -> d.logs.add( error(
"Encountered error: " + LogEvent.stackTrace( e ) ) ) );
reportUpdates.add( d -> reportCustomizer.customizeReport( d, assrt ) );
report( w -> w.with( flow, reportUpdates.stream()
.reduce( d -> {
// no-op
Expand Down Expand Up @@ -612,6 +628,9 @@ private int processMessage( Flow flow, Assertion assertion,
// (which populates the report) before failing
parseFailures.add( e );
}
finally {
reportUpdates.add( d -> reportCustomizer.customizeReport( d, assertion ) );
}
return 1;
}
return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.mastercard.test.flow.assrt;

import com.mastercard.test.flow.report.data.FlowData;

/**
* Interface for customizing the report data before it is written to storage.
* Implementations of this interface can modify the {@link FlowData#motivation}
* and assertion to add additional information, such as links to external logs.
*/
public interface ReportCustomizer {
/**
* Customizes the report data. Called after the flow has been processed and
* before the report is written to storage.
*
* @param flowData The data of the flow being reported.
* @param assertion The assertion related to the flow.
*/
default void customizeReport( FlowData flowData, Assertion assertion ) {
// no-op
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void fluency() {
assertSame( tf, tf.checkers() );
assertSame( tf, tf.logs( null ) );
assertSame( tf, tf.autonomous() );
assertSame( tf, tf.motivation( null ) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.mastercard.test.flow.assrt;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
vchaitanya marked this conversation as resolved.
Show resolved Hide resolved

import com.mastercard.test.flow.report.Reader;
import com.mastercard.test.flow.report.data.Entry;
import com.mastercard.test.flow.report.data.FlowData;
import com.mastercard.test.flow.report.data.Index;

/**
* Demonstrates the execution {@link ReportCustomizer}
*/
class ReportCustomizerTest {

/**
* Update motivation in the report even when flow is not processed due to some
* exception
*/
@Test
void motivationNoBehaviour() {
TestFlocessor tf = new TestFlocessor( "motivation without behaviour", TestModel.abc() )
.motivation( new ReportCustomizer() {
@Override
public void customizeReport( FlowData flowData, Assertion assertion ) {
flowData.motivation += "common motivation";
}
} )
.reporting( Reporting.QUIETLY )
.system( AbstractFlocessor.State.LESS, TestModel.Actors.B );

tf.execute();

assertEquals( "abc [] error No test behaviour specified", tf.events() );

// This is also recorded to the report
Reader r = new Reader( tf.report() );
Index index = r.read();
Entry ie = index.entries.get( 0 );
FlowData fd = r.detail( ie );
assertEquals( "common motivation", fd.motivation );
}

/**
* Motivation can be updated in the report after the flow is processed
*/
@Test
void motivation() {
TestFlocessor tf = new TestFlocessor( "motivation", TestModel.abc() )
.motivation( new ReportCustomizer() {
@Override
public void customizeReport( FlowData flowData, Assertion assertion ) {
String baseUrl = "https://www.google.com/search?q=";
// Extract data from request or response to build a link
String queryToken = new String( assertion.expected().request().content() ).substring( 0,
1 );
queryToken += new String( assertion.actual().response() ).substring( 0,
1 );
String logLink = baseUrl + queryToken;
flowData.motivation += "\n\n[View Logs](" + logLink + ")";
}
} )
.behaviour( assrt -> {
assrt.actual().response( assrt.expected().response().content() );
} )
.reporting( Reporting.QUIETLY )
.system( AbstractFlocessor.State.LESS, TestModel.Actors.B );

tf.execute();

// This is also recorded to the report
Reader r = new Reader( tf.report() );
Index index = r.read();
Entry ie = index.entries.get( 0 );
FlowData fd = r.detail( ie );
assertEquals( "\n\n[View Logs](https://www.google.com/search?q=AB)", fd.motivation );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ public class FlowData {
@JsonProperty("tags")
public final Set<String> tags;
/**
* public access to allow for enhancing the report with additional information
* using {@link com.mastercard.test.flow.assrt.ReportCustomizer}
*
* @see Metadata#motivation()
*/
@JsonProperty("motivation")
public final String motivation;
public String motivation;
/**
* @see Metadata#tags()
*/
Expand Down
Loading