package com.mosquito.project.controller; import com.fasterxml.jackson.databind.ObjectMapper; import com.mosquito.project.domain.Activity; import com.mosquito.project.dto.CreateActivityRequest; import com.mosquito.project.dto.UpdateActivityRequest; import com.mosquito.project.dto.ActivityStatsResponse; import com.mosquito.project.dto.ActivityGraphResponse; import com.mosquito.project.exception.ActivityNotFoundException; import com.mosquito.project.service.ActivityService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import java.time.ZonedDateTime; import java.util.List; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @WebMvcTest(ActivityController.class) class ActivityControllerTest { @Autowired private MockMvc mockMvc; @Autowired private ObjectMapper objectMapper; @MockBean private ActivityService activityService; @Test void whenCreateActivity_withValidInput_thenReturns201() throws Exception { CreateActivityRequest request = new CreateActivityRequest(); request.setName("Valid Activity"); request.setStartTime(ZonedDateTime.now().plusDays(1)); request.setEndTime(ZonedDateTime.now().plusDays(2)); Activity activity = new Activity(); activity.setId(1L); activity.setName(request.getName()); given(activityService.createActivity(any(CreateActivityRequest.class))).willReturn(activity); mockMvc.perform(post("/api/v1/activities") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) .andExpect(status().isCreated()) .andExpect(jsonPath("$.id").value(1L)) .andExpect(jsonPath("$.name").value("Valid Activity")); } @Test void whenGetActivity_withExistingId_thenReturns200() throws Exception { Activity activity = new Activity(); activity.setId(1L); activity.setName("Test Activity"); given(activityService.getActivityById(1L)).willReturn(activity); mockMvc.perform(get("/api/v1/activities/1")) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(1L)) .andExpect(jsonPath("$.name").value("Test Activity")); } @Test void whenGetActivity_withNonExistentId_thenReturns404() throws Exception { given(activityService.getActivityById(999L)).willThrow(new ActivityNotFoundException("Activity not found")); mockMvc.perform(get("/api/v1/activities/999")) .andExpect(status().isNotFound()); } @Test void whenUpdateActivity_withValidInput_thenReturns200() throws Exception { UpdateActivityRequest request = new UpdateActivityRequest(); request.setName("Updated Activity"); request.setStartTime(ZonedDateTime.now().plusDays(1)); request.setEndTime(ZonedDateTime.now().plusDays(2)); Activity activity = new Activity(); activity.setId(1L); activity.setName(request.getName()); given(activityService.updateActivity(eq(1L), any(UpdateActivityRequest.class))).willReturn(activity); mockMvc.perform(put("/api/v1/activities/1") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(1L)) .andExpect(jsonPath("$.name").value("Updated Activity")); } @Test void whenGetActivityStats_withExistingId_thenReturns200() throws Exception { List dailyStats = List.of( new ActivityStatsResponse.DailyStats("2025-09-28", 100, 50), new ActivityStatsResponse.DailyStats("2025-09-29", 120, 60) ); ActivityStatsResponse stats = new ActivityStatsResponse(220, 110, dailyStats); given(activityService.getActivityStats(1L)).willReturn(stats); mockMvc.perform(get("/api/v1/activities/1/stats")) .andExpect(status().isOk()) .andExpect(jsonPath("$.totalParticipants").value(220)) .andExpect(jsonPath("$.totalShares").value(110)); } @Test void whenGetActivityGraph_withExistingId_thenReturns200() throws Exception { List nodes = List.of( new ActivityGraphResponse.Node("1", "User A"), new ActivityGraphResponse.Node("2", "User B"), new ActivityGraphResponse.Node("3", "User C") ); List edges = List.of( new ActivityGraphResponse.Edge("1", "2"), new ActivityGraphResponse.Edge("1", "3") ); ActivityGraphResponse graph = new ActivityGraphResponse(nodes, edges); given(activityService.getActivityGraph(1L)).willReturn(graph); mockMvc.perform(get("/api/v1/activities/1/graph")) .andExpect(status().isOk()) .andExpect(jsonPath("$.nodes.length()").value(3)) .andExpect(jsonPath("$.edges.length()").value(2)); } }