### Question:
JaxbUtil { public static void marshal(Object object, OutputStream outputStream) { try { MarshallerBuilder.create().build().marshal(object, outputStream); } catch (Throwable ex) { Optional<ConstraintViolationException> validationException = unwrapConstraintViolationException(ex); if (validationException.isPresent()) { throw validationException.get(); } throw new ConfigException(ex); } } private  JaxbUtil(); static T unmarshal(InputStream inputStream, Class<T> type); static void marshal(Object object, OutputStream outputStream); static void marshalWithNoValidation(Object object, OutputStream outputStream); static String marshalToString(Object object); static String marshalToStringNoValidation(Object object); static void marshalMasked(Config object, OutputStream outputStream); static final Class[] JAXB_CLASSES; }### Answer:
@Test public void marshallingOutputStream() throws Exception { final KeyDataConfig input = new KeyDataConfig(new PrivateKeyData("VAL", null, null, null, null), PrivateKeyType.UNLOCKED); try (ByteArrayOutputStream bout = new ByteArrayOutputStream()) { JaxbUtil.marshal(input, bout); JsonObject result = Json.createReader(new ByteArrayInputStream(bout.toByteArray())).readObject(); assertThat(result).containsOnlyKeys("type", "data"); assertThat(result.getString("type")).isEqualTo("unlocked"); JsonObject jsonDataNode = result.getJsonObject("data"); assertThat(jsonDataNode).containsOnlyKeys("bytes"); assertThat(jsonDataNode.getString("bytes")).isEqualTo("VAL"); } }
@Test public void marshallingProducesError() { final Exception ex = new Exception(); OutputStream out = mock(OutputStream.class); final Throwable throwable = catchThrowable(() -> JaxbUtil.marshal(ex, out)); assertThat(throwable).isInstanceOf(ConfigException.class).hasCauseExactlyInstanceOf(MarshalException.class); }
@Test public void marshallingFailsBeanValidation() throws Exception { final KeyDataConfig input = new KeyDataConfig(null, null); try (OutputStream bout = new ByteArrayOutputStream()) { JaxbUtil.marshal(input, bout); failBecauseExceptionWasNotThrown(ConstraintViolationException.class); } catch (ConstraintViolationException ex) { assertThat(ex).isInstanceOf(ConstraintViolationException.class); } }
@Test public void marshallingProducesNonJaxbException() { final KeyDataConfig input = new KeyDataConfig(new PrivateKeyData("VAL", null, null, null, null), PrivateKeyType.UNLOCKED); IOException exception = new IOException("What you talking about willis?"); OutputStream out = mock( OutputStream.class, (iom) -> { throw exception; }); final Throwable throwable = catchThrowable(() -> JaxbUtil.marshal(input, out)); assertThat(throwable) .isInstanceOf(ConfigException.class) .hasCauseExactlyInstanceOf(javax.xml.bind.MarshalException.class); }