JsonUtil { static DatanodeInfo toDatanodeInfo(final Map<?, ?> m) throws IOException { if (m == null) { return null; } Object tmpValue = m.get("ipAddr"); String ipAddr = (tmpValue == null) ? null : (String)tmpValue; tmpValue = m.get("xferPort"); int xferPort = (tmpValue == null) ? -1 : (int)(long)(Long)tmpValue; if (ipAddr == null) { tmpValue = m.get("name"); if (tmpValue != null) { String name = (String)tmpValue; int colonIdx = name.indexOf(':'); if (colonIdx > 0) { ipAddr = name.substring(0, colonIdx); xferPort = Integer.parseInt(name.substring(colonIdx +1)); } else { throw new IOException( "Invalid value in server response: name=[" + name + "]"); } } else { throw new IOException( "Missing both 'ipAddr' and 'name' in server response."); } } if (xferPort == -1) { throw new IOException( "Invalid or missing 'xferPort' in server response."); } return new DatanodeInfo( ipAddr, (String)m.get("hostName"), (String)m.get("storageID"), xferPort, (int)(long)(Long)m.get("infoPort"), getInt(m, "infoSecurePort", 0), (int)(long)(Long)m.get("ipcPort"), getLong(m, "capacity", 0l), getLong(m, "dfsUsed", 0l), getLong(m, "remaining", 0l), getLong(m, "blockPoolUsed", 0l), getLong(m, "cacheCapacity", 0l), getLong(m, "cacheUsed", 0l), getLong(m, "lastUpdate", 0l), getInt(m, "xceiverCount", 0), getString(m, "networkLocation", ""), AdminStates.valueOf(getString(m, "adminState", "NORMAL"))); }  static String toJsonString(final Token<? extends TokenIdentifier> token
      ); static Token<? extends TokenIdentifier> toToken(
      final Map<?, ?> m); @SuppressWarnings("unchecked") static Token<DelegationTokenIdentifier> toDelegationToken(
      final Map<?, ?> json); static String toJsonString(final Token<? extends TokenIdentifier>[] tokens
      ); static List<Token<?>> toTokenList(final Map<?, ?> json); static String toJsonString(final Exception e); static RemoteException toRemoteException(final Map<?, ?> json); static String toJsonString(final String key, final Object value); static String toJsonString(final HdfsFileStatus status,
      boolean includeType); static HdfsFileStatus toFileStatus(final Map<?, ?> json, boolean includesType); static String toJsonString(final LocatedBlocks locatedblocks
      ); static LocatedBlocks toLocatedBlocks(final Map<?, ?> json
      ); static String toJsonString(final ContentSummary contentsummary); static ContentSummary toContentSummary(final Map<?, ?> json); static String toJsonString(final MD5MD5CRC32FileChecksum checksum); static MD5MD5CRC32FileChecksum toMD5MD5CRC32FileChecksum(
      final Map<?, ?> json); static String toJsonString(final AclStatus status); static AclStatus toAclStatus(final Map<?, ?> json); static String toJsonString(final List<XAttr> xAttrs, 
      final XAttrCodec encoding); static String toJsonString(final List<XAttr> xAttrs); static byte[] getXAttr(final Map<?, ?> json, final String name); static Map<String, byte[]> toXAttrs(final Map<?, ?> json); static List<String> toXAttrNames(final Map<?, ?> json);  }
@Test public void testToDatanodeInfoWithoutSecurePort() throws Exception { Map<String, Object> response = new HashMap<String, Object>(); response.put("ipAddr", "127.0.0.1"); response.put("hostName", "localhost"); response.put("storageID", "fake-id"); response.put("xferPort", 1337l); response.put("infoPort", 1338l); response.put("ipcPort", 1339l); response.put("capacity", 1024l); response.put("dfsUsed", 512l); response.put("remaining", 512l); response.put("blockPoolUsed", 512l); response.put("lastUpdate", 0l); response.put("xceiverCount", 4096l); response.put("networkLocation", "foo.bar.baz"); response.put("adminState", "NORMAL"); response.put("cacheCapacity", 123l); response.put("cacheUsed", 321l); JsonUtil.toDatanodeInfo(response); }