### Question:
LongValueMapper extends ValueMapper { @Override public Writable getValue(BSONWritable entity) { Writable rval = NullWritable.get(); String value = null; try { value = BSONUtilities.getValue(entity, fieldName); if (value != null) { rval = new LongWritable(Long.parseLong(value.toString())); } } catch (NumberFormatException e) { log.severe(String.format("Failed to convert value {'%s'} to Long", value)); } return rval; } LongValueMapper(String fieldName); @Override Writable getValue(BSONWritable entity);  }### Answer:
@Test public void testGetValue() { BSONObject field = new BasicBSONObject("field", 123L); BSONObject entry = new BasicBSONObject("long", field); BSONWritable entity = new BSONWritable(entry); LongValueMapper mapper = new LongValueMapper("long.field"); Writable value = mapper.getValue(entity); assertFalse(value instanceof NullWritable); assertTrue(value instanceof LongWritable); assertEquals(((LongWritable) value).get(), 123L); }
@Test public void testValueNotFound() { BSONObject field = new BasicBSONObject("field", 123L); BSONObject entry = new BasicBSONObject("long", field); BSONWritable entity = new BSONWritable(entry); LongValueMapper mapper = new LongValueMapper("long.missing_field"); Writable value = mapper.getValue(entity); assertTrue(value instanceof NullWritable); }
@Test public void testValueNotLong() { BSONObject field = new BasicBSONObject("field", true); BSONObject entry = new BasicBSONObject("long", field); BSONWritable entity = new BSONWritable(entry); LongValueMapper mapper = new LongValueMapper("long.field"); Writable value = mapper.getValue(entity); assertTrue(value instanceof NullWritable); }