|
| 1 | +package com.techtorque.time_logging_service.controller; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.techtorque.time_logging_service.dto.request.TimeLogRequest; |
| 5 | +import com.techtorque.time_logging_service.dto.request.TimeLogUpdateRequest; |
| 6 | +import com.techtorque.time_logging_service.dto.response.TimeLogResponse; |
| 7 | +import com.techtorque.time_logging_service.service.TimeLogService; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; |
| 13 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 14 | +import org.springframework.http.MediaType; |
| 15 | +import org.springframework.security.test.context.support.WithMockUser; |
| 16 | +import org.springframework.test.context.ActiveProfiles; |
| 17 | +import org.springframework.test.web.servlet.MockMvc; |
| 18 | + |
| 19 | +import java.time.LocalDate; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import static org.mockito.ArgumentMatchers.*; |
| 25 | +import static org.mockito.Mockito.when; |
| 26 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 27 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 28 | + |
| 29 | +@SpringBootTest |
| 30 | +@AutoConfigureMockMvc(addFilters = false) |
| 31 | +@ActiveProfiles("test") |
| 32 | +class TimeLogControllerIntegrationTest { |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private MockMvc mockMvc; |
| 36 | + |
| 37 | + @Autowired |
| 38 | + private ObjectMapper objectMapper; |
| 39 | + |
| 40 | + @MockBean |
| 41 | + private TimeLogService timeLogService; |
| 42 | + |
| 43 | + private TimeLogResponse testResponse; |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + void setUp() { |
| 47 | + testResponse = new TimeLogResponse(); |
| 48 | + testResponse.setId("log123"); |
| 49 | + testResponse.setEmployeeId("employee123"); |
| 50 | + testResponse.setServiceId("service456"); |
| 51 | + testResponse.setProjectId("project789"); |
| 52 | + testResponse.setHours(8.0); |
| 53 | + testResponse.setDate(LocalDate.of(2025, 11, 21)); |
| 54 | + testResponse.setDescription("Worked on feature"); |
| 55 | + testResponse.setWorkType("Development"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + @WithMockUser(roles = "EMPLOYEE") |
| 60 | + void testCreateTimeLog_Success() throws Exception { |
| 61 | + TimeLogRequest request = new TimeLogRequest(); |
| 62 | + request.setServiceId("service456"); |
| 63 | + request.setProjectId("project789"); |
| 64 | + request.setHours(8.0); |
| 65 | + request.setDate(LocalDate.of(2025, 11, 21)); |
| 66 | + request.setDescription("Worked on feature"); |
| 67 | + request.setWorkType("Development"); |
| 68 | + |
| 69 | + when(timeLogService.createTimeLog(anyString(), any(TimeLogRequest.class))) |
| 70 | + .thenReturn(testResponse); |
| 71 | + |
| 72 | + mockMvc.perform(post("/time-logs") |
| 73 | + .header("X-User-Subject", "employee123") |
| 74 | + .contentType(MediaType.APPLICATION_JSON) |
| 75 | + .content(objectMapper.writeValueAsString(request))) |
| 76 | + .andExpect(status().isCreated()) |
| 77 | + .andExpect(jsonPath("$.id").value("log123")) |
| 78 | + .andExpect(jsonPath("$.hours").value(8.0)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @WithMockUser(roles = "EMPLOYEE") |
| 83 | + void testGetMyTimeLogs_Employee() throws Exception { |
| 84 | + when(timeLogService.getAllTimeLogsByEmployee("employee123")) |
| 85 | + .thenReturn(Arrays.asList(testResponse)); |
| 86 | + |
| 87 | + mockMvc.perform(get("/time-logs") |
| 88 | + .header("X-User-Subject", "employee123") |
| 89 | + .header("X-User-Roles", "ROLE_EMPLOYEE")) |
| 90 | + .andExpect(status().isOk()) |
| 91 | + .andExpect(jsonPath("$").isArray()) |
| 92 | + .andExpect(jsonPath("$[0].id").value("log123")); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + @WithMockUser(roles = "ADMIN") |
| 97 | + void testGetMyTimeLogs_Admin() throws Exception { |
| 98 | + when(timeLogService.getAllTimeLogs()) |
| 99 | + .thenReturn(Arrays.asList(testResponse)); |
| 100 | + |
| 101 | + mockMvc.perform(get("/time-logs") |
| 102 | + .header("X-User-Subject", "admin123") |
| 103 | + .header("X-User-Roles", "ROLE_ADMIN")) |
| 104 | + .andExpect(status().isOk()) |
| 105 | + .andExpect(jsonPath("$").isArray()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + @WithMockUser(roles = "EMPLOYEE") |
| 110 | + void testGetTimeLogById_Success() throws Exception { |
| 111 | + when(timeLogService.getTimeLogByIdWithAuthorization(anyString(), anyString(), anyString())) |
| 112 | + .thenReturn(testResponse); |
| 113 | + |
| 114 | + mockMvc.perform(get("/time-logs/{logId}", "log123") |
| 115 | + .header("X-User-Subject", "employee123") |
| 116 | + .header("X-User-Role", "ROLE_EMPLOYEE")) |
| 117 | + .andExpect(status().isOk()) |
| 118 | + .andExpect(jsonPath("$.id").value("log123")); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + @WithMockUser(roles = "EMPLOYEE") |
| 123 | + void testUpdateTimeLog_Success() throws Exception { |
| 124 | + TimeLogUpdateRequest updateRequest = new TimeLogUpdateRequest(); |
| 125 | + updateRequest.setHours(10.0); |
| 126 | + updateRequest.setDescription("Updated description"); |
| 127 | + |
| 128 | + when(timeLogService.updateTimeLogWithAuthorization(anyString(), anyString(), any(TimeLogUpdateRequest.class))) |
| 129 | + .thenReturn(testResponse); |
| 130 | + |
| 131 | + mockMvc.perform(put("/time-logs/{logId}", "log123") |
| 132 | + .header("X-User-Subject", "employee123") |
| 133 | + .contentType(MediaType.APPLICATION_JSON) |
| 134 | + .content(objectMapper.writeValueAsString(updateRequest))) |
| 135 | + .andExpect(status().isOk()) |
| 136 | + .andExpect(jsonPath("$.id").value("log123")); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + @WithMockUser(roles = "EMPLOYEE") |
| 141 | + void testDeleteTimeLog_Success() throws Exception { |
| 142 | + mockMvc.perform(delete("/time-logs/{logId}", "log123") |
| 143 | + .header("X-User-Subject", "employee123") |
| 144 | + .header("X-User-Roles", "ROLE_EMPLOYEE")) |
| 145 | + .andExpect(status().isNoContent()); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + @WithMockUser(roles = "CUSTOMER") |
| 150 | + void testGetTimeLogsForService_Success() throws Exception { |
| 151 | + when(timeLogService.getTimeLogsByServiceId("service456")) |
| 152 | + .thenReturn(Arrays.asList(testResponse)); |
| 153 | + |
| 154 | + mockMvc.perform(get("/time-logs/service/{serviceId}", "service456")) |
| 155 | + .andExpect(status().isOk()) |
| 156 | + .andExpect(jsonPath("$").isArray()) |
| 157 | + .andExpect(jsonPath("$[0].serviceId").value("service456")); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + @WithMockUser(roles = "EMPLOYEE") |
| 162 | + void testGetTimeLogsForProject_Success() throws Exception { |
| 163 | + when(timeLogService.getTimeLogsByProjectId("project789")) |
| 164 | + .thenReturn(Arrays.asList(testResponse)); |
| 165 | + |
| 166 | + mockMvc.perform(get("/time-logs/project/{projectId}", "project789")) |
| 167 | + .andExpect(status().isOk()) |
| 168 | + .andExpect(jsonPath("$").isArray()) |
| 169 | + .andExpect(jsonPath("$[0].projectId").value("project789")); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + @WithMockUser(roles = "EMPLOYEE") |
| 174 | + void testGetEmployeeStats_Success() throws Exception { |
| 175 | + Map<String, Object> stats = new HashMap<>(); |
| 176 | + stats.put("employeeId", "employee123"); |
| 177 | + stats.put("totalHours", 40.0); |
| 178 | + stats.put("totalLogs", 5); |
| 179 | + |
| 180 | + when(timeLogService.getEmployeeStatistics("employee123")) |
| 181 | + .thenReturn(stats); |
| 182 | + |
| 183 | + mockMvc.perform(get("/time-logs/stats") |
| 184 | + .header("X-User-Subject", "employee123")) |
| 185 | + .andExpect(status().isOk()) |
| 186 | + .andExpect(jsonPath("$.employeeId").value("employee123")) |
| 187 | + .andExpect(jsonPath("$.totalHours").value(40.0)); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + @WithMockUser(roles = "EMPLOYEE") |
| 192 | + void testGetMyTimeLogs_WithDateRange() throws Exception { |
| 193 | + when(timeLogService.getTimeLogsByDateRange(anyString(), any(LocalDate.class), any(LocalDate.class))) |
| 194 | + .thenReturn(Arrays.asList(testResponse)); |
| 195 | + |
| 196 | + mockMvc.perform(get("/time-logs") |
| 197 | + .header("X-User-Subject", "employee123") |
| 198 | + .header("X-User-Roles", "ROLE_EMPLOYEE") |
| 199 | + .param("from", "2025-11-01") |
| 200 | + .param("to", "2025-11-30")) |
| 201 | + .andExpect(status().isOk()) |
| 202 | + .andExpect(jsonPath("$").isArray()); |
| 203 | + } |
| 204 | +} |
0 commit comments