Skip to content
Snippets Groups Projects
Commit 8f7ef456 authored by OZGCloud's avatar OZGCloud
Browse files

Merge pull request 'OZG-5659_npe-on-missing-text' (#360) from...

Merge pull request 'OZG-5659_npe-on-missing-text' (#360) from OZG-5659_npe-on-missing-text into master

Reviewed-on: https://git.ozg-sh.de/ozgcloud-app/vorgang-manager/pulls/360


Reviewed-by: default avatarOZGCloud <ozgcloud@mgm-tp.com>
parents ba9fcb62 b9690133
No related branches found
No related tags found
No related merge requests found
......@@ -120,23 +120,29 @@ class SmartDocumentsBescheidRemoteService implements BescheidRemoteService {
}
Optional<String> extractTextFormXmlFile(File xmlFile) {
var xPath = XPathFactory.newInstance().newXPath();
try {
var document = DocumentBuilderFactory.newDefaultInstance().newDocumentBuilder().parse(xmlFile);
var expr = xPath.compile("/root/SmartDocument/Fields/NachrichtenText/text()");
var text = (Text) expr.evaluate(document, XPathConstants.NODE);
return Optional.ofNullable(text.getTextContent());
return doExtractText(xmlFile);
} catch (XPathExpressionException | SAXException | IOException | ParserConfigurationException e) {
LOG.error("XML-Parsing error on extracting Nachricht-Text: {}", e.getMessage(), e);
LOG.warn("XML-Parsing error on extracting Nachricht-Text: {}", e.getMessage(), e);
} catch (ClassCastException e) {
LOG.error("Error on extraction Nachricht-Text. XPath return unexpected Type.", e);
LOG.warn("Error on extraction Nachricht-Text. XPath return unexpected Type.", e);
} catch (RuntimeException e) {
LOG.error("Unexpected Error on extracting NachrichtText: {}", e.getMessage(), e);
LOG.warn("Unexpected Error on extracting NachrichtText: {}", e.getMessage(), e);
}
return Optional.empty();
}
Optional<String> doExtractText(File xmlFile)
throws SAXException, IOException, ParserConfigurationException, XPathExpressionException {
var xPath = XPathFactory.newInstance().newXPath();
var document = DocumentBuilderFactory.newDefaultInstance().newDocumentBuilder().parse(xmlFile);
var expr = xPath.compile("/root/SmartDocument/Fields/NachrichtenText/text()");
return Optional.ofNullable((Text) expr.evaluate(document, XPathConstants.NODE))
.map(Text::getTextContent);
}
Optional<File> getXMLFile(SmartDocumentsResponse response) {
return getSmartDocumentsFile(response, FILE_TYPE_XML)
.map(SmartDocumentFile::getDocument)
......
......@@ -20,6 +20,7 @@ import de.ozgcloud.bescheid.smartdocuments.SmartDocumentsRequest.CustomerData.Us
import de.ozgcloud.bescheid.vorgang.VorgangTestFactory;
import de.ozgcloud.common.binaryfile.TempFileUtils;
import de.ozgcloud.common.test.TestUtils;
import lombok.SneakyThrows;
class SmartDocumentsBescheidRemoteServiceTest {
......@@ -84,6 +85,16 @@ class SmartDocumentsBescheidRemoteServiceTest {
assertThat(text).isEmpty();
}
@Test
@SneakyThrows
void shouldExpectMissingTextNode() {
File xmlFileWithoutText = TempFileUtils.writeTmpFile(TestUtils.loadFile("SD_answer_without_text.xml"));
var text = service.doExtractText(xmlFileWithoutText);
assertThat(text).isEmpty();
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<root>
<SmartDocument Version="2.0">
<Fields>
</Fields>
</SmartDocument>
</root>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment