### Question:
MapValueFunction extends DefaultScalarFunction { @Override public Object evaluate(Row row, Object[] parameters, SelectItem operandItem) { if (parameters.length == 0) { throw new IllegalArgumentException("Expecting path parameter to MAP_VALUE function"); } final Object value = row.getValue(operandItem); return CollectionUtils.find(value, (String) parameters[0]); }  @Override Object evaluate(Row row, Object[] parameters, SelectItem operandItem); @Override ColumnType getExpectedColumnType(ColumnType type); @Override String getFunctionName();  }### Answer:
@Test public void testGetValueFromMap() throws Exception { final Map<String, Map<String, Object>> value = new HashMap<>(); final Map<String, Object> innerMap = new HashMap<>(); innerMap.put("bar", "baz"); value.put("foo", innerMap); final SelectItem operandItem = new SelectItem("foo", "f"); final Row row = new DefaultRow(new SimpleDataSetHeader(new SelectItem[] { operandItem }), new Object[] { value }); final Object v1 = function.evaluate(row, new Object[] { "foo.bar" }, operandItem); assertEquals("baz", v1.toString()); }
@Test public void testGetValueFromList() throws Exception { final List<Map<String, Object>> value = new ArrayList<>(); final Map<String, Object> innerMap = new HashMap<>(); innerMap.put("bar", "baz"); value.add(innerMap); final SelectItem operandItem = new SelectItem("foo", "f"); final Row row = new DefaultRow(new SimpleDataSetHeader(new SelectItem[] { operandItem }), new Object[] { value }); final Object v1 = function.evaluate(row, new Object[] { "[0]bar" }, operandItem); assertEquals("baz", v1.toString()); }
@Test public void testGetValueFromArray() throws Exception { final Map<String, Object> innerMap = new HashMap<>(); innerMap.put("bar", "baz"); final Object[] value = {innerMap}; final SelectItem operandItem = new SelectItem("foo", "f"); final Row row = new DefaultRow(new SimpleDataSetHeader(new SelectItem[] { operandItem }), new Object[] { value }); final Object v1 = function.evaluate(row, new Object[] { "[0]bar" }, operandItem); assertEquals("baz", v1.toString()); }
@Test public void testNotAMap() throws Exception { final SelectItem operandItem = new SelectItem("foo", "f"); final Row row = new DefaultRow(new SimpleDataSetHeader(new SelectItem[] { operandItem }), new Object[] { "not a map" }); final Object v1 = function.evaluate(row, new Object[] { "foo.bar" }, operandItem); assertEquals(null, v1); }