### Question:
FactCreateHandler { public Fact saveFact(FactRecord fact, String comment, List<UUID> subjectIds) { FactRecord existingFact = resolveExistingFact(fact); FactRecord effectiveFact = existingFact != null ? existingFact : fact; effectiveFact = withAcl(effectiveFact, securityContext.getCurrentUserID(), subjectIds); effectiveFact = withComment(effectiveFact, comment); if (existingFact != null) { effectiveFact = objectFactDao.refreshFact(effectiveFact); } else { effectiveFact = objectFactDao.storeFact(effectiveFact); } Fact addedFact = factResponseConverter.apply(effectiveFact); registerTriggerEvent(addedFact); return addedFact; } @Inject  FactCreateHandler(TiSecurityContext securityContext,
                           SubjectResolver subjectResolver,
                           OrganizationResolver organizationResolver,
                           OriginManager originManager,
                           ValidatorFactory validatorFactory,
                           ObjectFactDao objectFactDao,
                           FactResponseConverter factResponseConverter,
                           TriggerContext triggerContext); Organization resolveOrganization(String idOrName, OriginEntity fallback); OriginEntity resolveOrigin(String idOrName); List<Subject> resolveSubjects(List<String> acl); FactRecord.AccessMode resolveAccessMode(FactRecord referencedFact, AccessMode requestedAccessMode); Fact saveFact(FactRecord fact, String comment, List<UUID> subjectIds); void assertValidFactValue(FactTypeEntity type, String value);  }### Answer:
@Test public void testSaveNewFact() { FactRecord factToSave = new FactRecord() .setId(UUID.randomUUID()) .setAccessMode(FactRecord.AccessMode.RoleBased) .setOrganizationID(UUID.randomUUID()); mockSaveFact(); List<UUID> subjectIds = list(UUID.randomUUID()); handler.saveFact(factToSave, "some comment", subjectIds); verify(objectFactDao).storeFact(argThat(fact -> { assertEquals(factToSave, fact); assertEquals(set("some comment"), set(fact.getComments(), FactCommentRecord::getComment)); assertEquals(set(subjectIds), set(fact.getAcl(), FactAclEntryRecord::getSubjectID)); return true; })); verify(objectFactDao, never()).refreshFact(any()); verify(factResponseConverter).apply(same(factToSave)); }
@Test public void testRefreshExistingFact() { FactRecord factToSave = new FactRecord() .setId(UUID.randomUUID()) .setAccessMode(FactRecord.AccessMode.Public) .setOrganizationID(UUID.randomUUID()); mockSaveFact(); FactRecord existingFact = new FactRecord() .setId(UUID.randomUUID()) .setAccessMode(FactRecord.AccessMode.RoleBased) .setOrganizationID(UUID.randomUUID()); when(objectFactDao.retrieveExistingFacts(factToSave)) .thenReturn(ResultContainer.<FactRecord>builder().setValues(list(existingFact).iterator()).build()); when(securityContext.hasReadPermission(existingFact)).thenReturn(true); when(objectFactDao.refreshFact(existingFact)).thenReturn(existingFact); List<UUID> subjectIds = list(UUID.randomUUID()); handler.saveFact(factToSave, "some comment", subjectIds); verify(objectFactDao).refreshFact(argThat(fact -> { assertEquals(existingFact, fact); assertEquals(set("some comment"), set(fact.getComments(), FactCommentRecord::getComment)); assertEquals(set(subjectIds), set(fact.getAcl(), FactAclEntryRecord::getSubjectID)); return true; })); verify(objectFactDao, never()).storeFact(any()); verify(factResponseConverter).apply(same(existingFact)); }