content: package com.konsol.beatstream.security;
  
  import static org.assertj.core.api.Assertions.assertThat;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Optional;
  import org.junit.jupiter.api.AfterEach;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Test;
  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.authority.SimpleGrantedAuthority;
  import org.springframework.security.core.context.SecurityContext;
  import org.springframework.security.core.context.SecurityContextHolder;
  
  /**
   * Test class for the {@link SecurityUtils} utility class.
   */
  class SecurityUtilsUnitTest {
  
      @BeforeEach
      @AfterEach
      void cleanup() {
          SecurityContextHolder.clearContext();
      }
  
      @Test
      void testGetCurrentUserLogin() {
          SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
          securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "admin"));
          SecurityContextHolder.setContext(securityContext);
          Optional<String> login = SecurityUtils.getCurrentUserLogin();
          assertThat(login).contains("admin");
      }
  
      @Test
      void testGetCurrentUserJWT() {
          SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
          securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "token"));
          SecurityContextHolder.setContext(securityContext);
          Optional<String> jwt = SecurityUtils.getCurrentUserJWT();
          assertThat(jwt).contains("token");
      }
  
      @Test
      void testIsAuthenticated() {
          SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
          securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "admin"));
          SecurityContextHolder.setContext(securityContext);
          boolean isAuthenticated = SecurityUtils.isAuthenticated();
          assertThat(isAuthenticated).isTrue();
      }
  
      @Test
      void testAnonymousIsNotAuthenticated() {
          SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
          Collection<GrantedAuthority> authorities = new ArrayList<>();
          authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.ANONYMOUS));
          securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("anonymous", "anonymous", authorities));
          SecurityContextHolder.setContext(securityContext);
          boolean isAuthenticated = SecurityUtils.isAuthenticated();
          assertThat(isAuthenticated).isFalse();
      }
  
      @Test
      void testHasCurrentUserThisAuthority() {
          SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
          Collection<GrantedAuthority> authorities = new ArrayList<>();
          authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.USER));
          securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("user", "user", authorities));
          SecurityContextHolder.setContext(securityContext);
  
          assertThat(SecurityUtils.hasCurrentUserThisAuthority(AuthoritiesConstants.USER)).isTrue();
          assertThat(SecurityUtils.hasCurrentUserThisAuthority(AuthoritiesConstants.ADMIN)).isFalse();
      }
  
      @Test
      void testHasCurrentUserAnyOfAuthorities() {
          SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
          Collection<GrantedAuthority> authorities = new ArrayList<>();
          authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.USER));
          securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("user", "user", authorities));
          SecurityContextHolder.setContext(securityContext);
  
          assertThat(SecurityUtils.hasCurrentUserAnyOfAuthorities(AuthoritiesConstants.USER, AuthoritiesConstants.ADMIN)).isTrue();
          assertThat(SecurityUtils.hasCurrentUserAnyOfAuthorities(AuthoritiesConstants.ANONYMOUS, AuthoritiesConstants.ADMIN)).isFalse();
      }
  
      @Test
      void testHasCurrentUserNoneOfAuthorities() {
          SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
          Collection<GrantedAuthority> authorities = new ArrayList<>();
          authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.USER));
          securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("user", "user", authorities));
          SecurityContextHolder.setContext(securityContext);
  
          assertThat(SecurityUtils.hasCurrentUserNoneOfAuthorities(AuthoritiesConstants.USER, AuthoritiesConstants.ADMIN)).isFalse();
          assertThat(SecurityUtils.hasCurrentUserNoneOfAuthorities(AuthoritiesConstants.ANONYMOUS, AuthoritiesConstants.ADMIN)).isTrue();
      }
  }
  