### Question:
IDMapper extends Mapper<T, BSONWritable, T, BSONWritable> { @Override public void map(T id, BSONWritable entity, Context context) throws IOException, InterruptedException { for (String field : idFields.values()) { BSONUtilities.removeField(entity, field); } context.write(id, entity); }  @Override void map(T id, BSONWritable entity, Context context);  }### Answer:
@SuppressWarnings("unchecked") @Test public void testMapIdFieldKey() throws Exception { String[] fields = { "data.element.id" }; BSONObject elem = new BasicBSONObject("value", 7631); BSONObject data = new BasicBSONObject("element", elem); BSONObject entry = new BasicBSONObject("data", data); final BSONWritable entity = new BSONWritable(entry); IDMapper<IdFieldEmittableKey> mapper = new IDMapper<IdFieldEmittableKey>(); IDMapper<IdFieldEmittableKey>.Context context = Mockito.mock(IDMapper.Context.class); PowerMockito.when(context, "write", Matchers.any(EmittableKey.class), Matchers.any(BSONObject.class)).thenAnswer(new Answer<BSONObject>() { @Override public BSONObject answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); assertNotNull(args); assertEquals(args.length, 2); assertTrue(args[0] instanceof IdFieldEmittableKey); assertTrue(args[1] instanceof BSONWritable); IdFieldEmittableKey id = (IdFieldEmittableKey) args[0]; assertEquals(id.getIdField().toString(), "data.element.id"); Text idValue = id.getId(); assertEquals(Long.parseLong(idValue.toString()), 3697); BSONWritable e = (BSONWritable) args[1]; assertEquals(e, entity); return null; } }); IdFieldEmittableKey id = new IdFieldEmittableKey(); id.setFieldNames(fields); id.setId(new Text("3697")); mapper.map(id, entity, context); }
@SuppressWarnings("unchecked") @Test public void testMapTenantAndIdKey() throws Exception { String[] fields = { "metaData.tenantId", "data.element.id" }; BSONObject elem = new BasicBSONObject("value", 7632); BSONObject data = new BasicBSONObject("element", elem); BSONObject entry = new BasicBSONObject("data", data); final BSONWritable entity = new BSONWritable(entry); BSONObject tenantId = new BasicBSONObject("EdOrgs", "Midtown"); entity.put("metaData", tenantId); IDMapper<TenantAndIdEmittableKey> mapper = new IDMapper<TenantAndIdEmittableKey>(); IDMapper<TenantAndIdEmittableKey>.Context context = Mockito.mock(IDMapper.Context.class); PowerMockito.when(context, "write", Matchers.any(EmittableKey.class), Matchers.any(BSONObject.class)).thenAnswer(new Answer<BSONObject>() { @Override public BSONObject answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); assertNotNull(args); assertEquals(args.length, 2); assertTrue(args[0] instanceof TenantAndIdEmittableKey); assertTrue(args[1] instanceof BSONObject); TenantAndIdEmittableKey id = (TenantAndIdEmittableKey) args[0]; assertEquals(id.getIdField().toString(), "data.element.id"); Text idValue = id.getId(); assertEquals(Long.parseLong(idValue.toString()), 90210); assertEquals(id.getTenantIdField().toString(), "metaData.tenantId"); idValue = id.getTenantId(); assertEquals(idValue.toString(), "Midgar"); BSONObject e = (BSONObject) args[1]; assertEquals(e, entity); return null; } }); TenantAndIdEmittableKey id = new TenantAndIdEmittableKey(); id.setFieldNames(fields); id.setTenantId(new Text("Midgar")); id.setId(new Text("90210")); mapper.map(id, entity, context); }