1
+ package com .app .login .service ;
2
+
3
+ import com .app .login .domain .Authority ;
4
+ import com .app .login .domain .User ;
5
+ import com .app .login .repository .UserRepository ;
6
+ import com .app .login .security .DomainUserDetailsServiceImpl ;
7
+ import com .app .login .security .UserNotActivatedException ;
8
+ import org .junit .jupiter .api .BeforeEach ;
9
+ import org .junit .jupiter .api .Test ;
10
+ import org .junit .jupiter .api .extension .ExtendWith ;
11
+ import org .mockito .InjectMocks ;
12
+ import org .mockito .Mock ;
13
+ import org .mockito .junit .jupiter .MockitoExtension ;
14
+ import org .springframework .security .core .authority .SimpleGrantedAuthority ;
15
+ import org .springframework .security .core .userdetails .UserDetails ;
16
+ import org .springframework .security .core .userdetails .UsernameNotFoundException ;
17
+
18
+ import java .util .Collections ;
19
+ import java .util .Optional ;
20
+
21
+ import static org .junit .jupiter .api .Assertions .*;
22
+ import static org .mockito .ArgumentMatchers .anyString ;
23
+ import static org .mockito .Mockito .*;
24
+
25
+ @ ExtendWith (MockitoExtension .class )
26
+ public class DomainUserDetailsServiceImplAIGenTest {
27
+
28
+ @ Mock
29
+ private UserRepository userRepository ;
30
+
31
+ @ InjectMocks
32
+ private DomainUserDetailsServiceImpl userDetailsService ;
33
+
34
+ private User testUser ;
35
+
36
+ @ BeforeEach
37
+ public void setUp () {
38
+ // Create a test user
39
+ testUser = new User ();
40
+ testUser .setId (1L );
41
+ testUser .setLogin ("testuser" );
42
+ testUser .setPassword ("encodedPassword" );
43
+ testUser .setActivated (true );
44
+ testUser .
setEmail (
"[email protected] " );
45
+
46
+ // Add some authorities to the test user
47
+ Authority authority = new Authority ();
48
+ authority .setName ("ROLE_USER" );
49
+ testUser .setAuthorities (Collections .singleton (authority ));
50
+ }
51
+
52
+ @ Test
53
+ public void testLoadUserByUsername_Success () {
54
+ // Mock the repository to return the test user
55
+ when (userRepository .findOneWithAuthoritiesByLogin (anyString ())).thenReturn (Optional .of (testUser ));
56
+
57
+ // Call the method under test
58
+ UserDetails userDetails = userDetailsService .loadUserByUsername ("testuser" );
59
+
60
+ // Verify the interactions with the mock
61
+ verify (userRepository , times (1 )).findOneWithAuthoritiesByLogin ("testuser" );
62
+
63
+ // Assert the returned user details
64
+ assertNotNull (userDetails );
65
+ assertEquals ("testuser" , userDetails .getUsername ());
66
+ assertEquals ("encodedPassword" , userDetails .getPassword ());
67
+ assertTrue (userDetails .isAccountNonExpired ());
68
+ assertTrue (userDetails .isAccountNonLocked ());
69
+ assertTrue (userDetails .isCredentialsNonExpired ());
70
+ assertTrue (userDetails .isEnabled ());
71
+ assertEquals (1 , userDetails .getAuthorities ().size ());
72
+ assertTrue (userDetails .getAuthorities ().contains (new SimpleGrantedAuthority ("ROLE_USER" )));
73
+ }
74
+
75
+ @ Test
76
+ public void testLoadUserByUsername_UserNotActivated () {
77
+ // Create a test user that is not activated
78
+ User inactiveUser = new User ();
79
+ inactiveUser .setId (1L );
80
+ inactiveUser .setLogin ("inactiveuser" );
81
+ inactiveUser .setPassword ("encodedPassword" );
82
+ inactiveUser .setActivated (false );
83
+ inactiveUser .
setEmail (
"[email protected] " );
84
+
85
+ // Mock the repository to return the inactive user
86
+ when (userRepository .findOneWithAuthoritiesByLogin (anyString ())).thenReturn (Optional .of (inactiveUser ));
87
+
88
+ // Call the method under test and expect an exception
89
+ assertThrows (UserNotActivatedException .class , () -> userDetailsService .loadUserByUsername ("inactiveuser" ));
90
+
91
+ // Verify the interactions with the mock
92
+ verify (userRepository , times (1 )).findOneWithAuthoritiesByLogin ("inactiveuser" );
93
+ }
94
+
95
+ @ Test
96
+ public void testLoadUserByUsername_UserNotFound () {
97
+ // Mock the repository to return an empty optional
98
+ when (userRepository .findOneWithAuthoritiesByLogin (anyString ())).thenReturn (Optional .empty ());
99
+
100
+ // Call the method under test and expect an exception
101
+ assertThrows (UsernameNotFoundException .class , () -> userDetailsService .loadUserByUsername ("nonexistentuser" ));
102
+
103
+ // Verify the interactions with the mock
104
+ verify (userRepository , times (1 )).findOneWithAuthoritiesByLogin ("nonexistentuser" );
105
+ }
106
+ }
0 commit comments