Skip to content

Commit

Permalink
test: HitTrackerService.getStatistics() 테스트 코드
Browse files Browse the repository at this point in the history
  • Loading branch information
deepredk committed Oct 17, 2023
1 parent 546f6df commit bef9793
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 26 deletions.
13 changes: 7 additions & 6 deletions src/main/java/it/numble/hittracker/entity/DailyHitLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;

import java.time.LocalDate;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -27,10 +24,14 @@ public class DailyHitLog {
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate date;
private int dailyHit;
@ManyToOne
@JoinColumn(name = "url_id")
private Url url;

public DailyHitLog(LocalDate date, int dailyHit) {
public DailyHitLog(LocalDate date, int dailyHit, Url url) {
this.date = date;
this.dailyHit = dailyHit;
this.url = url;
}

public boolean isSevenDaysAgo(LocalDate today) {
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/it/numble/hittracker/entity/Url.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,8 @@ public class Url {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String url;
@OneToMany
@JoinColumn(name = "url_id")
private List<DailyHitLog> dailyHitLogs;

public Url(String url) {
this.url = url;
this.dailyHitLogs = new ArrayList<>();
}

public void addLog(DailyHitLog savedDailyHitLog) {
dailyHitLogs.add(savedDailyHitLog);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import it.numble.hittracker.entity.DailyHitLog;
import java.util.List;

import it.numble.hittracker.entity.Url;
import org.springframework.data.jpa.repository.JpaRepository;

public interface DailyHitLogRepository extends JpaRepository<DailyHitLog, Long> {

List<DailyHitLog> findAllByUrl(Url url);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public void hit(String url) {
}

public List<DailyHitLog> getStatistics(String url) {
return urlRepository.findByUrl(url).orElseThrow().getDailyHitLogs();
Url savedUrl = urlRepository.findByUrl(url).orElseGet(() -> urlRepository.save(new Url(url)));
return dailyHitLogRepository.findAllByUrl(savedUrl);
}

public void tomorrow() {
Expand All @@ -49,12 +50,10 @@ public void tomorrow() {
// 일간조회수 모두 0으로 초기화 및 저장
List<Url> urls = urlRepository.findAll();
for (Url url : urls) {
DailyHitLog dailyHitLog = new DailyHitLog(today, hitRepository.getTodayHit(url.getUrl()));
DailyHitLog dailyHitLog = new DailyHitLog(today, hitRepository.getTodayHit(url.getUrl()), url);
DailyHitLog savedDailyHitLog = dailyHitLogRepository.save(dailyHitLog);

hitRepository.deleteTodayHit(url.getUrl());
url.addLog(savedDailyHitLog);
urlRepository.save(url);
}

// 7일이 지난 일간조회수는 삭제
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,16 @@ public class HitTrackerRepositoryTests {
@Test
void Url_및_DailyHitLog_신규_저장() {
Url url = new Url("https://example.com");
urlRepository.save(url);
Url savedUrl = urlRepository.save(url);

LocalDate today = LocalDate.now();
DailyHitLog dailyHitLog = new DailyHitLog(today, 100);
DailyHitLog dailyHitLog = new DailyHitLog(today, 100, savedUrl);

DailyHitLog savedDailyHitLog = dailyHitLogRepository.save(dailyHitLog);

url.getDailyHitLogs().add(savedDailyHitLog);
urlRepository.save(url);
dailyHitLogRepository.save(dailyHitLog);

List<DailyHitLog> dailyHitLogs = dailyHitLogRepository.findAllByUrl(url);
Url updatedUrl = urlRepository.findById(url.getId()).orElse(null);
assertThat(updatedUrl).isNotNull();

List<DailyHitLog> dailyHitLogs = updatedUrl.getDailyHitLogs();
assertThat(dailyHitLogs).isNotNull();
assertThat(dailyHitLogs).hasSize(1);
assertThat(dailyHitLogs.get(0).getDate()).isEqualTo(today);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import it.numble.hittracker.common.config.redis.RedisProperties;
import it.numble.hittracker.controller.response.UrlHitInfoResponse;
import it.numble.hittracker.entity.DailyHitLog;
import it.numble.hittracker.entity.Url;
import it.numble.hittracker.repository.DailyHitLogRepository;
import it.numble.hittracker.repository.HitRepository;
import it.numble.hittracker.repository.UrlRepository;
Expand All @@ -15,6 +17,9 @@
import org.springframework.boot.test.context.TestConfiguration;
import redis.embedded.RedisServer;

import java.time.LocalDate;
import java.util.List;

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

@SpringBootTest(classes = TestRedisConfiguration.class)
Expand All @@ -34,6 +39,8 @@ public class HitTrackerServiceTests {
@BeforeEach
void setUp() {
hitRepository.flushAll();
urlRepository.deleteAll();
dailyHitLogRepository.deleteAll();
}

@Test
Expand Down Expand Up @@ -62,6 +69,21 @@ void hit() {
assertThat(hitRepository.getTodayHit(url)).isEqualTo(1);
assertThat(hitRepository.getTotalHit(url)).isEqualTo(1);
}

@Test
void getStatistics() {
// given
String url = "http://test.com";
Url savedUrl = urlRepository.save(new Url(url));
DailyHitLog savedLog = dailyHitLogRepository.save(new DailyHitLog(LocalDate.of(2020, 1, 1), 3, savedUrl));

// when
List<DailyHitLog> dailyHitLogs = hitTrackerService.getStatistics(url);

// then
assertThat(dailyHitLogs.size()).isEqualTo(1);
assertThat(dailyHitLogs.get(0)).isEqualTo(savedLog);
}
}

@TestConfiguration
Expand Down

0 comments on commit bef9793

Please sign in to comment.