### Question:
NestedMetadata { public static ObjectNode attributesToMetadataPayload(Map<String, String> attributes) { final String namespace = attributes.get(Attribute.DOCUMENT_NAMESPACE); ObjectNode metadata = Json.createObjectNode(); metadata.set(GEO, geoFromAttributes(attributes)); metadata.set(ISP, ispFromAttributes(attributes)); metadata.set(Attribute.USER_AGENT, userAgentFromAttributes(attributes)); metadata.set(HEADER, headersFromAttributes(attributes)); if (Namespace.TELEMETRY.equals(namespace)) { metadata.set(Attribute.URI, uriFromAttributes(attributes)); } IDENTIFYING_FIELDS.forEach(name -> Optional .ofNullable(attributes.get(name)) .ifPresent(value -> metadata.put(name, value))); ObjectNode payload = Json.createObjectNode(); payload.set(FieldName.METADATA, metadata); TOP_LEVEL_STRING_FIELDS.forEach(name -> Optional .ofNullable(attributes.get(name)) .ifPresent(value -> payload.put(name, value))); TOP_LEVEL_INT_FIELDS.forEach(name -> Optional .ofNullable(attributes.get(name)) .flatMap(value -> { try { return Optional.of(Integer.parseInt(value)); } catch (NumberFormatException e) { return Optional.empty(); } }) .ifPresent(value -> payload.put(name, value))); return payload; } private  NestedMetadata(); static byte[] mergedPayload(byte[] payload, byte[] metadata); static ObjectNode attributesToMetadataPayload(Map<String, String> attributes); static void stripPayloadMetadataToAttributes(Map<String, String> attributes,
      ObjectNode payload);  }### Answer:
@Test public void testAttributesToMetadataPayload() throws Exception { Map<String, String> attributes = ImmutableMap.<String, String>builder() .put("document_namespace", "telemetry") .put("app_name", "Firefox") .put("sample_id", "18") .put("geo_country", "CA") .put("isp_name", "my isp") .put("x_debug_id", "mysession") .put("normalized_channel", "release") .put("x_forwarded_for", "??") .build(); ObjectNode payload = NestedMetadata.attributesToMetadataPayload(attributes); ObjectNode expected = mapToObjectNode(ImmutableMap.<String, Object>builder() .put("metadata", ImmutableMap.<String, Object>builder() .put("document_namespace", "telemetry") .put("uri", ImmutableMap.of("app_name", "Firefox")) .put("header", ImmutableMap.of("x_debug_id", "mysession")) .put("geo", ImmutableMap.of("country", "CA")) .put("isp", ImmutableMap.of("name", "my isp")) .put("user_agent", ImmutableMap.of()) .build()) .put("normalized_channel", "release") .put("sample_id", 18) .build()); assertEquals(expected, payload); }
@Test public void testIgnoreInvalidIntField() { JsonNode missingNode = Json.createObjectNode().get("sample_id"); JsonNode actual = NestedMetadata .attributesToMetadataPayload(ImmutableMap.of("sample_id", "1.0")).get("sample_id"); assertEquals(missingNode, actual); }