### Question:
OriginSearchDelegate implements Delegate { public ResultSet<Origin> handle(SearchOriginRequest request) throws AccessDeniedException, AuthenticationFailedException, InvalidArgumentException { securityContext.checkPermission(TiFunctionConstants.viewThreatIntelOrigin); int limit = ObjectUtils.ifNull(request.getLimit(), DEFAULT_LIMIT); List<Origin> origins = originManager.fetchOrigins() .stream() .filter(filterIncludedDeleted(request)) .filter(securityContext::hasReadPermission) .map(originResponseConverter) .limit(limit == 0 ? Integer.MAX_VALUE : limit) .collect(Collectors.toList()); return StreamingResultSet.<Origin>builder() .setCount(origins.size()) .setLimit(limit) .setValues(origins) .build(); } @Inject  OriginSearchDelegate(TiSecurityContext securityContext,
                              OriginManager originManager,
                              OriginResponseConverter originResponseConverter); ResultSet<Origin> handle(SearchOriginRequest request);  }### Answer:
@Test(expected = AccessDeniedException.class) public void testSearchOriginWithoutGeneralViewPermission() throws Exception { doThrow(AccessDeniedException.class).when(securityContext).checkPermission(TiFunctionConstants.viewThreatIntelOrigin); delegate.handle(new SearchOriginRequest()); }
@Test public void testSearchOriginWithDefaultValues() throws Exception { ResultSet<Origin> result = delegate.handle(new SearchOriginRequest()); assertEquals(2, result.getCount()); assertEquals(25, result.getLimit()); assertEquals(2, ListUtils.list(result.iterator()).size()); verify(securityContext, times(2)).hasReadPermission(isA(OriginEntity.class)); verify(originResponseConverter, times(2)).apply(notNull()); }
@Test public void testSearchOriginIncludeDeleted() throws Exception { ResultSet<Origin> result = delegate.handle(new SearchOriginRequest().setIncludeDeleted(true)); assertEquals(3, result.getCount()); assertEquals(3, ListUtils.list(result.iterator()).size()); verify(securityContext, times(3)).hasReadPermission(isA(OriginEntity.class)); verify(originResponseConverter, times(3)).apply(notNull()); }
@Test public void testSearchOriginWithLimit() throws Exception { ResultSet<Origin> result = delegate.handle(new SearchOriginRequest().setLimit(1)); assertEquals(1, result.getCount()); assertEquals(1, result.getLimit()); assertEquals(1, ListUtils.list(result.iterator()).size()); verify(securityContext, times(1)).hasReadPermission(isA(OriginEntity.class)); verify(originResponseConverter, times(1)).apply(notNull()); }
@Test public void testSearchOriginUnlimited() throws Exception { ResultSet<Origin> result = delegate.handle(new SearchOriginRequest().setLimit(0)); assertEquals(2, result.getCount()); assertEquals(0, result.getLimit()); assertEquals(2, ListUtils.list(result.iterator()).size()); verify(securityContext, times(2)).hasReadPermission(isA(OriginEntity.class)); verify(originResponseConverter, times(2)).apply(notNull()); }
@Test public void testSearchOriginFiltersNonAccessibleOrigins() throws Exception { when(securityContext.hasReadPermission(isA(OriginEntity.class))).thenReturn(false); ResultSet<Origin> result = delegate.handle(new SearchOriginRequest()); assertEquals(0, result.getCount()); assertEquals(0, ListUtils.list(result.iterator()).size()); verify(securityContext, times(2)).hasReadPermission(isA(OriginEntity.class)); verify(originResponseConverter, never()).apply(any()); }