Skip to content

Commit

Permalink
Feat/#25 Log를 저장할 떄 appKey를 검증하도록 수정 (#84)
Browse files Browse the repository at this point in the history
* feat: appKey가 존재하는 지 확인하는 쿼리 메서드 추가

- existsByToken메서드 추가

* feat: Log를 저장할 때 appKey가 존재하는 지 확인하도록 수정

* test: Log를 저장할 때 appKey가 존재하는 지 확인하도록 수정

- 존재하지 않는 Application Key로 Log를 저장할 수 없다.
- 잘못된 형태의 Application Key로 Log를 저장할 수 없다.

* refactor: Log를 저장할 때 appKey 검증 로직 리팩토링
  • Loading branch information
miiiinju1 authored and LuizyHub committed Aug 19, 2024
1 parent d9c3e1e commit c58f81e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import info.logbat.domain.log.application.payload.request.CreateLogServiceRequest;
import info.logbat.domain.log.domain.Log;
import info.logbat.domain.log.repository.LogRepository;
import info.logbat.domain.project.repository.AppJpaRepository;
import java.time.LocalDateTime;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -12,16 +14,29 @@
public class LogService {

private final LogRepository logRepository;
private final AppJpaRepository appJpaRepository;

public long saveLog(CreateLogServiceRequest request) {
String appKey = request.appKey();
String level = request.level();
String data = request.data();
LocalDateTime timestamp = request.timestamp();
// TODO Log 저장 전 Application ID 체크 로직 추가 필요

String appKey = getAppKey(request.appKey());

Log log = Log.of(appKey, level, data, timestamp);

return logRepository.save(log);
}

private String getAppKey(String appKey) {
try {
UUID appKeyUuid = UUID.fromString(appKey);
if (appJpaRepository.existsByToken(appKeyUuid)) {
return appKeyUuid.toString();
}
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("잘못된 형식의 Application Key 입니다.");
}
throw new IllegalArgumentException("존재하지 않는 Application Key 입니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
@Repository
public interface AppJpaRepository extends JpaRepository<App, Long> {

Optional<App> findByToken(@NonNull UUID token);
Optional<App> findByToken(@NonNull UUID token);

Optional<App> findByProject_IdAndId(@NonNull Long id, @NonNull Long id1);
Optional<App> findByProject_IdAndId(@NonNull Long id, @NonNull Long id1);

List<App> findByProject_Id(@NonNull Long id);
List<App> findByProject_Id(@NonNull Long id);

boolean existsByToken(@NonNull UUID token);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package info.logbat.domain.log.application;

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

import info.logbat.domain.log.application.payload.request.CreateLogServiceRequest;
import info.logbat.domain.log.domain.Log;
import info.logbat.domain.log.domain.enums.Level;
import info.logbat.domain.log.repository.LogRepository;
import info.logbat.domain.project.domain.App;
import info.logbat.domain.project.domain.Project;
import info.logbat.domain.project.domain.enums.AppType;
import info.logbat.domain.project.repository.AppJpaRepository;
import info.logbat.domain.project.repository.ProjectJpaRepository;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.UUID;
Expand All @@ -28,17 +34,34 @@ class LogServiceTest {
@Autowired
private LogRepository logRepository;

private static final String__문자열 = UUID.randomUUID().toString();
@Autowired
private ProjectJpaRepository projectJpaRepository;

@Autowired
private AppJpaRepository appJpaRepository;

@DisplayName("Log를 저장할 수 있다.")
@Test
void saveLog() {
// given
String 프로젝트_이름 = "테스트_프로젝트";
Project 프로젝트 = Project.from(프로젝트_이름);

AppType_타입 = AppType.JAVA;

App 앱 = App.of(프로젝트, 앱_타입);

projectJpaRepository.save(프로젝트);
appJpaRepository.save(앱);

UUID_키 = 앱.getToken();

String 로그_레벨 = "INFO";
String 로그_데이터 = "테스트_로그_데이터";
LocalDateTime 타임스탬프 = LocalDateTime.of(2021, 1, 1, 0, 0, 0);

CreateLogServiceRequest 요청_DTO = new CreateLogServiceRequest(앱__문자열, 로그_레벨, 로그_데이터, 타임스탬프);
CreateLogServiceRequest 요청_DTO = new CreateLogServiceRequest(앱_키.toString(), 로그_레벨, 로그_데이터,
타임스탬프);

// when
long 저장된_ID = logService.saveLog(요청_DTO);
Expand All @@ -49,7 +72,43 @@ void saveLog() {
assertThat(찾은_로그).isPresent()
.get()
.extracting("logId", "appKey", "level", "data.value", "timestamp")
.contains(저장된_ID, 앱__문자열, Level.INFO, "테스트_로그_데이터", 타임스탬프);
.contains(저장된_ID, 앱_키.toString(), Level.INFO, "테스트_로그_데이터", 타임스탬프);
}

@DisplayName("존재하지 않는 Application Key로 Log를 저장할 수 없다.")
@Test
void saveLogWithNonExistentAppKey() {
// given
String 존재하지_않는___문자열 = UUID.randomUUID().toString();
String 로그_레벨 = "INFO";
String 로그_데이터 = "테스트_로그_데이터";
LocalDateTime 타임스탬프 = LocalDateTime.of(2021, 1, 1, 0, 0, 0);

CreateLogServiceRequest 요청_DTO = new CreateLogServiceRequest(존재하지_않는___문자열, 로그_레벨, 로그_데이터,
타임스탬프);

// when & then
assertThatThrownBy(() -> logService.saveLog(요청_DTO))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("존재하지 않는 Application Key 입니다.");
}

@DisplayName("잘못된 형태의 Application Key로 Log를 저장할 수 없다.")
@Test
void saveLogWithInvalidAppKey() {
// given
String 잘못된___문자열 = "잘못된_앱_키_문자열";
String 로그_레벨 = "INFO";
String 로그_데이터 = "테스트_로그_데이터";
LocalDateTime 타임스탬프 = LocalDateTime.of(2021, 1, 1, 0, 0, 0);

CreateLogServiceRequest 요청_DTO = new CreateLogServiceRequest(잘못된___문자열, 로그_레벨, 로그_데이터,
타임스탬프);

// when & then
assertThatThrownBy(() -> logService.saveLog(요청_DTO))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("잘못된 형식의 Application Key 입니다.");
}

}

0 comments on commit c58f81e

Please sign in to comment.