-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Announcement 도메인 추가 * feat: Announcement 조회 로직 추가 * feat: Announcement 조회 API 추가 * style: method, dto naming 수정 * test: 공지사항 조회 api test 추가 * refactor: 불필요한 응답 필드 제거 * test: test 패키지 이동 * test: ProductControllerTest에 ApiTestConfig 적용 * refactor: BaseEntity 적용
- Loading branch information
1 parent
69cd895
commit 8ab5b18
Showing
11 changed files
with
191 additions
and
7 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/petqua/application/announcement/AnnouncementDtos.kt
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,19 @@ | ||
package com.petqua.application.announcement | ||
|
||
import com.petqua.domain.announcement.Announcement | ||
|
||
data class AnnouncementResponse( | ||
val id: Long, | ||
val title: String, | ||
val linkUrl: String, | ||
) { | ||
companion object { | ||
fun from(announcement: Announcement): AnnouncementResponse { | ||
return AnnouncementResponse( | ||
id = announcement.id, | ||
title = announcement.title, | ||
linkUrl = announcement.linkUrl, | ||
) | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/com/petqua/application/announcement/AnnouncementService.kt
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,20 @@ | ||
package com.petqua.application.announcement | ||
|
||
import com.petqua.domain.announcement.AnnouncementRepository | ||
import org.springframework.cache.annotation.Cacheable | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Transactional | ||
@Service | ||
class AnnouncementService( | ||
private val announcementRepository: AnnouncementRepository, | ||
) { | ||
|
||
@Cacheable("announcements") | ||
@Transactional(readOnly = true) | ||
fun readAll(): List<AnnouncementResponse> { | ||
val announcements = announcementRepository.findAll() | ||
return announcements.map { AnnouncementResponse.from(it) } | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/com/petqua/domain/announcement/Announcement.kt
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,20 @@ | ||
package com.petqua.domain.announcement | ||
|
||
import com.petqua.common.domain.BaseEntity | ||
import jakarta.persistence.Column | ||
import jakarta.persistence.Entity | ||
import jakarta.persistence.GeneratedValue | ||
import jakarta.persistence.GenerationType | ||
import jakarta.persistence.Id | ||
|
||
@Entity | ||
class Announcement( | ||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long = 0L, | ||
|
||
@Column(nullable = false) | ||
val title: String, | ||
|
||
@Column(nullable = false) | ||
val linkUrl: String, | ||
) : BaseEntity() |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/petqua/domain/announcement/AnnouncementRepository.kt
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 com.petqua.domain.announcement | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface AnnouncementRepository : JpaRepository<Announcement, Long> |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/petqua/presentation/announcement/AnnouncementController.kt
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,21 @@ | ||
package com.petqua.presentation.announcement | ||
|
||
import com.petqua.application.announcement.AnnouncementResponse | ||
import com.petqua.application.announcement.AnnouncementService | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RequestMapping("/announcements") | ||
@RestController | ||
class AnnouncementController( | ||
private val announcementService: AnnouncementService | ||
) { | ||
|
||
@GetMapping | ||
fun readAll(): ResponseEntity<List<AnnouncementResponse>> { | ||
val response = announcementService.readAll() | ||
return ResponseEntity.ok(response) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/kotlin/com/petqua/application/announcement/AnnouncementServiceTest.kt
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,47 @@ | ||
package com.petqua.application.announcement | ||
|
||
import com.petqua.domain.announcement.Announcement | ||
import com.petqua.domain.announcement.AnnouncementRepository | ||
import io.kotest.core.spec.style.BehaviorSpec | ||
import io.kotest.matchers.shouldBe | ||
import org.mockito.Mockito.atMost | ||
import org.mockito.Mockito.verify | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE | ||
import org.springframework.boot.test.mock.mockito.SpyBean | ||
import org.springframework.test.context.TestConstructor | ||
import org.springframework.test.context.TestConstructor.AutowireMode.ALL | ||
|
||
@TestConstructor(autowireMode = ALL) | ||
@SpringBootTest(webEnvironment = NONE) | ||
class AnnouncementServiceTest( | ||
private var announcementService: AnnouncementService, | ||
@SpyBean private var announcementRepository: AnnouncementRepository, | ||
) : BehaviorSpec({ | ||
|
||
Given("공지사항 조회 테스트") { | ||
announcementRepository.saveAll( | ||
listOf( | ||
Announcement(title = "titleA", linkUrl = "linkUrlA"), | ||
Announcement(title = "titleB", linkUrl = "linkUrlB"), | ||
Announcement(title = "titleC", linkUrl = "linkUrlC"), | ||
) | ||
) | ||
|
||
When("공지사항을 전체 조회 하면") { | ||
val results = announcementService.readAll() | ||
|
||
Then("모든 공지사항이 조회 된다") { | ||
results.size shouldBe 3 | ||
} | ||
} | ||
|
||
When("공지사항이 캐싱 되어 있으면") { | ||
repeat(5) { announcementService.readAll() } | ||
|
||
Then("퀴리가 발생 하지 않는다") { | ||
verify(announcementRepository, atMost(1)).findAll() | ||
} | ||
} | ||
} | ||
}) |
3 changes: 1 addition & 2 deletions
3
.../petqua/application/ProductServiceTest.kt → ...application/product/ProductServiceTest.kt
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
49 changes: 49 additions & 0 deletions
49
src/test/kotlin/com/petqua/presentation/announcement/AnnouncementControllerTest.kt
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,49 @@ | ||
package com.petqua.presentation.announcement | ||
|
||
import com.petqua.application.announcement.AnnouncementResponse | ||
import com.petqua.domain.announcement.Announcement | ||
import com.petqua.domain.announcement.AnnouncementRepository | ||
import com.petqua.test.ApiTestConfig | ||
import io.restassured.module.kotlin.extensions.Extract | ||
import io.restassured.module.kotlin.extensions.Given | ||
import io.restassured.module.kotlin.extensions.Then | ||
import io.restassured.module.kotlin.extensions.When | ||
import org.assertj.core.api.SoftAssertions.assertSoftly | ||
import org.springframework.http.HttpStatus | ||
|
||
class AnnouncementControllerTest( | ||
private val announcementRepository: AnnouncementRepository | ||
) : ApiTestConfig() { | ||
|
||
init { | ||
Given("공지 사항이 존재할 때") { | ||
val announcements = announcementRepository.saveAll( | ||
listOf( | ||
Announcement(title = "announcementsA", linkUrl = "linkUrlA"), | ||
Announcement(title = "announcementsB", linkUrl = "linkUrlB") | ||
) | ||
) | ||
|
||
When("공지 사항 목록을 조회하면") { | ||
val response = Given { | ||
log().all() | ||
} When { | ||
get("/announcements") | ||
} Then { | ||
log().all() | ||
} Extract { | ||
response() | ||
} | ||
|
||
Then("모든 공지 사항 목록이 반환된다.") { | ||
val responseData = response.`as`(Array<AnnouncementResponse>::class.java) | ||
|
||
assertSoftly { | ||
it.assertThat(response.statusCode).isEqualTo(HttpStatus.OK.value()) | ||
it.assertThat(responseData.size).isEqualTo(2) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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 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