Skip to content

Commit

Permalink
fix: #91 골 생성시 참여자 목록이 null이거나 빈값인 경우 에러 발생하는 버그 해결 (#93)
Browse files Browse the repository at this point in the history
* bug: 골 참여자 수가 0명일 때 골 추가되는 버그 해결

* bug: 골 참여자 수가 0명일 때 골 추가되는 버그 해결

* docs: api 문서 업데이트
  • Loading branch information
jhsseonn authored Mar 28, 2024
1 parent 63fd028 commit 4bdd32b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.backend.blooming.goal.application;

import com.backend.blooming.common.util.DayUtil;
import com.backend.blooming.friend.infrastructure.repository.FriendRepository;
import com.backend.blooming.goal.application.dto.CreateGoalDto;
import com.backend.blooming.goal.application.dto.ReadAllGoalDto;
Expand All @@ -12,13 +13,12 @@
import com.backend.blooming.goal.domain.Goal;
import com.backend.blooming.goal.domain.GoalTeam;
import com.backend.blooming.goal.infrastructure.repository.GoalRepository;
import com.backend.blooming.notification.application.NotificationService;
import com.backend.blooming.stamp.domain.Stamp;
import com.backend.blooming.stamp.infrastructure.repository.StampRepository;
import com.backend.blooming.notification.application.NotificationService;
import com.backend.blooming.user.application.exception.NotFoundUserException;
import com.backend.blooming.user.domain.User;
import com.backend.blooming.user.infrastructure.repository.UserRepository;
import com.backend.blooming.common.util.DayUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -47,6 +47,9 @@ public Long createGoal(final CreateGoalDto createGoalDto) {
}

private List<User> getUsers(final List<Long> userIds) {
if (userIds == null || userIds.isEmpty()) {
throw new InvalidGoalException.InvalidInvalidUsersSize();
}
return userRepository.findAllByUserIds(userIds);
}

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/backend/blooming/goal/domain/Goal.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
public class Goal extends BaseTimeEntity {

private static final String MEMO_DEFAULT = "";
private static final int TEAMS_MAXIMUM_LENGTH = 5;
private static final int MAX_LENGTH_OF_NAME = 50;
private static final int START_INDEX_OF_NAME = 0;
private static final int END_INDEX_OF_NAME = 50;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.backend.blooming.goal.application;

import com.backend.blooming.configuration.IsolateDatabase;
import com.backend.blooming.goal.application.dto.CreateGoalDto;
import com.backend.blooming.goal.application.dto.ReadAllGoalDto;
import com.backend.blooming.goal.application.dto.ReadGoalDetailDto;
import com.backend.blooming.goal.application.exception.DeleteGoalForbiddenException;
Expand All @@ -16,6 +17,8 @@
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
Expand Down Expand Up @@ -46,16 +49,34 @@ class GoalServiceTest extends GoalServiceTestFixture {
assertThat(goalId).isPositive();
}

@ParameterizedTest
@NullAndEmptySource
void_생성시__참여자_목록이_null이거나_비어있는_경우_예외를_발생한다(final List<Long> teamUserIds) {
// given
final CreateGoalDto_참여자_목록이_비어있는__생성_dto = new CreateGoalDto(
_제목,
_메모,
_시작일,
_종료일,
유효한_사용자_아이디,
teamUserIds
);

// when & then
assertThatThrownBy(() -> goalService.createGoal(골_참여자_목록이_비어있는__생성_dto))
.isInstanceOf(InvalidGoalException.InvalidInvalidUsersSize.class);
}

@Test
void_생성시_존재하지_않는_사용자가_관리자인_경우_예외를_발생한다() {
// when
// when & then
assertThatThrownBy(() -> goalService.createGoal(존재하지_않는_사용자가_관리자인__생성_dto))
.isInstanceOf(NotFoundUserException.class);
}

@Test
void_생성시_친구가_아닌_사용자가_참여자로_있는_경우_예외를_발생한다() {
// when
// when & then
assertThatThrownBy(() -> goalService.createGoal(친구가_아닌_사용자가_참여자로_있는__생성_dto))
.isInstanceOf(InvalidGoalException.InvalidInvalidUserToParticipate.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ class GoalControllerTest extends GoalControllerTestFixture {
));
}

@Test
void_생성시__참여자_목록이_null이거나_비어있는_경우_400_예외를_발생시킨다() throws Exception {
// given
given(tokenProvider.parseToken(액세스_토큰_타입, 액세스_토큰)).willReturn(사용자_토큰_정보);
given(userRepository.existsByIdAndDeletedIsFalse(사용자_토큰_정보.userId())).willReturn(true);
given(goalService.createGoal(유효한__생성_dto))
.willThrow(new InvalidGoalException.InvalidInvalidUsersSize());

// when & then
mockMvc.perform(post("/goals")
.header("X-API-VERSION", 1)
.header(HttpHeaders.AUTHORIZATION, 액세스_토큰)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(요청한__dto))
).andExpectAll(
status().isBadRequest(),
jsonPath("$.message").exists()
).andDo(print());
}

@Test
void_생성시_관리자와_친구가_아닌_사용자가_참여자로_있는_경우_400_예외를_발생시킨다() throws Exception {
// given
Expand Down

0 comments on commit 4bdd32b

Please sign in to comment.