-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
d5cb1e6
commit f41c146
Showing
2 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
21 changes: 20 additions & 1 deletion
21
src/main/java/org/idiot/yesslave/worktimer/ui/WorkTimerController.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,12 +1,31 @@ | ||
package org.idiot.yesslave.worktimer.ui; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.idiot.yesslave.worktimer.application.WorkTimerService; | ||
import org.idiot.yesslave.worktimer.domain.RandomCodeGenerator; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@RequestMapping("/timer") | ||
@RestController | ||
@RequiredArgsConstructor | ||
class WorkTimerController { | ||
private final WorkTimerService workTimerService; | ||
|
||
@PostMapping | ||
public ResponseEntity<Void> timerCreate() { | ||
Long response = workTimerService.registerTimer(LocalDateTime.now(), new RandomCodeGenerator()); | ||
|
||
|
||
return ResponseEntity.created(ServletUriComponentsBuilder | ||
.fromCurrentRequest() | ||
.path("/{id}") | ||
.buildAndExpand(response) | ||
.toUri()) | ||
.build(); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/test/java/org/idiot/yesslave/worktimer/ui/WorkTimerControllerTest.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,75 @@ | ||
package org.idiot.yesslave.worktimer.ui; | ||
|
||
import org.idiot.yesslave.worktimer.application.WorkTimerService; | ||
import org.idiot.yesslave.worktimer.domain.RandomCodeGenerator; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
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.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
import org.springframework.web.filter.CharacterEncodingFilter; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
|
||
@AutoConfigureMockMvc | ||
@ExtendWith(SpringExtension.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) | ||
class WorkTimerControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private WebApplicationContext ctx; | ||
|
||
@MockBean | ||
private WorkTimerService workTimerService; | ||
|
||
final String baseUrl = "/timer"; | ||
|
||
@BeforeEach | ||
void init() { | ||
this.mockMvc = MockMvcBuilders.webAppContextSetup(ctx) | ||
.addFilters(new CharacterEncodingFilter("UTF-8", true)) | ||
.alwaysDo(print()) | ||
.build(); | ||
} | ||
|
||
@Nested | ||
@DisplayName("타이머 등록(출근) 할 때,") | ||
class timerCreate { | ||
@Test | ||
@DisplayName("성공적으로 저장합니다.") | ||
void success() throws Exception { | ||
//given | ||
final Long expectedId = 1L; | ||
|
||
given(workTimerService.registerTimer(any(LocalDateTime.class), any(RandomCodeGenerator.class))) | ||
.willReturn(expectedId); | ||
|
||
//when | ||
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(baseUrl)) | ||
.andExpect(MockMvcResultMatchers.status().isCreated()) | ||
.andReturn(); | ||
|
||
//TODO: 조회 기능 생성 되면 값 검증 필요 | ||
assertThat(mvcResult.getResponse().getHeader("Location")).isNotNull(); | ||
} | ||
} | ||
} |