Skip to content

Commit 7006605

Browse files
committed
feat: 기관 존재 여부 검증 기능 추가
- 기관 존재 여부를 확인하는 validateCenterExists 메서드 구현 - 존재하지 않는 센터에 대해 BadRequestException 처리 추가 - 테스트 코드 작성및 검증 완
1 parent e708f74 commit 7006605

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.somemore.center.repository;
2+
3+
import com.somemore.center.domain.Center;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
import java.util.UUID;
8+
9+
@Repository
10+
public interface CenterRepository extends JpaRepository<Center, UUID> {
11+
12+
boolean existsById(UUID id);
13+
14+
default boolean doesNotExistById(UUID id) {
15+
return !existsById(id);
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.somemore.center.service.query;
2+
3+
import com.somemore.center.repository.CenterRepository;
4+
import com.somemore.center.usecase.query.CenterQueryUseCase;
5+
import com.somemore.global.exception.BadRequestException;
6+
import lombok.RequiredArgsConstructor;
7+
import org.springframework.stereotype.Service;
8+
9+
import java.util.UUID;
10+
11+
import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_CENTER;
12+
13+
@RequiredArgsConstructor
14+
@Service
15+
public class CenterQueryService implements CenterQueryUseCase {
16+
17+
private final CenterRepository centerRepository;
18+
19+
@Override
20+
public void validateCenterExists(UUID id) {
21+
22+
if (centerRepository.doesNotExistById(id)) {
23+
throw new BadRequestException(NOT_EXISTS_CENTER.getMessage());
24+
}
25+
}
26+
27+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.somemore.center.usecase.query;
2+
3+
import java.util.UUID;
4+
5+
public interface CenterQueryUseCase {
6+
7+
void validateCenterExists(UUID id);
8+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.somemore.center.repository;
2+
3+
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.center.domain.Center;
5+
import jakarta.transaction.Transactional;
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
10+
import java.util.UUID;
11+
12+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
13+
14+
@Transactional
15+
class CenterRepositoryTest extends IntegrationTestSupport {
16+
17+
@Autowired
18+
private CenterRepository centerRepository;
19+
20+
@DisplayName("기관 id로 기관 프로필을 조회할 수 있다.")
21+
@Test
22+
void findById() {
23+
//given
24+
Center center = Center.create(
25+
"기본 기관 이름",
26+
"010-1234-5678",
27+
"http://example.com/image.jpg",
28+
"기관 소개 내용",
29+
"http://example.com",
30+
"account123",
31+
"password123"
32+
);
33+
centerRepository.save(center);
34+
35+
//when
36+
Center foundCenter = centerRepository.findById(center.getId()).orElse(null);
37+
38+
//then
39+
assertThat(foundCenter).isNotNull();
40+
assertThat(foundCenter.getName()).isEqualTo("기본 기관 이름");
41+
assertThat(foundCenter.getContactNumber()).isEqualTo("010-1234-5678");
42+
assertThat(foundCenter.getImgUrl()).isEqualTo("http://example.com/image.jpg");
43+
assertThat(foundCenter.getIntroduce()).isEqualTo("기관 소개 내용");
44+
assertThat(foundCenter.getHomepageLink()).isEqualTo("http://example.com");
45+
assertThat(foundCenter.getAccountId()).isEqualTo("account123");
46+
assertThat(foundCenter.getAccountPw()).isEqualTo("password123");
47+
}
48+
49+
@DisplayName("기관 id로 기관이 존재하는지 확인할 수 있다.")
50+
@Test
51+
void existsById() {
52+
//given
53+
Center center = Center.create(
54+
"기본 기관 이름",
55+
"010-1234-5678",
56+
"http://example.com/image.jpg",
57+
"기관 소개 내용",
58+
"http://example.com",
59+
"account123",
60+
"password123"
61+
);
62+
centerRepository.save(center);
63+
64+
//when
65+
Boolean isExist = centerRepository.existsById(center.getId());
66+
67+
//then
68+
assertThat(isExist).isTrue();
69+
}
70+
71+
@DisplayName("존재하지 않는 기관 ID로 조회 시 false를 반환한다")
72+
@Test
73+
void notExistsById() {
74+
//given
75+
UUID nonExistentId = UUID.randomUUID();
76+
77+
//when
78+
Boolean isExist = centerRepository.existsById(nonExistentId);
79+
80+
//then
81+
assertThat(isExist).isFalse();
82+
}
83+
84+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.somemore.center.service.query;
2+
3+
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.center.domain.Center;
5+
import com.somemore.center.repository.CenterRepository;
6+
import com.somemore.global.exception.BadRequestException;
7+
import com.somemore.global.exception.ExceptionMessage;
8+
import org.assertj.core.api.ThrowableAssert;
9+
import org.junit.jupiter.api.DisplayName;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
13+
import java.util.UUID;
14+
15+
import static org.assertj.core.api.Assertions.assertThatCode;
16+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
17+
18+
class CenterQueryServiceTest extends IntegrationTestSupport {
19+
20+
@Autowired
21+
private CenterQueryService centerQueryService;
22+
23+
@Autowired
24+
private CenterRepository centerRepository;
25+
26+
@DisplayName("존재하지 않는 기관 ID로 조회 시 예외가 발생한다")
27+
@Test
28+
void validateNonExistentCenter() {
29+
// given
30+
UUID nonExistentId = UUID.randomUUID();
31+
32+
// when
33+
ThrowableAssert.ThrowingCallable throwingCallable = () ->
34+
centerQueryService.validateCenterExists(nonExistentId);
35+
36+
// then
37+
assertThatExceptionOfType(BadRequestException.class)
38+
.isThrownBy(throwingCallable)
39+
.withMessage(ExceptionMessage.NOT_EXISTS_CENTER.getMessage());
40+
}
41+
42+
@DisplayName("존재하는 기관 ID로 조회 시 예외가 발생하지 않는다")
43+
@Test
44+
void validateExistingCenter() {
45+
// given
46+
Center center = Center.create(
47+
"기본 기관 이름",
48+
"010-1234-5678",
49+
"http://example.com/image.jpg",
50+
"기관 소개 내용",
51+
"http://example.com",
52+
"account123",
53+
"password123"
54+
);
55+
Center savedCenter = centerRepository.save(center);
56+
57+
// when
58+
ThrowableAssert.ThrowingCallable callable = () -> centerQueryService.validateCenterExists(savedCenter.getId());
59+
60+
// then
61+
assertThatCode(callable).doesNotThrowAnyException();
62+
}
63+
64+
}

0 commit comments

Comments
 (0)