Skip to content

Commit

Permalink
test: 피플 상세 정보 조회 기능 컨트롤러 단위 테스트 작성 (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
scv1702 authored May 17, 2024
1 parent b21eb41 commit 88b1011
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 70 deletions.
2 changes: 2 additions & 0 deletions src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ include::{docdir}/member/upload-profile-image.adoc[]
=== 피플 정보 등록
include::{docdir}/people/create-people.adoc[]

=== 피플 상세 조회
include::{docdir}/people/get-people.adoc[]
17 changes: 17 additions & 0 deletions src/docs/asciidoc/people/get-people.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
==== HTTP request
include::{snippets}/get-people/get-people_-when-user-logined/http-request.adoc[]

==== Request headers
include::{snippets}/get-people/get-people_-when-user-logined/request-headers.adoc[]

==== Path parameters
include::{snippets}/get-people/get-people_-when-user-logined/path-parameters.adoc[]

==== HTTP response when user logined
include::{snippets}/get-people/get-people_-when-user-logined/http-response.adoc[]

==== HTTP response when user not logined
include::{snippets}/get-people/get-people_-when-user-not-logined/http-response.adoc[]

==== Response fields
include::{snippets}/get-people/get-people_-when-user-not-logined/response-fields-data.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import es.princip.getp.domain.people.dto.response.people.PublicDetailPeopleResponse;
import es.princip.getp.domain.people.service.PeopleService;
import es.princip.getp.global.security.details.PrincipalDetails;
import es.princip.getp.global.support.ControllerSupport;
import es.princip.getp.global.util.ApiResponse;
import es.princip.getp.global.util.ApiResponse.ApiSuccessResult;
import jakarta.validation.Valid;
Expand All @@ -19,9 +20,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
Expand All @@ -30,7 +29,7 @@
@RestController
@RequestMapping("/people")
@RequiredArgsConstructor
public class PeopleController {
public class PeopleController extends ControllerSupport {

private final PeopleService peopleService;

Expand Down Expand Up @@ -59,15 +58,12 @@ public ResponseEntity<ApiSuccessResult<CreatePeopleResponse>> createPeople(
*/
@GetMapping("/{peopleId}")
public ResponseEntity<ApiSuccessResult<?>> getPeople(@PathVariable Long peopleId) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication.isAuthenticated()) {
DetailPeopleResponse response = DetailPeopleResponse.from(peopleService.getByPeopleId(peopleId), null);
return ResponseEntity.ok().body(ApiResponse.success(HttpStatus.OK, response));
}
else{
PublicDetailPeopleResponse response = PublicDetailPeopleResponse.from(peopleService.getByPeopleId(peopleId), null);
if (isAuthenticated()) {
DetailPeopleResponse response = DetailPeopleResponse.from(peopleService.getByPeopleId(peopleId));
return ResponseEntity.ok().body(ApiResponse.success(HttpStatus.OK, response));
}
PublicDetailPeopleResponse response = PublicDetailPeopleResponse.from(peopleService.getByPeopleId(peopleId));
return ResponseEntity.ok().body(ApiResponse.success(HttpStatus.OK, response));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class People extends BaseTimeEntity {
private Member member;

@OneToOne(mappedBy = "people")
private PeopleProfile peopleProfile;
private PeopleProfile profile;

@Builder
public People(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import es.princip.getp.domain.people.domain.entity.People;
import es.princip.getp.domain.people.domain.enums.PeopleType;
import es.princip.getp.domain.people.dto.response.peopleProfile.DetailPeopleProfileResponse;
import lombok.Builder;

@Builder
public record DetailPeopleResponse(
Long peopleId,
String nickname,
Expand All @@ -12,13 +14,13 @@ public record DetailPeopleResponse(
DetailPeopleProfileResponse profile
) {

public static DetailPeopleResponse from(People people, DetailPeopleProfileResponse profile) {
return new DetailPeopleResponse(
people.getPeopleId(),
people.getNickname(),
people.getPeopleType(),
people.getProfileImageUri(),
profile
);
public static DetailPeopleResponse from(People people) {
return DetailPeopleResponse.builder()
.peopleId(people.getPeopleId())
.nickname(people.getNickname())
.peopleType(people.getPeopleType())
.profileImageUri(people.getProfileImageUri())
.profile(DetailPeopleProfileResponse.from(people.getProfile()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
import es.princip.getp.domain.people.domain.entity.People;
import es.princip.getp.domain.people.domain.enums.PeopleType;
import es.princip.getp.domain.people.dto.response.peopleProfile.PublicDetailPeopleProfileResponse;
import jakarta.validation.Valid;
import lombok.Builder;

@Builder
public record PublicDetailPeopleResponse(
Long peopleId,
String nickname,
PeopleType peopleType,
@Valid PublicDetailPeopleProfileResponse profile
String profileImageUri,
PublicDetailPeopleProfileResponse profile
) {

public static PublicDetailPeopleResponse from(
People people,
PublicDetailPeopleProfileResponse profile
) {
return new PublicDetailPeopleResponse(
people.getPeopleId(),
people.getNickname(),
people.getPeopleType(),
profile
);
public static PublicDetailPeopleResponse from(People people) {
return PublicDetailPeopleResponse.builder()
.peopleId(people.getPeopleId())
.nickname(people.getNickname())
.peopleType(people.getPeopleType())
.profileImageUri(people.getProfileImageUri())
.profile(PublicDetailPeopleProfileResponse.from(people.getProfile()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import es.princip.getp.domain.people.domain.values.Portfolio;
import es.princip.getp.global.domain.values.Hashtag;
import es.princip.getp.global.domain.values.TechStack;
import lombok.Builder;

import java.util.List;

@Builder
public record DetailPeopleProfileResponse(
String introduction,
String activityArea,
Expand All @@ -19,16 +22,15 @@ public record DetailPeopleProfileResponse(
) {

public static DetailPeopleProfileResponse from(final PeopleProfile peopleProfile) {
return new DetailPeopleProfileResponse(
peopleProfile.getIntroduction(),
peopleProfile.getActivityArea(),
peopleProfile.getTechStacks(),
peopleProfile.getEducation(),
peopleProfile.getHashtags(),
//TODO: 계산 프로퍼티 구현
0,
0,
peopleProfile.getPortfolios()
);
return DetailPeopleProfileResponse.builder()
.introduction(peopleProfile.getIntroduction())
.activityArea(peopleProfile.getActivityArea())
.techStacks(peopleProfile.getTechStacks())
.education(peopleProfile.getEducation())
.hashtags(peopleProfile.getHashtags())
.completedProjectsCount(0) //TODO: 완수한 프로젝트 수 계산
.interestsCount(0) //TODO: 받은 관심 수 계산
.portfolios(peopleProfile.getPortfolios())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

import es.princip.getp.domain.people.domain.entity.PeopleProfile;
import es.princip.getp.global.domain.values.Hashtag;
import lombok.Builder;

import java.util.List;

@Builder
public record PublicDetailPeopleProfileResponse(
List<Hashtag> hashtags,
Integer completedProjectsCount,
Integer interestsCount
) {

public static PublicDetailPeopleProfileResponse from(final PeopleProfile peopleProfile) {
return new PublicDetailPeopleProfileResponse(
peopleProfile.getHashtags(),
//TODO: 계산 프로퍼티 구현
0,
0
);
return PublicDetailPeopleProfileResponse.builder()
.hashtags(peopleProfile.getHashtags())
.completedProjectsCount(0) //TODO: 완수한 프로젝트 수 계산
.interestsCount(0) //TODO: 받은 관심 수 계산
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package es.princip.getp.domain.people.repository;

import static com.querydsl.core.types.Order.ASC;
import static com.querydsl.core.types.Order.DESC;
import static es.princip.getp.domain.people.domain.entity.QPeople.people;
import static es.princip.getp.domain.people.domain.entity.QPeopleHashtag.peopleHashtag;
import static es.princip.getp.domain.people.domain.entity.QPeopleProfile.peopleProfile;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.OrderSpecifier;
import es.princip.getp.domain.people.domain.entity.People;
Expand All @@ -15,13 +10,20 @@
import es.princip.getp.domain.people.dto.response.peopleProfile.CardPeopleProfileResponse;
import es.princip.getp.global.domain.values.Hashtag;
import es.princip.getp.global.support.QueryDslRepositorySupport;
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;

import java.util.ArrayList;
import java.util.List;

import static com.querydsl.core.types.Order.ASC;
import static com.querydsl.core.types.Order.DESC;
import static es.princip.getp.domain.people.domain.entity.QPeople.people;
import static es.princip.getp.domain.people.domain.entity.QPeopleHashtag.peopleHashtag;
import static es.princip.getp.domain.people.domain.entity.QPeopleProfile.peopleProfile;

public class PeopleQueryDslRepositoryImpl extends QueryDslRepositorySupport implements
PeopleQueryDslRepository {

Expand Down Expand Up @@ -72,7 +74,7 @@ public List<CardPeopleResponse> getCardPeopleContent_cursor(Long lastPeopleId, i
peopleProfile.activityArea
)
.from(people)
.join(people.peopleProfile, peopleProfile)
.join(people.profile, peopleProfile)
.where(lastPeopleId == null ? null : people.peopleId.lt(lastPeopleId))
.orderBy(people.peopleId.desc())
.limit(size)
Expand All @@ -99,7 +101,7 @@ private List<CardPeopleResponse> getCardPeopleContent(Pageable pageable) {
peopleProfile.activityArea
)
.from(people)
.join(people.peopleProfile, peopleProfile)
.join(people.profile, peopleProfile)
.orderBy(getPeopleOrderSpecifiers(pageable.getSort()))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package es.princip.getp.global.support;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public abstract class ControllerSupport {

public boolean isAuthenticated() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication != null && authentication.isAuthenticated();
}
}
Loading

0 comments on commit 88b1011

Please sign in to comment.