Skip to content

Commit

Permalink
Merge pull request #4 from f-lab-edu/feature/3
Browse files Browse the repository at this point in the history
[#3] controller, dto 생성, Test 코드 작성
  • Loading branch information
austinhong22 authored Dec 18, 2024
2 parents 3effa7f + bc26c1a commit 99e8409
Show file tree
Hide file tree
Showing 22 changed files with 440 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Gradle ###
.gradle/
/build/
**/build/

### 로그 ###
*.log
Expand Down
1 change: 1 addition & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.4'
id 'io.spring.dependency-management' version '1.1.6'
}
Expand Down
Binary file not shown.
Binary file not shown.
38 changes: 38 additions & 0 deletions api/src/main/java/com/whalewatch/controller/AlertController.java
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 api/src/main/java/com/whalewatch/controller/PostController.java
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;
}
}
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 api/src/main/java/com/whalewatch/controller/UserController.java
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");
}
}
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"));
}
}
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"));
}
}
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"));
}
}
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"));
}
}
4 changes: 4 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
plugins {
id 'java'
}

dependencies {

}
Binary file removed common/build/libs/common.jar
Binary file not shown.
2 changes: 0 additions & 2 deletions common/build/tmp/jar/MANIFEST.MF

This file was deleted.

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;
}
}
Loading

0 comments on commit 99e8409

Please sign in to comment.