Skip to content

Commit

Permalink
feat: 타이머 등록하는 API 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
zinzoddari committed Jan 4, 2024
1 parent d5cb1e6 commit f41c146
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
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();
}
}
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();
}
}
}

0 comments on commit f41c146

Please sign in to comment.