-
Notifications
You must be signed in to change notification settings - Fork 3
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
메시지 전송 api 구현 #24
메시지 전송 api 구현 #24
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
package web.chat.backend.controller.request; | ||
|
||
import javax.validation.constraints.Size; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class MessageRequest { | ||
|
||
@Size(min = 1) | ||
private String contents; | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ | |||||||||||||||
|
||||||||||||||||
import lombok.RequiredArgsConstructor; | ||||||||||||||||
import web.chat.backend.entity.Room; | ||||||||||||||||
import web.chat.backend.exception.NotFoundException; | ||||||||||||||||
import web.chat.backend.repository.RoomRepository; | ||||||||||||||||
|
||||||||||||||||
@RequiredArgsConstructor | ||||||||||||||||
|
@@ -21,4 +22,9 @@ public Room createRoom(Room room) { | |||||||||||||||
public List<Room> getRooms() { | ||||||||||||||||
return roomRepository.findAll(); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
public Room getOrThrow(Long roomId) { | ||||||||||||||||
return roomRepository.findById(roomId) | ||||||||||||||||
.orElseThrow(() -> new NotFoundException(String.format("Room [id:%d] not found", roomId))); | ||||||||||||||||
} | ||||||||||||||||
Comment on lines
+26
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 유효하지 않는 roomId 를 받았을 때 null 을 리턴하지 않고 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
web-chat-backend/src/main/java/web/chat/backend/service/MessageService.java Lines 17 to 23 in ce7b639
|
||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package web.chat.backend.controller; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.TestConstructor; | ||
import org.springframework.test.context.jdbc.Sql; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.RequiredArgsConstructor; | ||
import web.chat.backend.controller.request.MessageRequest; | ||
|
||
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL) | ||
@RequiredArgsConstructor | ||
@AutoConfigureMockMvc | ||
@SpringBootTest | ||
public class MessageCreationTest { | ||
|
||
final MockMvc mockMvc; | ||
final ObjectMapper objectMapper; | ||
|
||
final RoomController roomController; | ||
|
||
@Test | ||
@Sql("/test-sql/messages.sql") | ||
void createMessage() throws Exception { | ||
|
||
// given | ||
MessageRequest req = new MessageRequest(); | ||
req.setContents("hi hello"); | ||
|
||
final String body = objectMapper.writeValueAsString(req); | ||
|
||
// when | ||
ResultActions action = mockMvc.perform( | ||
post("/api/rooms/{roomId}/messages", 1) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(body)); | ||
|
||
// then | ||
action.andDo(print()) | ||
.andExpect(status().isCreated()) | ||
.andExpect(jsonPath("$.contents").value(req.getContents())); | ||
} | ||
Comment on lines
+31
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 메서드를
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package web.chat.backend.repository; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; | ||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; | ||
import org.springframework.test.context.TestConstructor; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import web.chat.backend.entity.Message; | ||
import web.chat.backend.entity.Room; | ||
|
||
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL) | ||
@RequiredArgsConstructor | ||
@EnableJpaAuditing | ||
@DataJpaTest | ||
class MessageRepositoryTest { | ||
|
||
final TestEntityManager entityManager; | ||
final MessageRepository messageRepository; | ||
|
||
@Test | ||
void save() { | ||
|
||
// given | ||
Room room = new Room(); | ||
room.setTitle("room"); | ||
|
||
entityManager.persist(room); | ||
|
||
Message message = new Message(); | ||
message.setContents("hi hello"); | ||
message.setRoom(room); | ||
|
||
// when | ||
Message savedMessage = messageRepository.save(message); | ||
|
||
// then | ||
assertThat(savedMessage.getId()).isNotNull(); | ||
assertThat(savedMessage.getContents()).isEqualTo(message.getContents()); | ||
assertThat(savedMessage.getCreatedAt()).isNotNull(); | ||
assertThat(savedMessage.getRoom()).isNotNull(); | ||
assertThat(savedMessage.getRoom().getTitle()).isEqualTo(room.getTitle()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JPA Auditing 적용
c691006 에서 누락된 작업 보강
ref. https://www.baeldung.com/database-auditing-jpa
ref. https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.auditing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ref. https://gist.github.com/dlxotn216/94c34a2debf848396cf82a7f21a32abe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 나중에 공통 프로퍼티는 BaseEntity 클래스로 빼는 방법도 고려해봐야겠다