Skip to content

Commit

Permalink
Merge pull request #26 from this-is-spear/25-계약서를-개선한다
Browse files Browse the repository at this point in the history
계약서를 개선한다
  • Loading branch information
this-is-spear authored Jun 4, 2024
2 parents c438a26 + 0bf45e9 commit b073289
Show file tree
Hide file tree
Showing 18 changed files with 711 additions and 162 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/actions_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: ✅ Contract Test with Gradle
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
with:
arguments: contractTest
arguments: verifierStubsJar

- name: ✅ Publish To Maven Local
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ class EventInfraAdapter(
eventClient.get()
.uri("/events/{eventId}", eventId)
.retrieve()
.body(EventState::class.java)
?: throw RuntimeException("Event not found")
.body(EventState::class.java)!!
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties
import org.springframework.data.repository.findByIdOrNull

@SpringBootTest
@AutoConfigureStubRunner(
Expand All @@ -37,21 +38,17 @@ class ShopOwnerAdapterTest(
@Autowired
private val shopOwnerAdapter: ShopOwnerAdapter,
) {
lateinit var ownerEntity: ShopOwnerEntity

@BeforeEach
fun setUp() {
memberRepository.deleteAll()
shopOwnerRepository.deleteAll()
couponRepository.deleteAll()
ownerEntity = shopOwnerRepository.save(
ShopOwnerEntity(ShopEntity(listOf(), listOf(), listOf(), listOf()))
)
}

@Test
fun `가게 주인 식별자로 가게 주인을 찾는다`() {
val shopEntity = ShopEntity(listOf(), listOf(), listOf(), listOf())
val shopOwnerEntity = ShopOwnerEntity(shopEntity)

val ownerEntity = shopOwnerRepository.save(shopOwnerEntity)

// when
val owner = shopOwnerAdapter.findById(ownerEntity.id!!)

Expand All @@ -61,10 +58,6 @@ class ShopOwnerAdapterTest(

@Test
fun `가게 식별자로 가게 주인을 찾는다`() {
val shopEntity = ShopEntity(listOf(), listOf(), listOf(), listOf())
val shopOwnerEntity = ShopOwnerEntity(shopEntity)
val ownerEntity = shopOwnerRepository.save(shopOwnerEntity)

val owner = shopOwnerAdapter.findByShopId(ownerEntity.shopEntity.id!!)

// then
Expand All @@ -73,10 +66,6 @@ class ShopOwnerAdapterTest(

@Test
fun `수정된 쿠폰 목록을 저장한다`() {
val shopEntity = ShopEntity(listOf(), listOf(), listOf(), listOf())
val shopOwnerEntity = ShopOwnerEntity(shopEntity)
val ownerEntity = shopOwnerRepository.save(shopOwnerEntity)

val owner = shopOwnerAdapter.findById(ownerEntity.id!!)
val 게시할_쿠폰 = toCoupon(couponRepository.save(fromCoupon(게시할_쿠폰)))
val 나눠줄_쿠폰 = toCoupon(couponRepository.save(fromCoupon(나눠줄_쿠폰)))
Expand All @@ -93,10 +82,7 @@ class ShopOwnerAdapterTest(

@Test
fun `단골 고객을 추가한다`() {
val shopEntity = ShopEntity(listOf(), listOf(), listOf(), listOf())
val shopOwnerEntity = ShopOwnerEntity(shopEntity)
val ownerEntity = shopOwnerRepository.save(shopOwnerEntity)
val member = memberAdapter.findMember(10L)
val member = memberAdapter.findMember(9L)
val owner = shopOwnerAdapter.findById(ownerEntity.id!!)

owner.addRoyalCustomersInShop(member)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ package com.example.estdelivery.coupon.application.port.out.adapter.infra
import com.example.estdelivery.coupon.application.port.out.adapter.infra.dto.UpdateParticipatedMembersState
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertAll
import org.junit.jupiter.api.assertThrows
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties
import org.springframework.web.client.HttpClientErrorException
import org.springframework.web.client.HttpClientErrorException.BadRequest
import org.springframework.web.client.HttpServerErrorException

@SpringBootTest
@AutoConfigureStubRunner(
Expand All @@ -20,12 +25,72 @@ class EventInfraAdapterTest(
@Test
fun `이벤트를 조회한다`() {
val event = eventInfraAdapter.findEvent(1)
assertEquals(1L, event.id)
assertEquals(1, event.id)
}

@Test
fun `회원을 이벤트에 참가시킨다`() {
val participatedMembersState = UpdateParticipatedMembersState(1, 1)
eventInfraAdapter.participateMember(participatedMembersState)
}

@Test
fun `1000번 식별자를 조회하면 400 에러가 발생한다`() {
assertAll(
{ assertThrows<BadRequest> { eventInfraAdapter.findEvent(1000) } },
{
assertThrows<BadRequest> {
eventInfraAdapter.participateMember(UpdateParticipatedMembersState(1, 1000))
}
}
)
}

@Test
fun `1001번 식별자를 조회하면 429 에러가 발생한다`() {
assertAll(
{
assertThrows<HttpClientErrorException.TooManyRequests> {
eventInfraAdapter.findEvent(1001)
}
},
{
assertThrows<HttpClientErrorException.TooManyRequests> {
eventInfraAdapter.participateMember(UpdateParticipatedMembersState(1, 1001))
}
}
)
}

@Test
fun `1100번 식별자를 조회하면 500 에러가 발생한다`() {
assertAll(
{
assertThrows<HttpServerErrorException.InternalServerError> {
eventInfraAdapter.findEvent(1100)
}
},
{
assertThrows<HttpServerErrorException.InternalServerError> {
eventInfraAdapter.participateMember(UpdateParticipatedMembersState(1, 1100))
}
}
)
}

@Test
fun `1101번 식별자를 조회하면 503 에러가 발생한다`() {
assertAll(
{
assertThrows<HttpServerErrorException.ServiceUnavailable> {
eventInfraAdapter.findEvent(1101)
}
},
{
assertThrows<HttpServerErrorException.ServiceUnavailable> {
eventInfraAdapter.participateMember(UpdateParticipatedMembersState(1, 1101))
}
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package com.example.estdelivery.coupon.application.port.out.adapter.infra

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties
import org.springframework.web.client.HttpClientErrorException
import org.springframework.web.client.HttpServerErrorException


@SpringBootTest
Expand All @@ -24,4 +27,32 @@ class MemberInfraAdapterTest(
assertThat(member.id).isEqualTo(1L)
assertThat(member.name).isEqualTo("이건창")
}

@Test
fun `1000번 식별자를 조회하면 400 에러가 발생한다`() {
assertThrows<HttpClientErrorException.BadRequest> {
memberInfraAdapter.findMember(1000)
}
}

@Test
fun `1001번 식별자를 조회하면 429 에러가 발생한다`() {
assertThrows<HttpClientErrorException.TooManyRequests> {
memberInfraAdapter.findMember(1001)
}
}

@Test
fun `1100번 식별자를 조회하면 500 에러가 발생한다`() {
assertThrows<HttpServerErrorException.InternalServerError> {
memberInfraAdapter.findMember(1100)
}
}

@Test
fun `1101번 식별자를 조회하면 503 에러가 발생한다`() {
assertThrows<HttpServerErrorException.ServiceUnavailable> {
memberInfraAdapter.findMember(1101)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package com.example.estdelivery.coupon.application.port.out.adapter.infra

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties
import org.springframework.web.client.HttpClientErrorException
import org.springframework.web.client.HttpServerErrorException

@SpringBootTest
@AutoConfigureStubRunner(
Expand All @@ -25,12 +28,40 @@ class ShopInfraAdapterTest(

@Test
fun `가게 식별자로 가게 주인을 찾는다`() {
val shopOwner = shopInfraAdapter.findShopOwnerByShopId(2L)
assertThat(shopOwner.id).isEqualTo(2L)
val shopOwner = shopInfraAdapter.findShopOwnerByShopId(1L)
assertThat(shopOwner.id).isEqualTo(1L)
}

@Test
fun `가게 주인에게 단골 소님을 추가한다`() {
shopInfraAdapter.addRoyalCustomers(3L, 4L)
shopInfraAdapter.addRoyalCustomers(3L, 7L)
}

@Test
fun `1000번 식별자를 조회하면 400 에러가 발생한다`() {
assertThrows<HttpClientErrorException.BadRequest> {
shopInfraAdapter.findShopOwner(1000)
}
}

@Test
fun `1001번 식별자를 조회하면 429 에러가 발생한다`() {
assertThrows<HttpClientErrorException.TooManyRequests> {
shopInfraAdapter.findShopOwner(1001)
}
}

@Test
fun `1100번 식별자를 조회하면 500 에러가 발생한다`() {
assertThrows<HttpServerErrorException.InternalServerError> {
shopInfraAdapter.findShopOwner(1100)
}
}

@Test
fun `1101번 식별자를 조회하면 503 에러가 발생한다`() {
assertThrows<HttpServerErrorException.ServiceUnavailable> {
shopInfraAdapter.findShopOwner(1101)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.estdelivery.event.controller

import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice

@RestControllerAdvice
class EventExceptionHandler {
@ExceptionHandler(BadRequestException::class)
fun handleIllegalArgumentException(e: BadRequestException): ResponseEntity<Void> {
return ResponseEntity.badRequest().build()
}

@ExceptionHandler(InternalServerErrorException::class)
fun handleIllegalStateException(e: InternalServerErrorException): ResponseEntity<Void> {
return ResponseEntity.internalServerError().build()
}

@ExceptionHandler(TooManyRequestsException::class)
fun handleTooManyRequests(e: TooManyRequestsException): ResponseEntity<Void> {
return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).build()
}

@ExceptionHandler(ServiceUnavailableException::class)
fun handleServiceUnavailable(e: ServiceUnavailableException): ResponseEntity<Void> {
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build()
}
}

class BadRequestException : RuntimeException()
class InternalServerErrorException : RuntimeException()
class TooManyRequestsException : RuntimeException()
class ServiceUnavailableException : RuntimeException()
55 changes: 46 additions & 9 deletions event/src/test/kotlin/com/example/estdelivery/event/EventBase.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
package com.example.estdelivery.event

import com.example.estdelivery.event.controller.EventController
import com.example.estdelivery.event.controller.BadRequestException
import com.example.estdelivery.event.controller.InternalServerErrorException
import com.example.estdelivery.event.controller.ServiceUnavailableException
import com.example.estdelivery.event.controller.TooManyRequestsException
import com.example.estdelivery.event.dto.EventResponse
import com.example.estdelivery.event.entity.EventDiscountType
import com.example.estdelivery.event.entity.ProbabilityRange
import com.example.estdelivery.event.service.EventService
import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import io.restassured.module.mockmvc.RestAssuredMockMvc
import org.junit.jupiter.api.BeforeEach
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpStatus
import org.springframework.web.client.HttpClientErrorException
import org.springframework.web.context.WebApplicationContext

@SpringBootTest
open class EventBase {
@Autowired
lateinit var context: WebApplicationContext

@MockkBean
lateinit var eventService: EventService

@BeforeEach
fun setup() {
val eventService = mockk<EventService>()
val id = slot<Long>()
every { eventService.findById(capture(id)) } answers {
every { eventService.findById(any()) } answers {
val id = firstArg<Long>()
handleDefaultException(id)

EventResponse(
id.captured,
id,
"이벤트 설명",
true,
EventDiscountType.FIXED,
Expand All @@ -33,7 +48,29 @@ open class EventBase {
)
}

every { eventService.participate(any(), any()) } returns Unit
RestAssuredMockMvc.standaloneSetup(EventController(eventService))
every { eventService.participate(any(), any()) } answers {
val id = secondArg<Long>()
handleDefaultException(id)
}

RestAssuredMockMvc.webAppContextSetup(this.context)
}

private fun handleDefaultException(id: Long) {
if (id == 1000L) {
throw BadRequestException()
}

if (id == 1001L) {
throw TooManyRequestsException()
}

if (id == 1100L) {
throw InternalServerErrorException()
}

if (id == 1101L) {
throw ServiceUnavailableException()
}
}
}
Loading

0 comments on commit b073289

Please sign in to comment.