Skip to content
Snippets Groups Projects
Commit 0c1580cf authored by Jan Zickermann's avatar Jan Zickermann
Browse files

Merge remote-tracking branch 'origin/OZG-5412-Dataport-Mantelantrag' into...

Merge remote-tracking branch 'origin/OZG-5412-Dataport-Mantelantrag' into OZG-5412-Dataport-Mantelantrag
parents dcc68027 8777e619
No related branches found
No related tags found
No related merge requests found
......@@ -11,18 +11,35 @@ import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathFactoryConfigurationException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import de.ozgcloud.eingang.common.errorhandling.TechnicalException;
import de.ozgcloud.eingang.common.formdata.IncomingFile;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class XMLHelper {
private XMLHelper() {
private static final DocumentBuilder DOCUMENT_BUILDER = createDocumentBuilder();
private static final XPathFactory X_PATH_FACTORY = createXPathFactory();
public static Document parseDocument(IncomingFile incomingFile) {
try (var inputStream = incomingFile.getContentStream()) {
return DOCUMENT_BUILDER.parse(inputStream);
} catch (SAXException | IOException e) {
throw new TechnicalException("Failed to parse xml document!", e);
}
}
private static final DocumentBuilder DOCUMENT_BUILDER = createDocumentBuilder();
public static XPathExpression compileXPathExpression(String xPathString) {
try {
return X_PATH_FACTORY.newXPath().compile(xPathString);
} catch (XPathExpressionException e) {
throw new TechnicalException("Failed to compile xpath expression!", e);
}
}
private static DocumentBuilder createDocumentBuilder() {
var documentBuilderFactory = DocumentBuilderFactory.newInstance();
......@@ -34,8 +51,6 @@ public class XMLHelper {
}
}
private static final XPathFactory X_PATH_FACTORY = createXPathFactory();
private static XPathFactory createXPathFactory() {
var xPathFactory = XPathFactory.newInstance();
try {
......@@ -45,20 +60,4 @@ public class XMLHelper {
throw new TechnicalException("Failed to configure xpath factory!", e);
}
}
public static Document parseDocument(IncomingFile incomingFile) {
try (var inputStream = incomingFile.getContentStream()) {
return DOCUMENT_BUILDER.parse(inputStream);
} catch (SAXException | IOException e) {
throw new TechnicalException("Failed to parse xml document!", e);
}
}
public static XPathExpression compileXPathExpression(String xPathString) {
try {
return X_PATH_FACTORY.newXPath().compile(xPathString);
} catch (XPathExpressionException e) {
throw new TechnicalException("Failed to compile xpath expression!", e);
}
}
}
......@@ -65,8 +65,4 @@ public class IncomingFileTestFactory {
public static MockMultipartFile asMultipartFile(String multipartName, IncomingFile file) {
return new MockMultipartFile(multipartName, file.getName(), file.getContentType(), file.getContentStream().readAllBytes());
}
public static IncomingFile createWithName(String name) {
return createBuilder().name(name).build();
}
}
......@@ -36,11 +36,11 @@ import java.util.stream.Stream;
@Log4j2
@Component
public class XtaIncomingFilesMapper {
class XtaIncomingFilesMapper {
public static final String ZIP_CONTENT_TYPE = "application/zip";
static final Predicate<IncomingFile> IS_ZIP_FILE = contentType -> ZIP_CONTENT_TYPE.equals(contentType.getContentType());
List<IncomingFile> toIncomingFiles(Collection<XtaFile> messageFiles) {
public List<IncomingFile> toIncomingFiles(Collection<XtaFile> messageFiles) {
if (Objects.nonNull(messageFiles)) {
return messageFiles.stream()
.map(this::toIncomingFile)
......
......@@ -39,7 +39,7 @@ class XdomeaMessageDataMapperTest {
@DisplayName("should throw if not found")
@Test
void shouldThrowIfNotFound() {
var incomingFilesWithout = List.of(IncomingFileTestFactory.createWithName(FILE_NAME_WITHOUT_SUFFIX));
var incomingFilesWithout = List.of(IncomingFileTestFactory.createBuilder().name(FILE_NAME_WITHOUT_SUFFIX).build());
assertThatThrownBy(() -> fileClassifier.findXdomeaXMLFile(incomingFilesWithout))
.isInstanceOf(TechnicalException.class);
......@@ -48,9 +48,9 @@ class XdomeaMessageDataMapperTest {
@DisplayName("should return if found")
@Test
void shouldReturnIfFound() {
var targetIncomingFile = IncomingFileTestFactory.createWithName(FILE_NAME_WITH_SUFFIX);
var targetIncomingFile = IncomingFileTestFactory.createBuilder().name(FILE_NAME_WITH_SUFFIX).build();
var incomingFilesWith = List.of(
IncomingFileTestFactory.createWithName(FILE_NAME_WITHOUT_SUFFIX),
IncomingFileTestFactory.createBuilder().name(FILE_NAME_WITHOUT_SUFFIX).build(),
targetIncomingFile
);
......@@ -114,7 +114,7 @@ class XdomeaMessageDataMapperTest {
ATTATCHMENT_PNG_NAME,
ATTATCHMENT_PDF_NAME
)
.map(IncomingFileTestFactory::createWithName)
.map(name -> IncomingFileTestFactory.createBuilder().name(name).build())
.toList();
doReturn(xdomeaXMLFile).when(fileClassifier).findXdomeaXMLFile(incomingFileList);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment