UserFunctionExecution implements InternalFunction<Object[]> { boolean loginRequired(SecurityService securityService) { try { Subject subject = securityService.getSubject(); return subject == null || !subject.isAuthenticated(); } catch (AuthenticationRequiredException e) { return true; } }  @Override boolean isHA(); @Override String getId(); @Override Collection<ResourcePermission> getRequiredPermissions(String regionName); @Override void execute(FunctionContext<Object[]> context); static final String ID; }
@Test public void loginRequiredShouldReturnTrueWhenSubjectIsNull() { when(securityService.getSubject()).thenReturn(null); when(function.loginRequired(securityService)).thenCallRealMethod(); assertThat(function.loginRequired(securityService)).isTrue(); }
@Test public void loginRequiredShouldReturnTrueWhenSubjectIsNotAuthenticated() { Subject subject = mock(Subject.class); when(subject.isAuthenticated()).thenReturn(false); when(securityService.getSubject()).thenReturn(subject); when(function.loginRequired(securityService)).thenCallRealMethod(); assertThat(function.loginRequired(securityService)).isTrue(); }
@Test public void loginRequiredShouldReturnTrueWhenSecurityServiceFailsToLoadSubject() { when(function.loginRequired(securityService)).thenCallRealMethod(); doThrow(new AuthenticationRequiredException("Dummy Exception")).when(securityService) .getSubject(); assertThat(function.loginRequired(securityService)).isTrue(); }
@Test public void loginRequiredShouldReturnFalseWhenSubjectIsAuthenticated() { Subject subject = mock(Subject.class); when(subject.isAuthenticated()).thenReturn(true); when(securityService.getSubject()).thenReturn(subject); when(function.loginRequired(securityService)).thenCallRealMethod(); assertThat(function.loginRequired(securityService)).isFalse(); }