Skip to content

Commit

Permalink
Merge pull request #191 from Kernel360/develop
Browse files Browse the repository at this point in the history
로그 기록, 테스트 코드
  • Loading branch information
xorwns118 authored Aug 29, 2024
2 parents 9eda7d6 + e383940 commit e941cf8
Show file tree
Hide file tree
Showing 50 changed files with 1,944 additions and 79 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-security'

implementation 'ch.qos.logback:logback-classic'

//jjwt
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
Expand All @@ -48,7 +50,7 @@ dependencies {
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'

// implement for test
//implement for test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.speech.up.api.etri.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;

import com.speech.up.api.etri.dto.ResponsePronunciationApiDto;
import com.speech.up.api.etri.service.VoiceToTextService;

import lombok.RequiredArgsConstructor;
Expand All @@ -19,7 +17,7 @@ public class ETRIApiController {
public void createTask(
@RequestPart("script") String script,
@RequestPart("recordId") String recordId
) {
) {
voiceToTextService.callRecognitionApi(script, Long.parseLong(recordId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private RequestPronunciationDto(String requestId, ArgumentDTO argument) {
this.argument = argument;
}

public static RequestPronunciationDto createPronunciation(String requestId, String audio, String script){
public static RequestPronunciationDto createPronunciation(String requestId, String audio, String script) {
ArgumentDTO argumentDTO = ArgumentDTO.builder()
.audio(audio)
.script(script)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public static class ReturnObjectDTO {
private String recognized;
private Double score;// 음성 인식 결과
}
public ResponsePronunciationApiDto(String request_id, int result, ReturnObjectDTO return_object) {
this.request_id = request_id;
this.result = result;
this.return_object = return_object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public class ResponseRecognitionDto {
public static class ReturnObjectDTO {
private String recognized;
}
public ResponseRecognitionDto(String request_id, int result, ReturnObjectDTO return_object) {
this.request_id = request_id;
this.result = result;
this.return_object = return_object;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/speech/up/common/aspect/LoggingAspect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.speech.up.common.aspect;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {
private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);

@AfterThrowing(pointcut = "execution(* com.speech.up.user.service.*.*(..))", throwing = "exception")
public void logAfterThrowing(Exception exception) {
logger.error("Exception in method: {}", exception.getMessage(), exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ private Response(Long reportId, double score, boolean isUse) {
this.score = score;
this.isUse = isUse;
}

public static ReportGetDto.Response toResponse(Long reportId, double score, boolean isUse) {
return new ReportGetDto.Response(reportId, score, isUse);
}
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ speech-flow:

logging:
level:
com.speech.up: DEBUG #com.speech.up 패키지의 로그 레벨을 DEBUG로 설정
com.speech.up: INFO #com.speech.up 패키지의 로그 레벨을 INFO 로 설정

kakao:
map:
app:
key: ${KAKAO_MAP_APP_KEY}

server:
forward-headers-strategy: framework # X-Forwarded-Proto와 같은 헤더를 인식하도록 설정
forward-headers-strategy: framework # X-Forwarded-Proto 와 같은 헤더를 인식하도록 설정
60 changes: 60 additions & 0 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- DEBUG 레벨 로깅 -->
<appender name="DEBUG_FILE" class="ch.qos.logback.core.FileAppender">
<file>/root/logs/debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeBasedRollingPolicy">
<maxFileSize>10MB</maxFileSize> <!-- 파일 최대 크기 설정 -->
<fileNamePattern>logs/debug.%i.log</fileNamePattern> <!-- 롤링된 파일 이름 패턴 -->
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>

<!-- INFO 레벨 로깅 -->
<appender name="INFO_FILE" class="ch.qos.logback.core.FileAppender">
<file>/root/logs/info.log</file> <!-- 날짜 없이 단일 파일 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeBasedRollingPolicy">
<maxFileSize>10MB</maxFileSize> <!-- 파일 최대 크기 설정 -->
<fileNamePattern>logs/info.%i.log</fileNamePattern> <!-- 롤링된 파일 이름 패턴 -->
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>

<!-- ERROR 레벨 로깅 -->
<appender name="ERROR_FILE" class="ch.qos.logback.core.FileAppender">
<file>/root/logs/error.log</file> <!-- 날짜 없이 단일 파일 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeBasedRollingPolicy">
<maxFileSize>10MB</maxFileSize> <!-- 파일 최대 크기 설정 -->
<fileNamePattern>logs/error.%i.log</fileNamePattern> <!-- 롤링된 파일 이름 패턴 -->
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>

<!-- 로깅 레벨 설정 -->
<root level="INFO">
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>
</configuration>
100 changes: 100 additions & 0 deletions src/test/java/com/speech/up/admin/controller/AdminControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.speech.up.admin.controller;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.ResponseEntity;

import com.speech.up.user.repository.UserRepository;
import com.speech.up.user.service.UserService;
import com.speech.up.user.service.dto.UserGetInfoDto;

import jakarta.servlet.http.HttpServletRequest;

public class AdminControllerTest {

@Mock
UserService userService;

@Mock
UserRepository userRepository;

@InjectMocks
AdminController adminController;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@DisplayName("사용자 정보 가져오기")
@Test
void getUserTest(){
//given
//when
List<UserGetInfoDto.Response> actualResponse = adminController.getUser();

//then
assertNotNull(actualResponse);
}

@DisplayName("현재 관리자의 사용자 정보")
@Test
void checkAdminTest(){
//given
HttpServletRequest request = mock(HttpServletRequest.class);

//when
ResponseEntity<UserGetInfoDto.Response> actualResponse = adminController.checkAdmin(request);

//then
assertNotNull(actualResponse);
}

@DisplayName("지정된 사용자 ID를 가진 사용자를 삭제")
@Test
void dropUserTest(){
//given
Long userId = 1L;

//when
adminController.dropUser(userId);

//then
verify(userRepository, times(1)).deleteById(userId);
}

@DisplayName("지정된 사용자 ID를 가진 사용자를 비활성화")
@Test
void unUsedUserTest(){
//given
Long userId = 1L;

//when
adminController.unUsedUser(userId);

//then
verify(userService, times(1)).unUsedUser(userId);
}

@DisplayName("지정된 사용자 ID를 가진 사용자를 복원")
@Test
void restoreUserTest(){
//given
Long userId = 1L;

//when
adminController.restoreUser(userId);

//then
verify(userService, times(1)).restoreUser(userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.speech.up.admin.controller;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;

public class AdminPageControllerTest {

@InjectMocks
AdminPageController adminPageController;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@DisplayName("admin 페이지 이동")
@Test
void adminPage() {
String actualResponse = adminPageController.adminPage();

assertNotNull(actualResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.speech.up.admin.controller;

import static org.mockito.Mockito.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.speech.up.admin.service.StatisticsService;

public class OpenDataControllerTest {

@Mock
StatisticsService statisticsService;

@InjectMocks
OpenDataController openDataController;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@DisplayName("통계 테이블의 가장 최근 데이터를 반환")
@Test
void getStatisticsTest() {

openDataController.getStatistics();

verify(statisticsService, times(1)).getStatistics();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.speech.up.api.etri.controller;

import static org.mockito.Mockito.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.speech.up.api.etri.service.VoiceToTextService;

public class ETRIApiControllerTest {
@Mock
VoiceToTextService voiceToTextService;

@InjectMocks
ETRIApiController etriApiController;


@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@DisplayName("api 호출하기")
@Test
void createTaskTest(){
//given
String script = "script";
String recordId = "1";

//when
etriApiController.createTask(script, recordId);

//then
verify(voiceToTextService, times(1)).callRecognitionApi(script, Long.valueOf(recordId));
}
}
Loading

0 comments on commit e941cf8

Please sign in to comment.