-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
*/ | ||
package org.drools.scenariosimulation.backend.util; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
@@ -26,6 +27,9 @@ | |
import org.junit.Test; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Node; | ||
import org.xml.sax.SAXException; | ||
|
||
import javax.xml.parsers.ParserConfigurationException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
@@ -349,24 +353,95 @@ public void migrateIfNecessary() throws Exception { | |
} | ||
|
||
@Test | ||
public void extractVersion() { | ||
String version = instance.extractVersion("<ScenarioSimulationModel version=\"1.0\" version=\"1.1\">"); | ||
assertThat(version).isEqualTo("1.0"); | ||
public void extractVersionNullDocument() { | ||
assertThatThrownBy(() -> instance.extractVersion(null)) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
public void extractVersionMissingAttribute() { | ||
assertThatThrownBy(() -> instance.extractVersion(DOMParserUtil.getDocument("<ScenarioSimulationModel />"))) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
public void extractVersionWhenXmlPrologIsPresent() { | ||
String version = instance.extractVersion("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||
"<ScenarioSimulationModel version=\"1.1\">"); | ||
public void extractVersionMissingVersionAttribute_prolog() { | ||
assertThatThrownBy(() -> instance.extractVersion(DOMParserUtil.getDocument( | ||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||
"<ScenarioSimulationModel />"))) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
public void extractVersionMissingVersionAttribute_prolog_attribute() { | ||
assertThatThrownBy(() -> instance.extractVersion(DOMParserUtil.getDocument( | ||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||
"<ScenarioSimulationModel xmlns=\"whatever\" />"))) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
public void extractVersionWithVersionAttribute() throws ParserConfigurationException, IOException, SAXException { | ||
String version = instance.extractVersion(DOMParserUtil.getDocument( | ||
"<ScenarioSimulationModel version=\"1.1\" />")); | ||
assertThat(version).isEqualTo("1.1"); | ||
} | ||
|
||
@Test | ||
public void extractVersionWhenMoreVersionAttributesArePresent() { | ||
String version = instance.extractVersion("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||
"<ScenarioSimulationModel version=\"1.2\">\n" + | ||
"<someUnknownTag version=\"1.1\"/>\n" + | ||
"</ScenarioSimulationModel>"); | ||
public void extractVersionWithVersionAttribute_prolog() throws ParserConfigurationException, IOException, SAXException { | ||
String version = instance.extractVersion(DOMParserUtil.getDocument( | ||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||
"<ScenarioSimulationModel version=\"1.2\" />")); | ||
assertThat(version).isEqualTo("1.2"); | ||
} | ||
|
||
@Test | ||
public void extractVersionWithVersionAttribute_prolog_attributeRight() throws ParserConfigurationException, IOException, SAXException { | ||
String version = instance.extractVersion(DOMParserUtil.getDocument( | ||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||
"<ScenarioSimulationModel version=\"1.4\" xmlns=\"whatever\" />")); | ||
assertThat(version).isEqualTo("1.4"); | ||
} | ||
|
||
@Test | ||
public void extractVersionWithVersionAttribute_prolog_attributeLeft() throws ParserConfigurationException, IOException, SAXException { | ||
String version = instance.extractVersion(DOMParserUtil.getDocument( | ||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||
"<ScenarioSimulationModel xmlns=\"whatever\" version=\"1.5\" />")); | ||
assertThat(version).isEqualTo("1.5"); | ||
} | ||
|
||
@Test | ||
public void extractVersionWithVersionAttribute_prolog_attributesMiddle() throws ParserConfigurationException, IOException, SAXException { | ||
String version = instance.extractVersion(DOMParserUtil.getDocument( | ||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||
"<ScenarioSimulationModel attribute2=\"whatever\" version=\"1.6\" attribute1=\"whatever\" />")); | ||
assertThat(version).isEqualTo("1.6"); | ||
} | ||
|
||
@Test | ||
public void extractVersionWithVersionAttribute_prolog_attributesRight() throws ParserConfigurationException, IOException, SAXException { | ||
String version = instance.extractVersion(DOMParserUtil.getDocument( | ||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||
"<ScenarioSimulationModel attribute2=\"whatever\" attribute1=\"whatever\" version=\"1.7\" />")); | ||
assertThat(version).isEqualTo("1.7"); | ||
} | ||
|
||
@Test | ||
public void extractVersionWithVersionAttribute_prolog_attributesLeft() throws ParserConfigurationException, IOException, SAXException { | ||
String version = instance.extractVersion(DOMParserUtil.getDocument( | ||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||
"<ScenarioSimulationModel version=\"1.8\" attribute2=\"whatever\" attribute1=\"whatever\" />")); | ||
assertThat(version).isEqualTo("1.8"); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
@Test | ||
public void extractVersionWhenMoreVersionAttributesArePresent() throws ParserConfigurationException, IOException, SAXException { | ||
String version = instance.extractVersion(DOMParserUtil.getDocument("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||
"<ScenarioSimulationModel version=\"1.2\">\n" + | ||
"<someTag version=\"1.1\"/>\n" + | ||
"</ScenarioSimulationModel>")); | ||
assertThat(version).isEqualTo("1.2"); | ||
} | ||
|
||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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