|
| 1 | +package com.synergy.backend.domain.member.api; |
| 2 | + |
| 3 | +import static org.mockito.BDDMockito.*; |
| 4 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; |
| 5 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 6 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; |
| 7 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.DisplayName; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.springframework.http.MediaType; |
| 14 | +import org.springframework.mock.web.MockMultipartFile; |
| 15 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 16 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 17 | +import org.springframework.test.util.ReflectionTestUtils; |
| 18 | + |
| 19 | +import com.synergy.backend.domain.member.api.dto.resposne.LikedRecruiterResponseDto; |
| 20 | +import com.synergy.backend.domain.member.api.dto.resposne.ProfileImageUpdatedResponseDto; |
| 21 | +import com.synergy.backend.domain.member.entity.Attendee; |
| 22 | +import com.synergy.backend.global.security.CustomUserDetails; |
| 23 | +import com.synergy.backend.module.ControllerTestSupport; |
| 24 | + |
| 25 | +@DisplayName("AttendeeController 테스트") |
| 26 | +class AttendeeControllerTest extends ControllerTestSupport { |
| 27 | + |
| 28 | + @DisplayName("좋아요한 채용담당자 목록을 조회한다") |
| 29 | + @Test |
| 30 | + void getLikedRecruiters() throws Exception { |
| 31 | + // given |
| 32 | + Long attendeeId = 1L; |
| 33 | + CustomUserDetails userDetails = new CustomUserDetails(createAttendee(attendeeId)); |
| 34 | + SecurityContextHolder.getContext().setAuthentication( |
| 35 | + new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()) |
| 36 | + ); |
| 37 | + |
| 38 | + List<LikedRecruiterResponseDto> liked = List.of( |
| 39 | + new LikedRecruiterResponseDto("회사1", "책임1", "홍길동"), |
| 40 | + new LikedRecruiterResponseDto("회사2", "책임2", "김철수") |
| 41 | + ); |
| 42 | + |
| 43 | + given(recruiterAttendeeLikeService.getLikedRecruiters(attendeeId)).willReturn(liked); |
| 44 | + |
| 45 | + // when & then |
| 46 | + mockMvc.perform(get("/api/v1/attendee/liked-recruiters") |
| 47 | + .contentType(MediaType.APPLICATION_JSON)) |
| 48 | + .andExpect(status().isOk()) |
| 49 | + .andExpect(jsonPath("$.data.length()").value(2)) |
| 50 | + .andExpect(jsonPath("$.data[0].company").value("회사1")) |
| 51 | + .andExpect(jsonPath("$.data[0].responsibility").value("책임1")) |
| 52 | + .andExpect(jsonPath("$.data[0].name").value("홍길동")) |
| 53 | + .andDo(print()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @DisplayName("참가자 현재 직무 정보 등록 요청을 처리한다") |
| 58 | + void addJobInfo() throws Exception { |
| 59 | + Long attendeeId = 1L; |
| 60 | + CustomUserDetails userDetails = new CustomUserDetails(createAttendee(attendeeId)); |
| 61 | + SecurityContextHolder.getContext().setAuthentication( |
| 62 | + new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()) |
| 63 | + ); |
| 64 | + |
| 65 | + String requestBody = """ |
| 66 | + { |
| 67 | + "interestCodes": [101, 102, 103], |
| 68 | + "jobGroupCode": 1, |
| 69 | + "jobPositionCode": 101, |
| 70 | + "hiringInterested": true |
| 71 | + } |
| 72 | + """; |
| 73 | + |
| 74 | + mockMvc.perform(patch("/api/v1/attendee/onboarding/job-info") |
| 75 | + .with(csrf()) |
| 76 | + .contentType(MediaType.APPLICATION_JSON) |
| 77 | + .content(requestBody)) |
| 78 | + .andExpect(status().isOk()) |
| 79 | + .andExpect(jsonPath("$.code").value(200)) |
| 80 | + .andDo(print()); |
| 81 | + |
| 82 | + then(attendeeService).should().addJobInfo(eq(attendeeId), any()); |
| 83 | + } |
| 84 | + |
| 85 | + @DisplayName("내 정보를 조회한다") |
| 86 | + @Test |
| 87 | + void getMyInformation() throws Exception { |
| 88 | + Long attendeeId = 1L; |
| 89 | + CustomUserDetails userDetails = new CustomUserDetails(createAttendee(attendeeId)); |
| 90 | + SecurityContextHolder.getContext().setAuthentication( |
| 91 | + new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()) |
| 92 | + ); |
| 93 | + |
| 94 | + given(attendeeService.getMyInformation(attendeeId)).willReturn(null); |
| 95 | + |
| 96 | + mockMvc.perform(get("/api/v1/attendee/my") |
| 97 | + .contentType(MediaType.APPLICATION_JSON)) |
| 98 | + .andExpect(status().isOk()) |
| 99 | + .andExpect(jsonPath("$.code").value(200)) |
| 100 | + .andDo(print()); |
| 101 | + |
| 102 | + then(attendeeService).should().getMyInformation(attendeeId); |
| 103 | + } |
| 104 | + |
| 105 | + @DisplayName("특정 참가자의 상세 정보를 조회한다") |
| 106 | + @Test |
| 107 | + void getAttendeeInfoDetail() throws Exception { |
| 108 | + Long attendeeId = 1L; |
| 109 | + Long targetId = 2L; |
| 110 | + CustomUserDetails userDetails = new CustomUserDetails(createAttendee(attendeeId)); |
| 111 | + SecurityContextHolder.getContext().setAuthentication( |
| 112 | + new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()) |
| 113 | + ); |
| 114 | + |
| 115 | + given(attendeeService.getAttendeeInfoDetail(targetId, attendeeId, userDetails.getRole())).willReturn(null); |
| 116 | + |
| 117 | + mockMvc.perform(get("/api/v1/attendee/{attendeeId}", targetId) |
| 118 | + .contentType(MediaType.APPLICATION_JSON)) |
| 119 | + .andExpect(status().isOk()) |
| 120 | + .andExpect(jsonPath("$.code").value(200)) |
| 121 | + .andDo(print()); |
| 122 | + |
| 123 | + then(attendeeService).should().getAttendeeInfoDetail(targetId, attendeeId, userDetails.getRole()); |
| 124 | + } |
| 125 | + |
| 126 | + @DisplayName("참가자 상세 직무 정보 등록 요청을 처리한다") |
| 127 | + @Test |
| 128 | + void addJobInfoDetails() throws Exception { |
| 129 | + Long attendeeId = 1L; |
| 130 | + CustomUserDetails userDetails = new CustomUserDetails(createAttendee(attendeeId)); |
| 131 | + SecurityContextHolder.getContext().setAuthentication( |
| 132 | + new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()) |
| 133 | + ); |
| 134 | + |
| 135 | + MockMultipartFile request = new MockMultipartFile( |
| 136 | + "request", |
| 137 | + "request.json", |
| 138 | + MediaType.APPLICATION_JSON_VALUE, |
| 139 | + """ |
| 140 | + { |
| 141 | + "desiredJobGroupCode": 1, |
| 142 | + "desiredJobPositionCode": 101, |
| 143 | + "educationLevelCode": 3, |
| 144 | + "ageGroupCode": 20, |
| 145 | + "techStacks": "Java,Spring", |
| 146 | + "experienceLevelCode": 2, |
| 147 | + "desiredWorkRegionCodes": [11, 22], |
| 148 | + "selfIntroduction": "저는 열정적인 백엔드 개발자입니다.", |
| 149 | + "additionalInfo": "스타트업 인턴 경험, 외부 해커톤 참가", |
| 150 | + "workplaceSelectionFactorCodes": [1, 2], |
| 151 | + "preferredCorporateCultureCodes": [1], |
| 152 | + "conferencePurposeCodes": [1, 2] |
| 153 | + } |
| 154 | + """.getBytes() |
| 155 | + ); |
| 156 | + |
| 157 | + MockMultipartFile profileImage = new MockMultipartFile("profileImage", "profile.png", MediaType.IMAGE_PNG_VALUE, |
| 158 | + "image content".getBytes()); |
| 159 | + |
| 160 | + mockMvc.perform(multipart("/api/v1/attendee/onboarding/job-info-details") |
| 161 | + .file(request) |
| 162 | + .file(profileImage) |
| 163 | + .with(csrf()) |
| 164 | + .with(requestBuilder -> { |
| 165 | + requestBuilder.setMethod("PATCH"); |
| 166 | + return requestBuilder; |
| 167 | + }) |
| 168 | + .contentType(MediaType.MULTIPART_FORM_DATA)) |
| 169 | + .andExpect(status().isOk()) |
| 170 | + .andExpect(jsonPath("$.code").value(200)) |
| 171 | + .andDo(print()); |
| 172 | + |
| 173 | + then(attendeeService).should().addJobInfoDetails(eq(attendeeId), any(), any()); |
| 174 | + } |
| 175 | + |
| 176 | + @DisplayName("참가자 프로필 사진을 수정한다") |
| 177 | + @Test |
| 178 | + void updateProfileImage() throws Exception { |
| 179 | + Long attendeeId = 1L; |
| 180 | + CustomUserDetails userDetails = new CustomUserDetails(createAttendee(attendeeId)); |
| 181 | + SecurityContextHolder.getContext().setAuthentication( |
| 182 | + new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()) |
| 183 | + ); |
| 184 | + |
| 185 | + MockMultipartFile profileImage = new MockMultipartFile("profileImage", "profile.png", MediaType.IMAGE_PNG_VALUE, |
| 186 | + "image content".getBytes()); |
| 187 | + |
| 188 | + given(attendeeService.updateProfileImage(eq(attendeeId), any())).willReturn( |
| 189 | + ProfileImageUpdatedResponseDto.from("updated-url")); |
| 190 | + |
| 191 | + mockMvc.perform(multipart("/api/v1/attendee/profile-image") |
| 192 | + .file(profileImage) |
| 193 | + .with(csrf()) |
| 194 | + .with(request -> { |
| 195 | + request.setMethod("PATCH"); |
| 196 | + return request; |
| 197 | + }) |
| 198 | + .contentType(MediaType.MULTIPART_FORM_DATA)) |
| 199 | + .andExpect(status().isOk()) |
| 200 | + .andExpect(jsonPath("$.code").value(200)) |
| 201 | + .andExpect(jsonPath("$.data.profileImageUrl").value("updated-url")) |
| 202 | + .andDo(print()); |
| 203 | + |
| 204 | + then(attendeeService).should().updateProfileImage(eq(attendeeId), any()); |
| 205 | + } |
| 206 | + |
| 207 | + private Attendee createAttendee(Long id) { |
| 208 | + Attendee attendee = Attendee.of("exaple@example.com", "pass", "참가자", "01101010"); |
| 209 | + ReflectionTestUtils.setField(attendee, "id", id); |
| 210 | + return attendee; |
| 211 | + } |
| 212 | +} |
0 commit comments