-
Notifications
You must be signed in to change notification settings - Fork 3
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 #24 from LandvibeDev/feat/add-message
Close: #19 메시지 전송 api 구현
- Loading branch information
Showing
9 changed files
with
194 additions
and
4 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
10 changes: 10 additions & 0 deletions
10
src/main/java/web/chat/backend/controller/request/MessageRequest.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 |
---|---|---|
@@ -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; | ||
} |
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
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 |
---|---|---|
|
@@ -20,8 +20,6 @@ | |
@Getter | ||
@Setter | ||
@Entity | ||
@Builder | ||
|
||
public class Room { | ||
|
||
@Id | ||
|
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
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
52 changes: 52 additions & 0 deletions
52
src/test/java/web/chat/backend/controller/MessageCreationTest.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,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())); | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
src/test/java/web/chat/backend/repository/MessageRepositoryTest.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,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()); | ||
} | ||
} |