Skip to content

Commit

Permalink
Merge pull request #65 from HongDam-org/feat/plan-service-test
Browse files Browse the repository at this point in the history
[FEAT] plan service test
  • Loading branch information
ohksj77 authored Nov 20, 2023
2 parents 7fe1b12 + 1e67206 commit 49d495e
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ public Group(String name, String groupImage, UUID leaderId) {
this.groupImage = groupImage;
this.leaderId = leaderId;
}

public void addToGroup(final Plan plan) {
this.groupPlans.add(plan);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,12 @@
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
import org.mapstruct.Named;

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface PlaceMapper {
@Mapping(target = "distance", source = "distance", qualifiedByName = "convertInteger")
@Mapping(target = "distance")
Place toEntity(PlaceDetails detail);

@Mapping(target = "distance", source = "distance", qualifiedByName = "convertString")
@Mapping(target = "distance")
PlaceDetails toPlaceResponse(Place place);

@Named("convertInteger")
default Integer convertInteger(String distance) {
return Integer.parseInt(distance);
}

@Named("convertString")
default String convertString(Integer distance) {
return String.valueOf(distance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ private URI getUri(final SearchDestinationRequest request, final UriBuilder uriB
.queryParam("size", MAX_SIZE_PER_REQUEST);

final CategoryGroupCode categoryGroupCode = request.getCategoryGroupCode();
final String x = request.getX();
final String y = request.getY();
final Double x = request.getX();
final Double y = request.getY();

if (x != null && y != null) {
builder.queryParam("x", x).queryParam("y", y);
builder.queryParam("x", Double.toString(x)).queryParam("y", Double.toString(y));
}
if (categoryGroupCode.isNone()) {
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
@AllArgsConstructor
public class PlaceDetails {
private String placeName;
private String distance;
private Integer distance;
private String placeUrl;
private String categoryName;
private String addressName;
private String roadAddressName;
private CategoryGroupCode categoryGroupCode;
private String x;
private String y;
private Double x;
private Double y;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
@AllArgsConstructor
public class SearchDestinationRequest {
private String query;
private String x;
private String y;
private Double x;
private Double y;
private Integer page;
private CategoryGroupCode categoryGroupCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.twtw.backend.domain.group.entity.Group;
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.domain.place.entity.Place;
import com.twtw.backend.domain.plan.exception.InvalidPlanMemberException;
import com.twtw.backend.global.audit.AuditListener;
import com.twtw.backend.global.audit.Auditable;
import com.twtw.backend.global.audit.BaseTime;
Expand Down Expand Up @@ -72,15 +73,22 @@ public void addMember(final Member member) {
}

public void deleteMember(final Member member) {
this.planMembers.remove(member);
this.planMembers.remove(findPlanMember(member));
}

private PlanMember findPlanMember(final Member member) {
return this.planMembers.stream()
.filter(planMember -> planMember.hasSameMember(member))
.findAny()
.orElseThrow(InvalidPlanMemberException::new);
}

public void addPlace(final Place place) {
this.place = place;
}

public void addGroup(Group group) {
public void addGroup(final Group group) {
this.group = group;
group.getGroupPlans().add(this);
this.group.addToGroup(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ public PlanMember(final Plan plan, final Member member) {
this.plan = plan;
this.member = member;
}

public boolean hasSameMember(final Member member) {
return this.member.equals(member);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.twtw.backend.domain.plan.exception;

public class InvalidPlanMemberException extends IllegalArgumentException {

private static final String MESSAGE = "계획에 추가되지 않은 멤버입니다.";

public InvalidPlanMemberException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,24 @@ void searchSurroundPlace() throws Exception {
List.of(
new PlaceDetails(
"이디야커피 안성죽산점",
"435",
435,
"http://place.map.kakao.com/1562566188",
"음식점 > 카페 > 커피전문점 > 이디야커피",
"경기 안성시 죽산면 죽산리 118-3",
"경기 안성시 죽산면 죽주로 287-1",
CategoryGroupCode.CE7,
"127.426865189637",
"37.0764635355795"),
127.426865189637,
37.0764635355795),
new PlaceDetails(
"카페 온마이마인드",
"345",
345,
"https://place.map.kakao.com/1625295668",
"음식점 > 카페",
"경기 안성시 죽산면 죽산리 414",
"경기 안성시 죽산면 죽산초교길 36-4",
CategoryGroupCode.CE7,
"127.420430538256",
"37.0766874564297")),
127.420430538256,
37.0766874564297)),
false);
given(placeService.searchSurroundPlace(any())).willReturn(expected);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,24 @@ void searchPlanDestination() throws Exception {
List.of(
new PlaceDetails(
"이디야커피 안성죽산점",
"435",
435,
"http://place.map.kakao.com/1562566188",
"음식점 > 카페 > 커피전문점 > 이디야커피",
"경기 안성시 죽산면 죽산리 118-3",
"경기 안성시 죽산면 죽주로 287-1",
CategoryGroupCode.CE7,
"127.426865189637",
"37.0764635355795"),
127.426865189637,
37.0764635355795),
new PlaceDetails(
"카페 온마이마인드",
"345",
345,
"https://place.map.kakao.com/1625295668",
"음식점 > 카페",
"경기 안성시 죽산면 죽산리 414",
"경기 안성시 죽산면 죽산초교길 36-4",
CategoryGroupCode.CE7,
"127.420430538256",
"37.0766874564297")),
127.420430538256,
37.0766874564297)),
false);
given(planService.searchPlanDestination(any())).willReturn(expected);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.twtw.backend.domain.plan.service;

import static org.assertj.core.api.Assertions.assertThat;

import com.twtw.backend.domain.group.repository.GroupRepository;
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.domain.place.entity.CategoryGroupCode;
import com.twtw.backend.domain.plan.dto.client.SearchDestinationRequest;
import com.twtw.backend.domain.plan.dto.request.PlanMemberRequest;
import com.twtw.backend.domain.plan.dto.request.SavePlanRequest;
import com.twtw.backend.domain.plan.dto.response.PlanDestinationResponse;
import com.twtw.backend.domain.plan.dto.response.PlanInfoResponse;
import com.twtw.backend.domain.plan.dto.response.PlanResponse;
import com.twtw.backend.domain.plan.entity.Plan;
import com.twtw.backend.domain.plan.repository.PlanRepository;
import com.twtw.backend.fixture.group.GroupEntityFixture;
import com.twtw.backend.fixture.member.MemberEntityFixture;
import com.twtw.backend.fixture.place.PlaceDetailsFixture;
import com.twtw.backend.fixture.plan.PlanEntityFixture;
import com.twtw.backend.support.service.LoginTest;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Optional;
import java.util.UUID;

@DisplayName("PlanService의")
class PlanServiceTest extends LoginTest {

@Autowired private PlanService planService;
@Autowired private GroupRepository groupRepository;
@Autowired private PlanRepository planRepository;

@Test
@DisplayName("목적지 검색이 수행되는가")
void searchPlanDestination() {
// given
final SearchDestinationRequest given =
new SearchDestinationRequest("스타벅스", 123.321, 123.123, 1, CategoryGroupCode.CE7);

// when
final PlanDestinationResponse result = planService.searchPlanDestination(given);

// then
assertThat(result.getResults()).isNotNull();
}

@Test
@DisplayName("계획 저장이 수행되는가")
void savePlan() {
// given
final Plan plan = PlanEntityFixture.FIRST_PLACE.toEntity(loginUser);
final UUID groupId = groupRepository.save(GroupEntityFixture.HDJ_GROUP.toEntity()).getId();

// when
final PlanResponse planResponse =
planService.savePlan(
new SavePlanRequest(
groupId, PlaceDetailsFixture.FIRST_PLACE.toPlaceDetails()));

// then
final Optional<Plan> result = planRepository.findById(planResponse.getPlanId());
assertThat(result).isPresent();
}

@Test
@DisplayName("계획 참여가 수행되는가")
void joinPlan() {
// given
final Member member = memberRepository.save(MemberEntityFixture.FIRST_MEMBER.toEntity());
final Plan plan = PlanEntityFixture.FIRST_PLACE.toEntity(member);
final Plan savedPlan = planRepository.save(plan);
final UUID planId = savedPlan.getId();

// when
planService.joinPlan(new PlanMemberRequest(planId));

// then
final Plan result = planRepository.findById(planId).orElseThrow();
assertThat(result.getPlanMembers()).hasSize(2);
}

@Test
@DisplayName("계획 나가기가 수행되는가") // TODO: 계획에 1명 있는데 나가는 경우 생각해보기
void outPlan() {
// given
final Plan plan = PlanEntityFixture.SECOND_PLACE.toEntity(loginUser);
final UUID planId = planRepository.save(plan).getId();

// when
planService.outPlan(new PlanMemberRequest(planId));

// then
final Plan result = planRepository.findById(planId).orElseThrow();
assertThat(result.getPlanMembers()).isEmpty();
}

@Test
@DisplayName("PK로 계획 조회가 수행되는가")
void getPlanById() {
// given
final Plan plan = PlanEntityFixture.FIRST_PLACE.toEntity(loginUser);
final UUID planId = planRepository.save(plan).getId();

// when
final PlanInfoResponse result = planService.getPlanById(planId);

// then
assertThat(result.getPlanId()).isEqualTo(planId);
}

@Test
@DisplayName("삭제가 수행되는가")
void deletePlan() {
// given
final Plan plan = PlanEntityFixture.SECOND_PLACE.toEntity(loginUser);
final UUID planId = planRepository.save(plan).getId();

// when
planService.deletePlan(planId);

// then
assertThat(planRepository.findById(planId)).isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
package com.twtw.backend.fixture.place;

import com.twtw.backend.domain.place.entity.CategoryGroupCode;
import com.twtw.backend.domain.plan.dto.client.PlaceDetails;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum PlaceDetailsFixture {
;
FIRST_PLACE(
"스타벅스",
30,
"http://someUrlToPlaceDetails",
"카페",
"주소",
"도로명주소",
CategoryGroupCode.CE7,
123.123,
321.321),
SECOND_PLACE(
"별다방",
20,
"http://someUrlToPlaceInMap",
"숙박",
"밥좀주소",
"도로명좀주소",
CategoryGroupCode.AD5,
345.543,
543.345);

private final String placeName;
private final String distance;
private final Integer distance;
private final String placeUrl;
private final String categoryName;
private final String addressName;
private final String roadAddressName;
private final CategoryGroupCode categoryGroupCode;
private final Double x;
private final Double y;

public PlaceDetails toPlaceDetails() {
return new PlaceDetails(
placeName,
distance,
placeUrl,
categoryName,
addressName,
roadAddressName,
categoryGroupCode,
x,
y);
}
}

0 comments on commit 49d495e

Please sign in to comment.