### Question:
SearchFilterFactory { public SearchFilter getFilter(String filter) throws EntitlementException { Object[] parts = parseFilter(filter); String name = (String) parts[0]; SearchFilter.Operator operator = (SearchFilter.Operator) parts[1]; String value = (String) parts[2]; if (isAttributeNumeric(name)) { try { return new SearchFilter(getSearchAttribute(name), Long.parseLong(value), operator); } catch (NumberFormatException e) { throw new EntitlementException( INVALID_SEARCH_FILTER, new Object[]{"Invalid value for attribute", name, value}); } } else { return new SearchFilter(getSearchAttribute(name), value); } } SearchFilterFactory(); SearchFilter getFilter(String filter); static SearchAttribute getSearchAttribute(String attributeName); static final int INVALID_SEARCH_FILTER; }### Answer:
@Test public void shouldParseEquals() throws EntitlementException { String filter = "badger=mammal"; SearchFilter result = factory.getFilter(filter); assertThat(result.getOperator()).isEqualTo(SearchFilter.Operator.EQUALS_OPERATOR); }
@Test public void shouldParseNumericOperator() throws EntitlementException { String filter = Privilege.LAST_MODIFIED_DATE_ATTRIBUTE + ">1235"; SearchFilter result = factory.getFilter(filter); assertThat(result.getOperator()).isEqualTo(SearchFilter.Operator.GREATER_THAN_OPERATOR); }
@Test public void shouldThrowExceptionIfNoOperatorProvided() { String filter = "badgerbadgerbadgermushroom"; try { factory.getFilter(filter); fail(); } catch (EntitlementException e) { assertThat(e.getErrorCode()).isEqualTo(EntitlementException.INVALID_SEARCH_FILTER); } }
@Test public void shouldCatchInvalidNumericFormat() { String filter = Privilege.LAST_MODIFIED_DATE_ATTRIBUTE + ">ferret"; try { factory.getFilter(filter); fail(); } catch (EntitlementException e) { assertThat(e.getErrorCode()).isEqualTo(EntitlementException.INVALID_SEARCH_FILTER); } }
@Test public void shouldConsiderWhiteSpaceOK() throws EntitlementException { String filter = "badger = another badger"; SearchFilter result = factory.getFilter(filter); assertThat(result.getName()).isEqualTo("badger"); }