File size: 2,758 Bytes
1c8e50c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package in.garvit.tasks.controller;

import in.garvit.tasks.exception.UserException;
import in.garvit.tasks.response.UserResponse;
import in.garvit.tasks.service.UserService;
import in.garvit.tasks.usermodel.User;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;

class UserControllerTest {

    @Mock
    private UserService userService;

    @InjectMocks
    private UserController userController;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
    }

    @Test
    void testGetUserProfile() throws UserException {
        // Arrange
        String jwt = "mockJWT";
        User mockUser = new User();
        mockUser.setEmail("mock@example.com");
        // Set up mock behavior
        when(userService.findUserProfileByJwt(jwt)).thenReturn(mockUser);

        // Act
        ResponseEntity<UserResponse> responseEntity = userController.getUserProfile(jwt);

        // Assert
        assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
        assertNotNull(responseEntity.getBody());
        assertEquals(mockUser.getEmail(), responseEntity.getBody().email());
    }

    @Test
    void testFindUserById() throws UserException {
        // Arrange
        String userId = "mockUserId";
        String jwt = "mockJWT";
        User mockUser = new User();
        mockUser.setId(userId);
        // Set up mock behavior
        when(userService.findUserById(userId)).thenReturn(mockUser);

        // Act
        ResponseEntity<UserResponse> responseEntity = userController.findUserById(userId, jwt);

        // Assert
        assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
        assertNotNull(responseEntity.getBody());
        assertEquals(mockUser.getId(), responseEntity.getBody().id());
    }

    @Test
    void testFindAllUsers() {
        // Arrange
        String jwt = "mockJWT";
        List<User> mockUsers = new ArrayList<>();
        // Set up mock behavior
        when(userService.findAllUsers()).thenReturn(mockUsers);

        // Act
        ResponseEntity<List<UserResponse>> responseEntity = userController.findAllUsers(jwt);

        // Assert
        assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
        assertNotNull(responseEntity.getBody());
        assertEquals(mockUsers.size(), responseEntity.getBody().size());
    }
}