Skip to content

Commit

Permalink
[#28] 동네 인증 Controller 구현 및 Service 수정 (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Srltas authored Oct 6, 2024
1 parent 9f912c1 commit c126c51
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.srltas.runtogether.adapter.in;

import static com.srltas.runtogether.adapter.in.web.dto.mapper.NeighborhoodVerificationMapper.*;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.srltas.runtogether.adapter.in.web.dto.NeighborhoodVerificationRequest;
import com.srltas.runtogether.application.port.in.NeighborhoodVerificationCommand;
import com.srltas.runtogether.application.port.in.NeighborhoodVerificationResponse;
import com.srltas.runtogether.application.port.in.NeighborhoodVerificationUseCase;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class NeighborhoodVerificationController {

private final NeighborhoodVerificationUseCase neighborhoodVerificationUseCase;

@PostMapping("/neighborhood/verification")
public ResponseEntity<NeighborhoodVerificationResponse> verifyNeighborhood(
@RequestBody @Valid NeighborhoodVerificationRequest neighborhoodVerificationRequest,
@SessionAttribute(name = "login_user_id", required = false) Long userId) {
NeighborhoodVerificationCommand neighborhoodVerificationCommand = toCommand(neighborhoodVerificationRequest);

NeighborhoodVerificationResponse response = neighborhoodVerificationUseCase.verifyAndRegisterNeighborhood(
userId, neighborhoodVerificationCommand);

return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
package com.srltas.runtogether.adapter.in.web.dto;

import org.hibernate.validator.constraints.Range;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.PositiveOrZero;

public record NeighborhoodVerificationRequest(

@NotNull
@Range(min = -90, max = 90, message="위도는 -90에서 90 사이여야 합니다.")
double latitude,

@NotNull
@Range(min = -180, max = 180, message="경도는 -180에서 180 사이여야 합니다.")
double longitude,

@NotNull
@PositiveOrZero(message = "동네 ID는 0 이상의 값이어야 합니다.")
int neighborhoodId
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import static com.srltas.runtogether.application.mappper.LocationMapper.*;

import java.time.LocalDateTime;
import java.util.UUID;

import org.springframework.stereotype.Service;

import com.srltas.runtogether.application.port.in.NeighborhoodVerificationCommand;
import com.srltas.runtogether.application.port.in.NeighborhoodVerificationResponse;
import com.srltas.runtogether.application.port.in.NeighborhoodVerificationUseCase;
import com.srltas.runtogether.domain.exception.NeighborhoodNotFoundException;
import com.srltas.runtogether.domain.exception.OutOfNeighborhoodBoundaryException;
Expand All @@ -14,14 +20,16 @@

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class NeighborhoodVerificationService implements NeighborhoodVerificationUseCase {

private final NeighborhoodRepository neighborhoodRepository;
private final UserRepository userRepository;

@Override
public void verifyAndRegisterNeighborhood(long userId, NeighborhoodVerificationCommand command) {
public NeighborhoodVerificationResponse verifyAndRegisterNeighborhood(long userId,
NeighborhoodVerificationCommand command) {
Neighborhood neighborhood = neighborhoodRepository.findById(command.neighborhoodId())
.orElseThrow(NeighborhoodNotFoundException::new);

Expand All @@ -31,6 +39,12 @@ public void verifyAndRegisterNeighborhood(long userId, NeighborhoodVerificationC
User user = userRepository.findById(userId);
user.addVerifiedNeighborhood(neighborhood);
userRepository.save(user);

return NeighborhoodVerificationResponse.builder()
.verifyId(UUID.randomUUID().toString())
.verified(true)
.verificationTime(LocalDateTime.now().toString())
.build();
} else {
throw new OutOfNeighborhoodBoundaryException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.srltas.runtogether.application.port.in;

import lombok.Builder;

@Builder
public record NeighborhoodVerificationResponse(
String verifyId,
boolean verified,
String verificationTime
) {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.srltas.runtogether.application.port.in;

public interface NeighborhoodVerificationUseCase {
void verifyAndRegisterNeighborhood(long userId, NeighborhoodVerificationCommand neighborhoodVerificationCommand);
NeighborhoodVerificationResponse verifyAndRegisterNeighborhood(long userId,
NeighborhoodVerificationCommand neighborhoodVerificationCommand);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.Optional;

import org.springframework.stereotype.Repository;

@Repository
public interface NeighborhoodRepository {
Optional<Neighborhood> findById(int neighborhoodId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.srltas.runtogether.domain.model.user;

import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository {
User findById(long id);
void save(User user);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.srltas.runtogether.adapter.in;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.BDDMockito.*;

import java.time.LocalDateTime;
import java.util.UUID;
import java.util.stream.Stream;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.srltas.runtogether.adapter.in.web.dto.NeighborhoodVerificationRequest;
import com.srltas.runtogether.application.port.in.NeighborhoodVerificationCommand;
import com.srltas.runtogether.application.port.in.NeighborhoodVerificationResponse;
import com.srltas.runtogether.application.port.in.NeighborhoodVerificationUseCase;

@ExtendWith(MockitoExtension.class)
class NeighborhoodVerificationControllerTest {

@Mock
private NeighborhoodVerificationUseCase neighborhoodVerificationUseCase;

@InjectMocks
private NeighborhoodVerificationController neighborhoodVerificationController;

@ParameterizedTest
@MethodSource("provideNeighborhoodVerificationRequests")
@DisplayName("verifyNeighborhood 메서드에 대한 Parameterized 테스트")
void verifyNeighborhood_ShouldReturnOkResponse(NeighborhoodVerificationRequest request, Long userId) {
// given
NeighborhoodVerificationCommand command = new NeighborhoodVerificationCommand(request.latitude(),
request.longitude(), request.neighborhoodId());
NeighborhoodVerificationResponse expectedResponse = new NeighborhoodVerificationResponse(
UUID.randomUUID().toString(), true, LocalDateTime.now().toString());

given(neighborhoodVerificationUseCase.verifyAndRegisterNeighborhood(userId, command)).willReturn(
expectedResponse);

// when
ResponseEntity<NeighborhoodVerificationResponse> response = neighborhoodVerificationController.verifyNeighborhood(
request, userId);

// then
verify(neighborhoodVerificationUseCase).verifyAndRegisterNeighborhood(userId, command);
assertThat(response.getStatusCode(), is(HttpStatus.OK));
assertThat(response.getBody(), is(expectedResponse));
}

static Stream<Arguments> provideNeighborhoodVerificationRequests() {
return Stream.of(
Arguments.of(new NeighborhoodVerificationRequest(37.579617, 126.977041, 1), 100L),
Arguments.of(new NeighborhoodVerificationRequest(37.556201, 126.972286, 2), 101L),
Arguments.of(new NeighborhoodVerificationRequest(37.497911, 127.027618, 3), 102L));
}
}

0 comments on commit c126c51

Please sign in to comment.