### Question:
FactCreateHandler { public OriginEntity resolveOrigin(String idOrName) throws AccessDeniedException, AuthenticationFailedException, InvalidArgumentException { if (idOrName != null) { OriginEntity origin = fetchOrigin(idOrName); if (origin != null) { securityContext.checkReadPermission(origin); return origin; } throw new InvalidArgumentException() .addValidationError("Origin does not exist.", "origin.not.exist", "origin", idOrName); } OriginEntity origin = fetchOrigin(securityContext.getCurrentUserID().toString()); if (origin != null) return origin; Subject currentUser = resolveCurrentUser(); return originManager.saveOrigin(new OriginEntity() .setId(securityContext.getCurrentUserID()) .setNamespaceID(GLOBAL_NAMESPACE) .setOrganizationID(ObjectUtils.ifNotNull(currentUser.getOrganization(), Organization.Info::getId)) .setName(createOriginName(currentUser)) .setTrust(ORIGIN_DEFAULT_TRUST) .setType(OriginEntity.Type.User) ); } @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 testResolveOriginProvidedOriginNotExists() throws Exception { handler.resolveOrigin("origin"); }
@Test public void testResolveOriginProvidedFetchesExistingOriginById() throws Exception { UUID originID = UUID.randomUUID(); OriginEntity origin = new OriginEntity().setId(originID); when(originManager.getOrigin(originID)).thenReturn(origin); assertSame(origin, handler.resolveOrigin(originID.toString())); verify(originManager).getOrigin(originID); verify(securityContext).checkReadPermission(origin); verifyNoMoreInteractions(originManager); }
@Test public void testResolveOriginProvidedFetchesExistingOriginByName() throws Exception { OriginEntity origin = new OriginEntity(); when(originManager.getOrigin("origin")).thenReturn(origin); assertSame(origin, handler.resolveOrigin("origin")); verify(originManager).getOrigin("origin"); verify(securityContext).checkReadPermission(origin); verifyNoMoreInteractions(originManager); }
@Test(expected = InvalidArgumentException.class) public void testResolveOriginProvidedFetchesDeletedOrigin() throws Exception { UUID originID = UUID.randomUUID(); OriginEntity origin = new OriginEntity() .setId(originID) .addFlag(OriginEntity.Flag.Deleted); when(originManager.getOrigin(originID)).thenReturn(origin); handler.resolveOrigin(originID.toString()); }
@Test public void testResolveOriginNonProvidedFetchesExistingOrigin() throws Exception { UUID currentUserID = UUID.randomUUID(); OriginEntity origin = new OriginEntity().setId(currentUserID); when(securityContext.getCurrentUserID()).thenReturn(currentUserID); when(originManager.getOrigin(currentUserID)).thenReturn(origin); assertSame(origin, handler.resolveOrigin(null)); verify(originManager).getOrigin(currentUserID); verifyNoMoreInteractions(originManager); }
@Test(expected = InvalidArgumentException.class) public void testResolveOriginNonProvidedFetchesDeletedOrigin() throws Exception { UUID currentUserID = UUID.randomUUID(); OriginEntity origin = new OriginEntity() .setId(currentUserID) .addFlag(OriginEntity.Flag.Deleted); when(securityContext.getCurrentUserID()).thenReturn(currentUserID); when(originManager.getOrigin(currentUserID)).thenReturn(origin); handler.resolveOrigin(null); }