### Question:
DefaultAlertsPoliciesApi extends ApiBase implements AlertsPoliciesApi { @Override public Optional<AlertsPolicy> getByName(String alertsPolicyName) { Invocation.Builder builder = client .target(POLICIES_URL) .queryParam("filter[name]", alertsPolicyName) .request(APPLICATION_JSON_TYPE); return getPageable(builder, AlertsPolicyList.class) .filter(alertsPolicy -> alertsPolicy.getName().equals(alertsPolicyName)) .getSingle(); } DefaultAlertsPoliciesApi(NewRelicClient client); @Override Optional<AlertsPolicy> getByName(String alertsPolicyName); @Override AlertsPolicy create(AlertsPolicy policy); @Override AlertsPolicy delete(int policyId); @Override AlertsPolicyChannels updateChannels(AlertsPolicyChannels channels);  }### Answer:
@Test public void getByName_shouldReturnPolicy_whenClientReturnsNotUniqueResult() throws Exception { when(responseMock.readEntity(AlertsPolicyList.class)).thenReturn(new AlertsPolicyList(asList( AlertsPolicy.builder().name("policy").build(), AlertsPolicy.builder().name("policy1").build() ))); Optional<AlertsPolicy> policyOptional = testee.getByName("policy"); assertThat(policyOptional).isNotEmpty(); }
@Test public void getByName_shouldNotReturnPolicy_whenClientReturnsNotMatchingResult() throws Exception { when(responseMock.readEntity(AlertsPolicyList.class)).thenReturn(new AlertsPolicyList(Collections.singletonList( AlertsPolicy.builder().name("policy1").build() ))); Optional<AlertsPolicy> policyOptional = testee.getByName("policy"); assertThat(policyOptional).isEmpty(); }
@Test public void getByName_shouldNotReturnPolicy_whenClientReturnsEmptyList() throws Exception { when(responseMock.readEntity(AlertsPolicyList.class)).thenReturn(new AlertsPolicyList(Collections.emptyList())); Optional<AlertsPolicy> policyOptional = testee.getByName("policy"); assertThat(policyOptional).isEmpty(); }