Skip to content
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

[출석 체크] test : 출석 체크 테스트(#89) #96

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions be/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ dependencies {

// redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

// h2
testRuntimeOnly 'com.h2database:h2'

testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
}

tasks.named('bootBuildImage') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.time.LocalDate;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -46,8 +47,8 @@ public ResponseEntity<CustomResponse<Void>> dailyCheck(
.ok()
.body(new CustomResponse<>());

LocalDate dailyCheckDay = LocalDate.now();
if (!arrowService.dailyCheck(userId, dailyCheckDay)) {
LocalDateTime dailyCheckAt = LocalDateTime.now();
if (!arrowService.dailyCheck(userId, dailyCheckAt)) {
response = ResponseEntity
.badRequest()
.body(new CustomResponse<>("이미 출석 체크한 사용자입니다."));
Expand Down
6 changes: 3 additions & 3 deletions be/src/main/java/yeonba/be/arrow/service/ArrowService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ArrowService {
private final ApplicationEventPublisher eventPublisher;

@Transactional
public boolean dailyCheck(long userId, LocalDate dailyCheckDay) {
public boolean dailyCheck(long userId, LocalDateTime dailyCheckAt) {

User dailyCheckUser = userQuery.findById(userId);

Expand All @@ -41,7 +41,7 @@ public boolean dailyCheck(long userId, LocalDate dailyCheckDay) {
throw new GeneralException(UserException.INACTIVE_USER);
}

boolean canDailyCheck = dailyCheckUser.canDailyCheckAt(dailyCheckDay);
boolean canDailyCheck = dailyCheckUser.canDailyCheckAt(dailyCheckAt.toLocalDate());

// 출석 체크 화살 내역 저장, 사용자 화살 증가
if (canDailyCheck) {
Expand All @@ -56,7 +56,7 @@ public boolean dailyCheck(long userId, LocalDate dailyCheckDay) {
}

// 사용자 최종 접속 일시 갱신
dailyCheckUser.updateLastAccessedAt(LocalDateTime.now());
dailyCheckUser.updateLastAccessedAt(dailyCheckAt);

return canDailyCheck;
}
Expand Down
5 changes: 5 additions & 0 deletions be/src/main/java/yeonba/be/config/FcmConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public class FcmConfig {
@Bean
public FirebaseMessaging firebaseMessaging() throws IOException {

if (!FirebaseApp.getApps().isEmpty()) {

return FirebaseMessaging.getInstance(FirebaseApp.getInstance());
}

ClassPathResource resource = new ClassPathResource(fcmAccountKeyPath);

FirebaseOptions firebaseOptions = FirebaseOptions.builder()
Expand Down
10 changes: 3 additions & 7 deletions be/src/main/java/yeonba/be/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.Objects;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand Down Expand Up @@ -159,12 +159,8 @@ public void delete() {

public boolean canDailyCheckAt(LocalDate dailyCheckDay) {

if (Optional.ofNullable(this.lastAccessedAt).isEmpty()) {

return true;
}

return this.lastAccessedAt.isBefore(dailyCheckDay.atStartOfDay());
return Objects.isNull(this.lastAccessedAt)
|| this.lastAccessedAt.isBefore(dailyCheckDay.atStartOfDay());
}

public String getRepresentativeProfilePhoto() {
Expand Down
3 changes: 2 additions & 1 deletion be/src/main/java/yeonba/be/user/entity/VocalRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
Expand All @@ -17,7 +18,7 @@
public class VocalRange {

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package yeonba.be.arrow.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.time.LocalDateTime;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
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.boot.test.mock.mockito.MockBean;
import yeonba.be.arrow.service.ArrowService;
import yeonba.be.support.ControllerTestSupport;

@WebMvcTest(ArrowController.class)
class ArrowControllerTest extends ControllerTestSupport {

@Autowired
private ArrowController arrowController;

@MockBean
private ArrowService arrowService;

@Override
protected Object controller() {

return arrowController;
}

@Nested
@DisplayName("사용자 고유 번호를 요청해 ")
class dailyCheckTest {

private long userId = 1L;

@DisplayName("출석 체크를 할 수 있다.")
@Test
void dailCheck() throws Exception {

// given
given(arrowService.dailyCheck(eq(userId), any(LocalDateTime.class))).willReturn(true);

// when & then
mockMvc.perform(post("/daily-check")
.requestAttr("userId", userId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value("success"))
.andExpect(jsonPath("$.message").isEmpty())
.andExpect(jsonPath("$.data").isEmpty());
}

@DisplayName("이미 출석 체크한 경우 출석 체크할 수 없다.")
@Test
void alreadyDailyChecked() throws Exception {

// given
given(arrowService.dailyCheck(eq(userId), any(LocalDateTime.class))).willReturn(false);

// when & then
mockMvc.perform(post("/daily-check")
.requestAttr("userId", userId))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.status").value("fail"))
.andExpect(jsonPath("$.message").value("이미 출석 체크한 사용자입니다."))
.andExpect(jsonPath("$.data").isEmpty());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package yeonba.be.arrow.integration;


import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import yeonba.be.arrow.controller.ArrowController;
import yeonba.be.exception.ExceptionAdvice;
import yeonba.be.fixture.UserFixtureFactory;
import yeonba.be.user.entity.Animal;
import yeonba.be.user.entity.Area;
import yeonba.be.user.entity.User;
import yeonba.be.user.entity.VocalRange;


@ActiveProfiles("test")
@TestInstance(Lifecycle.PER_CLASS)
@Transactional
@SpringBootTest
public class ArrowIntegrationTest {

@PersistenceContext
private EntityManager em;

@Autowired
private ExceptionAdvice exceptionAdvice;

@Autowired
private ArrowController arrowController;
private MockMvc mockMvc;
private long userId;

@BeforeEach
void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(arrowController)
.setControllerAdvice(exceptionAdvice)
.alwaysDo(print())
.build();

VocalRange vocalRange = new VocalRange("저음");
em.persist(vocalRange);

Animal animal = new Animal("강아지상");
em.persist(animal);

Area area = new Area("서울");
em.persist(area);
em.flush();

User user = UserFixtureFactory.builder()
.vocalRange(vocalRange)
.animal(animal)
.area(area)
.build().getFixture();
em.persist(user);
em.flush();

userId = user.getId();
}

@DisplayName("출석 체크를 할 수 있다.")
@Test
void dailyCheck() throws Exception {

// when & then
mockMvc.perform(
post("/daily-check")
.requestAttr("userId", userId)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value("success"))
.andExpect(jsonPath("$.message").isEmpty())
.andExpect(jsonPath("$.data").isEmpty());
}
}
Loading
Loading