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

Unit test testGetWithoutTid in CorrelationHandlerTest is order dependent #2012

Closed
SaaiVenkat opened this issue Nov 22, 2023 · 0 comments · Fixed by #2013
Closed

Unit test testGetWithoutTid in CorrelationHandlerTest is order dependent #2012

SaaiVenkat opened this issue Nov 22, 2023 · 0 comments · Fixed by #2013

Comments

@SaaiVenkat
Copy link
Contributor

Issue

  • In com.networknt.correlation.CorrelationHandlerTest, if testGetWithoutTid() is run after testGetWithoutTidNoAutogen(), we face the following issue
java.lang.AssertionError: 
Expected :200
Actual   :500
	at org.junit.Assert.fail(Assert.java:89)
	at org.junit.Assert.failNotEquals(Assert.java:835)
	at org.junit.Assert.assertEquals(Assert.java:647)
	at org.junit.Assert.assertEquals(Assert.java:633)
	at com.networknt.correlation.CorrelationHandlerTest.test02_testGetWithoutTid(CorrelationHandlerTest.java:158)
  • Since JUnit 4 doesn't guarantee the order of test executions by default, JUnit could execute testGetWithoutTid() after testGetWithoutTidNoAutogen() resulting in the above error.

Root Cause

  • The test testGetWithoutTidNoAutogen() sets the autogenCorrelationID to false at
    CorrelationHandler.config.setAutogenCorrelationID(false);
  • When the test testGetWithoutTid() is run after the above step, the following code is not executed because of the polluted autogenCorrelationID
    if(config.isAutogenCorrelationID()) {
    // generate a UUID and put it into the request header
    cId = Util.getUUID();
    exchange.getRequestHeaders().put(HttpStringConstants.CORRELATION_ID, cId);
    String tId = exchange.getRequestHeaders().getFirst(HttpStringConstants.TRACEABILITY_ID);
    if(tId != null && logger.isInfoEnabled()) {
    logger.info("Associate traceability Id " + tId + " with correlation Id " + cId);
    }
  • Since the UUID is not generated and not set in the request handler, the request given from the test testGetWithoutTid() results in a 5xx server error

Proposed Solution

  • Resetting the CorrelationHandler.config to default value before each test run will ensure that the unit tests are run with the correct config value
  • This can be done by setting the configuration to its default value through CorrelationHandler.config.setAutogenCorrelationID(true); after the test testGetWithoutTidNoAutogen() is executed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant