Skip to content

Commit

Permalink
Merge pull request #118 from swm-nodriversomabus/BUS-201-User-check-e…
Browse files Browse the repository at this point in the history
…xception

feat(BE): 조회한 사용자가 없을 경우 디폴트 유저 반환 BUS-201-User-check-exception
  • Loading branch information
Lemonade255 authored Oct 18, 2023
2 parents 89095ad + a2edd20 commit 7db6790
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.List;
import java.util.Optional;

@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -68,11 +67,11 @@ public List<FindUserDto> getAll() {
/**
* ID가 userId인 사용자 조회
* @param userId (ID)
* @return Optional<FindUserDto>
* @return FindUserDto
*/
@Operation(summary = "Get user", description = "ID가 userId인 사용자를 조회한다.")
@GetMapping("/user/{userId}")
public Optional<FindUserDto> getUserById(@PathVariable String userId) {
public FindUserDto getUserById(@PathVariable String userId) {
return findUserUsecase.getUserById(userId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import com.example.api.user.dto.FindUserDto;

import java.util.List;
import java.util.Optional;

public interface FindUserUsecase {
List<FindUserDto> getAll();
Optional<FindUserDto> getUserById(String userId);
FindUserDto getUserById(String userId);
}
2 changes: 1 addition & 1 deletion src/main/java/com/example/api/user/dto/FindUserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class FindUserDto {
@NotNull
private Boolean blacklist;

@NotEmpty
@NotNull
private String stateMessage;

@NotNull
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/com/example/api/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.api.user.service;

import com.example.api.auth.dto.SecurityUserDto;
import com.example.api.common.exception.CustomException;
import com.example.api.common.type.ErrorCodeEnum;
import com.example.api.common.utils.CustomBase64Utils;
Expand All @@ -18,12 +17,15 @@
import com.example.api.user.application.port.in.SaveUserUsecase;
import com.example.api.user.dto.CreateUserDto;
import com.example.api.user.dto.UpdateUserDto;
import com.example.api.user.type.UserGenderEnum;
import com.example.api.user.type.UserRoleEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.example.api.user.dto.FindUserDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -57,13 +59,27 @@ public List<FindUserDto> getAll() {
}

@Override
public Optional<FindUserDto> getUserById(String userId) {
public FindUserDto getUserById(String userId) {
FindUserDto defaultUser = FindUserDto.builder()
.username("익명")
.gender(UserGenderEnum.None)
.age(30)
.phone("010-0000-0000")
.role(UserRoleEnum.User)
.blacklist(false)
.stateMessage("")
.mannerScore(30)
.createdAt(LocalDateTime.now())
.updatedAt(LocalDateTime.now())
.isActive(false)
.build();
try {
return findUserPort.getByUserId(UUID.fromString(userId))
.map(userMapper::toDto);
.map(userMapper::toDto)
.orElse(defaultUser);
} catch (IllegalArgumentException e) {
log.warn("Invalid userId: UUID transform failed.");
return Optional.empty();
return defaultUser;
}
}

Expand Down
20 changes: 5 additions & 15 deletions src/test/java/com/example/api/user/UserServiceTests.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.example.api.user;

import com.example.api.sms.application.port.out.CheckVerifiedPhonePort;
import com.example.api.social.application.port.out.FindSocialPort;
import com.example.api.user.adapter.out.persistence.UserMapperInterface;
import com.example.api.user.application.port.out.DeleteUserPort;
import com.example.api.user.application.port.out.FindUserPort;
import com.example.api.user.application.port.out.SaveUserPort;
import com.example.api.user.dto.CreateUserDto;
import com.example.api.user.dto.FindUserDto;
import com.example.api.user.dto.UpdateUserDto;
import com.example.api.user.service.UserService;
import com.example.api.user.type.UserGenderEnum;
Expand Down Expand Up @@ -36,14 +36,15 @@ public class UserServiceTests {
private DeleteUserPort deleteUserPort;
@Mock
private FindSocialPort findSocialPort;
@Mock
private CheckVerifiedPhonePort checkVerifiedPhonePort;
private CreateUserDto user1, user2, user3;
private FindUserDto mockUser;
private UpdateUserDto newUser;
private final String userUUID = "09a46fb0-2ae0-4a35-8aad-0a9e4311a1a3";

@BeforeEach
void beforeEach() {
userService = new UserService(userMapper, saveUserPort, findUserPort, deleteUserPort, findSocialPort);
userService = new UserService(userMapper, saveUserPort, findUserPort, deleteUserPort, findSocialPort, checkVerifiedPhonePort);
user1 = CreateUserDto.builder()
.username("Andrew")
.gender(UserGenderEnum.Male)
Expand Down Expand Up @@ -77,17 +78,6 @@ void beforeEach() {
.mannerScore(73)
.isActive(true)
.build();
mockUser = FindUserDto.builder()
.username("Andrew")
.gender(UserGenderEnum.Male)
.age(24)
.phone("010-9876-5432")
.role(UserRoleEnum.User)
.blacklist(false)
.stateMessage(" ")
.mannerScore(75)
.isActive(true)
.build();
newUser = UpdateUserDto.builder()
.username("Andrew")
.gender(UserGenderEnum.Male)
Expand Down Expand Up @@ -117,7 +107,7 @@ void getAllTest() {

@Test
void getUserByIdTest() {
userService.getUserById(userUUID).orElse(mockUser);
userService.getUserById(userUUID);
verify(findUserPort, times(1)).getByUserId(UUID.fromString(userUUID));
}

Expand Down

0 comments on commit 7db6790

Please sign in to comment.