-
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.
Merge pull request #4 from f-lab-edu/feature/3
[#3] controller, dto 생성, Test 코드 작성
- Loading branch information
Showing
22 changed files
with
440 additions
and
5 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
### Gradle ### | ||
.gradle/ | ||
/build/ | ||
**/build/ | ||
|
||
### 로그 ### | ||
*.log | ||
|
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
Binary file removed
BIN
-747 Bytes
api/build/classes/java/main/com/whalewatch/WhaleWatchApplication.class
Binary file not shown.
Binary file not shown.
38 changes: 38 additions & 0 deletions
38
api/src/main/java/com/whalewatch/controller/AlertController.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,38 @@ | ||
package com.whalewatch.controller; | ||
|
||
import com.whalewatch.common.dto.AlertSettingsDto; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/api/alerts") | ||
public class AlertController { | ||
//알림 리스트 생성 | ||
private List<AlertSettingsDto> alertsettings = new ArrayList<>(); | ||
|
||
public AlertController(){ | ||
//초기 더미데이터 | ||
alertsettings.add(new AlertSettingsDto(1,"BTC",10000,true)); | ||
alertsettings.add(new AlertSettingsDto(2,"ETH",5000,false)); | ||
} | ||
|
||
@GetMapping() | ||
public List<AlertSettingsDto> getAlert() { | ||
return alertsettings; | ||
} | ||
|
||
//알림 생성 | ||
@PutMapping() | ||
public AlertSettingsDto createAlert(@PathVariable int id, @RequestBody AlertSettingsDto settings){ | ||
alertsettings.add(settings); | ||
return settings; | ||
} | ||
|
||
@PostMapping("/{id}") | ||
public AlertSettingsDto updateAlert(@PathVariable int id,@RequestBody AlertSettingsDto settings){ | ||
alertsettings.add(settings); | ||
return settings; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
api/src/main/java/com/whalewatch/controller/PostController.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,30 @@ | ||
package com.whalewatch.controller; | ||
|
||
import com.whalewatch.common.dto.PostDto; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/api/posts") | ||
public class PostController { | ||
private List<PostDto> posts = new ArrayList<>(); | ||
|
||
public PostController(){ | ||
posts.add(new PostDto(1,"Test1","Test1")); | ||
posts.add(new PostDto(2,"Test2","Test2")); | ||
} | ||
|
||
@GetMapping | ||
public List<PostDto> getPosts(){ | ||
return posts; | ||
} | ||
|
||
@PostMapping | ||
public PostDto createPost(@RequestBody PostDto post) { | ||
posts.add(post); | ||
return post; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
api/src/main/java/com/whalewatch/controller/TransactionController.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,28 @@ | ||
package com.whalewatch.controller; | ||
|
||
import com.whalewatch.common.dto.TransactionDto; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/api/transactions") | ||
public class TransactionController { | ||
|
||
@GetMapping("/list") | ||
public List<TransactionDto> getTransactions(){ | ||
return Arrays.asList( | ||
new TransactionDto(1,"0xabc123","BTC",20000), | ||
new TransactionDto(2,"0xabc123","ETH",15000) | ||
); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public TransactionDto getTransactionByID(@PathVariable int id){ | ||
return new TransactionDto(id,"0xabc123","BTC",20000); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
api/src/main/java/com/whalewatch/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,24 @@ | ||
package com.whalewatch.controller; | ||
|
||
import com.whalewatch.common.dto.UserDto; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/api/users") | ||
public class UserController { | ||
|
||
@PostMapping | ||
public UserDto registerUser(@RequestBody UserDto userDto) { | ||
return userDto; | ||
} | ||
|
||
@PostMapping("/login") | ||
public UserDto loginUser(@RequestBody UserDto userDto) { | ||
return userDto; | ||
} | ||
|
||
@GetMapping("/me") | ||
public UserDto getUserInfo() { | ||
return new UserDto("[email protected]", "Test"); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
api/src/test/java/com/whalewatch/controller/AlertControllerTest.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,43 @@ | ||
package com.whalewatch.controller; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.whalewatch.common.dto.AlertSettingsDto; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
@WebMvcTest(controllers = AlertController.class) | ||
public class AlertControllerTest { | ||
|
||
@Autowired | ||
MockMvc mockMvc; | ||
|
||
@Autowired | ||
ObjectMapper objectMapper; | ||
|
||
@Test | ||
void getAlertSettings() throws Exception { | ||
mockMvc.perform(get("/api/alerts/settings")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.coin").value("BTC")) | ||
.andExpect(jsonPath("$.notifyByEmail").value(true)); | ||
} | ||
|
||
@Test | ||
void updateAlertSettings() throws Exception { | ||
AlertSettingsDto dto = new AlertSettingsDto("ETH", 20000, false); | ||
String json = objectMapper.writeValueAsString(dto); | ||
|
||
mockMvc.perform(put("/api/alerts/settings") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(json)) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string("updated setting")); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
api/src/test/java/com/whalewatch/controller/CommunityControllerTest.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,45 @@ | ||
package com.whalewatch.controller; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.whalewatch.common.dto.PostDto; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
|
||
@WebMvcTest(controllers = CommunityController.class) | ||
public class CommunityControllerTest { | ||
|
||
@Autowired | ||
MockMvc mockMvc; | ||
|
||
@Autowired | ||
ObjectMapper objectMapper; | ||
|
||
@Test | ||
void getPosts() throws Exception { | ||
mockMvc.perform(get("/api/posts")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$", hasSize(2))) | ||
.andExpect(jsonPath("$[0].title").value("Post1")); | ||
} | ||
|
||
@Test | ||
void createPost() throws Exception { | ||
PostDto post = new PostDto(3, "Post1", "post1"); | ||
String postJson = objectMapper.writeValueAsString(post); | ||
|
||
mockMvc.perform(post("/api/posts") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(postJson)) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string("게시글 생성 : Post1")); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
api/src/test/java/com/whalewatch/controller/TransactionControllerTest.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,35 @@ | ||
package com.whalewatch.controller; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@WebMvcTest(controllers = TransactionController.class) | ||
public class TransactionControllerTest { | ||
|
||
@Autowired | ||
MockMvc mockMvc; | ||
|
||
@Test | ||
void getTransactions() throws Exception { | ||
mockMvc.perform(get("/api/transactions/list")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$", hasSize(2))) | ||
.andExpect(jsonPath("$[0].coin").value("BTC")) | ||
.andExpect(jsonPath("$[1].coin").value("ETH")); | ||
} | ||
|
||
@Test | ||
void getTransactionById() throws Exception { | ||
mockMvc.perform(get("/api/transactions/1")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.id").value(1)) | ||
.andExpect(jsonPath("$.coin").value("BTC")); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
api/src/test/java/com/whalewatch/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,58 @@ | ||
package com.whalewatch.controller; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.whalewatch.common.dto.UserDto; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
@WebMvcTest(controllers = UserController.class) | ||
public class UserControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@Test | ||
void registerUser() throws Exception{ | ||
UserDto userDto = new UserDto("[email protected]","Test"); | ||
String userJson = objectMapper.writeValueAsString(userDto); | ||
|
||
mockMvc.perform(post("/api/users") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(userJson)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.email").value("[email protected]")) | ||
.andExpect(jsonPath("$.username").value("Test")); | ||
} | ||
|
||
@Test | ||
void loginUser() throws Exception{ | ||
UserDto userDto = new UserDto("[email protected]","Test"); | ||
String userJson = objectMapper.writeValueAsString(userDto); | ||
|
||
mockMvc.perform(post("/api/users/login") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(userJson)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.email").value("[email protected]")) | ||
.andExpect(jsonPath("$.username").value("Test")); | ||
} | ||
|
||
@Test | ||
void getUserInfo() throws Exception{ | ||
|
||
mockMvc.perform(get("/api/users/me")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.email").value("[email protected]")) | ||
.andExpect(jsonPath("$.username").value("Test")); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
dependencies { | ||
|
||
} |
Binary file not shown.
This file was deleted.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
common/src/main/java/com/whalewatch/common/dto/AlertSettingsDto.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,45 @@ | ||
package com.whalewatch.common.dto; | ||
|
||
public class AlertSettingsDto { | ||
private int id; | ||
private String coin; | ||
private int threshold; | ||
private boolean notifyByEmail; | ||
|
||
public AlertSettingsDto() {} | ||
|
||
public AlertSettingsDto(int id,String coin, int threshold, boolean notifyByEmail) { | ||
this.id = id; | ||
this.coin = coin; | ||
this.threshold = threshold; | ||
this.notifyByEmail = notifyByEmail; | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public String getCoin() { | ||
return coin; | ||
} | ||
|
||
public int getThreshold() { | ||
return threshold; | ||
} | ||
|
||
public boolean isNotifyByEmail() { | ||
return notifyByEmail; | ||
} | ||
|
||
public void setCoin(String coin) { | ||
this.coin = coin; | ||
} | ||
|
||
public void setThreshold(int threshold) { | ||
this.threshold = threshold; | ||
} | ||
|
||
public void setNotifyByEmail(boolean notifyByEmail) { | ||
this.notifyByEmail = notifyByEmail; | ||
} | ||
} |
Oops, something went wrong.