Skip to content

Commit

Permalink
test(#104): 집안일 삭제 API 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
jusung-c committed Nov 17, 2023
1 parent 03387e7 commit db5b84f
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
import com.heachi.mysql.define.user.constant.UserPlatformType;
import com.heachi.mysql.define.user.constant.UserRole;
import com.heachi.mysql.define.user.repository.UserRepository;
import com.heachi.redis.define.housework.todo.Todo;
import com.heachi.redis.define.housework.todo.TodoList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestConstructor;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestConstructor;

Expand Down Expand Up @@ -144,6 +148,17 @@ public static HouseworkInfo generateCustomHouseworkInfo(HouseworkCategory catego
.build();
}

public static HouseworkInfo generateCustom2HouseworkInfo(HouseworkCategory category, GroupInfo groupInfo, String title, HouseworkPeriodType type) {

return HouseworkInfo.builder()
.houseworkCategory(category)
.title(title)
.detail("빨래 돌리기")
.type(type)
.endTime(LocalTime.of(18,0))
.build();
}

public static HouseworkMember generateHouseworkMember(GroupMember groupMember, HouseworkInfo houseworkInfo) {

return HouseworkMember.builder()
Expand All @@ -156,4 +171,13 @@ public static HouseworkTodo generateHouseworkTodo(HouseworkInfo houseworkInfo, G

return HouseworkTodo.makeTodoReferInfo(houseworkInfo, groupInfo, date);
}

public static TodoList generateTodoList(Long groupId, LocalDate date, List<Todo> todoList) {
return TodoList.builder()
.groupInfoId(groupId)
.date(date)
.todoList(todoList)
.build();

}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.heachi.housework.api.service.housework.info;

import com.heachi.admin.common.exception.ExceptionMessage;
import com.heachi.admin.common.exception.HeachiException;
import com.heachi.admin.common.exception.group.member.GroupMemberException;
import com.heachi.admin.common.exception.housework.HouseworkException;
import com.heachi.admin.common.response.JsonResult;
import com.heachi.external.clients.auth.AuthClients;
import com.heachi.external.clients.auth.response.UserInfoResponse;
import com.heachi.admin.common.utils.DayOfWeekUtils;
import com.heachi.housework.TestConfig;
import com.heachi.housework.api.service.auth.AuthExternalService;
import com.heachi.housework.api.controller.housework.info.request.HouseworkInfoDeleteType;
import com.heachi.housework.api.service.housework.info.request.HouseworkInfoCreateServiceRequest;
import com.heachi.housework.api.service.housework.info.request.HouseworkInfoDeleteRequest;
import com.heachi.mysql.define.group.info.GroupInfo;
import com.heachi.mysql.define.group.info.repository.GroupInfoRepository;
import com.heachi.mysql.define.group.member.GroupMember;
Expand All @@ -18,27 +18,28 @@
import com.heachi.mysql.define.housework.info.HouseworkInfo;
import com.heachi.mysql.define.housework.info.constant.HouseworkPeriodType;
import com.heachi.mysql.define.housework.info.repository.HouseworkInfoRepository;
import com.heachi.mysql.define.housework.member.HouseworkMember;
import com.heachi.mysql.define.housework.member.repository.HouseworkMemberRepository;
import com.heachi.mysql.define.housework.todo.HouseworkTodo;
import com.heachi.mysql.define.housework.todo.repository.HouseworkTodoRepository;
import com.heachi.mysql.define.user.User;
import com.heachi.mysql.define.user.repository.UserRepository;
import com.heachi.redis.define.housework.todo.Todo;
import com.heachi.redis.define.housework.todo.TodoList;
import com.heachi.redis.define.housework.todo.repository.TodoListRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
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 reactor.core.publisher.Mono;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;


import static org.assertj.core.api.Assertions.*;
Expand Down Expand Up @@ -362,4 +363,86 @@ void createHouseworkInfoWhenPeriodMonthDirtyBitChecking() {
assertThat(findTodoList.get(0).getId()).isEqualTo(todoList.getId());
assertThat(findTodoList.get(0).isDirtyBit()).isTrue();
}

@Test
@DisplayName("비단건 집안일이고 반복되는 모든 건 삭제하고 싶을 경우, " +
"담장자(HOUSEWORK_MEMBER)들의 정보가 삭제되고, " +
"해당 집안일로 생성된 HOUSEWORK_TODO의 status가 DELETE로 변경된 후 해당 집안일이 삭제된다.")
void deleteHouseworkInfoWhenNotPeriodDay() {
/*
given
*/
// User 2명 생성 - A, B
User A = userRepository.save(generateCustomUser( "[email protected]", "010-0000-0000"));
User B = userRepository.save(generateCustomUser("[email protected]", "010-1111-1111"));

// 그룹 생성 - A가 그룹장
GroupInfo groupInfo = groupInfoRepository.save(generateGroupInfo(A));
// 그룹 멤버 생성 - A, B
GroupMember gA = groupMemberRepository.save(generateGroupMember(A, groupInfo));
GroupMember gB = groupMemberRepository.save(generateGroupMember(B, groupInfo));

// 카테고리 2개 생성 - 집안일, 집 밖의 일
HouseworkCategory inWork = houseworkCategoryRepository.save(generateCustomHouseworkCategory("집안일"));
HouseworkCategory outWork = houseworkCategoryRepository.save(generateCustomHouseworkCategory("집 밖의 일"));

// 집안일 2개 생성 - 청소기, 빨래
HouseworkInfo vacuum = houseworkInfoRepository.save(generateCustom2HouseworkInfo(inWork, groupInfo, "청소기 돌리기", HouseworkPeriodType.HOUSEWORK_PERIOD_WEEK));
HouseworkInfo wash = houseworkInfoRepository.save(generateCustom2HouseworkInfo(outWork, groupInfo, "쓰레기 버리기", HouseworkPeriodType.HOUSEWORK_PERIOD_WEEK));

// 집안일 담당자 지정 - 청소기: gA, gB / 빨래: gB
HouseworkMember hA = houseworkMemberRepository.save(generateHouseworkMember(gA, vacuum));
HouseworkMember hB = houseworkMemberRepository.save(generateHouseworkMember(gB, vacuum));

// Today 집안일 생성
HouseworkTodo todayVacuum = houseworkTodoRepository.save(generateHouseworkTodo(vacuum, groupInfo, LocalDate.now()));
HouseworkTodo todayWash = houseworkTodoRepository.save(generateHouseworkTodo(wash, groupInfo, LocalDate.now()));

// TodoList 생성
List<Todo> todos = new ArrayList<>();
todos.add(Todo.builder().id(todayVacuum.getId()).build());
todos.add(Todo.builder().id(todayWash.getId()).build());
TodoList todoList = todoListRepository.save(generateTodoList(groupInfo.getId(), LocalDate.of(2023, 11, 17), todos));

HouseworkInfoDeleteRequest request = HouseworkInfoDeleteRequest.builder()
.groupId(groupInfo.getId())
.deleteType(HouseworkInfoDeleteType.ALL)
.todoId(todayVacuum.getId())
.build();

/*
when
*/
houseworkInfoService.deleteHouseworkInfo(request);

/*
then
*/
// HOUSEWORK_MEMBER가 조회되지 않아야 한다.
assertThat(houseworkMemberRepository.findById(hA.getId()).isEmpty());

// 요청 날짜 이후의 HOUSEWORKTODO는 DELETE 상태여야 한다.
List<HouseworkTodo> findTodos = houseworkTodoRepository.findHouseworkTodoByHouseworkInfo(hA.getId()).stream()
.filter(todo -> todo.getDate().isAfter(request.getDate()))
.collect(Collectors.toList());
assertAll(() -> findTodos.forEach(todo -> assertEquals("DELETE", todo.getStatus())));

// 요청 날짜 이후의 todoList는 dirtyBit이 true여야 한다.
List<TodoList> findTodoLists = todoListRepository.findByGroupInfoId(groupInfo.getId()).stream()
.filter(td -> // PERIOD에 맞는 TodoList 선별
switch (todayVacuum.getHouseworkInfo().getType()) {
case HOUSEWORK_PERIOD_DAY -> false;
case HOUSEWORK_PERIOD_EVERYDAY -> true;
case HOUSEWORK_PERIOD_WEEK ->
DayOfWeekUtils.equals(todayVacuum.getHouseworkInfo().getWeekDate(), td.getDate());
case HOUSEWORK_PERIOD_MONTH ->
Arrays.stream(todayVacuum.getHouseworkInfo().getMonthDate().split(","))
.anyMatch(d -> Integer.parseInt(d) == td.getDate().getDayOfMonth());
})
.collect(Collectors.toList());
assertAll(() -> findTodoLists.forEach(list -> assertTrue(list.isDirtyBit())));

// 집안일이 조회되지 않아야 한다.
assertThat(houseworkInfoRepository.findById(vacuum.getId())).isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.heachi.redis.define.housework.todo.repository;

import com.heachi.redis.define.housework.todo.Todo;
import com.heachi.redis.define.housework.todo.TodoList;
import com.heachi.redis.define.housework.todo.TodoUser;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
class TodoListRepositoryTest {
@Autowired
private TodoListRepository todoListRepository;

@AfterEach
void tearDown() {
todoListRepository.deleteAll();
}

@Test
@DisplayName("groupId로 TodoList를 조회한다.")
void test1() {
// given
LocalDate date = LocalDate.now();
LocalTime endTime = LocalTime.of(18, 0);

TodoList todoList = todoListRepository.save(TodoList.builder()
.groupInfoId(1L)
.date(date)
.todoList(List.of(Todo.builder()
.id(1L)
.houseworkMembers(List.of(TodoUser.builder()
.name("kms")
.email("[email protected]")
.profileImageUrl("https://google.com")
.build()))
.category("집안일")
.title("빨래")
.detail("빨래 돌리기")
.status("HOUSEWORK_TODO_INCOMPLETE")
.date(date)
.endTime(endTime)
.build()))
.build());

// when
TodoList findTodoList = todoListRepository.findByGroupInfoId(1L).get(0);

// then
assertThat(findTodoList).isNotNull();
assertThat(findTodoList.getDate()).isEqualTo(date);
}

@Test
@DisplayName("groupId와 date로 TodoList를 조회한다.")
void test2() {
// given
LocalDate date = LocalDate.now();
LocalTime endTime = LocalTime.of(18, 0);

TodoList todoList = todoListRepository.save(TodoList.builder()
.groupInfoId(1L)
.date(date)
.todoList(List.of(Todo.builder()
.id(1L)
.houseworkMembers(List.of(TodoUser.builder()
.name("kms")
.email("[email protected]")
.profileImageUrl("https://google.com")
.build()))
.category("집안일")
.title("빨래")
.detail("빨래 돌리기")
.status("HOUSEWORK_TODO_INCOMPLETE")
.date(date)
.endTime(endTime)
.build()))
.build());

// when
TodoList findTodoList = todoListRepository.findByGroupInfoIdAndDate(1L, date).get();

// then
assertThat(findTodoList).isNotNull();
assertThat(findTodoList.getDate()).isEqualTo(date);
}
}

0 comments on commit db5b84f

Please sign in to comment.