Skip to content

Commit

Permalink
Merge pull request #64 from co-niverse/feat/log
Browse files Browse the repository at this point in the history
로그 스펙 변경
  • Loading branch information
GIVEN53 authored Oct 24, 2023
2 parents 202a98e + 1bd9f27 commit be7814c
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.coniverse.dangjang.domain.guide.bloodsugar.document.TodayGuide;
import com.coniverse.dangjang.domain.guide.exercise.dto.ExerciseDayGuide;
import com.coniverse.dangjang.domain.guide.weight.dto.WeightDayGuide;
import com.coniverse.dangjang.domain.log.dto.request.UserLog;
import com.coniverse.dangjang.domain.user.dto.response.UserDiabeticResponse;

/**
* 하루 가이드 조회 응답
Expand All @@ -15,6 +15,6 @@
* @since 1.0.0
*/
public record DayGuideResponse(String nickname, LocalDate date, List<TodayGuide> bloodSugars, WeightDayGuide weight, ExerciseDayGuide exercise,
boolean notification, UserLog userLog) {
boolean notification, UserDiabeticResponse userLog) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import com.coniverse.dangjang.domain.guide.weight.document.WeightGuide;
import com.coniverse.dangjang.domain.guide.weight.dto.WeightDayGuide;
import com.coniverse.dangjang.domain.guide.weight.service.WeightGuideSearchService;
import com.coniverse.dangjang.domain.log.dto.request.UserLog;
import com.coniverse.dangjang.domain.notification.service.NotificationService;
import com.coniverse.dangjang.domain.user.dto.response.UserDiabeticResponse;
import com.coniverse.dangjang.domain.user.entity.User;
import com.coniverse.dangjang.domain.user.service.UserSearchService;

Expand Down Expand Up @@ -57,7 +57,8 @@ public DayGuideResponse getDayGuide(String oauthId, String date) {
WeightDayGuide weightDayGuide = getWeightGuide(oauthId, date);
ExerciseDayGuide exerciseDayGuide = getExerciseGuide(oauthId, localDate);
Boolean existsNotification = notificationService.isExistsNotReadNotification(oauthId);
UserLog userLog = new UserLog(user.getGender().isTrue(), user.getBirthYear(), user.isDiabetic(), user.getDiabetesYear(), weightDayGuide.alert());
UserDiabeticResponse userLog = new UserDiabeticResponse(user.getGender().isTrue(), user.getBirthYear(), user.isDiabetic(), user.getDiabetesYear(),
weightDayGuide.alert());
return new DayGuideResponse(userNickname, localDate, bloodSugarTodayGuide, weightDayGuide, exerciseDayGuide, existsNotification, userLog);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -28,9 +26,14 @@
public class AppLogController {
private final LogService logService;

/**
* 앱 로그를 post 요청한다.
*
* @since 1.0.0
*/
@PostMapping
public ResponseEntity<SuccessSingleResponse<?>> post(@Valid @RequestBody LogRequest request, @AuthenticationPrincipal User principal) {
logService.sendLog(request, principal.getUsername());
public ResponseEntity<SuccessSingleResponse<?>> post(@Valid @RequestBody LogRequest request) {
logService.sendLog(request);
return ResponseEntity.ok(new SuccessSingleResponse<>(HttpStatus.OK.getReasonPhrase(), null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
* @author TEO
* @since 1.0.0
*/
public record AppLog(Event event, String screenName, int logVersion, String sessionId, Map<String, Object> logData, boolean diabetic, int diabetesYear) {
public record AppLog(Event event, String screenName, int logVersion, String appVersion, String sessionId, Map<String, Object> logData) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.Map;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Positive;

/**
Expand All @@ -11,6 +13,7 @@
* @author TEO
* @since 1.0.0
*/
public record LogRequest(@NotBlank String eventLogName, @NotBlank String screenName, @Positive int logVersion, @NotBlank String sessionId,
Map<String, Object> logData) {
public record LogRequest(@NotBlank String eventLogName, @NotBlank String screenName, @Positive int logVersion,
@NotBlank @Pattern(regexp = "^\\d+\\.\\d+\\.\\d$") String appVersion, @NotBlank String sessionId,
@NotNull Map<@NotBlank String, Object> logData) {
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ public enum Event implements EnumCode {
HOME_CALENDAR_CLICK("home_calendar_click"),
BLOOD_SUGAR_STAY_TIME("bloodsugar_stay_time"),
HEALTH_CONNECT_JOIN_CLICK("healthconnect_join_click"),
HEALTH_CONNECT_MANUAL("healthconnect_manual");
HEALTH_CONNECT_MANUAL("healthconnect_manual"),
SIGNUP_STAY_TIME("signup_stay_time"),
SIGNUP_AFTER_TIME("signup_after_time"),
EXPOSURE_LOG("exposure_log");

private final String title;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.coniverse.dangjang.domain.log.dto.app.AppLog;
import com.coniverse.dangjang.domain.log.dto.request.LogRequest;
import com.coniverse.dangjang.domain.log.enums.Event;
import com.coniverse.dangjang.domain.user.entity.User;
import com.coniverse.dangjang.global.util.EnumFindUtil;

/**
Expand All @@ -23,5 +22,5 @@ public interface LogMapper {
* @since 1.0.0
*/
@Mapping(target = "event", expression = "java(EnumFindUtil.findByTitle(Event.class, request.eventLogName()))")
AppLog toAppLog(LogRequest request, User user);
AppLog toAppLog(LogRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import com.coniverse.dangjang.domain.log.dto.app.AppLog;
import com.coniverse.dangjang.domain.log.dto.request.LogRequest;
import com.coniverse.dangjang.domain.log.mapper.LogMapper;
import com.coniverse.dangjang.domain.user.entity.User;
import com.coniverse.dangjang.domain.user.service.UserSearchService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -25,7 +23,6 @@
@Slf4j
public class LogService {
private final RestTemplate restTemplate;
private final UserSearchService userSearchService;
private final LogMapper logMapper;
@Value("${fluentbit.app-log-url}")
private String url;
Expand All @@ -35,9 +32,8 @@ public class LogService {
*
* @since 1.0.0
*/
public void sendLog(LogRequest request, String oauthId) {
User user = userSearchService.findUserByOauthId(oauthId);
AppLog appLog = logMapper.toAppLog(request, user);
public void sendLog(LogRequest request) {
AppLog appLog = logMapper.toAppLog(request);
try {
restTemplate.postForEntity(url, appLog, String.class);
} catch (ResourceAccessException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.coniverse.dangjang.domain.user.dto.response;

/**
* 유저 당뇨 정보
*
* @author EVE
* @since 1.1.0
*/
public record UserDiabeticResponse(boolean gender, int birthYear, boolean diabetic, int diabetesYear, String weightAlert) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.requestMatchers(HttpMethod.PATCH, "/api/health-connect/**").authenticated()
.requestMatchers(HttpMethod.POST, "/api/point/**").authenticated()
.requestMatchers(HttpMethod.GET, "/api/point/**").authenticated()
.requestMatchers(HttpMethod.POST, "/api/log/**").authenticated()
.requestMatchers(HttpMethod.POST, "/api/log/**").permitAll()
.requestMatchers(HttpMethod.GET, "/api/notification/**").authenticated()
.requestMatchers(HttpMethod.POST, "/api/notification/**").authenticated()
.requestMatchers(HttpMethod.PATCH, "/api/notification/**").authenticated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import static com.coniverse.dangjang.support.SimpleMockMvc.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import java.util.HashMap;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -14,20 +12,13 @@

import com.coniverse.dangjang.domain.log.dto.request.LogRequest;
import com.coniverse.dangjang.support.ControllerTest;
import com.coniverse.dangjang.support.annotation.WithDangjangUser;

/**
* @author TEO
* @since 1.0.0
*/
@WithDangjangUser
class AppLogControllerTest extends ControllerTest {
private static final String URL = "/api/log";
private static final String fixedEventLogName = "홈_혈당_클릭";
private static final String fixedScreenName = "홈";
private static final int fixedLogVersion = 1;
private static final String fixedSessionId = "12341234";
private static final HashMap<String, Object> fixedLogData = new HashMap<>();

@Test
void 로그를_등록하면_성공_메시지를_반환한다() throws Exception {
Expand All @@ -50,7 +41,7 @@ class AppLogControllerTest extends ControllerTest {
@ValueSource(strings = {"", " "})
void RequestBody의_EventLogName이_유효하지_않으면_예외가_발생한다(String eventLogName) throws Exception {
// given
LogRequest request = new LogRequest(eventLogName, fixedScreenName, fixedLogVersion, fixedSessionId, fixedLogData);
LogRequest request = 로그_요청_이벤트_로그명(eventLogName);
String content = objectMapper.writeValueAsString(request);

// when
Expand All @@ -69,7 +60,7 @@ class AppLogControllerTest extends ControllerTest {
@ValueSource(strings = {"", " "})
void RequestBody의_screenName이_유효하지_않으면_예외가_발생한다(String screenName) throws Exception {
// given
LogRequest request = new LogRequest(fixedEventLogName, screenName, fixedLogVersion, fixedSessionId, fixedLogData);
LogRequest request = 로그_요청_스크린명(screenName);
String content = objectMapper.writeValueAsString(request);

// when
Expand All @@ -88,7 +79,7 @@ class AppLogControllerTest extends ControllerTest {
@ValueSource(ints = {0, -1})
void RequestBody의_logVersion이_유효하지_않으면_예외가_발생한다(int logVersion) throws Exception {
// given
LogRequest request = new LogRequest(fixedEventLogName, fixedScreenName, logVersion, fixedSessionId, fixedLogData);
LogRequest request = 로그_요청_로그_버전(logVersion);
String content = objectMapper.writeValueAsString(request);

// when
Expand All @@ -107,7 +98,7 @@ class AppLogControllerTest extends ControllerTest {
@ValueSource(strings = {"", " "})
void RequestBody의_sessionId가_유효하지_않으면_예외가_발생한다(String sessionId) throws Exception {
// given
LogRequest request = new LogRequest(fixedEventLogName, fixedScreenName, fixedLogVersion, sessionId, fixedLogData);
LogRequest request = 로그_요청_세션_아이디(sessionId);
String content = objectMapper.writeValueAsString(request);

// when
Expand All @@ -121,4 +112,23 @@ class AppLogControllerTest extends ControllerTest {
jsonPath("$.fieldErrors[0].rejectedValue").value(sessionId)
);
}

@ParameterizedTest
@ValueSource(strings = {"", " ", "1/1/1", "r.1.1"})
void RequestBody의_appVersion이_유효하지_않으면_예외가_발생한다(String appVersion) throws Exception {
// given
LogRequest request = 로그_요청_앱_버전(appVersion);
String content = objectMapper.writeValueAsString(request);

// when
ResultActions resultActions = post(mockMvc, URL, content);

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.errorCode").value(400),
jsonPath("$.fieldErrors[0].field").value("appVersion"),
jsonPath("$.fieldErrors[0].rejectedValue").value(appVersion)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,26 @@ class LogMapperTest {
User user = 유저_테오();

// when
AppLog appLog = logMapper.toAppLog(request, user);
AppLog appLog = logMapper.toAppLog(request);

// then
assertAll(
() -> assertThat(appLog.event().getTitle()).isEqualTo(request.eventLogName()),
() -> assertThat(appLog.screenName()).isEqualTo(request.screenName()),
() -> assertThat(appLog.logVersion()).isEqualTo(request.logVersion()),
() -> assertThat(appLog.sessionId()).isEqualTo(request.sessionId()),
() -> assertThat(appLog.logData()).isEqualTo(request.logData()),
() -> assertThat(appLog.diabetic()).isEqualTo(user.isDiabetic()),
() -> assertThat(appLog.diabetesYear()).isEqualTo(user.getDiabetesYear())
() -> assertThat(appLog.logData()).isEqualTo(request.logData())
);
}

@Test
void LogRequest의_eventLogName이_존재하지_않으면_에러를_발생한다() {
// given
LogRequest request = new LogRequest("nonExistentEventLogName", "screenName", 1, "sessionId", null);
LogRequest request = 로그_요청_이벤트_로그명("nonExistentEventLogName");
User user = 유저_테오();

// when & then
assertThatThrownBy(() -> logMapper.toAppLog(request, user))
assertThatThrownBy(() -> logMapper.toAppLog(request))
.isInstanceOf(EnumNonExistentException.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package com.coniverse.dangjang.domain.log.service;

import static com.coniverse.dangjang.fixture.LogFixture.*;
import static com.coniverse.dangjang.fixture.UserFixture.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import com.coniverse.dangjang.domain.log.dto.request.LogRequest;
import com.coniverse.dangjang.domain.user.entity.User;
import com.coniverse.dangjang.domain.user.service.UserSearchService;

/**
* @author TEO
Expand All @@ -22,17 +17,13 @@
class LogServiceTest {
@Autowired
private LogService logService;
@MockBean
private UserSearchService userSearchService;

@Test
void 로그_커넥션이_끊겨도_에러를_발생하지_않는다() {
// given
LogRequest request = 로그_요청();
User user = 유저_테오();
doReturn(user).when(userSearchService).findUserByOauthId(anyString());

// when & then
assertDoesNotThrow(() -> logService.sendLog(request, user.getOauthId()));
assertDoesNotThrow(() -> logService.sendLog(request));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.coniverse.dangjang.fixture;

import static com.coniverse.dangjang.fixture.CommonCodeFixture.*;
import static com.coniverse.dangjang.fixture.UserFixture.*;

import java.time.LocalDate;
import java.util.ArrayList;
Expand All @@ -25,7 +26,6 @@
import com.coniverse.dangjang.domain.guide.weight.document.WeightGuide;
import com.coniverse.dangjang.domain.guide.weight.dto.WeightDayGuide;
import com.coniverse.dangjang.domain.guide.weight.dto.WeightGuideResponse;
import com.coniverse.dangjang.domain.log.dto.request.UserLog;
import com.coniverse.dangjang.domain.user.entity.User;

/**
Expand Down Expand Up @@ -154,11 +154,7 @@ public class GuideFixture {
}

public static DayGuideResponse 하루_가이드_응답(User user, LocalDate date, List<TodayGuide> 혈당가이드, WeightDayGuide 체중가이드, ExerciseDayGuide 운동가이드) {
return new DayGuideResponse(user.getNickname(), date, 혈당가이드, 체중가이드, 운동가이드, false, 유저_로깅정보(user, 체중가이드.alert()));
}

public static UserLog 유저_로깅정보(User 유저, String alert) {
return new UserLog(유저.getGender().isTrue(), 유저.getBirthYear(), 유저.isDiabetic(), 유저.getDiabetesYear(), alert);
return new DayGuideResponse(user.getNickname(), date, 혈당가이드, 체중가이드, 운동가이드, false, 유저_당뇨정보(user, 체중가이드.alert()));
}

public static WeightDayGuide 체중_하루_가이드() {
Expand Down
Loading

0 comments on commit be7814c

Please sign in to comment.