From d6723869b4591620973c8a807121bde7d06ffe2b Mon Sep 17 00:00:00 2001 From: Rune Flobakk Date: Tue, 14 Feb 2023 16:20:41 +0100 Subject: [PATCH] Provide unmarshalling from byte-array --- .../no/digipost/signature/jaxb/JaxbMarshaller.java | 12 ++++++++---- .../signature/jaxb/spring/JaxbMarshallerTest.java | 9 ++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/jaxb/src/main/java/no/digipost/signature/jaxb/JaxbMarshaller.java b/jaxb/src/main/java/no/digipost/signature/jaxb/JaxbMarshaller.java index f0b4c64..95f2010 100644 --- a/jaxb/src/main/java/no/digipost/signature/jaxb/JaxbMarshaller.java +++ b/jaxb/src/main/java/no/digipost/signature/jaxb/JaxbMarshaller.java @@ -50,7 +50,7 @@ * @see JaxbMarshaller.ForResponsesOfAllApis * @see JaxbMarshaller.ForRequestsOfAllApis */ -public abstract class JaxbMarshaller { +public class JaxbMarshaller { /** * Marshaller for creating requests for both the Direct and Portal API. @@ -173,13 +173,13 @@ private static JAXBContext initContext(Collection> classes) { private final JAXBContext jaxbContext; private final Optional schema; - JaxbMarshaller(Set> classes, Set schemaResources) { + public JaxbMarshaller(Set> classes, Set schemaResources) { this.jaxbContext = initContext(classes); this.schema = Optional.ofNullable(schemaResources).filter(s -> !s.isEmpty()).map(JaxbMarshaller::createSchema); } - JaxbMarshaller(Set> classes) { - this(classes, null); + public JaxbMarshaller(Set> classes) { + this(classes, null); } public void marshal(Object object, OutputStream outputStream){ @@ -202,4 +202,8 @@ public T unmarshal(InputStream inputStream, Class type){ } } + public T unmarshal(byte[] bytes, Class type) { + return unmarshal(new ByteArrayInputStream(bytes), type); + } + } diff --git a/jaxb/src/test/java/no/digipost/signature/jaxb/spring/JaxbMarshallerTest.java b/jaxb/src/test/java/no/digipost/signature/jaxb/spring/JaxbMarshallerTest.java index 20d8650..e776832 100644 --- a/jaxb/src/test/java/no/digipost/signature/jaxb/spring/JaxbMarshallerTest.java +++ b/jaxb/src/test/java/no/digipost/signature/jaxb/spring/JaxbMarshallerTest.java @@ -37,7 +37,6 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import java.io.ByteArrayOutputStream; @@ -46,7 +45,6 @@ import java.time.ZoneId; import java.time.ZonedDateTime; -import static uk.co.probablyfine.matchers.Java8Matchers.where; import static java.time.temporal.ChronoUnit.MILLIS; import static java.util.Arrays.asList; import static no.digipost.signature.api.xml.XMLAuthenticationLevel.FOUR; @@ -61,6 +59,7 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.sameInstance; import static org.junit.jupiter.api.Assertions.assertThrows; +import static uk.co.probablyfine.matchers.Java8Matchers.where; class JaxbMarshallerTest { @@ -113,7 +112,7 @@ void invalid_manifest_causes_exceptions() { RuntimeException marshallingFailure = - assertThrows(RuntimeException.class, () -> marshaller.marshal(directManifest, new StreamResult(new ByteArrayOutputStream()).getOutputStream())); + assertThrows(RuntimeException.class, () -> marshaller.marshal(directManifest, new ByteArrayOutputStream())); assertThat(marshallingFailure, where(Exception::getMessage, allOf(containsString("href"), containsString("must appear")))); } @@ -128,7 +127,7 @@ void invalid_signature_job_request_causes_exceptions() { XMLDirectSignatureJobRequest signatureJobRequest = new XMLDirectSignatureJobRequest("123abc", exitUrls, WAIT_FOR_CALLBACK, null); RuntimeException thrown = assertThrows(RuntimeException.class, - () -> marshaller.marshal(signatureJobRequest, new StreamResult(new ByteArrayOutputStream()).getOutputStream())); + () -> marshaller.marshal(signatureJobRequest, new ByteArrayOutputStream())); assertThat(thrown, where(Exception::getMessage, allOf(containsString("completion-url"), containsString("is expected")))); } @@ -178,7 +177,7 @@ public void invalid_manifest_causes_exceptions() { asList(portalSigner), sender, null, "Title", "nonsensitive title", "Description", asList(portalDocument) , FOUR, new XMLAvailability(), PERSONAL_IDENTIFICATION_NUMBER_AND_NAME); RuntimeException marshallingFailure = - assertThrows(RuntimeException.class, () -> marshaller.marshal(portalManifest, new StreamResult(new ByteArrayOutputStream()).getOutputStream())); + assertThrows(RuntimeException.class, () -> marshaller.marshal(portalManifest, new ByteArrayOutputStream())); assertThat(marshallingFailure, where(Exception::getMessage, allOf(containsString("signature-type"), containsString("notifications-using-lookup"), containsString("notifications")))); }