Skip to content

Commit

Permalink
feat: User Entity 스펙변경에 따른 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
masiljangajji committed Oct 11, 2024
1 parent 7946b7d commit c342502
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
16 changes: 7 additions & 9 deletions src/main/java/my/music/note/back/user/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package my.music.note.back.user.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import my.music.note.back.user.dto.request.DeleteAccountRequest;
import my.music.note.back.user.dto.request.LoginOrRegisterRequest;
import my.music.note.back.user.dto.request.ModifyNameRequest;
import my.music.note.back.user.dto.response.FindUserResponse;
Expand All @@ -15,8 +13,8 @@
@NoArgsConstructor
public class User {

private User(String userName, LocalDateTime createdAt, String email, String provider, String providerId, boolean isAdmin, boolean isDeleted) {
this.userName = userName;
private User(String name, LocalDateTime createdAt, String email, String provider, String providerId, boolean isAdmin, boolean isDeleted) {
this.name = name;
this.createdAt = createdAt;
this.email = email;
this.provider = provider;
Expand All @@ -27,9 +25,9 @@ private User(String userName, LocalDateTime createdAt, String email, String prov

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long userId;
Long id;

String userName;
String name;

LocalDateTime createdAt;

Expand All @@ -44,7 +42,7 @@ private User(String userName, LocalDateTime createdAt, String email, String prov
Boolean isDeleted;

public Long getUserId() {
return userId;
return id;
}

public Boolean getAdmin() {
Expand All @@ -56,15 +54,15 @@ public static User of(LoginOrRegisterRequest request) {
}

public FindUserResponse convertToFindUserResponse() {
return new FindUserResponse(this.userName, this.email, this.isAdmin);
return new FindUserResponse(this.name, this.email, this.isAdmin);
}

public void deleteAccount() {
this.isDeleted = true;
}

public void modifyName(ModifyNameRequest request) {
this.userName = request.name();
this.name = request.name();
}


Expand Down
16 changes: 13 additions & 3 deletions src/main/java/my/music/note/back/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import my.music.note.back.user.repository.UserRepository;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@Transactional
@AllArgsConstructor
Expand All @@ -30,19 +32,27 @@ public User loginOrRegister(LoginOrRegisterRequest request) {
}

public void deleteUser(DeleteAccountRequest request) {
User user = userRepository.findByUserId(request.userId());
Optional<User> optionalUser = userRepository.findById(request.id());


User user = optionalUser.orElseThrow(RuntimeException::new);

user.deleteAccount();
userRepository.save(user);
}

public void modifyName(ModifyNameRequest request, Long userId) {
User user = userRepository.findByUserId(userId);

Optional<User> optionalUser = userRepository.findById(userId);
User user = optionalUser.orElseThrow(RuntimeException::new);

user.modifyName(request);
userRepository.save(user);
}

public FindUserResponse findUser(FindUserRequest request) {
User user = userRepository.findByUserId(request.userId());
Optional<User> optionalUser = userRepository.findById(request.id());
User user = optionalUser.orElseThrow(RuntimeException::new);
return user.convertToFindUserResponse();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void givenToken_whenCallFindUserInfo_thenReturnOK() throws Exception {
.cookie(tokenCookie)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.userName").value("test-user"))
.andExpect(jsonPath("$.name").value("test-user"))
.andExpect(jsonPath("$.email").value("[email protected]"))
.andExpect(jsonPath("$.isAdmin").value(false));

Expand Down
20 changes: 11 additions & 9 deletions src/test/java/my/music/note/back/user/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Optional;

import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -60,13 +62,13 @@ void givenLoginOrRegisterRequest_whenCallLoginOrRegisterAndNeverLoginBefore_then
@DisplayName("회원탈퇴 성공")
void givenDeleteAccountRequest_whenCallDeleteUser_thenReturnVoid(@Mock DeleteAccountRequest deleteAccountRequest, @Mock User user) {

when(deleteAccountRequest.userId()).thenReturn(1L);
when(userRepository.findByUserId(anyLong())).thenReturn(user);
when(deleteAccountRequest.id()).thenReturn(1L);
when(userRepository.findById(anyLong())).thenReturn(Optional.of(user));
doNothing().when(user).deleteAccount();

userService.deleteUser(deleteAccountRequest);

verify(userRepository, times(1)).findByUserId(1L);
verify(userRepository, times(1)).findById(1L);
verify(user, times(1)).deleteAccount();
verify(userRepository, times(1)).save(user);
}
Expand All @@ -75,12 +77,12 @@ void givenDeleteAccountRequest_whenCallDeleteUser_thenReturnVoid(@Mock DeleteAcc
@DisplayName("이름변경 성공")
void givenModifyNameRequestAndUserId_whenCallModifyName_thenReturnVoid(@Mock ModifyNameRequest modifyNameRequest, @Mock User user) {

when(userRepository.findByUserId(1L)).thenReturn(user);
when(userRepository.findById(1L)).thenReturn(Optional.of(user));
doNothing().when(user).modifyName(modifyNameRequest);

userService.modifyName(modifyNameRequest, 1L);

verify(userRepository, times(1)).findByUserId(1L);
verify(userRepository, times(1)).findById(1L);
verify(user, times(1)).modifyName(modifyNameRequest);
verify(userRepository, times(1)).save(user);

Expand All @@ -90,14 +92,14 @@ void givenModifyNameRequestAndUserId_whenCallModifyName_thenReturnVoid(@Mock Mod
@DisplayName("유저조회 성공")
void givenFindUserRequest_whenCallFindUser_thenReturnFindUserResponse(@Mock FindUserRequest request, @Mock User user, @Mock FindUserResponse response) {

when(request.userId()).thenReturn(1L);
when(userRepository.findByUserId(1L)).thenReturn(user);
when(request.id()).thenReturn(1L);
when(userRepository.findById(1L)).thenReturn(Optional.of(user));
when(user.convertToFindUserResponse()).thenReturn(response);

userService.findUser(request);

verify(request, times(1)).userId();
verify(userRepository, times(1)).findByUserId(1L);
verify(request, times(1)).id();
verify(userRepository, times(1)).findById(1L);
verify(user, times(1)).convertToFindUserResponse();

}
Expand Down

0 comments on commit c342502

Please sign in to comment.