Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/#25 Log를 저장할 떄 appKey를 검증하도록 수정 #84

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,30 @@
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 체크 로직 추가 필요

UUID appKeyUuid = getAppKey(appKey);

if (!appJpaRepository.existsByToken(appKeyUuid)) {
throw new IllegalArgumentException("존재하지 않는 Application Key 입니다.");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분도 getAppKey() 부분에 들어가 있으면 어떨까요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋아요~~


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

return logRepository.save(log);
}

private UUID getAppKey(String appKey) {
try {
return UUID.fromString(appKey);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("잘못된 형식의 Application Key 입니다.");
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

린트 크아ㅏㅏㅏㄱ

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 입니다.");
}

}
Loading