ApiDeserializer { public <T extends BasicMessage> BasicMessageWithExtraData<T> deserialize(String nameAndJson) { String[] nameAndJsonArray = fromHawkularFormat(nameAndJson); String name = nameAndJsonArray[0]; String json = nameAndJsonArray[1]; if (name.indexOf(".") == -1) { name = String.format("%s.%s", API_PKG, name); } try { Class<T> pojo = (Class<T>) Class.forName(name); return new BasicMessageWithExtraData(AbstractMessage.fromJSON(json, pojo), null); } catch (Exception e) { throw new RuntimeException("Cannot deserialize: [" + nameAndJson + "]", e); } } ApiDeserializer(); static String toHawkularFormat(BasicMessage msg); static BinaryData toHawkularFormat(BasicMessage msg, InputStream extraData); BasicMessageWithExtraData<T> deserialize(String nameAndJson); BasicMessageWithExtraData<T> deserialize(InputStream input);  }
@Test public void testAuthMessage() { ApiDeserializer ad = new ApiDeserializer(); String nameAndJson = EchoRequest.class.getName() + "={\"echoMessage\":\"msg\", \"authentication\":{\"username\":\"foo\", \"password\":\"bar\"}}"; BasicMessage request = ad.deserialize(nameAndJson).getBasicMessage(); Assert.assertTrue(request instanceof EchoRequest); EchoRequest echoRequest = (EchoRequest) request; Assert.assertEquals("msg", echoRequest.getEchoMessage()); Assert.assertNotNull(echoRequest.getAuthentication()); Assert.assertEquals("foo", echoRequest.getAuthentication().getUsername()); Assert.assertEquals("bar", echoRequest.getAuthentication().getPassword()); Assert.assertNull(echoRequest.getAuthentication().getToken()); Assert.assertNull(echoRequest.getAuthentication().getPersona()); nameAndJson = EchoRequest.class.getName() + "={\"echoMessage\":\"msg\", \"authentication\":{\"token\":\"tok\", \"persona\":\"polly\"}}"; request = ad.deserialize(nameAndJson).getBasicMessage(); Assert.assertTrue(request instanceof EchoRequest); echoRequest = (EchoRequest) request; Assert.assertEquals("msg", echoRequest.getEchoMessage()); Assert.assertNotNull(echoRequest.getAuthentication()); Assert.assertNull(echoRequest.getAuthentication().getUsername()); Assert.assertNull(echoRequest.getAuthentication().getPassword()); Assert.assertEquals("tok", echoRequest.getAuthentication().getToken()); Assert.assertEquals("polly", echoRequest.getAuthentication().getPersona()); }
@Test public void testApiDeserializerError() { ApiDeserializer ad = new ApiDeserializer(); String nameAndJson = EchoRequest.class.getName() + "={\"boo\":\"msg\"}"; try { ad.deserialize(nameAndJson); Assert.fail("Should not have deserialized, RuntimeException expected."); } catch (RuntimeException expected) { Throwable root = expected; while (root.getCause() != null) { root = root.getCause(); } Assert.assertTrue("Expected an UnrecognizedPropertyException", root.getClass().getSimpleName().equals("UnrecognizedPropertyException")); final String expectedPrefix = "Unrecognized field \"boo\""; Assert.assertTrue("[" + root.getMessage() + "] should start with [" + expectedPrefix + "]", root.getMessage().startsWith(expectedPrefix)); } }
@Test public void testReadFromInputStreamWithExtraData() throws IOException { ApiDeserializer ad = new ApiDeserializer(); String nameAndJson = EchoRequest.class.getName() + "={\"echoMessage\":\"msg\"}"; String extra = "This is some extra data"; String nameAndJsonPlusExtra = nameAndJson + extra; ByteArrayInputStream in = new UncloseableByteArrayInputStream(nameAndJsonPlusExtra.getBytes()); BasicMessageWithExtraData<AbstractMessage> map = ad.deserialize(in); AbstractMessage request = map.getBasicMessage(); Assert.assertTrue(request instanceof EchoRequest); EchoRequest echoRequest = (EchoRequest) request; Assert.assertEquals("msg", echoRequest.getEchoMessage()); BinaryData leftover = map.getBinaryData(); byte[] leftoverByteArray = new byte[leftover.available()]; leftover.read(leftoverByteArray); String totalRemaining = new String(leftoverByteArray, "UTF-8"); Assert.assertEquals(extra.length(), totalRemaining.length()); Assert.assertEquals(extra, totalRemaining); in = new UncloseableByteArrayInputStream("this is not valid data".getBytes()); try { ad.deserialize(in); Assert.fail("Should have thrown an exception - the stream had invalid data"); } catch (Exception expected) { } }
@Test public void testReadFromInputStreamWithNoExtraData() throws IOException { ApiDeserializer ad = new ApiDeserializer(); String nameAndJson = EchoRequest.class.getName() + "={\"echoMessage\":\"msg\"}"; ByteArrayInputStream in = new UncloseableByteArrayInputStream(nameAndJson.getBytes()); BasicMessageWithExtraData<AbstractMessage> map = ad.deserialize(in); AbstractMessage request = map.getBasicMessage(); Assert.assertTrue(request instanceof EchoRequest); EchoRequest echoRequest = (EchoRequest) request; Assert.assertEquals("msg", echoRequest.getEchoMessage()); BinaryData leftover = map.getBinaryData(); Assert.assertEquals(0, leftover.available()); Assert.assertEquals(0, in.available()); }