-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
347 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/main/java/com/fastcampus/sns/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.fastcampus.sns.controller; | ||
|
||
import com.fastcampus.sns.service.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/users") | ||
@RequiredArgsConstructor | ||
public class UserController { | ||
|
||
private final UserService userService; | ||
|
||
// TODO: implement | ||
@PostMapping | ||
public void join() { | ||
// join | ||
userService.join("", ""); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/fastcampus/sns/controller/request/UserJoinRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.fastcampus.sns.controller.request; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class UserJoinRequest { | ||
|
||
private String userName; | ||
private String password; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/fastcampus/sns/controller/request/UserLoginRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.fastcampus.sns.controller.request; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class UserLoginRequest { | ||
|
||
private String userName; | ||
private String password; | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/fastcampus/sns/exception/SnsApplicationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.fastcampus.sns.exception; | ||
|
||
// TODO: implement | ||
public class SnsApplicationException extends RuntimeException { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.fastcampus.sns.model; | ||
|
||
// TODO: implement | ||
public class User { | ||
|
||
private String userName; | ||
private String password; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/fastcampus/sns/model/entity/UserEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.fastcampus.sns.model.entity; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
|
||
@Getter | ||
@Setter | ||
@Entity | ||
@Table | ||
public class UserEntity { | ||
|
||
@Id | ||
private Integer id; | ||
|
||
@Column(name = "user_name") | ||
private String userName; | ||
private String password; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/fastcampus/sns/repository/UserEntityRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.fastcampus.sns.repository; | ||
|
||
import com.fastcampus.sns.model.entity.UserEntity; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface UserEntityRepository extends JpaRepository<UserEntity, Integer> { | ||
|
||
Optional<UserEntity> findByUserName(String userName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.fastcampus.sns.service; | ||
|
||
import com.fastcampus.sns.exception.SnsApplicationException; | ||
import com.fastcampus.sns.model.User; | ||
import com.fastcampus.sns.model.entity.UserEntity; | ||
import com.fastcampus.sns.repository.UserEntityRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Optional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class UserService { | ||
|
||
private final UserEntityRepository userEntityRepository; | ||
|
||
// TODO: implement | ||
public User join(String userName, String password) { | ||
// userName 중복 확인 | ||
Optional<UserEntity> userEntity = userEntityRepository.findByUserName(userName); | ||
|
||
// user 등록 | ||
userEntityRepository.save(new UserEntity()); | ||
|
||
return new User(); | ||
} | ||
|
||
// TODO: implement | ||
public String login(String userName, String password) { // JWT -> String 반환 | ||
// 회원가입 여부 체크 | ||
UserEntity userEntity = userEntityRepository.findByUserName(userName).orElseThrow(() -> new SnsApplicationException()); | ||
|
||
// 비밀번호 체크 | ||
if (!userEntity.getPassword().equals(password)) { | ||
throw new SnsApplicationException(); | ||
} | ||
|
||
// 토큰 생성 | ||
return ""; | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
src/test/java/com/fastcampus/sns/controller/UserControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.fastcampus.sns.controller; | ||
|
||
import com.fastcampus.sns.exception.SnsApplicationException; | ||
import com.fastcampus.sns.model.User; | ||
import com.fastcampus.sns.controller.request.UserJoinRequest; | ||
import com.fastcampus.sns.controller.request.UserLoginRequest; | ||
import com.fastcampus.sns.service.UserService; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
public class UserControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@MockBean | ||
private UserService userService; | ||
|
||
@Test | ||
public void 회원가입() throws Exception { | ||
String userName = "userName"; | ||
String password = "password"; | ||
|
||
when(userService.join("userName", "password")).thenReturn(mock(User.class)); | ||
|
||
mockMvc.perform(post("/api/v1/users/join") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsBytes(new UserJoinRequest(userName, password))) | ||
).andDo(print()) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
public void 회원가입_중복_userName_에러반환() throws Exception { | ||
String userName = "userName"; | ||
String password = "password"; | ||
|
||
when(userService.join("userName", "password")).thenThrow(new SnsApplicationException()); | ||
|
||
mockMvc.perform(post("/api/v1/users/join") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsBytes(new UserJoinRequest(userName, password))) | ||
).andDo(print()) | ||
.andExpect(status().isConflict()); | ||
} | ||
|
||
@Test | ||
public void 로그인() throws Exception { | ||
String userName = "userName"; | ||
String password = "password"; | ||
|
||
when(userService.login(userName, password)).thenThrow(new SnsApplicationException()); | ||
|
||
mockMvc.perform(post("/api/v1/users/login") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsBytes(new UserLoginRequest(userName, password))) | ||
).andDo(print()) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
public void 로그인_없는_userName_에러반환() throws Exception { | ||
String userName = "userName"; | ||
String password = "password"; | ||
|
||
when(userService.login(userName, password)).thenThrow(new SnsApplicationException()); | ||
|
||
mockMvc.perform(post("/api/v1/users/login") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsBytes(new UserLoginRequest(userName, password))) | ||
).andDo(print()) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
public void 로그인_틀린_password_에러반환() throws Exception { | ||
String userName = "userName"; | ||
String password = "password"; | ||
|
||
when(userService.login(userName, password)).thenReturn("test_token"); | ||
|
||
mockMvc.perform(post("/api/v1/users/login") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsBytes(new UserLoginRequest(userName, password))) | ||
).andDo(print()) | ||
.andExpect(status().isUnauthorized()); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/test/java/com/fastcampus/sns/fixture/UserEntityFixture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.fastcampus.sns.fixture; | ||
|
||
import com.fastcampus.sns.model.entity.UserEntity; | ||
|
||
public class UserEntityFixture { | ||
|
||
public static UserEntity get(String userName, String password) { | ||
UserEntity result = new UserEntity(); | ||
result.setId(1); | ||
result.setUserName(userName); | ||
result.setPassword(password); | ||
|
||
return result | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/test/java/com/fastcampus/sns/service/UserServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.fastcampus.sns.service; | ||
|
||
import com.fastcampus.sns.exception.SnsApplicationException; | ||
import com.fastcampus.sns.fixture.UserEntityFixture; | ||
import com.fastcampus.sns.model.entity.UserEntity; | ||
import com.fastcampus.sns.repository.UserEntityRepository; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
@SpringBootTest | ||
public class UserServiceTest { | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@Autowired | ||
private UserEntityRepository userEntityRepository; | ||
|
||
@Test | ||
void 회원가입_성공() { | ||
String userName = "userName"; | ||
String password = "password"; | ||
|
||
// mocking | ||
when(userEntityRepository.findByUserName(userName)).thenReturn(Optional.empty()); | ||
when(userEntityRepository.save(any())).thenReturn(Optional.of(UserEntityFixture.get(userName, password))); | ||
|
||
Assertions.assertDoesNotThrow(() -> userService.join(userName, password)); | ||
} | ||
|
||
@Test | ||
void 회원가입_userName_중복() { | ||
String userName = "userName"; | ||
String password = "password"; | ||
|
||
UserEntity fixture = UserEntityFixture.get(userName, password); | ||
|
||
// mocking | ||
when(userEntityRepository.findByUserName(userName)).thenReturn(Optional.of(fixture)); | ||
when(userEntityRepository.save(any())).thenReturn(Optional.of(fixture)); | ||
|
||
Assertions.assertThrows(SnsApplicationException.class, () -> userService.join(userName, password)); | ||
} | ||
|
||
@Test | ||
void 로그인_성공() { | ||
String userName = "userName"; | ||
String password = "password"; | ||
|
||
UserEntity fixture = UserEntityFixture.get(userName, password); | ||
|
||
// mocking | ||
when(userEntityRepository.findByUserName(userName)).thenReturn(Optional.of(fixture)); | ||
Assertions.assertDoesNotThrow(() -> userService.login(userName, password)); | ||
} | ||
|
||
@Test | ||
void 로그인_없는_userName() { | ||
String userName = "userName"; | ||
String password = "password"; | ||
|
||
// mocking | ||
when(userEntityRepository.findByUserName(userName)).thenReturn(Optional.empty()); | ||
|
||
Assertions.assertThrows(SnsApplicationException.class, () -> userService.login(userName, password)); | ||
} | ||
|
||
@Test | ||
void 로그인_틀린_password() { | ||
String userName = "userName"; | ||
String password = "password"; | ||
String wrongPassword = "wrongPassword"; | ||
|
||
UserEntity fixture = UserEntityFixture.get(userName, password); | ||
|
||
// mocking | ||
when(userEntityRepository.findByUserName(userName)).thenReturn(Optional.of(fixture)); | ||
|
||
Assertions.assertThrows(SnsApplicationException.class, () -> userService.login(userName, wrongPassword)); | ||
} | ||
} |