-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e0b26b
commit 9c19506
Showing
3 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/test/java/com/tukorea/planding/schedule/controller/ScheduleControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.tukorea.planding.schedule.controller; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.tukorea.planding.domain.schedule.controller.ScheduleController; | ||
import com.tukorea.planding.domain.user.mapper.UserMapper; | ||
import com.tukorea.planding.global.config.security.SecurityConfig; | ||
import com.tukorea.planding.global.config.security.jwt.JwtAuthenticationFilter; | ||
import com.tukorea.planding.domain.schedule.dto.ScheduleRequest; | ||
import com.tukorea.planding.domain.schedule.service.ScheduleService; | ||
import com.tukorea.planding.domain.user.entity.User; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
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.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.FilterType; | ||
import org.springframework.data.jpa.mapping.JpaMetamodelMappingContext; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
|
||
import static org.mockito.BDDMockito.given; | ||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@MockBean(JpaMetamodelMappingContext.class) | ||
@WebMvcTest(controllers = {ScheduleController.class}, excludeFilters = { | ||
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {SecurityConfig.class, JwtAuthenticationFilter.class})}) | ||
class ScheduleControllerTest { | ||
|
||
@MockBean | ||
private ScheduleService scheduleService; | ||
|
||
@InjectMocks | ||
private ScheduleController scheduleController; | ||
|
||
@Autowired | ||
MockMvc mockMvc; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@Test | ||
@WithMockUser | ||
void createSchedule() throws Exception{ | ||
User user = User.builder() | ||
.email("test") | ||
.build(); | ||
|
||
ScheduleRequest schedule = ScheduleRequest.builder() | ||
.title("test") | ||
.build(); | ||
|
||
given(scheduleService.createSchedule(UserMapper.toUserInfo(user),schedule)).willThrow(UsernameNotFoundException.class); | ||
|
||
|
||
ResultActions actions = mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/schedule") | ||
.content(objectMapper.writeValueAsString(schedule)) | ||
.contentType(MediaType.APPLICATION_JSON).with(csrf())); | ||
|
||
actions.andExpect(MockMvcResultMatchers.status().isOk()); | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
src/test/java/com/tukorea/planding/schedule/service/ScheduleServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package com.tukorea.planding.schedule.service; | ||
|
||
import com.tukorea.planding.domain.schedule.dto.ScheduleRequest; | ||
import com.tukorea.planding.domain.schedule.dto.ScheduleResponse; | ||
import com.tukorea.planding.domain.schedule.entity.Schedule; | ||
import com.tukorea.planding.domain.schedule.repository.ScheduleRepositoryCustomImpl; | ||
import com.tukorea.planding.domain.schedule.service.ScheduleQueryService; | ||
import com.tukorea.planding.domain.schedule.service.ScheduleService; | ||
import com.tukorea.planding.domain.user.entity.SocialType; | ||
import com.tukorea.planding.domain.user.entity.User; | ||
import com.tukorea.planding.domain.user.mapper.UserMapper; | ||
import com.tukorea.planding.domain.user.service.UserQueryService; | ||
import com.tukorea.planding.global.oauth.details.Role; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalTime; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
|
||
@ExtendWith(MockitoExtension.class) | ||
@Transactional | ||
class ScheduleServiceTest { | ||
|
||
|
||
@InjectMocks | ||
private ScheduleService scheduleService; | ||
|
||
@Mock | ||
UserQueryService userQueryService; | ||
|
||
@Mock | ||
ScheduleRepositoryCustomImpl scheduleRepositoryCustomImpl; | ||
|
||
@Mock | ||
ScheduleQueryService scheduleQueryService; | ||
|
||
|
||
private User testUser; | ||
private Schedule schedule; | ||
private ScheduleRequest scheduleRequest; | ||
|
||
private Schedule createAndSaveSchedule(User user, LocalTime startTime, LocalTime endTime) { | ||
return Schedule.builder() | ||
.user(user) | ||
.scheduleDate(LocalDate.now()) | ||
.title("title") | ||
.content("content") | ||
.startTime(startTime) | ||
.endTime(endTime) | ||
.build(); | ||
} | ||
|
||
@BeforeEach | ||
void setUp() { | ||
testUser = new User("test", "profile", "username", Role.USER, SocialType.KAKAO, null, "#test"); // 테스트용 사용자 정보 초기화 | ||
schedule = new Schedule("title", "content", LocalDate.now(), LocalTime.of(9, 0), LocalTime.of(10, 0), true, null, testUser); | ||
scheduleRequest = new ScheduleRequest("#test", "title", "content", LocalDate.now(), LocalTime.of(9, 0), LocalTime.of(10, 0)); | ||
} | ||
|
||
|
||
@Test | ||
void create_스케줄_생성() { | ||
// given | ||
when(scheduleQueryService.save(any(Schedule.class))).thenReturn(schedule); | ||
|
||
// when | ||
ScheduleResponse result = scheduleService.createSchedule(UserMapper.toUserInfo(testUser), scheduleRequest); | ||
|
||
// then | ||
assertNotNull(result); | ||
assertEquals("title", result.title()); | ||
assertEquals("content", result.content()); | ||
assertEquals(LocalTime.of(9, 0), result.startTime()); | ||
assertEquals(LocalTime.of(10, 0), result.endTime()); | ||
} | ||
|
||
@Test | ||
void delete_스케줄_삭제() { | ||
//given | ||
when(scheduleQueryService.findScheduleById(1L)).thenReturn(schedule); | ||
|
||
//when | ||
scheduleService.deleteSchedule(UserMapper.toUserInfo(testUser), 1L); | ||
|
||
//then | ||
verify(scheduleQueryService).delete(schedule); | ||
} | ||
|
||
@Test | ||
public void 주간개인스케줄_가져오기() { | ||
//given | ||
List<Schedule> responses = Collections.singletonList(schedule); | ||
when(userQueryService.getUserByUserCode(any())).thenReturn(testUser); | ||
when(scheduleRepositoryCustomImpl.findWeeklyScheduleByUser(any(), any(), eq(testUser.getId()))).thenReturn(responses); | ||
|
||
//when | ||
List<ScheduleResponse> result = scheduleService.getWeekSchedule(LocalDate.now(), LocalDate.now().plusDays(7), UserMapper.toUserInfo(testUser)); | ||
|
||
//then | ||
assertNotNull(result); | ||
assertEquals(1, result.size()); | ||
assertEquals(result.get(0).startTime(), schedule.getStartTime()); | ||
assertEquals(result.get(0).endTime(), schedule.getEndTime()); | ||
} | ||
|
||
@Test | ||
public void update_스케줄수정() { | ||
//when | ||
String updateTitle = "update_title"; | ||
String updateContent = "update_content"; | ||
schedule.update(updateTitle, updateContent, null, null); | ||
|
||
//then | ||
assertEquals(schedule.getTitle(), updateTitle); | ||
assertEquals(schedule.getContent(), updateContent); | ||
} | ||
|
||
@Test | ||
public void update_스케줄수정시_endTime이startTime보다작을때() { | ||
//given | ||
assertThrows(IllegalArgumentException.class, () -> schedule.update(null, null, LocalTime.of(10, 0), LocalTime.of(9, 10))); | ||
} | ||
} |