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

Feature/13 implementing a user domain model #18

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
5 changes: 0 additions & 5 deletions src/main/java/bom/bom/meetingroom/MeetingroomApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@

@SpringBootApplication
public class MeetingroomApplication {

public static void main(String[] args) {
SpringApplication.run(MeetingroomApplication.class, args);


}


}
33 changes: 33 additions & 0 deletions src/main/java/bom/bom/meetingroom/user/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package bom.bom.meetingroom.user;

import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Getter
@Entity
@NoArgsConstructor
public class User {
@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);
}
}
16 changes: 16 additions & 0 deletions src/main/java/bom/bom/meetingroom/user/UserDto.java
Original file line number Diff line number Diff line change
@@ -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 userId;
private String pw;
private String name;
private String position;
private String department;
}
9 changes: 9 additions & 0 deletions src/main/java/bom/bom/meetingroom/user/UserRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bom.bom.meetingroom.user;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByUserId(String userId);
}
6 changes: 6 additions & 0 deletions src/main/java/bom/bom/meetingroom/user/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package bom.bom.meetingroom.user;

public interface UserService {
void signUp(UserDto userDto);
User findUserByUserId(String userId);
}
27 changes: 27 additions & 0 deletions src/main/java/bom/bom/meetingroom/user/UserServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package bom.bom.meetingroom.user;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;

public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}

@Override
public void signUp(UserDto userDto) {
User user = User.from(userDto);
try {
userRepository.save(user);
} catch (DataIntegrityViolationException dive) {
throw dive;
}
}

public User findUserByUserId(String userId) {
return userRepository.findByUserId(userId).orElse(new User());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/bom/bom/meetingroom/user/UserServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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.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;

@SpringBootTest
@RunWith(SpringRunner.class)
@ActiveProfiles(value = "test")
public class UserServiceTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();

@Autowired
UserService userService;

@MockBean
UserRepository userRepository;

@Test
public void testSignUp() {
UserDto userDto = new UserDto("jobata", "1234", "조현우", "부팀장", "백엔드");
userService.signUp(userDto);
}

@Test
public void testSignUpFail() {
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();
}
}
18 changes: 18 additions & 0 deletions src/test/java/bom/bom/meetingroom/user/UserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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.from(new UserDto("jobata", "1234", "조현우", "부팀장", "백엔드"));
assertThat(user.getUserId()).isEqualTo("jobata");
}
}