Skip to content

Commit

Permalink
#330 [feat] 메트릭 전송 비즈니스 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
KWY0218 committed Aug 30, 2024
1 parent e3a0529 commit 22a34f0
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/main/java/com/asap/server/service/internal/MetricsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.asap.server.service.internal;

import static com.asap.server.common.exception.Error.INVALID_DATE_FORMAT_EXCEPTION;

import com.asap.server.common.exception.model.BadRequestException;
import com.asap.server.infra.slack.MetricsEvent;
import com.asap.server.persistence.repository.internal.MetricsRepository;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.HashMap;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MetricsService {
private final MetricsRepository metricsRepository;
private final ApplicationEventPublisher publisher;

public void sendMetrics(final String fromStr, final String toStr) {
if (!isValidDate(fromStr) || !isValidDate(toStr)) {
throw new BadRequestException(INVALID_DATE_FORMAT_EXCEPTION);
}

LocalDateTime from = LocalDate.parse(fromStr, DateTimeFormatter.ISO_LOCAL_DATE).atStartOfDay();
LocalDateTime to = LocalDate.parse(toStr, DateTimeFormatter.ISO_LOCAL_DATE).atStartOfDay();

Map<String, String> metrics = new HashMap<>();
metrics.put("개설된 총 회의 수", String.valueOf(metricsRepository.countTotalMeetingCount(from, to)));
metrics.put("사용한 총 사용자 수", String.valueOf(metricsRepository.countTotalUserCount(from, to)));
metrics.put("확정된 총 회의 수", String.valueOf(metricsRepository.countTotalConfirmedMeetingCount(from, to)));

publisher.publishEvent(new MetricsEvent(metrics));
}

private boolean isValidDate(final String dateStr) {
try {
LocalDate.parse(dateStr, DateTimeFormatter.ISO_LOCAL_DATE);
return true;
} catch (DateTimeParseException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.asap.server.service.internal;


import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.asap.server.common.exception.model.BadRequestException;
import com.asap.server.infra.slack.MetricsEvent;
import com.asap.server.persistence.repository.internal.MetricsRepository;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationEventPublisher;

@ExtendWith(MockitoExtension.class)
class MetricsServiceTest {
@Mock
private MetricsRepository metricsRepository;
@Mock
private ApplicationEventPublisher publisher;
@InjectMocks
private MetricsService metricsService;

@DisplayName("날짜 형식은 yyyy-MM-dd 형식으로 입력한다.")
@Test
void test() {
// given
String fromStr = "2024-08-24";
String toStr = "2024-08-26";

LocalDateTime from = LocalDate.parse(fromStr, DateTimeFormatter.ISO_LOCAL_DATE).atStartOfDay();
LocalDateTime to = LocalDate.parse(toStr, DateTimeFormatter.ISO_LOCAL_DATE).atStartOfDay();

when(metricsRepository.countTotalMeetingCount(from, to)).thenReturn(1L);
when(metricsRepository.countTotalUserCount(from, to)).thenReturn(1L);
when(metricsRepository.countTotalConfirmedMeetingCount(from, to)).thenReturn(1L);
Map<String, String> metrics = Map.of(
"개설된 총 회의 수", "1",
"사용한 총 사용자 수", "1",
"확정된 총 회의 수", "1"
);

// when
metricsService.sendMetrics(fromStr, toStr);

// then
verify(publisher, times(1)).publishEvent(new MetricsEvent(metrics));
}

@DisplayName("날짜 형식(yyyy-MM-dd)과 다른 형식으로 입력했을 때, BadRequestException을 반환한다.")
@ParameterizedTest
@ValueSource(strings = {"2024/08/24", "2024 08 24", "2024-13-24", "2024/08/32", "2024-13-30"})
void test2(String fromStr) {
// given
String toStr = "2024-08-26";

// when
assertThatThrownBy(() -> metricsService.sendMetrics(fromStr, toStr))
.isInstanceOf(BadRequestException.class)
.hasMessage("유효하지 않은 날짜를 입력했습니다.");
}
}

0 comments on commit 22a34f0

Please sign in to comment.