### Question:
DefaultNotificationCreationPermissionChecker implements NotificationCreationPermissionChecker { @Override public <N extends NotificationDto<?>> boolean isNotificationCreationAllowed(final N notification) { Assert.notNull(notification, "Null was passed as an argument for parameter 'creationRequest'."); final SecurityContext context = SecurityContextHolder.getContext(); if (context == null) { throw new IllegalStateException("SecurityContext is missing."); } final Authentication authentication = context.getAuthentication(); if (authentication == null) { throw new IllegalStateException("SecurityContext should have Authentication assigned."); } if (authentication instanceof PreAuthenticatedAuthenticationToken) { return isPermitted(notification, authentication.getPrincipal().toString()); } throw new IllegalStateException("No access token was found associated with notification creation request."); } DefaultNotificationCreationPermissionChecker(final PermissionChecker permissionChecker,
                                                 final PermissionNameResolver permissionNameResolver); @Override boolean isNotificationCreationAllowed(final N notification);  }### Answer:
@Test public void isNotificationCreationAllowedInvalidArguments() { assertThatThrownBy(() -> notificationCreationPermissionChecker.isNotificationCreationAllowed(null)) .isInstanceOf(IllegalArgumentException.class); }
@Test public void isNotificationCreationAllowedMissingSecurityContext() { final EmailNotificationDto notificationDto = new EmailNotificationDto(); notificationDto.setTemplateName(uuid()); assertThatThrownBy(() -> notificationCreationPermissionChecker.isNotificationCreationAllowed(notificationDto)) .isInstanceOf(IllegalStateException.class); }
@Test public void isNotificationCreationAllowedNonPreAuthenticatedAuthenticationTokenSecurityContext() { final EmailNotificationDto notificationDto = new EmailNotificationDto(); notificationDto.setTemplateName(uuid()); final SecurityContext context = new SecurityContextImpl(); context.setAuthentication(new AnonymousAuthenticationToken(uuid(), uuid(), Collections.singleton(new SimpleGrantedAuthority("USER")))); SecurityContextHolder.setContext(context); replayAll(); assertThatThrownBy(() -> notificationCreationPermissionChecker.isNotificationCreationAllowed(notificationDto)) .isInstanceOf(IllegalStateException.class); verifyAll(); }
@Test public void isNotificationCreationAllowedMissingPermissionMapping() { final EmailNotificationDto notificationDto = new EmailNotificationDto(); notificationDto.setTemplateName(uuid()); final SecurityContext context = new SecurityContextImpl(); context.setAuthentication(new PreAuthenticatedAuthenticationToken(uuid(), uuid())); SecurityContextHolder.setContext(context); expect(permissionNameResolver.resolve(notificationDto)).andReturn(Optional.empty()); replayAll(); assertThat(notificationCreationPermissionChecker.isNotificationCreationAllowed(notificationDto)) .isFalse(); verifyAll(); }