### Question:
FactCreateCommentDelegate implements Delegate { public FactComment handle(CreateFactCommentRequest request) throws AccessDeniedException, AuthenticationFailedException, InvalidArgumentException, ObjectNotFoundException { FactRecord fact = factRequestResolver.resolveFact(request.getFact()); securityContext.checkReadPermission(fact); securityContext.checkPermission(TiFunctionConstants.addThreatIntelFactComment, fact.getOrganizationID()); verifyReplyToCommentExists(fact, request); return factCommentResponseConverter.apply(saveComment(fact, request)); } @Inject  FactCreateCommentDelegate(TiSecurityContext securityContext,
                                   ObjectFactDao objectFactDao,
                                   FactRequestResolver factRequestResolver,
                                   FactCommentResponseConverter factCommentResponseConverter); FactComment handle(CreateFactCommentRequest request);  }### Answer:
@Test(expected = AccessDeniedException.class) public void testCreateFactCommentNoAccessToFact() throws Exception { CreateFactCommentRequest request = createFactCommentRequest(); 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 testCreateFactCommentNoAddPermission() throws Exception { CreateFactCommentRequest request = createFactCommentRequest(); when(factRequestResolver.resolveFact(request.getFact())).thenReturn(new FactRecord()); doThrow(AccessDeniedException.class).when(securityContext).checkPermission(eq(TiFunctionConstants.addThreatIntelFactComment), any()); delegate.handle(request); }
@Test(expected = InvalidArgumentException.class) public void testCreateFactCommentReplyToNotExists() throws Exception { CreateFactCommentRequest request = createFactCommentRequest(); when(factRequestResolver.resolveFact(request.getFact())).thenReturn(createFactRecord(request.getFact(), UUID.randomUUID())); delegate.handle(request); }
@Test public void testCreateFactComment() throws Exception { UUID currentUser = UUID.randomUUID(); CreateFactCommentRequest request = createFactCommentRequest(); FactRecord fact = createFactRecord(request.getFact(), request.getReplyTo()); when(factRequestResolver.resolveFact(request.getFact())).thenReturn(fact); when(objectFactDao.storeFactComment(notNull(), notNull())).then(i -> i.getArgument(1)); when(securityContext.getCurrentUserID()).thenReturn(currentUser); delegate.handle(request); verify(objectFactDao).storeFactComment(same(fact), matchFactCommentRecord(request, currentUser)); verify(factCommentResponseConverter).apply(matchFactCommentRecord(request, currentUser)); }