Skip to content

Commit

Permalink
Provide unmarshalling from byte-array
Browse files Browse the repository at this point in the history
  • Loading branch information
runeflobakk committed Feb 14, 2023
1 parent 9550d1d commit d672386
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -173,13 +173,13 @@ private static JAXBContext initContext(Collection<Class<?>> classes) {
private final JAXBContext jaxbContext;
private final Optional<Schema> schema;

JaxbMarshaller(Set<Class<?>> classes, Set<String> schemaResources) {
public JaxbMarshaller(Set<Class<?>> classes, Set<String> schemaResources) {
this.jaxbContext = initContext(classes);
this.schema = Optional.ofNullable(schemaResources).filter(s -> !s.isEmpty()).map(JaxbMarshaller::createSchema);
}

JaxbMarshaller(Set<Class<?>> classes) {
this(classes, null);
public JaxbMarshaller(Set<Class<?>> classes) {
this(classes, null);
}

public void marshal(Object object, OutputStream outputStream){
Expand All @@ -202,4 +202,8 @@ public <T> T unmarshal(InputStream inputStream, Class<T> type){
}
}

public <T> T unmarshal(byte[] bytes, Class<T> type) {
return unmarshal(new ByteArrayInputStream(bytes), type);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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"))));
}
Expand All @@ -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"))));
}
Expand Down Expand Up @@ -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"))));
}
Expand Down

0 comments on commit d672386

Please sign in to comment.