refactor: 테스트 GWT로 통일해서 사용할 수 있도록 구성 및 서브에이전트 파일 수정

This commit is contained in:
donghyeon-ka
2026-09-21 15:40:21 +09:00
parent 1bbc6a6f76
commit d63cebae36
10 changed files with 235 additions and 22 deletions
@@ -8,6 +8,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import com.study.course_registration.entity.RegistrationIdempotency;
@@ -21,6 +22,7 @@ public interface RegistrationIdempotencyRepository extends JpaRepository<Registr
Instant now);
@Modifying
@Transactional
@Query("""
delete from RegistrationIdempotency value
where value.user.id = :userId
@@ -33,6 +35,7 @@ public interface RegistrationIdempotencyRepository extends JpaRepository<Registr
@Param("now") Instant now);
@Modifying
@Transactional
@Query("delete from RegistrationIdempotency value where value.expiresAt <= :now")
int deleteExpired(@Param("now") Instant now);
}
@@ -8,7 +8,6 @@ CREATE TABLE semesters (
registration_end_at TIMESTAMP WITH TIME ZONE NOT NULL,
max_credits INTEGER NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT uk_semester_name UNIQUE (name),
CONSTRAINT ck_semester_dates CHECK (end_date >= start_date),
@@ -21,7 +20,6 @@ CREATE TABLE professors (
version BIGINT NOT NULL DEFAULT 0,
name VARCHAR(255) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL
);
@@ -33,7 +31,6 @@ CREATE TABLE subjects (
description VARCHAR(1000) NOT NULL,
credit INTEGER NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT uk_subject_code UNIQUE (code),
CONSTRAINT ck_subject_credit CHECK (credit >= 1)
@@ -46,7 +43,6 @@ CREATE TABLE users (
grade BIGINT NOT NULL,
role VARCHAR(64) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT ck_user_grade CHECK (grade >= 1)
);
@@ -62,7 +58,6 @@ CREATE TABLE lessons (
min_grade BIGINT,
allowed_role VARCHAR(64),
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT fk_lesson_subject FOREIGN KEY (subject_id) REFERENCES subjects(id),
CONSTRAINT fk_lesson_professor FOREIGN KEY (professor_id) REFERENCES professors(id),
@@ -79,7 +74,6 @@ CREATE TABLE lesson_schedules (
start_time TIME NOT NULL,
end_time TIME NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT fk_schedule_lesson FOREIGN KEY (lesson_id) REFERENCES lessons(id),
CONSTRAINT ck_schedule_day CHECK (day_of_week BETWEEN 1 AND 7),
@@ -93,7 +87,6 @@ CREATE TABLE user_lessons (
user_id VARCHAR(36) NOT NULL,
lesson_id VARCHAR(36) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
canceled_at TIMESTAMP WITH TIME ZONE,
active_lesson_id VARCHAR(36) GENERATED ALWAYS AS (CASE WHEN canceled_at IS NULL THEN lesson_id END),
CONSTRAINT fk_user_lesson_user FOREIGN KEY (user_id) REFERENCES users(id),
@@ -0,0 +1,61 @@
package com.study.course_registration;
import static org.assertj.core.api.Assertions.assertThat;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
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 org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.postgresql.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;
import com.study.course_registration.support.Scenario;
@SpringBootTest
@ActiveProfiles("prod")
@Testcontainers
class ProdProfileContextTest {
private static final PostgreSQLContainer POSTGRES =
new PostgreSQLContainer(DockerImageName.parse("postgres:16-alpine"));
static {
POSTGRES.start();
}
@DynamicPropertySource
static void database(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", POSTGRES::getJdbcUrl);
registry.add("spring.datasource.username", POSTGRES::getUsername);
registry.add("spring.datasource.password", POSTGRES::getPassword);
registry.add("server.address", () -> "127.0.0.1");
}
@Autowired DataSource dataSource;
@Test
@DisplayName("prod profile → 빈 PostgreSQL에 Flyway 적용 후 context 기동")
void prodBootsWithFlyway() throws Exception {
try (Connection connection = dataSource.getConnection()) {
Scenario.given(connection)
.when(ProdProfileContextTest::isValid)
.then(valid -> assertThat(valid).isTrue());
}
}
private static boolean isValid(Connection connection) {
try {
return connection.isValid(1);
} catch (SQLException exception) {
throw new IllegalStateException("PostgreSQL connection validation failed", exception);
}
}
}
@@ -48,6 +48,7 @@ class ArchitectureTest {
noClasses()
.that().resideInAPackage("..service..")
.and().doNotHaveSimpleName("CourseRegistrationService")
.and().doNotHaveSimpleName("IdempotencyRetentionJob")
.should().dependOnClassesThat().haveSimpleName("RegistrationIdempotencyRepository")
.check(classes);
}
@@ -183,6 +183,54 @@ class CourseRegistrationServiceIntegrationTest extends PostgresIntegrationTest {
.then(response -> assertThat(response.totalCredits()).isEqualTo(3));
}
@Test
@DisplayName("다른 학기 동일 과목·겹치는 시간표 → 현재 학기 R5·R6 계산 제외")
void duplicateAndScheduleAreScopedBySemester() {
Semester otherSemester = fixture.semester(DomainFixtures.semester(
null,
"2027-2",
TestData.TODAY.plusDays(150),
TestData.TODAY.plusDays(260),
-3600,
3600,
18));
Lesson otherLesson = fixture.lesson(
DomainFixtures.lesson(data.subject(), data.professor(), otherSemester));
fixture.schedule(DomainFixtures.schedule(otherLesson, DayOfWeek.MONDAY, 9, 11));
fixture.registration(DomainFixtures.registration(data.student(), otherLesson));
Scenario.given(data)
.when(current -> service.register(
current.student().getId(),
new CourseRegistrationRequest(current.lesson().getId())))
.then(response -> {
assertThat(response.lessonId()).isEqualTo(data.lesson().getId());
assertThat(response.totalCredits()).isEqualTo(3);
});
}
@Test
@DisplayName("취소 후 TTL 내 같은 Idempotency-Key 재시도 → 최초 POST 결과 replay")
void idempotencyReplayAfterCancel() {
CourseRegistrationResponse first = service.register(
data.student().getId(),
new CourseRegistrationRequest(data.lesson().getId()),
"retry-after-cancel");
service.cancel(data.student().getId(), first.registrationId());
Scenario.given("retry-after-cancel")
.when(key -> service.register(
data.student().getId(),
new CourseRegistrationRequest(data.lesson().getId()),
key))
.then(replayed -> {
assertThat(replayed.registrationId()).isEqualTo(first.registrationId());
assertThat(replayed.registeredAt()).isEqualTo(first.registeredAt());
assertThat(registrations.countByLesson_IdAndCanceledAtIsNull(data.lesson().getId()))
.isZero();
});
}
@Test
@DisplayName("신청 기간 종료 → REGISTRATION_PERIOD_CLOSED")
void closedPeriod() {
@@ -0,0 +1,27 @@
package com.study.course_registration.service;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.time.Clock;
import java.time.ZoneOffset;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.study.course_registration.repository.RegistrationIdempotencyRepository;
import com.study.course_registration.support.fixture.TestData;
class IdempotencyRetentionJobTest {
@Test
@DisplayName("cleanup → 현재 시각까지 만료된 idempotency 삭제")
void cleanup() {
RegistrationIdempotencyRepository repository = mock(RegistrationIdempotencyRepository.class);
Clock clock = Clock.fixed(TestData.NOW, ZoneOffset.UTC);
IdempotencyRetentionJob job = new IdempotencyRetentionJob(clock, repository);
job.cleanup();
verify(repository).deleteExpired(TestData.NOW);
}
}
@@ -153,8 +153,16 @@ class RegistrationValidatorTest {
RegistrationFixture fixture = RegistrationFixture.student(2).semesterWithMaxCredits(max)
.alreadyRegistered("old", oldCredit).applyingFor("new", newCredit);
Scenario.given(fixture).when(RegistrationFixture::attempt).then(REJECTIONS, expected);
assertThat(fixture.totalCreditsAfter()).isEqualTo(oldCredit + newCredit);
if (expected == null) {
Scenario.given(fixture)
.when(RegistrationFixture::attempt)
.then(ignored -> assertThat(fixture.totalCreditsAfter())
.isEqualTo(oldCredit + newCredit));
} else {
Scenario.given(fixture)
.when(RegistrationFixture::attempt)
.thenRejectedWith(REJECTIONS, expected);
}
}
@Test