Skip to content

Commit

Permalink
test: HitTrackerService.tomorrow() 테스트 코드
Browse files Browse the repository at this point in the history
  • Loading branch information
deepredk committed Oct 17, 2023
1 parent bef9793 commit 111ebc7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
implementation ('it.ozimov:embedded-redis:0.7.3') { exclude group: "org.slf4j", module: "slf4j-simple" }
testImplementation 'com.github.codemonstur:embedded-redis:1.0.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Clock;

@Configuration
public class AppConfig {

Expand All @@ -14,4 +16,9 @@ public ObjectMapper objectMapper() {
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return objectMapper;
}

@Bean
public Clock clock() {
return Clock.systemDefaultZone();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Clock;

@Component
public class DailyHitLogScheduler {

private final HitTrackerService hitTrackerService;
private final Clock clock;

public DailyHitLogScheduler(HitTrackerService hitTrackerService) {
public DailyHitLogScheduler(HitTrackerService hitTrackerService, Clock clock) {
this.hitTrackerService = hitTrackerService;
this.clock = clock;
}

@Scheduled(cron = "0 0 0 * * ?")
public void logDailyHit() {
hitTrackerService.tomorrow();
hitTrackerService.tomorrow(clock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import it.numble.hittracker.repository.DailyHitLogRepository;
import it.numble.hittracker.repository.HitRepository;
import it.numble.hittracker.repository.UrlRepository;

import java.time.Clock;
import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -43,15 +45,15 @@ public List<DailyHitLog> getStatistics(String url) {
return dailyHitLogRepository.findAllByUrl(savedUrl);
}

public void tomorrow() {
public void tomorrow(Clock clock) {
List<DailyHitLog> logs = dailyHitLogRepository.findAll();
LocalDate today = LocalDate.now().minusDays(1);
LocalDate today = LocalDate.now(clock).minusDays(1);

// 일간조회수 모두 0으로 초기화 및 저장
List<Url> urls = urlRepository.findAll();
for (Url url : urls) {
DailyHitLog dailyHitLog = new DailyHitLog(today, hitRepository.getTodayHit(url.getUrl()), url);
DailyHitLog savedDailyHitLog = dailyHitLogRepository.save(dailyHitLog);
dailyHitLogRepository.save(dailyHitLog);

hitRepository.deleteTodayHit(url.getUrl());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
import org.springframework.boot.test.context.TestConfiguration;
import redis.embedded.RedisServer;

import java.time.LocalDate;
import java.io.IOException;
import java.time.*;
import java.util.List;

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

@SpringBootTest(classes = TestRedisConfiguration.class)
@Transactional
Expand Down Expand Up @@ -84,24 +85,56 @@ void getStatistics() {
assertThat(dailyHitLogs.size()).isEqualTo(1);
assertThat(dailyHitLogs.get(0)).isEqualTo(savedLog);
}

@Test
void tomorrow() {
// given
String url = "http://test.com";
Url savedUrl = urlRepository.save(new Url(url));
hitRepository.hit(url);

LocalDate NOW = LocalDate.of(2020, 1, 9);
ZoneId zone = ZoneOffset.systemDefault();
Instant fixedInstant = NOW.atStartOfDay(zone).toInstant();
Clock mockClock = Clock.fixed(fixedInstant, zone);

DailyHitLog givenToBeDeleted = dailyHitLogRepository.save(new DailyHitLog(LocalDate.of(2020, 1, 1), 0, savedUrl));
DailyHitLog givenToBeRemaining = dailyHitLogRepository.save(new DailyHitLog(LocalDate.of(2020, 1, 2), 0, savedUrl));

// when
hitTrackerService.tomorrow(mockClock);

// then
assertThat(hitRepository.getTodayHit(url)).isEqualTo(0);
assertThat(hitRepository.getTotalHit(url)).isEqualTo(1);

List<DailyHitLog> dailyHitLogs = dailyHitLogRepository.findAllByUrl(urlRepository.findByUrl(url).orElseThrow());
assertThat(dailyHitLogs.size()).isEqualTo(2);
assertThat(dailyHitLogs).contains(givenToBeRemaining);
assertThat(dailyHitLogs).doesNotContain(givenToBeDeleted);

DailyHitLog newLog = dailyHitLogs.stream().filter(log -> log != givenToBeRemaining).findFirst().orElseThrow();
assertThat(newLog.getDailyHit()).isEqualTo(1);
assertThat(newLog.getDate()).isEqualTo(NOW.minusDays(1));
}
}

@TestConfiguration
class TestRedisConfiguration {

private final RedisServer redisServer;

public TestRedisConfiguration(RedisProperties redisProperties) {
public TestRedisConfiguration(RedisProperties redisProperties) throws IOException {
this.redisServer = new RedisServer(redisProperties.getRedisPort());
}

@PostConstruct
public void postConstruct() {
public void postConstruct() throws IOException {
redisServer.start();
}

@PreDestroy
public void preDestroy() {
public void preDestroy() throws IOException {
redisServer.stop();
}
}

0 comments on commit 111ebc7

Please sign in to comment.