Skip to content

Commit

Permalink
[REFACTOR] 일급컬렉션 사용 및 테스트 코드 전반 수정 (#355)
Browse files Browse the repository at this point in the history
* [FIX] 테스트 코드 수정(토큰 변경, 빠진 로직 추가)

* [REFACTOR] List 객체 일급컬렉션 사용
  • Loading branch information
mikekks authored May 28, 2024
1 parent 566fd54 commit 4ffcb7e
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public void approveApplicant(Long postId, Long userId, List<Long> applicantIds)
@Transactional
public void rejectApplicants(Long postId, Long userId, List<Long> applicantIds) {

List<RecruitmentApplicant> applicants = recruitmentApplicantService.getAllApplicants(applicantIds);
recruitmentApplicantService.rejectApplicants(applicants, applicantIds, userId);
recruitmentApplicantService.rejectApplicants(applicantIds, userId);

recruitmentPostService.incrementResponseCount(postId, userId, applicantIds.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.transaction.annotation.Transactional;
import synk.meeteam.domain.recruitment.recruitment_applicant.dto.response.GetApplicantDto;
import synk.meeteam.domain.recruitment.recruitment_applicant.dto.response.GetApplicantResponseDto;
import synk.meeteam.domain.recruitment.recruitment_applicant.service.vo.RecruitmentApplicants;
import synk.meeteam.global.entity.DeleteStatus;
import synk.meeteam.domain.recruitment.recruitment_applicant.entity.RecruitStatus;
import synk.meeteam.domain.recruitment.recruitment_applicant.entity.RecruitmentApplicant;
Expand Down Expand Up @@ -78,22 +79,26 @@ public Map<Long, Long> getRecruitedCounts(List<RecruitmentApplicant> approvedApp

@Transactional
public void approveApplicants(List<RecruitmentApplicant> applicants, List<Long> applicantIds, Long userId) {
// TODO : List<RecruitmentApplicant> 일급컬렉션으로 리팩토링 필요.
validateCanProcess(applicants, userId);
validateApplicantCount(applicantIds.size(), applicants.size());
validateApplicants(applicantIds.size(), applicants.size());

recruitmentApplicantRepository.updateRecruitStatus(applicantIds, RecruitStatus.APPROVED);
}

@Transactional
public void rejectApplicants(List<RecruitmentApplicant> applicants, List<Long> applicantIds, Long userId) {
validateCanProcess(applicants, userId);
validateApplicantCount(applicantIds.size(), applicants.size());
public void rejectApplicants(List<Long> requestApplicantIds, Long writerId) {
List<RecruitmentApplicant> applicants = getAllApplicants(requestApplicantIds);
RecruitmentApplicants recruitmentApplicants = new RecruitmentApplicants(applicants, requestApplicantIds,
writerId);

recruitmentApplicantRepository.updateRecruitStatus(applicantIds, RecruitStatus.REJECTED);
recruitmentApplicantRepository.updateRecruitStatus(recruitmentApplicants.getRecruitmentApplicantIds(),
RecruitStatus.REJECTED);
}

@Transactional
public GetApplicantResponseDto getAllByRole(Long postId, Long roleId, Long userId, Long writerId, int page, int size) {
public GetApplicantResponseDto getAllByRole(Long postId, Long roleId, Long userId, Long writerId, int page,
int size) {
validateIsWriter(userId, writerId);

int pageNumber = page - 1;
Expand All @@ -120,8 +125,8 @@ public boolean isAppliedUser(RecruitmentPost recruitmentPost, User user) {
return true;
}

private void validateIsWriter(Long userId, Long writerId){
if(!userId.equals(writerId)){
private void validateIsWriter(Long userId, Long writerId) {
if (!userId.equals(writerId)) {
throw new RecruitmentApplicantException(INVALID_USER);
}
}
Expand All @@ -135,7 +140,7 @@ private void validateCanProcess(List<RecruitmentApplicant> applicants, Long user
.forEach(applicant -> applicant.validateCanApprove(userId));
}

private void validateApplicantCount(int requestCount, int actualCount) {
private void validateApplicants(int requestCount, int actualCount) {
if (requestCount != actualCount) {
throw new RecruitmentApplicantException(INVALID_REQUEST);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package synk.meeteam.domain.recruitment.recruitment_applicant.service.vo;

import static synk.meeteam.domain.recruitment.recruitment_applicant.exception.RecruitmentApplicantExceptionType.INVALID_REQUEST;

import java.util.List;
import lombok.Getter;
import synk.meeteam.domain.recruitment.recruitment_applicant.entity.RecruitmentApplicant;
import synk.meeteam.domain.recruitment.recruitment_applicant.exception.RecruitmentApplicantException;

public class RecruitmentApplicants {
private final List<RecruitmentApplicant> recruitmentApplicants;

public RecruitmentApplicants(List<RecruitmentApplicant> recruitmentApplicants, List<Long> requestApplicantIds,
Long writerId) {
validateApplicants(recruitmentApplicants, requestApplicantIds);
validateCanProcess(recruitmentApplicants, writerId);
this.recruitmentApplicants = recruitmentApplicants;
}

public List<Long> getRecruitmentApplicantIds() {
return recruitmentApplicants.stream()
.map(RecruitmentApplicant::getId)
.toList();
}

private void validateApplicants(List<RecruitmentApplicant> recruitmentApplicants, List<Long> requestApplicantIds) {
if (recruitmentApplicants.size() != requestApplicantIds.size()) {
throw new RecruitmentApplicantException(INVALID_REQUEST);
}

List<Long> actualApplicantIds = recruitmentApplicants.stream()
.map(RecruitmentApplicant::getId)
.toList();

for (Long id : requestApplicantIds) {
if (!actualApplicantIds.contains(id)) {
throw new RecruitmentApplicantException(INVALID_REQUEST);
}
}
}

private void validateCanProcess(List<RecruitmentApplicant> applicants, Long userId) {
// applicants 검증로직
// 호출한 사용자가 구인글 작성자인지 확인
// status가 다 NONE인지 확인

applicants.stream()
.forEach(applicant -> applicant.validateCanApprove(userId));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public class SkillRepositoryTest {
List<SkillDto> skills = skillRepository.findAllByKeywordAndTopLimit(keyword, limit);

//then
assertThat(skills).extracting("name").containsExactly("next.js", "node.js");
assertThat(skills.size()).isEqualTo(2);
assertThat(skills).extracting("name").containsExactly("kotlin", "next.js", "node.js", "python", "spring");
assertThat(skills.size()).isEqualTo(5);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import synk.meeteam.domain.portfolio.portfolio.service.PortfolioServiceImpl;
import synk.meeteam.domain.user.user.entity.User;
import synk.meeteam.global.entity.ProceedType;
import synk.meeteam.infra.s3.service.S3Service;

@ExtendWith(MockitoExtension.class)
public class PortfolioServiceTest {
Expand All @@ -49,8 +50,12 @@ public class PortfolioServiceTest {
@Mock
private RoleRepository roleRepository;

@Mock
private S3Service s3Service;

@BeforeEach
void setup() {

portfolioService = portfolioServiceImpl;
}

Expand Down Expand Up @@ -110,6 +115,7 @@ void setup() {
//given
doReturn(PortfolioFixture.createSlicePortfolioDtos()).when(portfolioRepository)
.findSlicePortfoliosByUserOrderByCreatedAtDesc(eq(PageRequest.of(0, 12)), any());
doReturn("url입니다.").when(s3Service).createPreSignedGetUrl(any(), any());
//when
GetUserPortfolioResponseDto userAllPortfolios = portfolioService.getSliceMyAllPortfolio(1, 12,
User.builder().build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,16 @@ public class RecruitmentApplicantServiceTest {

RecruitmentApplicant applicant1 = RecruitmentApplicantFixture.createRecruitmentApplicant(
recruitmentPost, user1, role1);
applicant1.setId(1L);
RecruitmentApplicant applicant2 = RecruitmentApplicantFixture.createRecruitmentApplicant(
recruitmentPost, user2, role2);
applicant2.setId(2L);

doReturn(2L).when(recruitmentApplicantRepository).updateRecruitStatus(any(), any());
doReturn(List.of(applicant1, applicant2)).when(recruitmentApplicantRepository).findAllInApplicantId(any());

// when, then
recruitmentApplicantService.rejectApplicants(List.of(applicant1, applicant2), List.of(userId1, userId2),
recruitmentApplicantService.rejectApplicants(List.of(userId1, userId2),
userId1);
}

Expand All @@ -347,13 +350,16 @@ public class RecruitmentApplicantServiceTest {

RecruitmentApplicant applicant1 = RecruitmentApplicantFixture.createRecruitmentApplicant(
recruitmentPost, user1, role1);
applicant1.setId(1L);
RecruitmentApplicant applicant2 = RecruitmentApplicantFixture.createRecruitmentApplicant(
recruitmentPost, user2, role2);
applicant2.setId(2L);

doReturn(List.of(applicant1, applicant2)).when(recruitmentApplicantRepository).findAllInApplicantId(any());

// when, then
Assertions.assertThatThrownBy(
() -> recruitmentApplicantService.rejectApplicants(List.of(applicant1, applicant2),
List.of(userId1, userId2), userId1))
() -> recruitmentApplicantService.rejectApplicants(List.of(userId1, userId2), userId1))
.isInstanceOf(RecruitmentApplicantException.class)
.hasMessageContaining(INVALID_USER.message());
}
Expand All @@ -376,13 +382,17 @@ public class RecruitmentApplicantServiceTest {

RecruitmentApplicant applicant1 = RecruitmentApplicantFixture.createApprovedRecruitmentApplicant(
recruitmentPost, user1, role1);
applicant1.setId(1L);

RecruitmentApplicant applicant2 = RecruitmentApplicantFixture.createApprovedRecruitmentApplicant(
recruitmentPost, user2, role2);
applicant2.setId(2L);

doReturn(List.of(applicant1, applicant2)).when(recruitmentApplicantRepository).findAllInApplicantId(any());

// when, then
Assertions.assertThatThrownBy(
() -> recruitmentApplicantService.rejectApplicants(List.of(applicant1, applicant2),
List.of(userId1, userId2), userId1))
() -> recruitmentApplicantService.rejectApplicants(List.of(userId1, userId2), userId1))
.isInstanceOf(RecruitmentApplicantException.class)
.hasMessageContaining(ALREADY_PROCESSED_APPLICANT.message());
}
Expand All @@ -405,11 +415,14 @@ public class RecruitmentApplicantServiceTest {

RecruitmentApplicant applicant1 = RecruitmentApplicantFixture.createRecruitmentApplicant(
recruitmentPost, user1, role1);
applicant1.setId(1L);

doReturn(List.of(applicant1)).when(recruitmentApplicantRepository).findAllInApplicantId(any());


// when, then
Assertions.assertThatThrownBy(
() -> recruitmentApplicantService.rejectApplicants(List.of(applicant1),
List.of(userId1, userId2), userId1))
() -> recruitmentApplicantService.rejectApplicants(List.of(userId1, userId2), userId1))
.isInstanceOf(RecruitmentApplicantException.class)
.hasMessageContaining(INVALID_REQUEST.message());
}
Expand Down Expand Up @@ -439,7 +452,7 @@ public class RecruitmentApplicantServiceTest {
utilities.when(() -> Encryption.encryptLong(any())).thenReturn("1234");

// when
GetApplicantResponseDto responseDtos = recruitmentApplicantService.getAllByRole(postId, roleId, 1, 12);
GetApplicantResponseDto responseDtos = recruitmentApplicantService.getAllByRole(postId, roleId,1L, 1L, 1, 12);

// then
Assertions.assertThat(responseDtos.applicants().size()).isEqualTo(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public class RecruitmentApplicantTest {
private String accessHeader;
@Value("${jwt.refresh.header}")
private String refreshHeader;
private String TOKEN = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBY2Nlc3NUb2tlbiIsInBsYXRmb3JtSWQiOiJEaTdsQ2hNR3hqWlZUYWk2ZDc2SG8xWUxEVV94TDh0bDFDZmRQTVY1U1FNIiwicGxhdGZvcm1UeXBlIjoiTkFWRVIiLCJpYXQiOjE3MDg1OTkyMTMsImV4cCI6MTgxNjU5OTIxM30.C9Rt8t2dM_9pmUIwyMiRwi2kZSXAFVJnjAPj2rTbQtw";
private String TOKEN_OTHER = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBY2Nlc3NUb2tlbiIsInBsYXRmb3JtSWQiOiJEaTdsQ2hNR3hqWlZUYWk2ZDc2SG8xWUxEVV94TDh0bDFDZmRQTVY1U1ExIiwicGxhdGZvcm1UeXBlIjoiTkFWRVIiLCJpYXQiOjE3MDg1OTkyMTMsImV4cCI6MTgxNjU5OTIxM30.ujVS6-qGhaOYJv0aPet3tgcc5iN93-k0Kv9w1rETFpA";
private String TOKEN = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBY2Nlc3NUb2tlbiIsInVzZXJJZCI6IjRPYVZFNDIxRFN3UjYzeGZLZjZ2eEEiLCJpYXQiOjE3MTQ5ODM5MDQsImV4cCI6MjAyMjk4MzkwNH0.PsQHWlh-tV-FY3dk0zVwiiBCfyLn4LPbFylGcau1Eis";
private String TOKEN_OTHER = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBY2Nlc3NUb2tlbiIsInVzZXJJZCI6ImJfVmtjT05oTUNKSWJHbEQ2eW9Ua3ciLCJpYXQiOjE3MTQ5ODM5MDQsImV4cCI6MjAyMjk4MzkwNH0.pGrBWCYOrR2RKQfqKgG705I7NHqIlykUcYrKqhj_nOM";
@Autowired
private TestRestTemplate restTemplate;
@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public class RecruitmentPostServiceTest {
// given
RecruitmentPost srcRecruitmentPost = RecruitmentPostFixture.createRecruitmentPost("수정하려는제목입니다");
RecruitmentPost dstRecruitmentPost = RecruitmentPostFixture.createRecruitmentPost("그냥제목입니다");

dstRecruitmentPost.setCreatedBy(1L);
doReturn(dstRecruitmentPost).when(recruitmentPostRepository).save(any());

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ public class RecruitmentPostTest {
@Value("${jwt.refresh.header}")
private String refreshHeader;

private String TOKEN = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBY2Nlc3NUb2tlbiIsInBsYXRmb3JtSWQiOiJEaTdsQ2hNR3hqWlZUYWk2ZDc2SG8xWUxEVV94TDh0bDFDZmRQTVY1U1FNIiwicGxhdGZvcm1UeXBlIjoiTkFWRVIiLCJpYXQiOjE3MDg1OTkyMTMsImV4cCI6MTgxNjU5OTIxM30.C9Rt8t2dM_9pmUIwyMiRwi2kZSXAFVJnjAPj2rTbQtw";

private String TOKEN_OTHER = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBY2Nlc3NUb2tlbiIsInBsYXRmb3JtSWQiOiJEaTdsQ2hNR3hqWlZUYWk2ZDc2SG8xWUxEVV94TDh0bDFDZmRQTVY1U1ExIiwicGxhdGZvcm1UeXBlIjoiTkFWRVIiLCJpYXQiOjE3MDg1OTkyMTMsImV4cCI6MTgxNjU5OTIxM30.ujVS6-qGhaOYJv0aPet3tgcc5iN93-k0Kv9w1rETFpA";
private String TOKEN = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBY2Nlc3NUb2tlbiIsInVzZXJJZCI6IjRPYVZFNDIxRFN3UjYzeGZLZjZ2eEEiLCJpYXQiOjE3MTQ5ODM5MDQsImV4cCI6MjAyMjk4MzkwNH0.PsQHWlh-tV-FY3dk0zVwiiBCfyLn4LPbFylGcau1Eis";
private String TOKEN_OTHER = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBY2Nlc3NUb2tlbiIsInVzZXJJZCI6ImJfVmtjT05oTUNKSWJHbEQ2eW9Ua3ciLCJpYXQiOjE3MTQ5ODM5MDQsImV4cCI6MjAyMjk4MzkwNH0.pGrBWCYOrR2RKQfqKgG705I7NHqIlykUcYrKqhj_nOM";


@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class UserControllerTest {
private MockMvc mockMvc;
private Gson gson;

private String TOKEN = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBY2Nlc3NUb2tlbiIsInVzZXJJZCI6IjRPYVZFNDIxRFN3UjYzeGZLZjZ2eEEiLCJpYXQiOjE3MTQ5ODM5MDQsImV4cCI6MjAyMjk4MzkwNH0.PsQHWlh-tV-FY3dk0zVwiiBCfyLn4LPbFylGcau1Eis";
private String TOKEN_OTHER = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBY2Nlc3NUb2tlbiIsInVzZXJJZCI6ImJfVmtjT05oTUNKSWJHbEQ2eW9Ua3ciLCJpYXQiOjE3MTQ5ODM5MDQsImV4cCI6MjAyMjk4MzkwNH0.pGrBWCYOrR2RKQfqKgG705I7NHqIlykUcYrKqhj_nOM";

@BeforeEach
public void init() {
GsonBuilder gsonBuilder = new GsonBuilder();
Expand Down Expand Up @@ -88,7 +91,7 @@ public void init() {
//when
final ResultActions resultActions = mockMvc.perform(
MockMvcRequestBuilders.put(url)
.header("Authorization", "aaaaa")
.header("Authorization", TOKEN)
.contentType(MediaType.APPLICATION_JSON)
.content(gson.toJson(UserFixture.createEditProfileDto()))
);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/synk/meeteam/domain/user/user/UserFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static User createUser() {

public static UpdateProfileRequestDto createEditProfileDto() {
return new UpdateProfileRequestDto(
"goder",
"goder1",
true,
"imageUrl.png",
"010-1234-5678",
Expand All @@ -56,7 +56,7 @@ public static UpdateProfileRequestDto createEditProfileDto() {
4.3,
4.5,
List.of(1L, 2L, 3L),
List.of(new UpdateUserLinkDto("naver.com", "네이버")),
List.of(new UpdateUserLinkDto("https://gemini.google.com/", "네이버")),
List.of(new UpdateAwardDto("공공데이터공모전", "장려상수상", LocalDate.parse("2023-02-02"),
LocalDate.parse("2023-03-01"))),
List.of(1L, 2L)
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/synk/meeteam/global/xss/XssSpringBootTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public class XssSpringBootTest {
@Value("${jwt.access.header}")
private String accessHeader;

private String TOKEN = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBY2Nlc3NUb2tlbiIsInBsYXRmb3JtSWQiOiI0SUlfbGZaY1Q2NW82MVRfZkh5d2hybzVJanlSTmpSZE1IVi1qNXJWMmtvIiwicGxhdGZvcm1UeXBlIjoiTkFWRVIiLCJpYXQiOjE3MDg1OTkyMTMsImV4cCI6MTgxNjU5OTIxM30.5kAEY2nJ3mNqlnhhFNV0_FVvXTD7JRTzTj6FjpresEA";
private String TOKEN = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBY2Nlc3NUb2tlbiIsInVzZXJJZCI6IjRPYVZFNDIxRFN3UjYzeGZLZjZ2eEEiLCJpYXQiOjE3MTQ5ODM5MDQsImV4cCI6MjAyMjk4MzkwNH0.PsQHWlh-tV-FY3dk0zVwiiBCfyLn4LPbFylGcau1Eis";
private String TOKEN_OTHER = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBY2Nlc3NUb2tlbiIsInVzZXJJZCI6ImJfVmtjT05oTUNKSWJHbEQ2eW9Ua3ciLCJpYXQiOjE3MTQ5ODM5MDQsImV4cCI6MjAyMjk4MzkwNH0.pGrBWCYOrR2RKQfqKgG705I7NHqIlykUcYrKqhj_nOM";

HttpHeaders headers;

Expand Down

0 comments on commit 4ffcb7e

Please sign in to comment.