Skip to content

Commit

Permalink
Merge pull request #88 from wafflestudio/dev
Browse files Browse the repository at this point in the history
중간총회 전 배포 점검
  • Loading branch information
JunBye authored Jan 11, 2025
2 parents 30a727e + 3c51ff5 commit 3754f46
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 116 deletions.
22 changes: 16 additions & 6 deletions src/test/kotlin/com/example/toyTeam6Airbnb/DataGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.example.toyTeam6Airbnb.reservation.persistence.ReservationRepository
import com.example.toyTeam6Airbnb.review.persistence.ReviewEntity
import com.example.toyTeam6Airbnb.review.persistence.ReviewRepository
import com.example.toyTeam6Airbnb.room.persistence.Address
import com.example.toyTeam6Airbnb.room.persistence.Price
import com.example.toyTeam6Airbnb.room.persistence.RoomDetails
import com.example.toyTeam6Airbnb.room.persistence.RoomEntity
import com.example.toyTeam6Airbnb.room.persistence.RoomRepository
Expand Down Expand Up @@ -76,14 +77,16 @@ class DataGenerator(
): ReservationEntity {
val userEntity = user ?: generateUserAndToken().first
val roomEntity = room ?: generateRoom()
val start = startDate ?: LocalDate.now().plusDays((1..100).random().toLong())
val end = endDate ?: start.plusDays((1..10).random().toLong())
return reservationRepository.save(
ReservationEntity(
user = userEntity,
room = roomEntity,
review = null,
startDate = startDate ?: LocalDate.now().plusDays((1..100).random().toLong()),
endDate = endDate ?: startDate!!.plusDays((1..10).random().toLong()),
totalPrice = roomEntity.price * ChronoUnit.DAYS.between(startDate, endDate),
startDate = start,
endDate = end,
totalPrice = totalPrice ?: (roomEntity.price.total * ChronoUnit.DAYS.between(start, end)),
numberOfGuests = numberOfGuests ?: (1..10).random()
)
)
Expand All @@ -96,7 +99,7 @@ class DataGenerator(
type: RoomType? = null,
address: Address? = null,
roomDetails: RoomDetails? = null,
price: Double? = null,
price: Price? = null,
maxOccupancy: Int? = null
): RoomEntity {
val hostEntity = host ?: generateUserAndToken().first
Expand All @@ -117,9 +120,16 @@ class DataGenerator(
wifi = (0..1).random() == 1,
selfCheckin = (0..1).random() == 1,
luggage = (0..1).random() == 1,
TV = (0..1).random() == 1
TV = (0..1).random() == 1,
bedroom = (1..5).random(),
bathroom = (1..3).random(),
bed = (1..3).random()
),
price = price ?: Price(
perNight = (10000..50000).random().toDouble(),
cleaningFee = (5000..10000).random().toDouble(),
charge = (1000..5000).random().toDouble()
),
price = price ?: (10000..100000).random().toDouble(),
maxOccupancy = maxOccupancy ?: (1..10).random()
)
)
Expand Down
136 changes: 134 additions & 2 deletions src/test/kotlin/com/example/toyTeam6Airbnb/ReviewIntegrationTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,138 @@
package com.example.toyTeam6Airbnb

import com.example.toyTeam6Airbnb.review.persistence.ReviewRepository
import com.example.toyTeam6Airbnb.room.persistence.RoomRepository
import com.example.toyTeam6Airbnb.user.persistence.UserRepository
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultHandlers
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import kotlin.test.assertEquals

// Review CRUD 테스트에 대한 테스트코드를 작성해줘
// DataGenerator 사용해
// ReviewRepository, UserRepository, RoomRepository를 사용해
class ReviewIntegrationTest
// ReviewRepository, UserRepository, RoomRepository를 사용해서 작성해줘
@SpringBootTest
@AutoConfigureMockMvc
class ReviewIntegrationTest {

@Autowired
private lateinit var mockMvc: MockMvc

@Autowired
private lateinit var reviewRepository: ReviewRepository

@Autowired
private lateinit var userRepository: UserRepository

@Autowired
private lateinit var roomRepository: RoomRepository

@Autowired
private lateinit var dataGenerator: DataGenerator

@BeforeEach
fun setUp() {
dataGenerator.clearAll()
}

@Test
fun `should create a review`() {
val (user, token) = dataGenerator.generateUserAndToken()
val room = dataGenerator.generateRoom(maxOccupancy = 100)
val reservation = dataGenerator.generateReservation(user, room)

val requestBody = """
{
"roomId": ${room.id},
"reservationId": ${reservation.id},
"content": "Great place!",
"rating": 5
}
""".trimIndent()

val result = mockMvc.perform(
MockMvcRequestBuilders.post("/api/v1/reviews")
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody)
.header("Authorization", "Bearer $token")
)
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isCreated)
.andReturn()

val reviews = reviewRepository.findAll()
assertEquals(1, reviews.size)
assertEquals("Great place!", reviews[0].content)
}

@Test
fun `should update a review`() {
val (user, token) = dataGenerator.generateUserAndToken()
val room = dataGenerator.generateRoom()
val reservation = dataGenerator.generateReservation(user, room)
val review = dataGenerator.generateReview(reservation, "Great place!", 5)

val requestBody = """
{
"content": "Good place!",
"rating": 4
}
""".trimIndent()

val result = mockMvc.perform(
MockMvcRequestBuilders.put("/api/v1/reviews/${review.id}")
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody)
.header("Authorization", "Bearer $token")
)
.andExpect(MockMvcResultMatchers.status().isOk)
.andReturn()

val updatedReview = review.id?.let { reviewRepository.findById(it).get() }
assertEquals(4, updatedReview?.rating)
assertEquals("Good place!", updatedReview?.content)
}

@Test
fun `should delete a review`() {
val (user, token) = dataGenerator.generateUserAndToken()
val room = dataGenerator.generateRoom()
val reservation = dataGenerator.generateReservation(user, room)
val review = dataGenerator.generateReview(reservation)

val result = mockMvc.perform(
MockMvcRequestBuilders.delete("/api/v1/reviews/${review.id}")
.header("Authorization", "Bearer $token")
)
.andExpect(MockMvcResultMatchers.status().isNoContent)
.andReturn()

val reviews = reviewRepository.findAll()
assertEquals(0, reviews.size)
}

@Test
fun `should get a review`() {
val (user, token) = dataGenerator.generateUserAndToken()
val room = dataGenerator.generateRoom()
val reservation = dataGenerator.generateReservation(user, room)
val review = dataGenerator.generateReview(reservation)

val result = mockMvc.perform(
MockMvcRequestBuilders.get("/api/v1/reviews/${review.id}")
.header("Authorization", "Bearer $token")
)
.andExpect(MockMvcResultMatchers.status().isOk)
.andReturn()

val responseContent = result.response.contentAsString
println(responseContent)
}
}
62 changes: 39 additions & 23 deletions src/test/kotlin/com/example/toyTeam6Airbnb/RoomConcurrencyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,18 @@ class RoomConcurrencyTest {
"wifi": true,
"selfCheckin": false,
"luggage": false,
"TV": true
"TV": true,
"bedroom": 1,
"bathroom": 1,
"bed": 1
},
"price": 75000.0,
"maxOccupancy": 4
"price": {
"perNight": 5000,
"cleaningFee": 5000,
"charge": 5000,
"total": 15000
},
"maxOccupancy": 1
}
""".trimIndent()

Expand Down Expand Up @@ -102,33 +110,41 @@ class RoomConcurrencyTest {

// requestBody를 위와 같은 형태로 하여 서로 다른 유저가 같은 방을 만드는 동시성 상황에 대한 테스트 케이스 형성
@Test
fun `동일한 요청이 서로 다른 유저로부터 들어오면 방이 각각 만들어져야함`() {
fun `동일한 요청이 서로 다른 유저로부터 들어오면 방이 하나만 만들어져야함`() {
val (user1, token1) = dataGenerator.generateUserAndToken()
val (user2, token2) = dataGenerator.generateUserAndToken()

val latch = CountDownLatch(2)
val executor = Executors.newFixedThreadPool(2)

val requestBody = """
{
"name": "Cozy Apartment in Seoul",
"description": "A beautiful and cozy apartment located in the heart of Seoul. Perfect for travelers!",
"type": "APARTMENT",
"address": {
"sido": "Seoul",
"sigungu": "Jongno-gu",
"street": "123 Hanok Street",
"detail": "Apartment 5B"
},
"roomDetails": {
"wifi": true,
"selfCheckin": false,
"luggage": false,
"TV": true
},
"price": 75000.0,
"maxOccupancy": 4
}
{
"name": "Cozy Apartment in Seoul",
"description": "A beautiful and cozy apartment located in the heart of Seoul. Perfect for travelers!",
"type": "APARTMENT",
"address": {
"sido": "Seoul",
"sigungu": "Jongno-gu",
"street": "123 Hanok Street",
"detail": "Apartment 5B"
},
"roomDetails": {
"wifi": true,
"selfCheckin": false,
"luggage": false,
"TV": true,
"bedroom": 1,
"bathroom": 1,
"bed": 1
},
"price": {
"perNight": 50000,
"cleaningFee": 20000,
"charge": 5000,
"total": 0
},
"maxOccupancy": 4
}
""".trimIndent()

executor.submit {
Expand Down
Loading

0 comments on commit 3754f46

Please sign in to comment.