### Question:
FileAttributesFilter extends GenericHeapObject implements Filter { @Override public Promise<Response, NeverThrowsException> filter(final Context context, final Request request, final Handler next) { final Bindings bindings = bindings(context, request); target.set(bindings, new LazyMap<>(new Factory<Map<String, String>>() { @Override public Map<String, String> newInstance() { try { String eval = value.eval(bindings); Map<String, String> record = file.getRecord(key, eval); if (record == null) { logger.debug(format("Couldn't select a row where column %s value is equal to %s", key, eval)); return Collections.emptyMap(); } else { return record; } } catch (IOException ioe) { logger.warning(ioe); return Collections.emptyMap(); } } })); return next.handle(context, request); } FileAttributesFilter(final SeparatedValuesFile file,
                                final String key,
                                final Expression<String> value,
                                @SuppressWarnings("rawtypes") final Expression<Map> target); @Override Promise<Response, NeverThrowsException> filter(final Context context,
                                                          final Request request,
                                                          final Handler next);  }### Answer:
@Test @SuppressWarnings("unchecked") public void shouldProduceEmptyMapInCaseOfFailure() throws Exception { when(file.getRecord("username", "joe")).thenThrow(new IOException()); Expression<String> value = Expression.valueOf("joe", String.class); Expression<Map> target = Expression.valueOf("${attributes.result}", Map.class); FileAttributesFilter filter = new FileAttributesFilter(file, "username", value, target); AttributesContext context = new AttributesContext(new RootContext()); filter.filter(context, null, new ResponseHandler(Status.OK)).get(); assertThat((Map) context.getAttributes().get("result")).isEmpty(); }
@Test @SuppressWarnings("unchecked") public void shouldProduceEmptyMapWhenSearchedEntryDoesNotExist() throws Exception { when(file.getRecord("username", "joe")).thenReturn(null); Expression<String> value = Expression.valueOf("joe", String.class); Expression<Map> target = Expression.valueOf("${attributes.result}", Map.class); FileAttributesFilter filter = new FileAttributesFilter(file, "username", value, target); filter.setLogger(logger); AttributesContext context = new AttributesContext(new RootContext()); filter.filter(context, null, new ResponseHandler(Status.OK)).get(); assertThat((Map) context.getAttributes().get("result")).isEmpty(); verify(logger).debug(anyString()); }