### Question:
IpMetadataFilter extends MetadataFilter { @Override protected void apply(final HttpServletRequest request) { final String ip = request.getRemoteAddr(); if (!InetAddresses.isInetAddress(ip)) { return; } final IpAddress address = ipLookup.lookup(ip); if (address != null) { request.setAttribute(RequestMetadata.IP_METADATA, new IpMetadata(true)); } } IpMetadataFilter(final IpLookup ipLookup);   }### Answer:
@Test void shouldSetIpMetadataOnRequest() { when(ipLookup.lookup(IP)) .thenReturn(IpAddress.fromIp(IP)); request.setRemoteAddr(IP); filter.apply(request); assertEquals(new IpMetadata(true), request.getAttribute(RequestMetadata.IP_METADATA)); }
@Test void shouldNotSetIpMetadataOnRequest() { when(ipLookup.lookup(IP)) .thenReturn(null); request.setRemoteAddr(IP); filter.apply(request); assertNull(request.getAttribute(RequestMetadata.IP_METADATA)); }
@Test void shouldSkipIpLookupForInvalidIp() { request.setRemoteAddr(INVALID_IP); filter.apply(request); assertNull(request.getAttribute(RequestMetadata.IP_METADATA)); verifyZeroInteractions(ipLookup); }