From 77ccf0f6aecfb76fbf3c0046696782a2b427cb72 Mon Sep 17 00:00:00 2001 From: Zepelown Date: Fri, 20 Dec 2024 22:06:32 +0900 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20=EB=A1=9C=EC=A7=81=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC=EC=97=90=20=EB=A7=9E=EA=B2=8C=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/wap/wabi/band/fixture/BandFixture.kt | 5 +- .../band/service/BandCommandServiceTest.kt | 210 +++++++++++++++- .../wabi/band/service/BandQueryServiceTest.kt | 230 +----------------- .../com/wap/wabi/common/TestConstants.kt | 1 + ...viceTest.kt => EventCommandServiceTest.kt} | 112 ++------- .../event/service/EventQueryServiceTest.kt | 141 +++++++++++ 6 files changed, 376 insertions(+), 323 deletions(-) rename wabi/src/test/kotlin/com/wap/wabi/event/service/{EventServiceTest.kt => EventCommandServiceTest.kt} (54%) create mode 100644 wabi/src/test/kotlin/com/wap/wabi/event/service/EventQueryServiceTest.kt diff --git a/wabi/src/test/kotlin/com/wap/wabi/band/fixture/BandFixture.kt b/wabi/src/test/kotlin/com/wap/wabi/band/fixture/BandFixture.kt index a639cd4..03d933b 100644 --- a/wabi/src/test/kotlin/com/wap/wabi/band/fixture/BandFixture.kt +++ b/wabi/src/test/kotlin/com/wap/wabi/band/fixture/BandFixture.kt @@ -2,11 +2,12 @@ package com.wap.wabi.band.fixture import com.wap.wabi.band.entity.Band import com.wap.wabi.common.Reflection +import com.wap.wabi.common.TestConstants object BandFixture { fun createBand(name: String): Band { return Band.builder() - .adminId(1) + .adminId(TestConstants.ADMIN_ID) .bandName(name) .build() } @@ -18,7 +19,7 @@ object BandFixture { fun createAnotherUserBand(name: String): Band { return Band.builder() - .adminId(2) + .adminId(TestConstants.ANOTHER_ADMIN_ID) .bandName(name) .build() } diff --git a/wabi/src/test/kotlin/com/wap/wabi/band/service/BandCommandServiceTest.kt b/wabi/src/test/kotlin/com/wap/wabi/band/service/BandCommandServiceTest.kt index dffed64..933e9c6 100644 --- a/wabi/src/test/kotlin/com/wap/wabi/band/service/BandCommandServiceTest.kt +++ b/wabi/src/test/kotlin/com/wap/wabi/band/service/BandCommandServiceTest.kt @@ -1,9 +1,13 @@ package com.wap.wabi.band.service import com.wap.wabi.band.fixture.BandFixture +import com.wap.wabi.band.fixture.BandStudentFixture import com.wap.wabi.band.payload.BandStudentDto +import com.wap.wabi.band.payload.request.BandCreateRequest import com.wap.wabi.band.payload.request.BandStudentEnrollRequest +import com.wap.wabi.band.payload.request.BandUpdateRequest import com.wap.wabi.band.repository.BandRepository +import com.wap.wabi.band.repository.BandStudentRepository import com.wap.wabi.exception.ErrorCode import com.wap.wabi.exception.RestApiException import com.wap.wabi.student.fixture.StudentFixture @@ -13,12 +17,13 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows +import org.mockito.ArgumentMatchers import org.mockito.Mockito.`when` import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.boot.test.mock.mockito.MockBean import java.time.LocalDate -import java.util.Optional +import java.util.* @Transactional @SpringBootTest @@ -30,6 +35,9 @@ class BandCommandServiceTest { @MockBean private lateinit var bandRepository: BandRepository + @MockBean + private lateinit var bandStudentRepository: BandStudentRepository + @MockBean private lateinit var studentRepository: StudentRepository @@ -116,4 +124,204 @@ class BandCommandServiceTest { //Then assertThat(exception.errorCode).isEqualTo(ErrorCode.NOT_FOUND_BAND) } + + @Test + fun 밴드를_생성한다() { + //Given + val adminId = 1L + val bandName = "band 1" + val bandCreateRequest = BandCreateRequest( + bandName = bandName, + bandMemo = "band memo" + ) + + val savedBand = BandFixture.createBand(id = 1, name = bandName) + + `when`(bandRepository.save(ArgumentMatchers.any())).thenReturn(savedBand) + + //When & Then + Assertions.assertDoesNotThrow { + bandCommandService.createBand(adminId = adminId, bandCreateRequest = bandCreateRequest) + } + } + + @Test + fun 밴드_생성_시_유효하지_않은_adminId_값을_입력하면_UNAUTHORIZED_REQUEST_예외를_반환한다() { + // Given + val invalidAdminId = -1L + val bandName = "band 1" + val bandCreateRequest = BandCreateRequest( + bandName = bandName, + bandMemo = "band memo" + ) + + // When + val exception = assertThrows { + bandCommandService.createBand(adminId = invalidAdminId, bandCreateRequest = bandCreateRequest) + } + + // Then + assertThat(exception.errorCode).isEqualTo(ErrorCode.UNAUTHORIZED_REQUEST) + } + + @Test + fun 밴드를_삭제한다() { + //Given + val adminId = 1L + val bandId = 1L + val bandName = "band 1" + + val savedBand = BandFixture.createBand(id = 1, name = bandName) + + `when`(bandRepository.findById(bandId)).thenReturn(Optional.of(savedBand)) + + //When & Then + Assertions.assertDoesNotThrow { + bandCommandService.deleteBand(adminId = adminId, bandId = bandId) + } + } + + @Test + fun 밴드_삭제_시_유효하지_않은_bandId_값을_입력하면_NOT_FOUND_BAND_예외를_반환한다() { + // Given + val adminId = 1L + val invalidBandId = 2L + + `when`(bandRepository.findById(invalidBandId)).thenReturn(Optional.empty()) + + // When + val exception = assertThrows { + bandCommandService.deleteBand(adminId = adminId, bandId = adminId) + } + + // Then + assertThat(exception.errorCode).isEqualTo(ErrorCode.NOT_FOUND_BAND) + } + + @Test + fun 밴드_삭제_시_자신이_생성한_밴드가_아니면_UNAUTHORIZED_BAND_예외를_반환한다() { + // Given + val adminId = 1L + val bandId = 1L + val savedBand = BandFixture.createAnotherUserBand(id = 1, name = "bandName") + + `when`(bandRepository.findById(bandId)).thenReturn(Optional.of(savedBand)) + + // When + val exception = assertThrows { + bandCommandService.deleteBand(adminId = adminId, bandId = bandId) + } + + // Then + assertThat(exception.errorCode).isEqualTo(ErrorCode.UNAUTHORIZED_BAND) + } + + @Test + fun 밴드_삭제_시_유효하지_않은_adminId_값을_입력하면_UNAUTHORIZED_REQUEST_예외를_반환한다() { + // Given + val invalidAdminId = 2L + val bandId = 1L + val bandName = "band 1" + + val savedBand = BandFixture.createBand(id = 1, name = bandName) + + `when`(bandRepository.findById(bandId)).thenReturn(Optional.of(savedBand)) + + // When + val exception = assertThrows { + bandCommandService.deleteBand(adminId = invalidAdminId, bandId = bandId) + } + + // Then + assertThat(exception.errorCode).isEqualTo(ErrorCode.UNAUTHORIZED_BAND) + } + + @Test + fun 밴드를_수정한다() { + // Given + val adminId = 1L + val bandId = 1L + val savedBand = BandFixture.createBand("Band 1", 1) + val bandUpdateRequest = BandUpdateRequest( + bandId = bandId, + bandName = "new band name", + bandMemo = "band memo" + ) + + `when`(bandRepository.findById(bandId)).thenReturn(Optional.of(savedBand)) + + //When & Then + Assertions.assertDoesNotThrow { + bandCommandService.updateBand(adminId = adminId, bandUpdateRequest = bandUpdateRequest) + } + } + + @Test + fun 밴드_수정_시_유효하지_않은_bandId_값을_입력하면_NOT_FOUND_BAND_예외를_반환한다() { + // Given + val adminId = 1L + val invalidBandId = 2L + val bandUpdateRequest = BandUpdateRequest( + bandId = invalidBandId, + bandName = "new band name", + bandMemo = "band memo" + ) + + `when`(bandRepository.findById(invalidBandId)).thenReturn(Optional.empty()) + + // When + val exception = assertThrows { + bandCommandService.updateBand(adminId = adminId, bandUpdateRequest = bandUpdateRequest) + } + + // Then + assertThat(exception.errorCode).isEqualTo(ErrorCode.NOT_FOUND_BAND) + } + + @Test + fun 밴드_수정_시_유효하지_않은_adminId_값을_입력하면_UNAUTHORIZED_REQUEST_예외를_반환한다() { + // Given + val invalidAdminId = 2L + val bandId = 1L + val bandUpdateRequest = BandUpdateRequest( + bandId = bandId, + bandName = "new band name", + bandMemo = "band memo" + ) + + val savedBand = BandFixture.createBand("Band 1", 1) + `when`(bandRepository.findById(bandId)).thenReturn(Optional.of(savedBand)) + + // When + val exception = assertThrows { + bandCommandService.updateBand(adminId = invalidAdminId, bandUpdateRequest = bandUpdateRequest) + } + + // Then + assertThat(exception.errorCode).isEqualTo(ErrorCode.UNAUTHORIZED_BAND) + } + + @Test + fun 밴드_수정_시_자신이_생성한_밴드가_아니면_UNAUTHORIZED_BAND_예외를_반환한다() { + // Given + val adminId = 1L + val bandId = 1L + val bandUpdateRequest = BandUpdateRequest( + bandId = bandId, + bandName = "new band name", + bandMemo = "band memo" + ) + val savedBand = BandFixture.createAnotherUserBand(id = 1, name = "bandName") + + `when`(bandRepository.findById(bandId)).thenReturn(Optional.of(savedBand)) + + // When + val exception = assertThrows { + bandCommandService.updateBand(adminId = adminId, bandUpdateRequest = bandUpdateRequest) + } + + // Then + assertThat(exception.errorCode).isEqualTo(ErrorCode.UNAUTHORIZED_BAND) + } + } diff --git a/wabi/src/test/kotlin/com/wap/wabi/band/service/BandQueryServiceTest.kt b/wabi/src/test/kotlin/com/wap/wabi/band/service/BandQueryServiceTest.kt index 2faa91b..b610079 100644 --- a/wabi/src/test/kotlin/com/wap/wabi/band/service/BandQueryServiceTest.kt +++ b/wabi/src/test/kotlin/com/wap/wabi/band/service/BandQueryServiceTest.kt @@ -16,12 +16,11 @@ import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertAll import org.junit.jupiter.api.assertThrows -import org.mockito.ArgumentMatchers import org.mockito.Mockito.`when` import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.boot.test.mock.mockito.MockBean -import java.util.Optional +import java.util.* @Transactional @SpringBootTest @@ -36,6 +35,7 @@ class BandQueryServiceTest { @MockBean private lateinit var bandStudentRepository: BandStudentRepository + @Test fun 밴드에_속한_학생들을_조회한다() { // Given @@ -80,144 +80,6 @@ class BandQueryServiceTest { assertThat(exception.errorCode).isEqualTo(ErrorCode.NOT_FOUND_BAND) } - @Test - fun 밴드를_생성한다() { - //Given - val adminId = 1L - val bandName = "band 1" - val bandCreateRequest = BandCreateRequest( - bandName = bandName, - bandMemo = "band memo" - ) - - val savedBand = BandFixture.createBand(id = 1, name = bandName) - - `when`(bandRepository.save(ArgumentMatchers.any())).thenReturn(savedBand) - - //When & Then - Assertions.assertDoesNotThrow { - bandQueryService.createBand(adminId = adminId, bandCreateRequest = bandCreateRequest) - } - } - - @Test - fun 밴드_생성_시_유효하지_않은_adminId_값을_입력하면_UNAUTHORIZED_REQUEST_예외를_반환한다() { - // Given - val invalidAdminId = 2L - val bandName = "band 1" - val bandCreateRequest = BandCreateRequest( - bandName = bandName, - bandMemo = "band memo" - ) - - // When - val exception = assertThrows { - bandQueryService.createBand(adminId = invalidAdminId, bandCreateRequest = bandCreateRequest) - } - - // Then - assertThat(exception.errorCode).isEqualTo(ErrorCode.UNAUTHORIZED_REQUEST) - } - - @Test - fun 밴드를_삭제한다() { - //Given - val adminId = 1L - val bandId = 1L - val bandName = "band 1" - - val savedBand = BandFixture.createBand(id = 1, name = bandName) - - `when`(bandRepository.findById(bandId)).thenReturn(Optional.of(savedBand)) - - //When & Then - Assertions.assertDoesNotThrow { - bandQueryService.deleteBand(adminId = adminId, bandId = bandId) - } - } - - @Test - fun 밴드_삭제_시_유효하지_않은_bandId_값을_입력하면_NOT_FOUND_BAND_예외를_반환한다() { - // Given - val adminId = 1L - val invalidBandId = 2L - - `when`(bandRepository.findById(invalidBandId)).thenReturn(Optional.empty()) - - // When - val exception = assertThrows { - bandQueryService.deleteBand(adminId = adminId, bandId = adminId) - } - - // Then - assertThat(exception.errorCode).isEqualTo(ErrorCode.NOT_FOUND_BAND) - } - - @Test - fun 밴드_삭제_시_자신이_생성한_밴드가_아니면_UNAUTHORIZED_BAND_예외를_반환한다() { - // Given - val adminId = 1L - val bandId = 1L - val savedBand = BandFixture.createAnotherUserBand(id = 1, name = "bandName") - - `when`(bandRepository.findById(bandId)).thenReturn(Optional.of(savedBand)) - - // When - val exception = assertThrows { - bandQueryService.deleteBand(adminId = adminId, bandId = bandId) - } - - // Then - assertThat(exception.errorCode).isEqualTo(ErrorCode.UNAUTHORIZED_BAND) - } - - @Test - fun 밴드_삭제_시_유효하지_않은_adminId_값을_입력하면_UNAUTHORIZED_REQUEST_예외를_반환한다() { - // Given - val invalidAdminId = 2L - val bandId = 1L - val bandName = "band 1" - - val savedBand = BandFixture.createBand(id = 1, name = bandName) - - `when`(bandRepository.findById(bandId)).thenReturn(Optional.of(savedBand)) - - // When - val exception = assertThrows { - bandQueryService.deleteBand(adminId = invalidAdminId, bandId = bandId) - } - - // Then - assertThat(exception.errorCode).isEqualTo(ErrorCode.UNAUTHORIZED_REQUEST) - } - - @Test - fun 밴드_삭제_시_학생명단이_추가된_밴드를_삭제_하려고_하면_ALREADY_ADD_STUDENT_예외를_반환한다() { - // Given - val adminId = 1L - val bandId = 1L - val studentName1 = "Student 1" - val studentName2 = "Student 2" - val band = BandFixture.createBand("Band 1") - val student1 = StudentFixture.createStudent(studentName1) - val student2 = StudentFixture.createStudent(studentName2) - val bandStudents = listOf( - BandStudentFixture.createBandStudent(student1, band), - BandStudentFixture.createBandStudent(student2, band) - ) - - `when`(bandRepository.findById(bandId)).thenReturn(Optional.of(band)) - `when`(bandStudentRepository.findAllByBand(band)).thenReturn(bandStudents) - - // When - val exception = assertThrows { - bandQueryService.deleteBand(adminId = adminId, bandId = bandId) - } - - // Then - assertThat(exception.errorCode).isEqualTo(ErrorCode.ALREADY_ADD_STUDENT) - } - @Test fun 내가_관리하는_밴드_목록을_조회한다() { // Given @@ -248,7 +110,7 @@ class BandQueryServiceTest { @Test fun 밴드_목록_조회_시_유효하지_않은_adminId_값을_입력하면_UNAUTHORIZED_REQUEST_예외를_반환한다() { // Given - val invalidAdminId = 2L + val invalidAdminId = -1L // When val exception = assertThrows { @@ -259,91 +121,5 @@ class BandQueryServiceTest { assertThat(exception.errorCode).isEqualTo(ErrorCode.UNAUTHORIZED_REQUEST) } - @Test - fun 밴드를_수정한다() { - // Given - val adminId = 1L - val bandId = 1L - val savedBand = BandFixture.createBand("Band 1", 1) - val bandUpdateRequest = BandUpdateRequest( - bandId = bandId, - bandName = "new band name", - bandMemo = "band memo" - ) - - `when`(bandRepository.findById(bandId)).thenReturn(Optional.of(savedBand)) - - //When & Then - Assertions.assertDoesNotThrow { - bandQueryService.updateBand(adminId = adminId, bandUpdateRequest = bandUpdateRequest) - } - } - - @Test - fun 밴드_수정_시_유효하지_않은_bandId_값을_입력하면_NOT_FOUND_BAND_예외를_반환한다() { - // Given - val adminId = 1L - val invalidBandId = 2L - val bandUpdateRequest = BandUpdateRequest( - bandId = invalidBandId, - bandName = "new band name", - bandMemo = "band memo" - ) - - `when`(bandRepository.findById(invalidBandId)).thenReturn(Optional.empty()) - - // When - val exception = assertThrows { - bandQueryService.updateBand(adminId = adminId, bandUpdateRequest = bandUpdateRequest) - } - - // Then - assertThat(exception.errorCode).isEqualTo(ErrorCode.NOT_FOUND_BAND) - } - - @Test - fun 밴드_수정_시_유효하지_않은_adminId_값을_입력하면_UNAUTHORIZED_REQUEST_예외를_반환한다() { - // Given - val invalidAdminId = 2L - val bandId = 1L - val bandUpdateRequest = BandUpdateRequest( - bandId = bandId, - bandName = "new band name", - bandMemo = "band memo" - ) - - val savedBand = BandFixture.createBand("Band 1", 1) - `when`(bandRepository.findById(bandId)).thenReturn(Optional.of(savedBand)) - - // When - val exception = assertThrows { - bandQueryService.updateBand(adminId = invalidAdminId, bandUpdateRequest = bandUpdateRequest) - } - - // Then - assertThat(exception.errorCode).isEqualTo(ErrorCode.UNAUTHORIZED_REQUEST) - } - - @Test - fun 밴드_수정_시_자신이_생성한_밴드가_아니면_UNAUTHORIZED_BAND_예외를_반환한다() { - // Given - val adminId = 1L - val bandId = 1L - val bandUpdateRequest = BandUpdateRequest( - bandId = bandId, - bandName = "new band name", - bandMemo = "band memo" - ) - val savedBand = BandFixture.createAnotherUserBand(id = 1, name = "bandName") - - `when`(bandRepository.findById(bandId)).thenReturn(Optional.of(savedBand)) - // When - val exception = assertThrows { - bandQueryService.updateBand(adminId = adminId, bandUpdateRequest = bandUpdateRequest) - } - - // Then - assertThat(exception.errorCode).isEqualTo(ErrorCode.UNAUTHORIZED_BAND) - } } diff --git a/wabi/src/test/kotlin/com/wap/wabi/common/TestConstants.kt b/wabi/src/test/kotlin/com/wap/wabi/common/TestConstants.kt index b98e58c..0bde851 100644 --- a/wabi/src/test/kotlin/com/wap/wabi/common/TestConstants.kt +++ b/wabi/src/test/kotlin/com/wap/wabi/common/TestConstants.kt @@ -2,5 +2,6 @@ package com.wap.wabi.common object TestConstants { const val ADMIN_ID = 1L + const val ANOTHER_ADMIN_ID = 2L const val CLUB = "Test Club" } diff --git a/wabi/src/test/kotlin/com/wap/wabi/event/service/EventServiceTest.kt b/wabi/src/test/kotlin/com/wap/wabi/event/service/EventCommandServiceTest.kt similarity index 54% rename from wabi/src/test/kotlin/com/wap/wabi/event/service/EventServiceTest.kt rename to wabi/src/test/kotlin/com/wap/wabi/event/service/EventCommandServiceTest.kt index a17ac20..ad0b97a 100644 --- a/wabi/src/test/kotlin/com/wap/wabi/event/service/EventServiceTest.kt +++ b/wabi/src/test/kotlin/com/wap/wabi/event/service/EventCommandServiceTest.kt @@ -6,14 +6,11 @@ import com.wap.wabi.band.repository.BandRepository import com.wap.wabi.band.repository.BandStudentRepository import com.wap.wabi.common.TestConstants import com.wap.wabi.event.entity.Enum.EventStudentStatus -import com.wap.wabi.event.fixture.EventBandFixture import com.wap.wabi.event.fixture.EventFixture import com.wap.wabi.event.fixture.EventStudentFixture import com.wap.wabi.event.payload.request.CheckInRequest import com.wap.wabi.event.payload.request.EventCreateRequest import com.wap.wabi.event.payload.request.EventUpdateRequest -import com.wap.wabi.event.payload.response.CheckInStatusCount -import com.wap.wabi.event.payload.response.EventData import com.wap.wabi.event.repository.EventBandRepository import com.wap.wabi.event.repository.EventRepository import com.wap.wabi.event.repository.EventStudentRepository @@ -22,20 +19,20 @@ import com.wap.wabi.student.repository.StudentRepository import jakarta.transaction.Transactional import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers import org.mockito.ArgumentMatchers.any -import org.mockito.Mockito.times -import org.mockito.Mockito.verify +import org.mockito.Mockito import org.mockito.Mockito.`when` import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.boot.test.mock.mockito.MockBean import java.time.LocalDateTime -import java.util.Optional +import java.util.* @Transactional @SpringBootTest @SuppressWarnings("NonAsciiCharacters") -class EventServiceTest { +class EventCommandServiceTest { @MockBean private lateinit var studentRepository: StudentRepository @@ -55,7 +52,8 @@ class EventServiceTest { private lateinit var bandStudentRepository: BandStudentRepository @Autowired - private lateinit var eventService: EventService + private lateinit var eventCommandService: EventCommandService + @Test fun 이벤트를_생성한다() { @@ -79,7 +77,8 @@ class EventServiceTest { `when`(bandRepository.findAllById(eventCreateRequest.bandIds)).thenReturn(listOf(band1, band2, band3)) //When - val result = eventService.createEvent(adminId = TestConstants.ADMIN_ID, eventCreateRequest = eventCreateRequest) + val result = + eventCommandService.createEvent(adminId = TestConstants.ADMIN_ID, eventCreateRequest = eventCreateRequest) //Then assertThat(result.id).isEqualTo(savedEvent.id) @@ -104,7 +103,7 @@ class EventServiceTest { val expected = 1L //When - val result = eventService.saveEventStudentsFromBand(event = event1, band = band1) + val result = eventCommandService.saveEventStudentsFromBand(event = event1, band = band1) //Then assertThat(result).isEqualTo(expected) @@ -130,88 +129,26 @@ class EventServiceTest { `when`(eventRepository.findById(any())).thenReturn(Optional.of(savedEvent)) //When - val result = eventService.updateEvent(adminId = TestConstants.ADMIN_ID, eventUpdateRequest = eventUpdateRequest) + val result = + eventCommandService.updateEvent(adminId = TestConstants.ADMIN_ID, eventUpdateRequest = eventUpdateRequest) //Then assertThat(result.name).isEqualTo(updatedEvent.name) } - @Test - fun 이벤트를_단일조회_한다() { - //Given - val eventId = 1L - val event = EventFixture.createEvent(id = eventId, name = "Event1") - val band1 = BandFixture.createBand(id = 1, name = "Band 1") - val band2 = BandFixture.createBand(id = 2, name = "Band 2") - val band3 = BandFixture.createBand(id = 3, name = "Band 3") - - val eventBand1 = EventBandFixture.createEventBnd(event, band1) - val eventBand2 = EventBandFixture.createEventBnd(event, band2) - val eventBand3 = EventBandFixture.createEventBnd(event, band3) - val eventBands = listOf(eventBand1, eventBand2, eventBand3) - - val checkInStatusCount = CheckInStatusCount(checkIn = 20, notCheckIn = 20) - - `when`(eventRepository.findById(any())).thenReturn(Optional.of(event)) - `when`(eventBandRepository.findAllByEvent(any())).thenReturn(eventBands) - `when`(eventStudentRepository.getEventStudentStatusCount(any(), any())).thenReturn(20) - - val expected = EventData.of(event, eventBands, checkInStatusCount) - - //When - val result = eventService.getEvent(adminId = TestConstants.ADMIN_ID, eventId = eventId) - - //Then - assertThat(result).isEqualTo(expected) - } - - @Test - fun 이벤트를_목록으로_조회_한다() { - //Given - val event1 = EventFixture.createEvent(id = 1, name = "Event 1") - val event2 = EventFixture.createEvent(id = 2, name = "Event 2") - val band1 = BandFixture.createBand(id = 1, name = "Band 1") - val band2 = BandFixture.createBand(id = 2, name = "Band 2") - val band3 = BandFixture.createBand(id = 3, name = "Band 3") - - val eventBand1 = EventBandFixture.createEventBnd(event1, band1) - val eventBand2 = EventBandFixture.createEventBnd(event1, band2) - val eventBand3 = EventBandFixture.createEventBnd(event2, band2) - val eventBand4 = EventBandFixture.createEventBnd(event2, band3) - - val checkInStatusCount = CheckInStatusCount(checkIn = 20, notCheckIn = 20) - - val eventData1 = EventData.of(event1, listOf(eventBand1, eventBand2), checkInStatusCount) - val eventData2 = EventData.of(event2, listOf(eventBand3, eventBand4), checkInStatusCount) - - `when`(eventRepository.findAllByAdminId(any())).thenReturn(listOf(event1, event2)) - `when`(eventBandRepository.findAllByEvent(event1)).thenReturn(listOf(eventBand1, eventBand2)) - `when`(eventBandRepository.findAllByEvent(event2)).thenReturn(listOf(eventBand3, eventBand4)) - `when`(eventStudentRepository.getEventStudentStatusCount(any(), any())).thenReturn(20) - `when`(eventRepository.findById(1L)).thenReturn(Optional.of(event1)) - `when`(eventRepository.findById(2L)).thenReturn(Optional.of(event2)) - `when`(eventStudentRepository.getEventStudentStatusCount(any(), any())).thenReturn(20) - - //When - val result = eventService.getEvents(TestConstants.ADMIN_ID) - - //Then - assertThat(result).isEqualTo(listOf(eventData1, eventData2)) - } - @Test fun 이벤트를_삭제한다() { //Given val eventId = 1L val event = EventFixture.createEvent(id = eventId, name = "Event 1") - `when`(eventRepository.findById(any())).thenReturn(Optional.of(event)) + Mockito.`when`(eventRepository.findById(ArgumentMatchers.any())).thenReturn(Optional.of(event)) //When - eventService.deleteEvent(adminId = TestConstants.ADMIN_ID, eventId = eventId) + eventCommandService.deleteEvent(adminId = TestConstants.ADMIN_ID, eventId = eventId) //Then - verify(eventRepository, times(1)).delete(event) + Mockito.verify(eventRepository, Mockito.times(1)).delete(event) } @Test @@ -226,28 +163,17 @@ class EventServiceTest { val student = StudentFixture.createStudent(id = "201912050", name = "Student1") val eventStudent = EventStudentFixture.createEventStudent(id = 1, event = event, student = student) - `when`(studentRepository.findById(any())).thenReturn(Optional.of(student)) - `when`(eventRepository.findById(any())).thenReturn(Optional.of(event)) - `when`(eventStudentRepository.findByStudentAndEvent(any(), any())).thenReturn(Optional.of(eventStudent)) + Mockito.`when`(studentRepository.findById(ArgumentMatchers.any())).thenReturn(Optional.of(student)) + Mockito.`when`(eventRepository.findById(ArgumentMatchers.any())).thenReturn(Optional.of(event)) + Mockito.`when`(eventStudentRepository.findByStudentAndEvent(ArgumentMatchers.any(), ArgumentMatchers.any())) + .thenReturn(Optional.of(eventStudent)) //When - val result = eventService.checkIn(checkInRequest) + val result = eventCommandService.checkIn(checkInRequest) //Then assertThat(result).isEqualTo(EventStudentStatus.CHECK_IN) } - @Test - fun 이벤트의_주최자인지_판단한다() { - //Given - val event1 = EventFixture.createEvent(id = 1, name = "Event1") - - //When - val result = eventService.validateEventOwner(adminId = TestConstants.ADMIN_ID, event = event1) - - //Then - assertThat(result).isTrue() - } - } diff --git a/wabi/src/test/kotlin/com/wap/wabi/event/service/EventQueryServiceTest.kt b/wabi/src/test/kotlin/com/wap/wabi/event/service/EventQueryServiceTest.kt new file mode 100644 index 0000000..d499894 --- /dev/null +++ b/wabi/src/test/kotlin/com/wap/wabi/event/service/EventQueryServiceTest.kt @@ -0,0 +1,141 @@ +package com.wap.wabi.event.service + +import com.wap.wabi.band.fixture.BandFixture +import com.wap.wabi.band.repository.BandRepository +import com.wap.wabi.band.repository.BandStudentRepository +import com.wap.wabi.common.TestConstants +import com.wap.wabi.event.fixture.EventBandFixture +import com.wap.wabi.event.fixture.EventFixture +import com.wap.wabi.event.payload.response.CheckInStatusCount +import com.wap.wabi.event.payload.response.EventData +import com.wap.wabi.event.repository.EventBandRepository +import com.wap.wabi.event.repository.EventRepository +import com.wap.wabi.event.repository.EventStudentRepository +import com.wap.wabi.student.repository.StudentRepository +import jakarta.transaction.Transactional +import org.assertj.core.api.Assertions +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers +import org.mockito.Mockito +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.mock.mockito.MockBean +import java.util.* + +@Transactional +@SpringBootTest +@SuppressWarnings("NonAsciiCharacters") +class EventQueryServiceTest { + @MockBean + private lateinit var studentRepository: StudentRepository + + @MockBean + private lateinit var eventStudentRepository: EventStudentRepository + + @MockBean + private lateinit var eventRepository: EventRepository + + @MockBean + private lateinit var eventBandRepository: EventBandRepository + + @MockBean + private lateinit var bandRepository: BandRepository + + @MockBean + private lateinit var bandStudentRepository: BandStudentRepository + + @Autowired + private lateinit var eventQueryService: EventQueryService + + @Test + fun 이벤트를_단일조회_한다() { + //Given + val eventId = 1L + val event = EventFixture.createEvent(id = eventId, name = "Event1") + val band1 = BandFixture.createBand(id = 1, name = "Band 1") + val band2 = BandFixture.createBand(id = 2, name = "Band 2") + val band3 = BandFixture.createBand(id = 3, name = "Band 3") + + val eventBand1 = EventBandFixture.createEventBnd(event, band1) + val eventBand2 = EventBandFixture.createEventBnd(event, band2) + val eventBand3 = EventBandFixture.createEventBnd(event, band3) + val eventBands = listOf(eventBand1, eventBand2, eventBand3) + + val checkInStatusCount = CheckInStatusCount(checkIn = 20, notCheckIn = 20) + + Mockito.`when`(eventRepository.findById(ArgumentMatchers.any())).thenReturn(Optional.of(event)) + Mockito.`when`(eventBandRepository.findAllByEvent(ArgumentMatchers.any())).thenReturn(eventBands) + Mockito.`when`( + eventStudentRepository.getEventStudentStatusCount( + ArgumentMatchers.any(), + ArgumentMatchers.any() + ) + ).thenReturn(20) + + val expected = EventData.of(event, eventBands, checkInStatusCount) + + //When + val result = eventQueryService.getEvent(adminId = TestConstants.ADMIN_ID, eventId = eventId) + + //Then + assertThat(result).isEqualTo(expected) + } + + @Test + fun 이벤트를_목록으로_조회_한다() { + //Given + val event1 = EventFixture.createEvent(id = 1, name = "Event 1") + val event2 = EventFixture.createEvent(id = 2, name = "Event 2") + val band1 = BandFixture.createBand(id = 1, name = "Band 1") + val band2 = BandFixture.createBand(id = 2, name = "Band 2") + val band3 = BandFixture.createBand(id = 3, name = "Band 3") + + val eventBand1 = EventBandFixture.createEventBnd(event1, band1) + val eventBand2 = EventBandFixture.createEventBnd(event1, band2) + val eventBand3 = EventBandFixture.createEventBnd(event2, band2) + val eventBand4 = EventBandFixture.createEventBnd(event2, band3) + + val checkInStatusCount = CheckInStatusCount(checkIn = 20, notCheckIn = 20) + + val eventData1 = EventData.of(event1, listOf(eventBand1, eventBand2), checkInStatusCount) + val eventData2 = EventData.of(event2, listOf(eventBand3, eventBand4), checkInStatusCount) + + Mockito.`when`(eventRepository.findAllByAdminId(ArgumentMatchers.any())).thenReturn(listOf(event1, event2)) + Mockito.`when`(eventBandRepository.findAllByEvent(event1)).thenReturn(listOf(eventBand1, eventBand2)) + Mockito.`when`(eventBandRepository.findAllByEvent(event2)).thenReturn(listOf(eventBand3, eventBand4)) + Mockito.`when`( + eventStudentRepository.getEventStudentStatusCount( + ArgumentMatchers.any(), + ArgumentMatchers.any() + ) + ).thenReturn(20) + Mockito.`when`(eventRepository.findById(1L)).thenReturn(Optional.of(event1)) + Mockito.`when`(eventRepository.findById(2L)).thenReturn(Optional.of(event2)) + Mockito.`when`( + eventStudentRepository.getEventStudentStatusCount( + ArgumentMatchers.any(), + ArgumentMatchers.any() + ) + ).thenReturn(20) + + //When + val result = eventQueryService.getEvents(TestConstants.ADMIN_ID) + + //Then + assertThat(result).isEqualTo(listOf(eventData1, eventData2)) + } + + + @Test + fun 이벤트의_주최자인지_판단한다() { + //Given + val event1 = EventFixture.createEvent(id = 1, name = "Event1") + + //When + val result = eventQueryService.validateEventOwner(adminId = TestConstants.ADMIN_ID, event = event1) + + //Then + Assertions.assertThat(result).isTrue() + } +} \ No newline at end of file From 628c8eb9a45302e92c004b83012cd52cdd2874f2 Mon Sep 17 00:00:00 2001 From: Zepelown Date: Fri, 20 Dec 2024 22:07:48 +0900 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20=EC=83=88=20=EC=97=91=EC=84=B8?= =?UTF-8?q?=EC=8A=A4=20=ED=86=A0=ED=81=B0=20=EC=9D=91=EB=8B=B5=20=ED=97=A4?= =?UTF-8?q?=EB=8D=94=20=EB=AA=85=20newRefreshToken=20=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/wap/wabi/auth/jwt/JwtAuthenticationFilter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wabi/src/main/kotlin/com/wap/wabi/auth/jwt/JwtAuthenticationFilter.kt b/wabi/src/main/kotlin/com/wap/wabi/auth/jwt/JwtAuthenticationFilter.kt index 0c0d5b5..68b6571 100644 --- a/wabi/src/main/kotlin/com/wap/wabi/auth/jwt/JwtAuthenticationFilter.kt +++ b/wabi/src/main/kotlin/com/wap/wabi/auth/jwt/JwtAuthenticationFilter.kt @@ -71,7 +71,7 @@ class JwtAuthenticationFilter( val newAccessToken = jwtTokenProvider.recreateAccessToken(oldAccessToken) // 새 액세스 토큰을 응답 헤더에 추가 - response.setHeader("New-Access-Token", newAccessToken) + response.setHeader("newRefreshToken", newAccessToken) // SecurityContext에 새 인증 정보 업데이트 val user = parseUserSpecification(newAccessToken) From 5bf1c5982354e13cb9870c0f5a37806321dfed85 Mon Sep 17 00:00:00 2001 From: Zepelown Date: Fri, 20 Dec 2024 22:14:40 +0900 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20github=20actions=20=EC=9B=8C?= =?UTF-8?q?=ED=81=AC=ED=94=8C=EB=A1=9C=EC=9A=B0=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81=20-=20develop=20branch=20=EB=A8=B8=EC=A7=80?= =?UTF-8?q?=20=EC=8B=9C=EC=97=90=20=EC=9B=8C=ED=81=AC=ED=94=8C=EB=A1=9C?= =?UTF-8?q?=EC=9A=B0=20=EC=9E=91=EB=8F=99=EB=90=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20-=20build=EC=99=80=20deploy=20=EC=9B=8C?= =?UTF-8?q?=ED=81=AC=ED=94=8C=EB=A1=9C=EC=9A=B0=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yml | 39 ++++++++++++ .github/workflows/deploy.yml | 26 ++++++++ .github/workflows/docker-image.yml | 27 --------- .github/workflows/gradle.yml | 97 ------------------------------ 4 files changed, 65 insertions(+), 124 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/deploy.yml delete mode 100644 .github/workflows/docker-image.yml delete mode 100644 .github/workflows/gradle.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..368eadf --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,39 @@ +name: Build + +on: + pull_request: + branches: + - develop + types: + - closed + +jobs: + build: + if: github.event.pull_request.merged == true + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + - name: Grant execute permission for gradlew + run: chmod +x ./wabi/gradlew + + - name: Build with Gradle + run: | + cd wabi + ./gradlew clean build -x test + + - name: Docker build and push + run: | + docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} + docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest . + docker tag ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:${GITHUB_SHA::7} + docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest + docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:${GITHUB_SHA::7} diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..543265b --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,26 @@ +name: Deploy + +on: + workflow_run: + workflows: ["Build"] + types: + - completed + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Deploy using Docker + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.SERVER_IP }} + username: ${{ secrets.SSH_USERNAME }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + passphrase: ${{ secrets.SSH_PASSPHRASE }} + script: | + sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} + sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest + + sudo docker-compose down + sudo docker-compose up -d diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml deleted file mode 100644 index 89bce03..0000000 --- a/.github/workflows/docker-image.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Docker Image CI - -on: - push: - branches: [ "ci/cd" ] - pull_request: - branches: [ "ci/cd" ] - -jobs: - - build: - - runs-on: ubuntu-latest - - steps: - - name: Build with Gradle - uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 - with: - arguments: clean build -x test - - # Docker build - - name: Docker build - run: | - docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} - docker build -t seongwonyoon/wabi . - docker tag wabi seongwonyoon/wabi:${GITHUB_SHA::7} - docker push seongwonyoon/wabi:${GITHUB_SHA::7} diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml deleted file mode 100644 index 35fd967..0000000 --- a/.github/workflows/gradle.yml +++ /dev/null @@ -1,97 +0,0 @@ -name: Deploy - -on: - push: - branches: [ "develop" ] - pull_request: - branches: [ "develop" ] - -permissions: - contents: read - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - # 기본 체크아웃 - - name: Checkout - uses: actions/checkout@v3 - # 폴더 정보 확인 - - name: List the repository structure - run: ls -R - - # JDK 17 세팅 - - name: Set up JDK 17 - uses: actions/setup-java@v3 - with: - java-version: '17' - distribution: 'temurin' - - # Gradlew 실행 허용 - - name: Grant execute permission for gradlew - run: chmod +x ./wabi/gradlew - - # 환경 변수 설정 - - name: Set environment values - run: | - touch ./wabi/src/main/resources/env.properties - echo "SPRING_DATASOURCE_URL=${{ secrets.SPRING_DATASOURCE_URL }}" >> ./env.properties - echo "SPRING_DATASOURCE_USERNAME=${{ secrets.SPRING_DATASOURCE_USERNAME }}" >> ./env.properties - echo "SPRING_DATASOURCE_PASSWORD=${{ secrets.SPRING_DATASOURCE_PASSWORD }}" >> ./env.properties - echo "JWT_KEY=${{ secrets.JWT_KEY }}" >> ./env.properties - echo "JWT_EXPIRATION_MINUTES=${{ secrets.JWT_EXPIRATION_MINUTES }}" >> ./env.properties - echo "JWT_REFRESH_EXPIRATION_HOURS=${{ secrets.JWT_REFRESH_EXPIRATION_HOURS }}" >> ./env.properties - echo "JWT_ISSUER=${{ secrets.JWT_ISSUER }}" >> ./env.properties - shell: bash - - # Gradle build (Test 제외) - - name: Build with Gradle - run: | - ls - cd wabi - ls - ./gradlew clean build -x test - - - name: Docker build - run: | - docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} - cd wabi - docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest . # 이미지에 latest 태그를 추가 - docker tag ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest seongwonyoon/wabi:${GITHUB_SHA::7} # GITHUB_SHA를 이용한 커밋 태그 추가 - docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest # latest 버전 푸시 - docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:${GITHUB_SHA::7} # 커밋 해시 버전 푸시 - - # Deploy - - name: Deploy - uses: appleboy/ssh-action@master - with: - host: ${{ secrets.SERVER_IP }} - username: ${{ secrets.SSH_USERNAME }} - key: ${{ secrets.SSH_PRIVATE_KEY }} - passphrase: ${{ secrets.SSH_PASSPHRASE }} - envs: GITHUB_SHA - script: | - #!/bin/bash - ls - cd / - sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} - sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }} - - # Create .env file in the correct directory - echo "SPRING_DATASOURCE_URL=${{ secrets.SPRING_DATASOURCE_URL }}" | sudo tee -a .env > /dev/null - echo "SPRING_DATASOURCE_USERNAME=${{ secrets.SPRING_DATASOURCE_USERNAME }}" | sudo tee -a .env > /dev/null - echo "SPRING_DATASOURCE_PASSWORD=${{ secrets.SPRING_DATASOURCE_PASSWORD }}" | sudo tee -a .env > /dev/null - echo "JWT_KEY=${{ secrets.JWT_KEY }}" | sudo tee -a .env > /dev/null - echo "JWT_EXPIRATION_MINUTES=${{ secrets.JWT_EXPIRATION_MINUTES }}" | sudo tee -a .env > /dev/null - echo "JWT_REFRESH_EXPIRATION_HOURS=${{ secrets.JWT_REFRESH_EXPIRATION_HOURS }}" | sudo tee -a .env > /dev/null - echo "JWT_ISSUER=${{ secrets.JWT_ISSUER }}" | sudo tee -a .env > /dev/null - echo "DOCKERHUB_USERNAME=${{ secrets.DOCKERHUB_USERNAME }}" | sudo tee -a .env > /dev/null - echo "DOCKERHUB_REPOSITORY=${{ secrets.DOCKERHUB_REPOSITORY }}" | sudo tee -a .env > /dev/null - - sudo docker-compose down - sudo docker-compose up -d - - - From 30427236e7501b3a520ca5a2497cf526f849caa4 Mon Sep 17 00:00:00 2001 From: Zepelown Date: Fri, 20 Dec 2024 22:20:58 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=EC=9E=98=EB=AA=BB=EB=90=9C=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EB=B6=84=EB=A6=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yml | 11 +++++++++++ .github/workflows/deploy.yml | 27 ++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 368eadf..81a1601 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -25,6 +25,17 @@ jobs: - name: Grant execute permission for gradlew run: chmod +x ./wabi/gradlew + - name: Set environment values + run: | + touch ./wabi/src/main/resources/env.properties + echo "SPRING_DATASOURCE_URL=${{ secrets.SPRING_DATASOURCE_URL }}" >> ./env.properties + echo "SPRING_DATASOURCE_USERNAME=${{ secrets.SPRING_DATASOURCE_USERNAME }}" >> ./env.properties + echo "SPRING_DATASOURCE_PASSWORD=${{ secrets.SPRING_DATASOURCE_PASSWORD }}" >> ./env.properties + echo "JWT_KEY=${{ secrets.JWT_KEY }}" >> ./env.properties + echo "JWT_EXPIRATION_MINUTES=${{ secrets.JWT_EXPIRATION_MINUTES }}" >> ./env.properties + echo "JWT_REFRESH_EXPIRATION_HOURS=${{ secrets.JWT_REFRESH_EXPIRATION_HOURS }}" >> ./env.properties + echo "JWT_ISSUER=${{ secrets.JWT_ISSUER }}" >> ./env.properties + - name: Build with Gradle run: | cd wabi diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 543265b..259b3ae 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -11,6 +11,17 @@ jobs: runs-on: ubuntu-latest steps: + - name: Set environment values + run: | + touch ./wabi/src/main/resources/env.properties + echo "SPRING_DATASOURCE_URL=${{ secrets.SPRING_DATASOURCE_URL }}" >> ./env.properties + echo "SPRING_DATASOURCE_USERNAME=${{ secrets.SPRING_DATASOURCE_USERNAME }}" >> ./env.properties + echo "SPRING_DATASOURCE_PASSWORD=${{ secrets.SPRING_DATASOURCE_PASSWORD }}" >> ./env.properties + echo "JWT_KEY=${{ secrets.JWT_KEY }}" >> ./env.properties + echo "JWT_EXPIRATION_MINUTES=${{ secrets.JWT_EXPIRATION_MINUTES }}" >> ./env.properties + echo "JWT_REFRESH_EXPIRATION_HOURS=${{ secrets.JWT_REFRESH_EXPIRATION_HOURS }}" >> ./env.properties + echo "JWT_ISSUER=${{ secrets.JWT_ISSUER }}" >> ./env.properties + - name: Deploy using Docker uses: appleboy/ssh-action@master with: @@ -19,8 +30,22 @@ jobs: key: ${{ secrets.SSH_PRIVATE_KEY }} passphrase: ${{ secrets.SSH_PASSPHRASE }} script: | + #!/bin/bash + ls + cd / sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} - sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest + sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }} + + # Create .env file in the correct directory + echo "SPRING_DATASOURCE_URL=${{ secrets.SPRING_DATASOURCE_URL }}" | sudo tee -a .env > /dev/null + echo "SPRING_DATASOURCE_USERNAME=${{ secrets.SPRING_DATASOURCE_USERNAME }}" | sudo tee -a .env > /dev/null + echo "SPRING_DATASOURCE_PASSWORD=${{ secrets.SPRING_DATASOURCE_PASSWORD }}" | sudo tee -a .env > /dev/null + echo "JWT_KEY=${{ secrets.JWT_KEY }}" | sudo tee -a .env > /dev/null + echo "JWT_EXPIRATION_MINUTES=${{ secrets.JWT_EXPIRATION_MINUTES }}" | sudo tee -a .env > /dev/null + echo "JWT_REFRESH_EXPIRATION_HOURS=${{ secrets.JWT_REFRESH_EXPIRATION_HOURS }}" | sudo tee -a .env > /dev/null + echo "JWT_ISSUER=${{ secrets.JWT_ISSUER }}" | sudo tee -a .env > /dev/null + echo "DOCKERHUB_USERNAME=${{ secrets.DOCKERHUB_USERNAME }}" | sudo tee -a .env > /dev/null + echo "DOCKERHUB_REPOSITORY=${{ secrets.DOCKERHUB_REPOSITORY }}" | sudo tee -a .env > /dev/null sudo docker-compose down sudo docker-compose up -d