Skip to content

Commit

Permalink
fix: 유저 프로필 조회 기능 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
SangWoon123 committed May 19, 2024
1 parent 8e0b26b commit 9c19506
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.tukorea.planding.domain.group.entity.GroupFavorite;
import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.group.service.RedisGroupInviteService;
import com.tukorea.planding.domain.schedule.service.ScheduleQueryService;
import com.tukorea.planding.domain.user.dto.AndroidLoginRequest;
import com.tukorea.planding.domain.user.dto.ProfileResponse;
Expand All @@ -21,6 +22,7 @@
public class UserService {

private final UserQueryService userQueryService;
private final RedisGroupInviteService redisGroupInviteService;


@Transactional(readOnly = true)
Expand Down Expand Up @@ -58,7 +60,7 @@ public ProfileResponse getProfile(UserInfo userInfo) {
.username(userProfile.getUsername())
.profileImage(userProfile.getProfileImage())
.groupFavorite((long) userProfile.getGroupFavorites().size())
.groupRequest((long) 0)
.groupRequest((long) redisGroupInviteService.getAllInvitations(userInfo.getUserCode()).size())
.role(Role.USER)
.build();
}
Expand Down
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());
}
}
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)));
}
}

0 comments on commit 9c19506

Please sign in to comment.