From 659af6f2c2e710cb0bd8cb9a68d0949b7e61f645 Mon Sep 17 00:00:00 2001 From: jo-bata Date: Fri, 7 Jun 2019 23:38:32 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[#14][ADD]=20=EC=9C=A0=EC=A0=80=20=ED=9A=8C?= =?UTF-8?q?=EC=9B=90=EA=B0=80=EC=9E=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../meetingroom/MeetingroomApplication.java | 5 --- .../java/bom/bom/meetingroom/user/User.java | 19 +++++++++ .../bom/bom/meetingroom/user/UserDto.java | 16 ++++++++ .../bom/meetingroom/user/UserRepository.java | 7 ++++ .../bom/bom/meetingroom/user/UserService.java | 5 +++ .../bom/meetingroom/user/UserServiceImpl.java | 33 +++++++++++++++ .../MeetingroomApplicationTests.java | 1 + .../bom/meetingroom/user/UserServiceTest.java | 40 +++++++++++++++++++ .../bom/bom/meetingroom/user/UserTest.java | 24 +++++++++++ 9 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 src/main/java/bom/bom/meetingroom/user/User.java create mode 100644 src/main/java/bom/bom/meetingroom/user/UserDto.java create mode 100644 src/main/java/bom/bom/meetingroom/user/UserRepository.java create mode 100644 src/main/java/bom/bom/meetingroom/user/UserService.java create mode 100644 src/main/java/bom/bom/meetingroom/user/UserServiceImpl.java create mode 100644 src/test/java/bom/bom/meetingroom/user/UserServiceTest.java create mode 100644 src/test/java/bom/bom/meetingroom/user/UserTest.java diff --git a/src/main/java/bom/bom/meetingroom/MeetingroomApplication.java b/src/main/java/bom/bom/meetingroom/MeetingroomApplication.java index 470b799..aa6c6ce 100644 --- a/src/main/java/bom/bom/meetingroom/MeetingroomApplication.java +++ b/src/main/java/bom/bom/meetingroom/MeetingroomApplication.java @@ -5,12 +5,7 @@ @SpringBootApplication public class MeetingroomApplication { - public static void main(String[] args) { SpringApplication.run(MeetingroomApplication.class, args); - - } - - } diff --git a/src/main/java/bom/bom/meetingroom/user/User.java b/src/main/java/bom/bom/meetingroom/user/User.java new file mode 100644 index 0000000..4ae4750 --- /dev/null +++ b/src/main/java/bom/bom/meetingroom/user/User.java @@ -0,0 +1,19 @@ +package bom.bom.meetingroom.user; + +import lombok.Builder; +import lombok.Getter; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Builder +@Getter +@Entity +public class User { + @Id + private String id; + private String pw; + private String name; + private String position; + private String department; +} diff --git a/src/main/java/bom/bom/meetingroom/user/UserDto.java b/src/main/java/bom/bom/meetingroom/user/UserDto.java new file mode 100644 index 0000000..011dad5 --- /dev/null +++ b/src/main/java/bom/bom/meetingroom/user/UserDto.java @@ -0,0 +1,16 @@ +package bom.bom.meetingroom.user; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +public class UserDto { + private String id; + private String pw; + private String name; + private String position; + private String department; +} diff --git a/src/main/java/bom/bom/meetingroom/user/UserRepository.java b/src/main/java/bom/bom/meetingroom/user/UserRepository.java new file mode 100644 index 0000000..75435d0 --- /dev/null +++ b/src/main/java/bom/bom/meetingroom/user/UserRepository.java @@ -0,0 +1,7 @@ +package bom.bom.meetingroom.user; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { + +} diff --git a/src/main/java/bom/bom/meetingroom/user/UserService.java b/src/main/java/bom/bom/meetingroom/user/UserService.java new file mode 100644 index 0000000..fb6ed59 --- /dev/null +++ b/src/main/java/bom/bom/meetingroom/user/UserService.java @@ -0,0 +1,5 @@ +package bom.bom.meetingroom.user; + +public interface UserService { + void signUp(UserDto userDto); +} diff --git a/src/main/java/bom/bom/meetingroom/user/UserServiceImpl.java b/src/main/java/bom/bom/meetingroom/user/UserServiceImpl.java new file mode 100644 index 0000000..4f0fc23 --- /dev/null +++ b/src/main/java/bom/bom/meetingroom/user/UserServiceImpl.java @@ -0,0 +1,33 @@ +package bom.bom.meetingroom.user; + +import org.springframework.stereotype.Service; + +import java.util.Optional; + +@Service +public class UserServiceImpl implements UserService { + private final UserRepository userRepository; + + public UserServiceImpl(UserRepository userRepository) { + this.userRepository = userRepository; + } + + @Override + public void signUp(UserDto userDto) { + if (isUserId(userDto.getId())) { + throw new NullPointerException(); + } + User user = User.builder() + .id(userDto.getId()) + .pw(userDto.getPw()) + .name(userDto.getName()) + .position(userDto.getPosition()) + .department(userDto.getDepartment()) + .build(); + userRepository.save(user); + } + + private boolean isUserId(String id) { + return Optional.ofNullable(userRepository.findById(id)).isPresent(); + } +} diff --git a/src/test/java/bom/bom/meetingroom/MeetingroomApplicationTests.java b/src/test/java/bom/bom/meetingroom/MeetingroomApplicationTests.java index 7909c6e..ebf35de 100644 --- a/src/test/java/bom/bom/meetingroom/MeetingroomApplicationTests.java +++ b/src/test/java/bom/bom/meetingroom/MeetingroomApplicationTests.java @@ -3,6 +3,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) diff --git a/src/test/java/bom/bom/meetingroom/user/UserServiceTest.java b/src/test/java/bom/bom/meetingroom/user/UserServiceTest.java new file mode 100644 index 0000000..c2f0df5 --- /dev/null +++ b/src/test/java/bom/bom/meetingroom/user/UserServiceTest.java @@ -0,0 +1,40 @@ +package bom.bom.meetingroom.user; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@SpringBootTest +@RunWith(SpringRunner.class) +@ActiveProfiles(value = "test") +public class UserServiceTest { + @Autowired + UserService userService; + + @MockBean + UserRepository userRepository; + + @Test + public void testSignUp() { + when(userRepository.findById(any())).thenReturn(null); + UserDto userDto = new UserDto("jobata", "1234", "조현우", "부팀장", "백엔드"); + userService.signUp(userDto); + } + + @Test(expected = NullPointerException.class) + public void testSignUpFail() { + when(userRepository.findById(any())).thenReturn(Optional.of(User.builder().id("jobata").build())); + UserDto userDto = new UserDto("jobata", "1234", "조현우", "부팀장", "백엔드"); + userService.signUp(userDto); + } +} diff --git a/src/test/java/bom/bom/meetingroom/user/UserTest.java b/src/test/java/bom/bom/meetingroom/user/UserTest.java new file mode 100644 index 0000000..7a50829 --- /dev/null +++ b/src/test/java/bom/bom/meetingroom/user/UserTest.java @@ -0,0 +1,24 @@ +package bom.bom.meetingroom.user; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest +@RunWith(SpringRunner.class) +public class UserTest { + @Test + public void testCreateUser() { + User user = User.builder() + .id("jobata") + .pw("1234") + .name("조현우") + .position("부팀장") + .department("백엔드") + .build(); + assertThat(user.getId()).isEqualTo("jobata"); + } +} From 9326891ec809d5874f506cf710bc1725774656d0 Mon Sep 17 00:00:00 2001 From: jo-bata Date: Wed, 12 Jun 2019 22:34:38 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[#14][UPDATE]=20=EC=9C=A0=EC=A0=80=20?= =?UTF-8?q?=ED=9A=8C=EC=9B=90=EA=B0=80=EC=9E=85=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bom/meetingroom/user/UserServiceTest.java | 25 ++++++++++++++----- .../bom/bom/meetingroom/user/UserTest.java | 10 ++------ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/test/java/bom/bom/meetingroom/user/UserServiceTest.java b/src/test/java/bom/bom/meetingroom/user/UserServiceTest.java index c2f0df5..64110e8 100644 --- a/src/test/java/bom/bom/meetingroom/user/UserServiceTest.java +++ b/src/test/java/bom/bom/meetingroom/user/UserServiceTest.java @@ -1,23 +1,29 @@ package bom.bom.meetingroom.user; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; -import org.mockito.Mock; +import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import java.util.Optional; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; @SpringBootTest @RunWith(SpringRunner.class) @ActiveProfiles(value = "test") public class UserServiceTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Autowired UserService userService; @@ -26,15 +32,22 @@ public class UserServiceTest { @Test public void testSignUp() { - when(userRepository.findById(any())).thenReturn(null); UserDto userDto = new UserDto("jobata", "1234", "조현우", "부팀장", "백엔드"); userService.signUp(userDto); } - @Test(expected = NullPointerException.class) + @Test public void testSignUpFail() { - when(userRepository.findById(any())).thenReturn(Optional.of(User.builder().id("jobata").build())); - UserDto userDto = new UserDto("jobata", "1234", "조현우", "부팀장", "백엔드"); + expectedException.expect(DataIntegrityViolationException.class); + UserDto userDto = new UserDto("forest.grass", "1234", "이동준", "팀장", "백엔드"); + Mockito.when(userRepository.save(any())).thenThrow(DataIntegrityViolationException.class); userService.signUp(userDto); } + + @Test + public void testFindUserByUserIdFail() { + Mockito.when(userRepository.findByUserId(any())).thenReturn(Optional.empty()); + User user = userService.findUserByUserId("jobata"); + assertThat(user.getId()).isNull(); + } } diff --git a/src/test/java/bom/bom/meetingroom/user/UserTest.java b/src/test/java/bom/bom/meetingroom/user/UserTest.java index 7a50829..33f89fd 100644 --- a/src/test/java/bom/bom/meetingroom/user/UserTest.java +++ b/src/test/java/bom/bom/meetingroom/user/UserTest.java @@ -12,13 +12,7 @@ public class UserTest { @Test public void testCreateUser() { - User user = User.builder() - .id("jobata") - .pw("1234") - .name("조현우") - .position("부팀장") - .department("백엔드") - .build(); - assertThat(user.getId()).isEqualTo("jobata"); + User user = User.from(new UserDto("jobata", "1234", "조현우", "부팀장", "백엔드")); + assertThat(user.getUserId()).isEqualTo("jobata"); } } From d1b118703e6b6d7e63786fb7ae401ec2bd7c9b23 Mon Sep 17 00:00:00 2001 From: jo-bata Date: Wed, 12 Jun 2019 22:35:25 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[#14][UPDATE]=20=EC=9C=A0=EC=A0=80=20?= =?UTF-8?q?=ED=9A=8C=EC=9B=90=EA=B0=80=EC=9E=85=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/bom/bom/meetingroom/user/User.java | 24 +++++++++++++++---- .../bom/bom/meetingroom/user/UserDto.java | 2 +- .../bom/meetingroom/user/UserRepository.java | 4 +++- .../bom/bom/meetingroom/user/UserService.java | 1 + .../bom/meetingroom/user/UserServiceImpl.java | 22 +++++++---------- 5 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/main/java/bom/bom/meetingroom/user/User.java b/src/main/java/bom/bom/meetingroom/user/User.java index 4ae4750..c40f30a 100644 --- a/src/main/java/bom/bom/meetingroom/user/User.java +++ b/src/main/java/bom/bom/meetingroom/user/User.java @@ -1,19 +1,33 @@ package bom.bom.meetingroom.user; -import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; -import javax.persistence.Entity; -import javax.persistence.Id; +import javax.persistence.*; -@Builder @Getter @Entity +@NoArgsConstructor public class User { @Id - private String id; + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @Column(name = "user_id", unique = true) + private String userId; private String pw; private String name; private String position; private String department; + + private User(UserDto userDto) { + this.userId = userDto.getUserId(); + this.pw = userDto.getPw(); + this.name = userDto.getName(); + this.position = userDto.getPosition(); + this.department = userDto.getDepartment(); + } + + public static User from(UserDto userDto) { + return new User(userDto); + } } diff --git a/src/main/java/bom/bom/meetingroom/user/UserDto.java b/src/main/java/bom/bom/meetingroom/user/UserDto.java index 011dad5..9064419 100644 --- a/src/main/java/bom/bom/meetingroom/user/UserDto.java +++ b/src/main/java/bom/bom/meetingroom/user/UserDto.java @@ -8,7 +8,7 @@ @Setter @AllArgsConstructor public class UserDto { - private String id; + private String userId; private String pw; private String name; private String position; diff --git a/src/main/java/bom/bom/meetingroom/user/UserRepository.java b/src/main/java/bom/bom/meetingroom/user/UserRepository.java index 75435d0..9bac520 100644 --- a/src/main/java/bom/bom/meetingroom/user/UserRepository.java +++ b/src/main/java/bom/bom/meetingroom/user/UserRepository.java @@ -2,6 +2,8 @@ import org.springframework.data.jpa.repository.JpaRepository; -public interface UserRepository extends JpaRepository { +import java.util.Optional; +public interface UserRepository extends JpaRepository { + Optional findByUserId(String userId); } diff --git a/src/main/java/bom/bom/meetingroom/user/UserService.java b/src/main/java/bom/bom/meetingroom/user/UserService.java index fb6ed59..2f80189 100644 --- a/src/main/java/bom/bom/meetingroom/user/UserService.java +++ b/src/main/java/bom/bom/meetingroom/user/UserService.java @@ -2,4 +2,5 @@ public interface UserService { void signUp(UserDto userDto); + User findUserByUserId(String userId); } diff --git a/src/main/java/bom/bom/meetingroom/user/UserServiceImpl.java b/src/main/java/bom/bom/meetingroom/user/UserServiceImpl.java index 4f0fc23..e83e434 100644 --- a/src/main/java/bom/bom/meetingroom/user/UserServiceImpl.java +++ b/src/main/java/bom/bom/meetingroom/user/UserServiceImpl.java @@ -1,9 +1,8 @@ package bom.bom.meetingroom.user; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; -import java.util.Optional; - @Service public class UserServiceImpl implements UserService { private final UserRepository userRepository; @@ -14,20 +13,15 @@ public UserServiceImpl(UserRepository userRepository) { @Override public void signUp(UserDto userDto) { - if (isUserId(userDto.getId())) { - throw new NullPointerException(); + User user = User.from(userDto); + try { + userRepository.save(user); + } catch (DataIntegrityViolationException dive) { + throw dive; } - User user = User.builder() - .id(userDto.getId()) - .pw(userDto.getPw()) - .name(userDto.getName()) - .position(userDto.getPosition()) - .department(userDto.getDepartment()) - .build(); - userRepository.save(user); } - private boolean isUserId(String id) { - return Optional.ofNullable(userRepository.findById(id)).isPresent(); + public User findUserByUserId(String userId) { + return userRepository.findByUserId(userId).orElse(new User()); } }