HttpMessageUtils { public static void copy(Message from, HttpMessage to) { HttpMessage source; if (from instanceof HttpMessage) { source = (HttpMessage) from; } else { source = new HttpMessage(from); } copy(source, to); } private  HttpMessageUtils(); static void copy(Message from, HttpMessage to); static void copy(HttpMessage from, HttpMessage to);  }
@Test public void testCopy() { HttpMessage from = new HttpMessage("fooMessage") .header("X-Foo", "foo") .addHeaderData("HeaderData"); from.setName("FooMessage"); HttpMessage to = new HttpMessage(); HttpMessageUtils.copy(from, to); Assert.assertNotEquals(from.getId(), to.getId()); Assert.assertEquals(to.getName(), "FooMessage"); Assert.assertEquals(to.getPayload(String.class), "fooMessage"); Assert.assertEquals(to.getHeaders().size(), 4L); Assert.assertNotNull(to.getHeader(MessageHeaders.ID)); Assert.assertNotNull(to.getHeader(MessageHeaders.MESSAGE_TYPE)); Assert.assertNotNull(to.getHeader(MessageHeaders.TIMESTAMP)); Assert.assertEquals(to.getHeader("X-Foo"), "foo"); Assert.assertEquals(to.getHeaderData().size(), 1L); Assert.assertEquals(to.getHeaderData().get(0), "HeaderData"); }
@Test public void testCopyPreventExistingOverwritePayload() { HttpMessage from = new HttpMessage("fooMessage") .header("X-Foo", "foo") .cookie(new Cookie("Foo", "fooCookie")) .addHeaderData("HeaderData"); from.setName("FooMessage"); HttpMessage to = new HttpMessage("existingPayload") .header("X-Existing", "existing") .cookie(new Cookie("Existing", "existingCookie")) .addHeaderData("ExistingHeaderData"); to.setName("ExistingMessage"); HttpMessageUtils.copy(from, to); Assert.assertNotEquals(from.getId(), to.getId()); Assert.assertEquals(to.getName(), "FooMessage"); Assert.assertEquals(to.getPayload(String.class), "fooMessage"); Assert.assertEquals(to.getHeaders().size(), 7L); Assert.assertNotNull(to.getHeader(MessageHeaders.ID)); Assert.assertNotNull(to.getHeader(MessageHeaders.MESSAGE_TYPE)); Assert.assertNotNull(to.getHeader(MessageHeaders.TIMESTAMP)); Assert.assertEquals(to.getHeader("X-Foo"), "foo"); Assert.assertEquals(to.getHeader("X-Existing"), "existing"); Assert.assertEquals(to.getHeader(HttpMessageHeaders.HTTP_COOKIE_PREFIX + "Foo"), "Foo=fooCookie"); Assert.assertEquals(to.getHeader(HttpMessageHeaders.HTTP_COOKIE_PREFIX + "Existing"), "Existing=existingCookie"); Assert.assertEquals(to.getHeaderData().size(), 2L); Assert.assertEquals(to.getHeaderData().get(0), "ExistingHeaderData"); Assert.assertEquals(to.getHeaderData().get(1), "HeaderData"); }
@Test public void testConvertAndCopy() { Message from = new DefaultMessage("fooMessage") .setHeader("X-Foo", "foo") .addHeaderData("HeaderData"); from.setName("FooMessage"); HttpMessage to = new HttpMessage(); HttpMessageUtils.copy(from, to); Assert.assertNotEquals(from.getId(), to.getId()); Assert.assertEquals(to.getName(), "FooMessage"); Assert.assertEquals(to.getPayload(String.class), "fooMessage"); Assert.assertEquals(to.getHeader("X-Foo"), "foo"); Assert.assertEquals(to.getHeaderData().size(), 1L); Assert.assertEquals(to.getHeaderData().get(0), "HeaderData"); }