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

kie-issues#1807: Test Scenario version attribute is not correctly retrieved #6254

Merged
merged 5 commits into from
Feb 17, 2025

Conversation

yesamer
Copy link
Contributor

@yesamer yesamer commented Feb 13, 2025

Closes: apache/incubator-kie-issues#1807

The problem
The Test Scenario Engine needs to retrieve the scesim version from the xml root node to apply the correct migration strategy.
The current logic that retrieves that info, uses a regex search, that is too strict: regex patterns with this exact structure
<ScenarioSimulationModel version=\"x.x\" will be found. That means if any other attribute is present in the XML node (or just a single whitespace), the search will fail
eg. <ScenarioSimulationModel version=\"x.x\" <ScenarioSimulationModel namespace="..." version=\"x.x\"

The solution
In the same block of code, the DomParser of the xml file is already called. So, I just moved that in the first method line and took advantage of that to retrieve the version in a "safer" way.
This means, the regex search is no longer required, and the version can be safely retrieved disregarding its position in the node and avoiding collision with other possible attributes with the name.

Copy link
Contributor

@gitgabrio gitgabrio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@yesamer yesamer requested a review from jomarko February 14, 2025 08:40
@yesamer
Copy link
Contributor Author

yesamer commented Feb 14, 2025

@jomarko Can you please review it?

@yesamer
Copy link
Contributor Author

yesamer commented Feb 17, 2025

@baldimir Can you please review it?

"<ScenarioSimulationModel attribute2=\"whatever\" attribute1=\"whatever\" version=\"1.4\" />"));
assertThat(version).isEqualTo("1.4");
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using a parameterized test here - and possibly add an explanation string, to explain the different use cases we are testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @pibizza, that was my first intent, but scesim module is still based on JUnit 4, so I preferred to don't introduce Junit4 Parametrized tests in this class. About the explanation string, which approach do you suggest, please?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yesamer I would suggest something that describe your use case. In Junit 5 you can add a string for a specific test name. In this case add some infos in the name and restructure grouping for cases. So you can have a test where you have one entry (version or attribute), one case where you have two entries (version and attribute, times the various possible cases good or bad) and finally one case where you have three entries (version and two attributes, times the two possible values.) I expect order is not relevant, so it should not be present in the tests. at this point the name of the test could be extractXXX_version, extract XXX_version_attribute, extractXXX_version_twoattributes. You can go for different groupings, if you prefer, but that is the general idea.

Copy link
Contributor

@pibizza pibizza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments on tests, as usual.

"<ScenarioSimulationModel attribute2=\"whatever\" attribute1=\"whatever\" version=\"1.4\" />"));
assertThat(version).isEqualTo("1.4");
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yesamer I would suggest something that describe your use case. In Junit 5 you can add a string for a specific test name. In this case add some infos in the name and restructure grouping for cases. So you can have a test where you have one entry (version or attribute), one case where you have two entries (version and attribute, times the various possible cases good or bad) and finally one case where you have three entries (version and two attributes, times the two possible values.) I expect order is not relevant, so it should not be present in the tests. at this point the name of the test could be extractXXX_version, extract XXX_version_attribute, extractXXX_version_twoattributes. You can go for different groupings, if you prefer, but that is the general idea.

Copy link
Contributor

@jomarko jomarko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for a PR @yesamer

Comment on lines +184 to 190
public String extractVersion(Document document) {
try {
return document.getElementsByTagName(SCENARIO_SIMULATION_MODEL_NODE)
.item(0).getAttributes().getNamedItem(VERSION_ATTRIBUTE).getTextContent();
} catch (Exception e) {
throw new IllegalArgumentException("Impossible to extract version from the file", e);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only point is only about catching an exception and immediately throwing new one. I believe we could use this approach to avoid try catch approach.

https://medium.com/@gurkanucar/how-to-prevent-null-pointer-exceptions-in-java-8d7c894c9bb4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @jomarko, I would argue that the try-catch is present in the approach you shared, It just moved out in another method. In this case, I don't see the need for that, it would create an unnecessary indirection.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then, I have nothing to share for this PR

@yesamer yesamer merged commit d27006e into apache:main Feb 17, 2025
10 checks passed
@yesamer yesamer deleted the kie-issues#1807 branch February 17, 2025 14:42
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 this pull request may close these issues.

New SceSim Editor saved file can not be run as mvn clean verify
5 participants