-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Be/#5 : Spring Batch를 적용한 문제 완전 삭제 기능 구현
- Loading branch information
Showing
18 changed files
with
264 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
config.stopBubbling = true | ||
lombok.addLombokGeneratedAnnotation = true |
47 changes: 47 additions & 0 deletions
47
backend/src/main/java/com/psq/backend/batch/BatchConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.psq.backend.batch; | ||
|
||
import com.psq.backend.problem.domain.Problem; | ||
import com.psq.backend.problem.persistence.ProblemRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | ||
import org.springframework.batch.repeat.RepeatStatus; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Slf4j | ||
@Configuration | ||
@EnableBatchProcessing | ||
@RequiredArgsConstructor | ||
public class BatchConfiguration { | ||
|
||
private final JobBuilderFactory jobBuilderFactory; | ||
private final StepBuilderFactory stepBuilderFactory; | ||
|
||
private final ProblemRepository problemRepository; | ||
|
||
@Bean | ||
public Job removeDeletedProblemJob() { | ||
log.info("[문제 완전 삭제] job start"); | ||
Job job = jobBuilderFactory.get("job") | ||
.start(removeDeletedProblemStep()) | ||
.build(); | ||
return job; | ||
} | ||
|
||
@Bean | ||
public Step removeDeletedProblemStep() { | ||
log.info("[문제 완전 삭제] step start"); | ||
return stepBuilderFactory.get("step") | ||
.tasklet((contribution, chunkContext) -> { | ||
long deletedCount = problemRepository.deleteSoftDeletedProblem(); | ||
log.info("[문제 완전 삭제] 삭제된 문제 : {} 문제", deletedCount); | ||
return RepeatStatus.FINISHED; | ||
}) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 0 additions & 67 deletions
67
backend/src/main/java/com/psq/backend/global/config/BatchConfiguration.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ spring: | |
show-sql: true | ||
hibernate: | ||
ddl-auto: create | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ spring: | |
server: | ||
servlet: | ||
session: | ||
timeout: 315360000 | ||
timeout: 315350000 |
101 changes: 101 additions & 0 deletions
101
backend/src/test/java/com/psq/backend/batch/RemoveDeletedProblemTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.psq.backend.batch; | ||
|
||
import com.psq.backend.member.domain.Member; | ||
import com.psq.backend.member.persistence.MemberRepository; | ||
import com.psq.backend.problem.domain.Category; | ||
import com.psq.backend.problem.domain.Problem; | ||
import com.psq.backend.problem.persistence.ProblemRepository; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.batch.core.*; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
import org.springframework.batch.test.context.SpringBatchTest; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.Month; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
@SpringBatchTest | ||
@ActiveProfiles("test") | ||
@SpringBootTest | ||
public class RemoveDeletedProblemTest { | ||
|
||
@Autowired | ||
private BatchScheduler batchScheduler; | ||
|
||
@Autowired | ||
private JobLauncher jobLauncher; | ||
|
||
@Autowired | ||
private ProblemRepository problemRepository; | ||
|
||
@Autowired | ||
private MemberRepository memberRepository; | ||
|
||
@Autowired | ||
private Job problemDeleteJob; | ||
|
||
@AfterEach | ||
public void cleanup() { | ||
problemRepository.deleteAll(); | ||
memberRepository.deleteAll(); | ||
} | ||
|
||
@BeforeEach | ||
public void setup() { | ||
Member member = new Member("[email protected]", "password", "salt"); | ||
memberRepository.save(member); | ||
|
||
LocalDateTime dateTime = LocalDateTime.of(2023, Month.AUGUST, 14, 0, 0); | ||
|
||
// 삭제 대상 Problem 데이터 7개 생성 | ||
for (int i = 0; i < 7; i++) { | ||
Problem problem = new Problem(member, "title" + i, "url" + i, i % 5 + 1, Category.DFS, false); | ||
problemRepository.save(problem); | ||
problem.softDelete(); | ||
ReflectionTestUtils.setField(problem, "updatedAt", dateTime); | ||
problemRepository.save(problem); | ||
} | ||
|
||
// 일반 Problem 데이터 5개 생성 | ||
for (int i = 0; i < 5; i++) { | ||
problemRepository.save(new Problem(member, "title" + (i + 7), "url" + (i + 7), i % 5 + 1, Category.DFS, false)); | ||
} | ||
} | ||
|
||
@Test | ||
@DisplayName("BatchScheduler를 통해 problemDeleteJob을 실행시킨다") | ||
public void removeDeletedProblemJobTest() throws Exception { | ||
// when | ||
batchScheduler.deleteProblemJob(); | ||
List<Problem> actual = problemRepository.findAll(); | ||
|
||
// then | ||
assertThat(actual).hasSize(5); | ||
} | ||
|
||
@Test | ||
@DisplayName("삭제된지 3일이 지난 문제는 완전 삭제된다") | ||
public void removeDeletedProblemTest() throws Exception { | ||
// when | ||
JobParameters jobParameters = new JobParametersBuilder() | ||
.addLong("time", System.currentTimeMillis()) | ||
.toJobParameters(); | ||
|
||
JobExecution execution = jobLauncher.run(problemDeleteJob, jobParameters); | ||
|
||
List<Problem> actual = problemRepository.findAll(); | ||
|
||
// then 전체 문제(12) - 삭제된 문제(7) = 5 | ||
assertThat(execution.getStatus()).isEqualTo(BatchStatus.COMPLETED); | ||
assertThat(actual).hasSize(5); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
backend/src/test/java/com/psq/backend/common/annotation/TestBatchConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.psq.backend.common.annotation; | ||
|
||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@EnableAutoConfiguration | ||
@EnableBatchProcessing | ||
public class TestBatchConfig {} |
Oops, something went wrong.