ObjectMD5 implements Function<Object, byte[]> { public byte[] apply(Object from) { checkNotNull(from, "thing to md5"); PayloadEnclosing payloadEnclosing; if (from instanceof PayloadEnclosing) { payloadEnclosing = (PayloadEnclosing) from; } else { payloadEnclosing = HttpMessage.builder().payload(Payloads.newPayload(from)).build(); } if (payloadEnclosing.getPayload().getContentMetadata().getContentMD5() == null) try { Payloads.calculateMD5(payloadEnclosing); } catch (IOException e) { Throwables.propagate(e); } return payloadEnclosing.getPayload().getContentMetadata().getContentMD5(); }  byte[] apply(Object from);  }
@Test public void testAlreadyHasMD5() { Payload payload = Payloads.newPayload("foo"); payload.getContentMetadata().setContentMD5(new byte[] {}); HttpMessage payloadEnclosing = HttpMessage.builder().payload(payload).build(); assertEquals(fn.apply(payloadEnclosing), new byte[] {}); }
@Test public void testMD5PayloadEnclosing() throws IOException { Payload payload = Payloads.newPayload("foo"); HttpMessage payloadEnclosing = HttpMessage.builder().payload(payload).build(); assertEquals(fn.apply(payloadEnclosing), md5().hashString("foo", UTF_8).asBytes()); }
@Test public void testMD5String() throws IOException { assertEquals(fn.apply("foo"), md5().hashString("foo", UTF_8).asBytes()); }
@Test(expectedExceptions = { NullPointerException.class, IllegalStateException.class }) public void testNullIsBad() { fn.apply(null); }