Skip to content
Snippets Groups Projects
Commit 76526eb5 authored by OZG-Cloud Team's avatar OZG-Cloud Team
Browse files

OZG-5412 refactor

parent 394342ab
No related branches found
No related tags found
No related merge requests found
...@@ -11,18 +11,35 @@ import javax.xml.xpath.XPathExpressionException; ...@@ -11,18 +11,35 @@ import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathFactoryConfigurationException; import javax.xml.xpath.XPathFactoryConfigurationException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import de.ozgcloud.eingang.common.errorhandling.TechnicalException; import de.ozgcloud.eingang.common.errorhandling.TechnicalException;
import de.ozgcloud.eingang.common.formdata.IncomingFile; import de.ozgcloud.eingang.common.formdata.IncomingFile;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class XMLHelper { 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() { private static DocumentBuilder createDocumentBuilder() {
var documentBuilderFactory = DocumentBuilderFactory.newInstance(); var documentBuilderFactory = DocumentBuilderFactory.newInstance();
...@@ -34,8 +51,6 @@ public class XMLHelper { ...@@ -34,8 +51,6 @@ public class XMLHelper {
} }
} }
private static final XPathFactory X_PATH_FACTORY = createXPathFactory();
private static XPathFactory createXPathFactory() { private static XPathFactory createXPathFactory() {
var xPathFactory = XPathFactory.newInstance(); var xPathFactory = XPathFactory.newInstance();
try { try {
...@@ -45,20 +60,4 @@ public class XMLHelper { ...@@ -45,20 +60,4 @@ public class XMLHelper {
throw new TechnicalException("Failed to configure xpath factory!", e); 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 { ...@@ -65,8 +65,4 @@ public class IncomingFileTestFactory {
public static MockMultipartFile asMultipartFile(String multipartName, IncomingFile file) { public static MockMultipartFile asMultipartFile(String multipartName, IncomingFile file) {
return new MockMultipartFile(multipartName, file.getName(), file.getContentType(), file.getContentStream().readAllBytes()); 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; ...@@ -36,11 +36,11 @@ import java.util.stream.Stream;
@Log4j2 @Log4j2
@Component @Component
public class XtaIncomingFilesMapper { class XtaIncomingFilesMapper {
public static final String ZIP_CONTENT_TYPE = "application/zip"; public static final String ZIP_CONTENT_TYPE = "application/zip";
static final Predicate<IncomingFile> IS_ZIP_FILE = contentType -> ZIP_CONTENT_TYPE.equals(contentType.getContentType()); 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)) { if (Objects.nonNull(messageFiles)) {
return messageFiles.stream() return messageFiles.stream()
.map(this::toIncomingFile) .map(this::toIncomingFile)
......
...@@ -39,7 +39,7 @@ class XdomeaMessageDataMapperTest { ...@@ -39,7 +39,7 @@ class XdomeaMessageDataMapperTest {
@DisplayName("should throw if not found") @DisplayName("should throw if not found")
@Test @Test
void shouldThrowIfNotFound() { 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)) assertThatThrownBy(() -> fileClassifier.findXdomeaXMLFile(incomingFilesWithout))
.isInstanceOf(TechnicalException.class); .isInstanceOf(TechnicalException.class);
...@@ -48,9 +48,9 @@ class XdomeaMessageDataMapperTest { ...@@ -48,9 +48,9 @@ class XdomeaMessageDataMapperTest {
@DisplayName("should return if found") @DisplayName("should return if found")
@Test @Test
void shouldReturnIfFound() { void shouldReturnIfFound() {
var targetIncomingFile = IncomingFileTestFactory.createWithName(FILE_NAME_WITH_SUFFIX); var targetIncomingFile = IncomingFileTestFactory.createBuilder().name(FILE_NAME_WITH_SUFFIX).build();
var incomingFilesWith = List.of( var incomingFilesWith = List.of(
IncomingFileTestFactory.createWithName(FILE_NAME_WITHOUT_SUFFIX), IncomingFileTestFactory.createBuilder().name(FILE_NAME_WITHOUT_SUFFIX).build(),
targetIncomingFile targetIncomingFile
); );
...@@ -114,7 +114,7 @@ class XdomeaMessageDataMapperTest { ...@@ -114,7 +114,7 @@ class XdomeaMessageDataMapperTest {
ATTATCHMENT_PNG_NAME, ATTATCHMENT_PNG_NAME,
ATTATCHMENT_PDF_NAME ATTATCHMENT_PDF_NAME
) )
.map(IncomingFileTestFactory::createWithName) .map(name -> IncomingFileTestFactory.createBuilder().name(name).build())
.toList(); .toList();
doReturn(xdomeaXMLFile).when(fileClassifier).findXdomeaXMLFile(incomingFileList); 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