1
+ package com .app .login .security ;
2
+
3
+ import com .app .login .domain .Authority ;
4
+ import com .app .login .domain .User ;
5
+ import com .app .login .repository .UserRepository ;
6
+ import org .junit .jupiter .api .BeforeEach ;
7
+ import org .junit .jupiter .api .Test ;
8
+ import org .junit .jupiter .api .extension .ExtendWith ;
9
+ import org .mockito .Mock ;
10
+ import org .mockito .junit .jupiter .MockitoExtension ;
11
+ import org .springframework .security .core .userdetails .UserDetails ;
12
+ import org .springframework .security .core .userdetails .UsernameNotFoundException ;
13
+
14
+ import java .util .HashSet ;
15
+ import java .util .Optional ;
16
+ import java .util .Set ;
17
+
18
+ import static org .junit .jupiter .api .Assertions .*;
19
+ import static org .mockito .ArgumentMatchers .anyString ;
20
+ import static org .mockito .Mockito .*;
21
+
22
+ /**
23
+ * DomainUserDetailsServiceImplClaudeTest for the DomainUserDetailsServiceImpl class.
24
+ */
25
+ @ ExtendWith (MockitoExtension .class )
26
+ public class DomainUserDetailsServiceImplClaudeTest {
27
+
28
+ @ Mock
29
+ private UserRepository userRepository ;
30
+
31
+ private DomainUserDetailsServiceImpl userDetailsService ;
32
+
33
+ private User testUser ;
34
+ private static final String TEST_LOGIN = "testuser" ;
35
+ private static final String TEST_PASSWORD = "password123" ;
36
+ private static final String TEST_AUTHORITY = "ROLE_USER" ;
37
+
38
+ @ BeforeEach
39
+ void setUp () {
40
+ userDetailsService = new DomainUserDetailsServiceImpl (userRepository );
41
+ testUser = createTestUser ();
42
+ }
43
+
44
+ @ Test
45
+ void loadUserByUsername_WhenUserExistsAndActivated_ReturnsUserDetails () {
46
+ // Arrange
47
+ when (userRepository .findOneWithAuthoritiesByLogin (TEST_LOGIN .toLowerCase ()))
48
+ .thenReturn (Optional .of (testUser ));
49
+
50
+ // Act
51
+ UserDetails userDetails = userDetailsService .loadUserByUsername (TEST_LOGIN );
52
+
53
+ // Assert
54
+ assertNotNull (userDetails );
55
+ assertEquals (TEST_LOGIN , userDetails .getUsername ());
56
+ assertEquals (TEST_PASSWORD , userDetails .getPassword ());
57
+ assertEquals (1 , userDetails .getAuthorities ().size ());
58
+ assertTrue (userDetails .getAuthorities ().stream ()
59
+ .anyMatch (auth -> auth .getAuthority ().equals (TEST_AUTHORITY )));
60
+ verify (userRepository , times (1 ))
61
+ .findOneWithAuthoritiesByLogin (TEST_LOGIN .toLowerCase ());
62
+ }
63
+
64
+ @ Test
65
+ void loadUserByUsername_WhenUserNotFound_ThrowsUsernameNotFoundException () {
66
+ // Arrange
67
+ when (userRepository .findOneWithAuthoritiesByLogin (anyString ()))
68
+ .thenReturn (Optional .empty ());
69
+
70
+ // Act & Assert
71
+ Exception exception = assertThrows (UsernameNotFoundException .class , () ->
72
+ userDetailsService .loadUserByUsername (TEST_LOGIN ));
73
+
74
+ assertTrue (exception .getMessage ().contains ("was not found in the database" ));
75
+ verify (userRepository , times (1 ))
76
+ .findOneWithAuthoritiesByLogin (TEST_LOGIN .toLowerCase ());
77
+ }
78
+
79
+ @ Test
80
+ void loadUserByUsername_WhenUserNotActivated_ThrowsUserNotActivatedException () {
81
+ // Arrange
82
+ testUser .setActivated (false );
83
+ when (userRepository .findOneWithAuthoritiesByLogin (TEST_LOGIN .toLowerCase ()))
84
+ .thenReturn (Optional .of (testUser ));
85
+
86
+ // Act & Assert
87
+ Exception exception = assertThrows (UserNotActivatedException .class , () ->
88
+ userDetailsService .loadUserByUsername (TEST_LOGIN ));
89
+
90
+ assertTrue (exception .getMessage ().contains ("was not activated" ));
91
+ verify (userRepository , times (1 ))
92
+ .findOneWithAuthoritiesByLogin (TEST_LOGIN .toLowerCase ());
93
+ }
94
+
95
+ @ Test
96
+ void loadUserByUsername_WhenLoginIsNull_ThrowsUsernameNotFoundException () {
97
+ // Act & Assert
98
+ Exception exception = assertThrows (UsernameNotFoundException .class , () ->
99
+ userDetailsService .loadUserByUsername (null ));
100
+
101
+ assertEquals ("User login is null" , exception .getMessage ());
102
+ verify (userRepository , never ()).findOneWithAuthoritiesByLogin (anyString ());
103
+ }
104
+
105
+ @ Test
106
+ void loadUserByUsername_WithDifferentCase_ConvertedToLowerCase () {
107
+ // Arrange
108
+ String mixedCaseLogin = "TeStUsEr" ;
109
+ when (userRepository .findOneWithAuthoritiesByLogin (mixedCaseLogin .toLowerCase ()))
110
+ .thenReturn (Optional .of (testUser ));
111
+
112
+ // Act
113
+ UserDetails userDetails = userDetailsService .loadUserByUsername (mixedCaseLogin );
114
+
115
+ // Assert
116
+ assertNotNull (userDetails );
117
+ assertEquals (mixedCaseLogin .toLowerCase (), userDetails .getUsername ());
118
+ verify (userRepository , times (1 ))
119
+ .findOneWithAuthoritiesByLogin (mixedCaseLogin .toLowerCase ());
120
+ }
121
+
122
+ private User createTestUser () {
123
+ User user = new User ();
124
+ user .setLogin (TEST_LOGIN );
125
+ user .setPassword (TEST_PASSWORD );
126
+ user .setActivated (true );
127
+
128
+ Set <Authority > authorities = new HashSet <>();
129
+ Authority authority = new Authority ();
130
+ authority .setName (TEST_AUTHORITY );
131
+ authorities .add (authority );
132
+ user .setAuthorities (authorities );
133
+
134
+ return user ;
135
+ }
136
+ }
0 commit comments