### Question:
FactCreateHandler { public Organization resolveOrganization(String idOrName, OriginEntity fallback) throws AccessDeniedException, AuthenticationFailedException, InvalidArgumentException { if (idOrName != null) { Organization organization = fetchOrganization(idOrName); securityContext.checkReadPermission(organization); return organization; } if (fallback != null && fallback.getOrganizationID() != null) { return fetchOrganization(fallback.getOrganizationID().toString()); } return resolveCurrentUserAffiliation(); } @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(expected = InvalidArgumentException.class) public void testResolveOrganizationProvidedOrganizationNotExists() throws Exception { handler.resolveOrganization("organization", null); }
@Test public void testResolveOrganizationProvidedFetchesExistingOrganizationById() throws Exception { UUID organizationID = UUID.randomUUID(); Organization organization = Organization.builder().build(); when(organizationResolver.resolveOrganization(organizationID)).thenReturn(organization); assertSame(organization, handler.resolveOrganization(organizationID.toString(), null)); verify(organizationResolver).resolveOrganization(organizationID); verify(securityContext).checkReadPermission(organization); }
@Test public void testResolveOrganizationProvidedFetchesExistingOrganizationByName() throws Exception { Organization organization = Organization.builder().build(); when(organizationResolver.resolveOrganization("organization")).thenReturn(organization); assertSame(organization, handler.resolveOrganization("organization", null)); verify(organizationResolver).resolveOrganization("organization"); verify(securityContext).checkReadPermission(organization); }
@Test(expected = InvalidArgumentException.class) public void testResolveOrganizationFallbackToOriginOrganizationNotExists() throws Exception { handler.resolveOrganization(null, new OriginEntity().setOrganizationID(UUID.randomUUID())); }
@Test public void testResolveOrganizationFallbackToOriginFetchesExistingOrganization() throws Exception { OriginEntity origin = new OriginEntity().setOrganizationID(UUID.randomUUID()); Organization organization = Organization.builder().build(); when(organizationResolver.resolveOrganization(origin.getOrganizationID())).thenReturn(organization); assertSame(organization, handler.resolveOrganization(null, origin)); verify(organizationResolver).resolveOrganization(origin.getOrganizationID()); }