### Question:
ObjectSearchDelegate implements Delegate { public ResultSet<Object> handle(SearchObjectRequest request) throws AccessDeniedException, AuthenticationFailedException, InvalidArgumentException { securityContext.checkPermission(TiFunctionConstants.viewThreatIntelFact); FactSearchCriteria criteria = requestConverter.apply(request); if (criteria.isUnbounded()) { throw new AccessDeniedException("Unbounded searches are not allowed. Specify at least one search parameter (in addition to 'limit')."); } ResultContainer<ObjectRecord> searchResult = objectFactDao.searchObjects(criteria); return StreamingResultSet.<Object>builder() .setCount(searchResult.getCount()) .setLimit(criteria.getLimit()) .setValues(new AddStatisticsIterator(searchResult, request.isIncludeStatistics())) .build(); } @Inject  ObjectSearchDelegate(TiSecurityContext securityContext,
                              ObjectFactDao objectFactDao,
                              SearchObjectRequestConverter requestConverter,
                              FactTypeByIdResponseResolver factTypeConverter,
                              ObjectTypeByIdResponseResolver objectTypeConverter); ResultSet<Object> handle(SearchObjectRequest request);  }### Answer:
@Test public void testSearchObjectsWithoutViewPermission() throws Exception { doThrow(AccessDeniedException.class).when(securityContext).checkPermission(TiFunctionConstants.viewThreatIntelFact); assertThrows(AccessDeniedException.class, () -> delegate.handle(new SearchObjectRequest())); }
@Test public void testSearchObjectsUnboundedRequest() throws Exception { when(requestConverter.apply(any())).thenReturn(FactSearchCriteria.builder() .setLimit(25) .setCurrentUserID(UUID.randomUUID()) .setAvailableOrganizationID(Collections.singleton(UUID.randomUUID())) .build()); assertThrows(AccessDeniedException.class, () -> delegate.handle(new SearchObjectRequest())); }
@Test public void testSearchObjectsNoResult() throws Exception { when(objectFactDao.searchObjects(any())).thenReturn(ResultContainer.<ObjectRecord>builder().build()); ResultSet<Object> result = delegate.handle(new SearchObjectRequest()); assertEquals(25, result.getLimit()); assertEquals(0, result.getCount()); assertEquals(0, ListUtils.list(result.iterator()).size()); verify(requestConverter).apply(notNull()); verify(objectFactDao).searchObjects(notNull()); verifyNoMoreInteractions(objectFactDao); }
@Test public void testSearchObjectsSkipStatistics() throws Exception { int count = 3; when(objectFactDao.searchObjects(any())).thenReturn(createSearchResult(count)); ResultSet<Object> result = delegate.handle(new SearchObjectRequest().setIncludeStatistics(false)); assertEquals(25, result.getLimit()); assertEquals(count, result.getCount()); assertEquals(count, ListUtils.list(result.iterator()).size()); verify(objectFactDao).searchObjects(notNull()); verify(objectFactDao, never()).calculateObjectStatistics(any()); }
@Test public void testSearchObjectsSingleBatchIncludeStatistics() throws Exception { int count = 3; when(objectFactDao.searchObjects(any())).thenReturn(createSearchResult(count)); when(objectFactDao.calculateObjectStatistics(any())).thenReturn(ObjectStatisticsContainer.builder().build()); ResultSet<Object> result = delegate.handle(new SearchObjectRequest().setIncludeStatistics(true)); assertEquals(25, result.getLimit()); assertEquals(count, result.getCount()); assertEquals(count, ListUtils.list(result.iterator()).size()); verify(objectFactDao).searchObjects(notNull()); verify(objectFactDao).calculateObjectStatistics(argThat(criteria -> { assertNotNull(criteria.getCurrentUserID()); assertNotNull(criteria.getAvailableOrganizationID()); assertEquals(count, criteria.getObjectID().size()); return true; })); }
@Test public void testSearchObjectsMultipleBatchesIncludeStatistics() throws Exception { int count = 1001; when(objectFactDao.searchObjects(any())).thenReturn(createSearchResult(count)); when(objectFactDao.calculateObjectStatistics(any())).thenReturn(ObjectStatisticsContainer.builder().build()); ResultSet<Object> result = delegate.handle(new SearchObjectRequest().setIncludeStatistics(true)); assertEquals(count, result.getCount()); assertEquals(count, ListUtils.list(result.iterator()).size()); verify(objectFactDao).searchObjects(notNull()); verify(objectFactDao, times(2)).calculateObjectStatistics(notNull()); }