Message unmarshallMessage(Node node) throws RepositoryException, Node2BeanException { Map<String, Object> map = Node2MapUtil.node2map(node); long timestamp = ((Long) map.get(AdmincentralNodeTypes.SystemMessage.TIMESTAMP)).longValue(); final Message message = new Message(timestamp); message.setId(node.getName()); message.setSender(node.getProperty(AdmincentralNodeTypes.SystemMessage.SENDER).getString()); message.setMessage(node.getProperty(AdmincentralNodeTypes.SystemMessage.MESSAGE).getString()); message.setSubject(node.getProperty(AdmincentralNodeTypes.SystemMessage.SUBJECT).getString()); message.setType(MessageType.valueOf(node.getProperty(AdmincentralNodeTypes.SystemMessage.MESSAGETYPE).getString())); message.setCleared(node.getProperty(AdmincentralNodeTypes.SystemMessage.CLEARED).getBoolean()); if (node.hasProperty(AdmincentralNodeTypes.SystemMessage.VIEW)) { message.setView(node.getProperty(AdmincentralNodeTypes.SystemMessage.VIEW).getString()); } // remove all attributes that are already explicitly treated - see above. map.remove(AdmincentralNodeTypes.SystemMessage.TIMESTAMP); map.remove(AdmincentralNodeTypes.SystemMessage.SENDER); map.remove(AdmincentralNodeTypes.SystemMessage.SUBJECT); map.remove(AdmincentralNodeTypes.SystemMessage.MESSAGETYPE); map.remove(AdmincentralNodeTypes.SystemMessage.VIEW); map.remove(AdmincentralNodeTypes.SystemMessage.CLEARED); final Iterator<String> propertyNames = map.keySet().iterator(); while (propertyNames.hasNext()) { final String propertyName = propertyNames.next(); message.addProperty(propertyName, map.get(propertyName)); } return message; }
@Test public void testExceptionOnNonPrimitive() throws Exception { // GIVEN map.put("failureExpected", new StringBuilder()); exception.expect(IllegalArgumentException.class); // WHEN node = Node2MapUtil.map2node(node, map); // THEN fail("No IllegalArgumentException thrown."); }
@Test public void testStorePrimitives() throws Exception { // GIVEN map.put(STRING, STRING_VALUE); map.put(LONG, LONG_VALUE); map.put(DOUBLE, DOUBLE_VALUE); map.put(BOOLEAN, BOOLEAN_VALUE); map.put(DATE, DATE_VALUE); // WHEN node = Node2MapUtil.map2node(node, map); // THEN assertTrue(node.hasProperty(STRING)); assertTrue(node.hasProperty(LONG)); assertTrue(node.hasProperty(DOUBLE)); assertTrue(node.hasProperty(BOOLEAN)); assertTrue(node.hasProperty(DATE)); assertEquals(STRING_VALUE, node.getProperty(STRING).getString()); assertEquals(LONG_VALUE, node.getProperty(LONG).getLong()); assertEquals(DOUBLE_VALUE, node.getProperty(DOUBLE).getDouble(), 0.0001); assertEquals(BOOLEAN_VALUE, node.getProperty(BOOLEAN).getBoolean()); assertEquals(DATE_VALUE, node.getProperty(DATE).getDate()); }
@Test public void testStoreSubnode() throws Exception { // GIVEN Map<String, Object> nested = new HashMap<String, Object>(); nested.put(STRING, STRING_VALUE); nested.put(LONG, LONG_VALUE); nested.put(DOUBLE, DOUBLE_VALUE); nested.put(BOOLEAN, BOOLEAN_VALUE); nested.put(DATE, DATE_VALUE); map.put(MAP, nested); // WHEN node = Node2MapUtil.map2node(node, map); // THEN assertTrue(node.hasNode(MAP)); Node nestedNode = node.getNode(MAP); assertTrue(nestedNode.hasProperty(STRING)); assertTrue(nestedNode.hasProperty(LONG)); assertTrue(nestedNode.hasProperty(DOUBLE)); assertTrue(nestedNode.hasProperty(BOOLEAN)); assertTrue(nestedNode.hasProperty(DATE)); assertEquals(STRING_VALUE, nestedNode.getProperty(STRING).getString()); assertEquals(LONG_VALUE, nestedNode.getProperty(LONG).getLong()); assertEquals(DOUBLE_VALUE, nestedNode.getProperty(DOUBLE).getDouble(), 0.0001); assertEquals(BOOLEAN_VALUE, nestedNode.getProperty(BOOLEAN).getBoolean()); assertEquals(DATE_VALUE, nestedNode.getProperty(DATE).getDate()); }
@Test public void testReadPrimitives() throws Exception { // GIVEN node.setProperty(STRING, STRING_VALUE); node.setProperty(LONG, LONG_VALUE); node.setProperty(DOUBLE, DOUBLE_VALUE); node.setProperty(BOOLEAN, BOOLEAN_VALUE); node.setProperty(DATE, DATE_VALUE); // WHEN map = Node2MapUtil.node2map(node); // THEN assertEquals(STRING_VALUE, (String) map.get(STRING)); long longValue = ((Long) map.get(LONG)).longValue(); assertEquals(LONG_VALUE, longValue); double doubleValue = ((Double) map.get(DOUBLE)).doubleValue(); assertEquals(DOUBLE_VALUE, doubleValue, 0.0001); boolean boolValue = ((Boolean) map.get(BOOLEAN)).booleanValue(); assertEquals(BOOLEAN_VALUE, boolValue); Calendar cal = Calendar.getInstance(); cal.setTime((Date) map.get(DATE)); assertEquals(DATE_VALUE, cal); }
@Test public void testReadSubnodes() throws Exception { // GIVEN Node nested = node.addNode(MAP); nested.setProperty(STRING, STRING_VALUE); nested.setProperty(LONG, LONG_VALUE); nested.setProperty(DOUBLE, DOUBLE_VALUE); nested.setProperty(BOOLEAN, BOOLEAN_VALUE); nested.setProperty(DATE, DATE_VALUE); // WHEN map = Node2MapUtil.node2map(node); // THEN map = (Map<String, Object>) map.get(MAP); assertNotNull(map); // check the values in the nested map assertEquals(STRING_VALUE, (String) map.get(STRING)); long longValue = ((Long) map.get(LONG)).longValue(); assertEquals(LONG_VALUE, longValue); double doubleValue = ((Double) map.get(DOUBLE)).doubleValue(); assertEquals(DOUBLE_VALUE, doubleValue, 0.0001); boolean boolValue = ((Boolean) map.get(BOOLEAN)).booleanValue(); assertEquals(BOOLEAN_VALUE, boolValue); Calendar cal = Calendar.getInstance(); cal.setTime((Date) map.get(DATE)); assertEquals(DATE_VALUE, cal); }