Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Mapper 수정 + Service, Repository 테스트 작성 #77

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,4 @@ public ResponseEntity<ApiResponse<String>> signUpMember(
.headers(headers)
.body(ApiResponse.with(HttpStatus.OK, "회원가입 성공", serviceToken));
}

@PostMapping("/signup")
public ResponseEntity<ApiResponse<String>> signUpMember(
@RequestParam String email, @RequestBody MemberInfoRequest memberInfoRequest) {
return authService.register(
email, memberInfoRequest.deliveryName(), memberInfoRequest.phoneNumber());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ordertogether.team14_be.member.application.service;

import com.ordertogether.team14_be.auth.JwtUtil;
import com.ordertogether.team14_be.member.application.dto.MemberInfoResponse;
import com.ordertogether.team14_be.member.application.exception.NotFoundMember;
import com.ordertogether.team14_be.member.persistence.MemberRepository;
Expand Down Expand Up @@ -36,25 +37,6 @@ public MemberInfoResponse findMemberInfo(Long memberId) {
.build();
}

@Transactional(readOnly = true)
public Long getMemberId(String email) {
return memberRepository
.findByEmail(email)
.map(Member::getId)
.orElseThrow(() -> new NoSuchElementException("Member with email " + email + " not found"));
}

@Transactional(readOnly = true)
public MemberInfoResponse findMemberInfo(Long memberId) {
Member member = findMember(memberId);

return MemberInfoResponse.builder()
.deliveryName(member.getDeliveryName())
.phoneNumber(member.getPhoneNumber())
.point(member.getPoint())
.build();
}

@Transactional
public MemberInfoResponse modifyMember(Long memberId, String deliveryName, String phoneNumber) {
Member member = findMember(memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ public interface SpotMapper {

Spot toEntity(SpotDto spotDto, @MappingTarget Spot spot); // 생성 또는 수정할 때 사용

@BeanMapping(ignoreByDefault = false) // 자동 매핑 활성화
@Mapping(target = "category", expression = "java(spotDto.getCategory().getCode())")
SpotCreationResponse toSpotCreationResponse(SpotDto spotDto);

@BeanMapping(ignoreByDefault = false) // 자동 매핑 활성화
@Mapping(target = "category", expression = "java(spotDto.getCategory().getCode())")
SpotDetailResponse toSpotDetailResponse(SpotDto spotDto);

@BeanMapping(ignoreByDefault = false) // 자동 매핑 활성화
@Mapping(target = "category", expression = "java(spotDto.getCategory().getCode())")
SpotViewedResponse toSpotViewedResponse(SpotDto spotDto);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
import com.ordertogether.team14_be.spot.entity.Spot;
import com.ordertogether.team14_be.spot.enums.Category;
import com.ordertogether.team14_be.spot.exception.SpotNotFoundException;
import com.ordertogether.team14_be.spot.mapper.SpotMapper;
import com.ordertogether.team14_be.spot.repository.SimpleSpotRepository;
import com.ordertogether.team14_be.spot.repository.SpotRepository;
import java.math.BigDecimal;
import java.time.LocalTime;
import java.util.List;
import java.util.Optional;
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.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
Expand All @@ -27,6 +30,8 @@ class SpotRepositoryTest {

@Mock private SimpleSpotRepository simpleSpotRepository;

@Spy private SpotMapper spotMapper;

private BigDecimal lat;
private BigDecimal lng;
private Spot spot;
Expand Down Expand Up @@ -87,10 +92,39 @@ void findByIdAndIsDeletedFalse_success() {
@Test
void findByIdAndIsDeletedFalse_exception() {
when(simpleSpotRepository.findByIdAndIsDeletedFalse(1L)).thenReturn(Optional.empty());
// todo: 테스트 추가
assertThat(spotRepository.findByIdAndIsDeletedFalse(1L)).isNull();

assertThrows(SpotNotFoundException.class, () -> spotRepository.findByIdAndIsDeletedFalse(1L));
}

// todo : 테스트 추가
@Test
void findByLatAndLngAndIsDeletedFalse_success() {
when(simpleSpotRepository.findByLatAndLngAndIsDeletedFalse(lat, lng)).thenReturn(List.of(spot));

List<SpotDto> spotDto = spotRepository.findByLatAndLngAndIsDeletedFalse(lat, lng);
assertThat(spotDto.getFirst().getId()).isEqualTo(1L);
assertThat(spotDto.getFirst().getLat()).isEqualTo(lat);
assertThat(spotDto.getFirst().getLng()).isEqualTo(lng);
assertThat(spotDto.getFirst().getCategory()).isEqualTo(Category.BURGER);
assertThat(spotDto.getFirst().getStoreName()).isEqualTo("맥도날드");
assertThat(spotDto.getFirst().getMinimumOrderAmount()).isEqualTo(12000);
assertThat(spotDto.getFirst().getDeadlineTime()).isEqualTo(LocalTime.of(12, 0, 0));
assertThat(spotDto.getFirst().getPickUpLocation()).isEqualTo("픽업위치");
assertThat(spotDto.getFirst().getCreatedBy()).isEqualTo(1L);
}

@Test
void findBygeoHash_success() {
when(simpleSpotRepository.findByGeoHash("9q8yyz")).thenReturn(List.of(spot));

List<SpotDto> spotDto = spotRepository.findBygeoHash("9q8yyz");
assertThat(spotDto.getFirst().getId()).isEqualTo(1L);
assertThat(spotDto.getFirst().getLat()).isEqualTo(lat);
assertThat(spotDto.getFirst().getLng()).isEqualTo(lng);
assertThat(spotDto.getFirst().getCategory()).isEqualTo(Category.BURGER);
assertThat(spotDto.getFirst().getStoreName()).isEqualTo("맥도날드");
assertThat(spotDto.getFirst().getMinimumOrderAmount()).isEqualTo(12000);
assertThat(spotDto.getFirst().getDeadlineTime()).isEqualTo(LocalTime.of(12, 0, 0));
assertThat(spotDto.getFirst().getPickUpLocation()).isEqualTo("픽업위치");
assertThat(spotDto.getFirst().getCreatedBy()).isEqualTo(1L);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.ordertogether.team14_be.spot.entity.Spot;
import com.ordertogether.team14_be.spot.enums.Category;
import com.ordertogether.team14_be.spot.exception.NotSpotMasterException;
import com.ordertogether.team14_be.spot.mapper.SpotMapper;
import com.ordertogether.team14_be.spot.repository.SpotRepository;
import com.ordertogether.team14_be.spot.service.SpotService;
import java.math.BigDecimal;
Expand All @@ -22,16 +21,13 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class SpotServiceTest {

@Mock private SpotRepository spotRepository;

@Spy private SpotMapper spotMapper;

@InjectMocks private SpotService spotService;

private BigDecimal lat;
Expand Down Expand Up @@ -65,6 +61,7 @@ void getSpot_success() {
List<SpotViewedResponse> result = spotService.getSpot(lat, lng);

assertNotNull(result);
assertEquals(1, result.size());
assertEquals("패스트푸드", result.getFirst().category());
assertEquals("맥도날드", result.getFirst().storeName());
assertEquals(12000, result.getFirst().minimumOrderAmount());
Expand All @@ -74,16 +71,19 @@ void getSpot_success() {

@Test
void createSpot_success() {
SpotCreationResponse expectedResponse =
new SpotCreationResponse(1L, "패스트푸드", "맥도날드", 12000, "픽업위치", LocalTime.of(12, 0, 0));

// Mocking the repository and mapper responses
when(spotRepository.save(any(Spot.class))).thenReturn(spotDto);

SpotCreationResponse response = spotService.createSpot(spotDto);

assertNotNull(response);
assertEquals(expectedResponse, response);
System.out.println(response);
assertEquals(spotDto.getId(), response.id());
assertEquals(spotDto.getCategory().getCode(), response.category());
assertEquals(spotDto.getStoreName(), response.storeName());
assertEquals(spotDto.getMinimumOrderAmount(), response.minimumOrderAmount());
assertEquals(spotDto.getPickUpLocation(), response.pickUpLocation());
assertEquals(spotDto.getDeadlineTime(), response.deadlineTime());

verify(spotRepository).save(any(Spot.class));
}

Expand Down