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

서비스 테스트 코드 작성하기 & 리드미 파일 최종 수정 #146

Merged
Show file tree
Hide file tree
Changes from all 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
74 changes: 74 additions & 0 deletions .github/workflows/gradle.yml
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
495 changes: 438 additions & 57 deletions README.md

Large diffs are not rendered by default.

Binary file added docs/source/be_structure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/care_bridge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/care_message.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/caregiver_difficulty.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/chart_summary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/chart_view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/chart_write.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/erd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/ocr_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/voice_recognition.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dbdr.domain.core.messaging.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
Expand Down Expand Up @@ -61,12 +60,12 @@ public void sendChartUpdate() {
String name = careworker.getName();

// (1) Line 채널 알림 보내기
if (alarm != null && careworker.isLineSubscription()) {
if (alarm != null && careworker.isLineSubscription() && careworker.isWorkingOn(currentDateTime.getDayOfWeek())) {
log.info("Line 알림 메세지를 받을 보호자 : {}", name);
alarmService.sendAlarmToSqs(alarm, MessageChannel.LINE, name, careworker.getPhone(), careworker.getLineUserId());
}
// (2) SMS 알림 보내기
if (alarm != null && careworker.isSmsSubscription()) {
if (alarm != null && careworker.isSmsSubscription() && careworker.isWorkingOn(currentDateTime.getDayOfWeek())) {
log.info("SMS 문자 알림 메세지를 받을 보호자 : {}", name);
alarmService.sendAlarmToSqs(alarm, MessageChannel.SMS, name, careworker.getPhone(), careworker.getLineUserId());
}
Expand Down
39 changes: 0 additions & 39 deletions src/main/java/dbdr/exception/ApplicationError.java

This file was deleted.

5 changes: 5 additions & 0 deletions src/test/java/dbdr/chart/OcrMockTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dbdr.chart;

public class OcrMockTest {

}
103 changes: 103 additions & 0 deletions src/test/java/dbdr/messaging/AlarmServiceTest.java
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());
}
}
96 changes: 96 additions & 0 deletions src/test/java/dbdr/messaging/CallSqsServiceTest.java
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());
}
}
Loading
Loading