-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
서비스 테스트 코드 작성하기 & 리드미 파일 최종 수정 (#146)
* refactor : 근무일에만 요양보호사에게 알림이 가도록 수정하기 * test : 알림 서비스 테스트 코드 작성 * feat : 리드미 업데이트 * feat : 리드미 업데이트 * feat : 리드미 업데이트 * feat : 리드미 업데이트 * feat : 리드미 업데이트 * feat : 리드미 업데이트 * feat : 리드미 업데이트 * feat : 리드미 업데이트 * feat : 리드미 업데이트 * feat : 리드미 업데이트 * feat : 리드미 업데이트 * feat : 최종 수정
- Loading branch information
Showing
18 changed files
with
789 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
name: Java CI and Deploy to AWS EC2 | ||
|
||
on: | ||
push: | ||
branches: [ "Week10" ] | ||
pull_request: | ||
branches: [ "Week10" ] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: '17' | ||
distribution: 'temurin' | ||
|
||
- name: Setup Gradle | ||
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 | ||
|
||
- name: Build with Gradle Wrapper | ||
run: ./gradlew clean build | ||
|
||
deploy: | ||
runs-on: ubuntu-latest | ||
needs: build | ||
environment: production | ||
permissions: | ||
contents: read | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: '17' | ||
distribution: 'temurin' | ||
|
||
- name: Decode and save .pem file | ||
env: | ||
EC2_SSH_KEY_BASE64: ${{ secrets.EC2_SSH_KEY_BASE64 }} | ||
run: | | ||
echo "$EC2_SSH_KEY_BASE64" | base64 -d > ec2-key.pem | ||
chmod 600 ec2-key.pem | ||
- name: Upload JAR to EC2 | ||
env: | ||
EC2_HOST: ${{ secrets.EC2_HOST }} | ||
EC2_USER: ${{ secrets.EC2_USER }} | ||
run: | | ||
scp -i ec2-key.pem -o StrictHostKeyChecking=no ./build/libs/your-app.jar $EC2_USER@$EC2_HOST:~/app/your-app.jar | ||
- name: Restart Application on EC2 | ||
env: | ||
EC2_HOST: ${{ secrets.EC2_HOST }} | ||
EC2_USER: ${{ secrets.EC2_USER }} | ||
run: | | ||
ssh -i ec2-key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST << 'EOF' | ||
# 현재 실행 중인 Java 프로세스 종료 | ||
echo "현재 실행 중인 Java 프로세스 종료 중..." | ||
pkill -f 'java' || echo "종료할 Java 프로세스 없음" | ||
# 새로운 애플리케이션 실행 | ||
JAR_FILE="dbdr-0.0.1-SNAPSHOT.jar" | ||
echo "새로운 애플리케이션을 실행합니다: $JAR_FILE" | ||
nohup java -jar ~/app/$JAR_FILE --server.port=8080 > ~/app/app.log 2>&1 & | ||
# 실행 확인 | ||
sleep 10 | ||
echo "새로 실행된 Java 프로세스:" | ||
pgrep -f 'java' | ||
EOF |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package dbdr.chart; | ||
|
||
public class OcrMockTest { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package dbdr.messaging; | ||
|
||
import static org.mockito.Mockito.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import dbdr.domain.core.alarm.entity.Alarm; | ||
import dbdr.domain.core.alarm.repository.AlarmRepository; | ||
import dbdr.domain.core.alarm.service.AlarmService; | ||
import dbdr.domain.core.messaging.MessageChannel; | ||
import dbdr.domain.core.messaging.dto.SqsMessageDto; | ||
import dbdr.domain.core.messaging.service.CallSqsService; | ||
import dbdr.domain.guardian.entity.Guardian; | ||
import dbdr.domain.guardian.repository.GuardianRepository; | ||
import dbdr.domain.recipient.entity.Recipient; | ||
import dbdr.domain.chart.entity.Chart; | ||
import dbdr.domain.recipient.service.RecipientService; | ||
import dbdr.domain.chart.repository.ChartRepository; | ||
import dbdr.openai.dto.response.SummaryResponse; | ||
import dbdr.openai.service.SummaryService; | ||
|
||
class AlarmServiceTest { | ||
|
||
@Mock | ||
private AlarmRepository alarmRepository; | ||
|
||
@Mock | ||
private CallSqsService callSqsService; | ||
|
||
@Mock | ||
private GuardianRepository guardianRepository; | ||
|
||
@Mock | ||
private RecipientService recipientService; | ||
|
||
@Mock | ||
private ChartRepository chartRepository; | ||
|
||
@Mock | ||
private SummaryService summaryService; | ||
|
||
@InjectMocks | ||
private AlarmService alarmService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
void testSendAlarmToSqs() { | ||
Alarm alarm = mock(Alarm.class); | ||
String name = "테스트 보호자"; | ||
String phoneNumber = "01012345678"; | ||
String lineUserId = "test_line_user"; | ||
|
||
String expectedMessage = String.format("알림 메시지 %s", name); | ||
SqsMessageDto messageDto = new SqsMessageDto(MessageChannel.LINE, lineUserId, expectedMessage, phoneNumber); | ||
|
||
when(alarm.getMessage()).thenReturn("알림 메시지 %s"); | ||
when(alarmRepository.save(alarm)).thenReturn(alarm); | ||
|
||
alarmService.sendAlarmToSqs(alarm, MessageChannel.LINE, name, phoneNumber, lineUserId); | ||
|
||
verify(callSqsService).sendMessage(messageDto); | ||
verify(alarmRepository).save(alarm); | ||
} | ||
|
||
@Test | ||
void testGetGuardianAlarmMessage() { | ||
when(recipientService.isChartWrittenYesterday(anyLong())).thenReturn(true); | ||
|
||
Guardian guardian = mock(Guardian.class); | ||
when(guardian.getPhone()).thenReturn("01012345678"); | ||
when(guardianRepository.findById(anyLong())).thenReturn(Optional.of(guardian)); | ||
|
||
// Recipient 객체 모킹 | ||
Recipient recipient = mock(Recipient.class); | ||
when(recipient.getGuardian()).thenReturn(guardian); | ||
|
||
// Chart 객체 모킹 | ||
Chart chart = mock(Chart.class); | ||
when(chart.getRecipient()).thenReturn(recipient); | ||
when(chartRepository.findById(anyLong())).thenReturn(Optional.of(chart)); | ||
|
||
// SummaryResponse 모킹 | ||
SummaryResponse mockSummaryResponse = new SummaryResponse("condition", "body", "nursing", "cognitive", "recovery"); | ||
when(summaryService.getSummarization(anyLong())).thenReturn(mockSummaryResponse); | ||
|
||
// 테스트 실행 | ||
Alarm alarm = alarmService.getGuardianAlarmMessage(1L, LocalDateTime.now()); | ||
assertNotNull(alarm); | ||
verify(recipientService).isChartWrittenYesterday(anyLong()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package dbdr.messaging; | ||
|
||
import static org.mockito.Mockito.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.awspring.cloud.sqs.operations.SendResult; | ||
import io.awspring.cloud.sqs.operations.SqsTemplate; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import dbdr.domain.core.messaging.MessageChannel; | ||
import dbdr.domain.core.messaging.dto.SqsMessageDto; | ||
import dbdr.domain.core.messaging.service.CallSqsService; | ||
import dbdr.domain.core.messaging.service.LineMessagingService; | ||
import dbdr.domain.core.messaging.service.SmsMessagingService; | ||
|
||
class CallSqsServiceTest { | ||
|
||
@Mock | ||
private SqsTemplate sqsTemplate; // 제네릭 타입 없이 그대로 사용 | ||
|
||
@Mock | ||
private LineMessagingService lineMessagingService; | ||
|
||
@Mock | ||
private SmsMessagingService smsMessagingService; | ||
|
||
@Mock | ||
private ObjectMapper objectMapper; | ||
|
||
@InjectMocks | ||
private CallSqsService callSqsService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
void testSendMessage() throws Exception { | ||
// Mock 데이터 생성 | ||
SqsMessageDto messageDto = new SqsMessageDto(MessageChannel.LINE, "test_user", "Test Message", "01012345678"); | ||
String messageJson = "{ \"message\": \"Test Message\" }"; | ||
SendResult<String> sendResult = mock(SendResult.class); | ||
|
||
// Mocking | ||
when(objectMapper.writeValueAsString(any(SqsMessageDto.class))).thenReturn(messageJson); | ||
when(sqsTemplate.send(any())).thenReturn((SendResult) sendResult); // 강제 형변환을 SendResult로 처리 | ||
|
||
// 메서드 실행 | ||
SendResult<String> result = callSqsService.sendMessage(messageDto); | ||
|
||
// 검증 | ||
verify(objectMapper).writeValueAsString(messageDto); | ||
verify(sqsTemplate).send(any()); | ||
assertNotNull(result); | ||
} | ||
|
||
@Test | ||
void testReceiveMessageWithLineChannel() throws Exception { | ||
// Mock 데이터 생성 | ||
SqsMessageDto messageDto = new SqsMessageDto(MessageChannel.LINE, "test_user", "Test Message", "01012345678"); | ||
String messageJson = "{ \"message\": \"Test Message\" }"; | ||
|
||
// Mocking | ||
when(objectMapper.readValue(messageJson, SqsMessageDto.class)).thenReturn(messageDto); | ||
|
||
// 메서드 실행 | ||
callSqsService.receiveMessage(messageJson); | ||
|
||
// 검증 | ||
verify(lineMessagingService).pushAlarmMessage(messageDto.getUserId(), messageDto.getMessage()); | ||
verify(smsMessagingService, never()).sendMessageToUser(anyString(), anyString()); | ||
} | ||
|
||
@Test | ||
void testReceiveMessageWithSmsChannel() throws Exception { | ||
// Mock 데이터 생성 | ||
SqsMessageDto messageDto = new SqsMessageDto(MessageChannel.SMS, null, "Test Message", "01012345678"); | ||
String messageJson = "{ \"message\": \"Test Message\" }"; | ||
|
||
// Mocking | ||
when(objectMapper.readValue(messageJson, SqsMessageDto.class)).thenReturn(messageDto); | ||
|
||
// 메서드 실행 | ||
callSqsService.receiveMessage(messageJson); | ||
|
||
// 검증 | ||
verify(smsMessagingService).sendMessageToUser(messageDto.getPhoneNumber(), messageDto.getMessage()); | ||
verify(lineMessagingService, never()).pushAlarmMessage(anyString(), anyString()); | ||
} | ||
} |
Oops, something went wrong.