### Question:
FactGetCommentsDelegate implements Delegate { public ResultSet<FactComment> handle(GetFactCommentsRequest request) throws AccessDeniedException, AuthenticationFailedException, InvalidArgumentException, ObjectNotFoundException { FactRecord fact = factRequestResolver.resolveFact(request.getFact()); securityContext.checkReadPermission(fact); securityContext.checkPermission(TiFunctionConstants.viewThreatIntelFactComment, fact.getOrganizationID()); List<FactComment> comments = ListUtils.list(fact.getComments()) .stream() .filter(comment -> request.getBefore() == null || comment.getTimestamp() < request.getBefore()) .filter(comment -> request.getAfter() == null || comment.getTimestamp() > request.getAfter()) .map(factCommentResponseConverter) .collect(Collectors.toList()); return StreamingResultSet.<FactComment>builder() .setCount(comments.size()) .setLimit(0) .setValues(comments) .build(); } @Inject  FactGetCommentsDelegate(TiSecurityContext securityContext,
                                 FactRequestResolver factRequestResolver,
                                 FactCommentResponseConverter factCommentResponseConverter); ResultSet<FactComment> handle(GetFactCommentsRequest request);  }### Answer:
@Test(expected = AccessDeniedException.class) public void testGetFactCommentsNoAccessToFact() throws Exception { GetFactCommentsRequest request = new GetFactCommentsRequest().setFact(UUID.randomUUID()); when(factRequestResolver.resolveFact(request.getFact())).thenReturn(new FactRecord()); doThrow(AccessDeniedException.class).when(securityContext).checkReadPermission(isA(FactRecord.class)); delegate.handle(request); }
@Test(expected = AccessDeniedException.class) public void testGetFactCommentsNoViewPermission() throws Exception { GetFactCommentsRequest request = new GetFactCommentsRequest().setFact(UUID.randomUUID()); when(factRequestResolver.resolveFact(request.getFact())).thenReturn(new FactRecord()); doThrow(AccessDeniedException.class).when(securityContext).checkPermission(eq(TiFunctionConstants.viewThreatIntelFactComment), any()); delegate.handle(request); }
@Test public void testGetFactComments() throws Exception { GetFactCommentsRequest request = new GetFactCommentsRequest().setFact(UUID.randomUUID()); List<FactCommentRecord> comments = createComments(); when(factRequestResolver.resolveFact(request.getFact())).thenReturn(new FactRecord().setComments(comments)); ResultSet<FactComment> result = delegate.handle(request); assertEquals(comments.size(), result.getCount()); assertEquals(0, result.getLimit()); assertEquals(comments.size(), ListUtils.list(result.iterator()).size()); verify(factCommentResponseConverter, times(comments.size())).apply(argThat(comments::contains)); }
@Test public void testGetFactCommentsFilterByBefore() throws Exception { GetFactCommentsRequest request = new GetFactCommentsRequest().setFact(UUID.randomUUID()).setBefore(150L); List<FactCommentRecord> comments = createComments(); when(factRequestResolver.resolveFact(request.getFact())).thenReturn(new FactRecord().setComments(comments)); assertEquals(1, delegate.handle(request).getCount()); verify(factCommentResponseConverter).apply(comments.get(0)); verifyNoMoreInteractions(factCommentResponseConverter); }
@Test public void testGetFactCommentsFilterByAfter() throws Exception { GetFactCommentsRequest request = new GetFactCommentsRequest().setFact(UUID.randomUUID()).setAfter(250L); List<FactCommentRecord> comments = createComments(); when(factRequestResolver.resolveFact(request.getFact())).thenReturn(new FactRecord().setComments(comments)); assertEquals(1, delegate.handle(request).getCount()); verify(factCommentResponseConverter).apply(comments.get(2)); verifyNoMoreInteractions(factCommentResponseConverter); }