|
1 |
| -package com.group4.ticketingservice.controller |
2 |
| - |
3 |
| -import com.google.gson.Gson |
4 |
| -import com.group4.ticketingservice.dto.BookingCreateRequest |
5 |
| -import com.group4.ticketingservice.dto.BookingDeleteRequest |
6 |
| -import com.group4.ticketingservice.dto.BookingUpdateRequest |
7 |
| -import com.group4.ticketingservice.entity.Booking |
8 |
| -import com.group4.ticketingservice.entity.Performance |
9 |
| -import com.group4.ticketingservice.entity.User |
10 |
| -import com.group4.ticketingservice.service.BookingService |
11 |
| -import com.ninjasquad.springmockk.MockkBean |
12 |
| -import io.mockk.every |
13 |
| -import io.mockk.junit5.MockKExtension |
14 |
| -import org.junit.jupiter.api.Test |
15 |
| -import org.junit.jupiter.api.extension.ExtendWith |
16 |
| -import org.springframework.beans.factory.annotation.Autowired |
17 |
| -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest |
18 |
| -import org.springframework.http.MediaType |
19 |
| -import org.springframework.test.web.servlet.MockMvc |
20 |
| -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete |
21 |
| -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get |
22 |
| -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post |
23 |
| -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put |
24 |
| -import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content |
25 |
| -import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath |
26 |
| -import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status |
27 |
| -import java.time.Duration |
28 |
| -import java.time.LocalDateTime |
29 |
| - |
30 |
| -@ExtendWith(MockKExtension::class) |
31 |
| -@WebMvcTest(BookingController::class) |
32 |
| -class BookingControllerTest( |
33 |
| - @Autowired val mockMvc: MockMvc |
34 |
| -) { |
35 |
| - @MockkBean |
36 |
| - private lateinit var bookingService: BookingService |
37 |
| - |
38 |
| - private val sampleBookingCreateRequest = BookingCreateRequest( |
39 |
| - performanceId = 1, |
40 |
| - userId = 1 |
41 |
| - ) |
42 |
| - private val sampleBookingDeleteRequest = BookingDeleteRequest( |
43 |
| - id = 1 |
44 |
| - ) |
45 |
| - private val sampleUser : User = User(id = 1, name = "John Doe", email = "[email protected]") |
46 |
| - private val samplePerformance: Performance = Performance( |
47 |
| - id = 1, |
48 |
| - title = "test title", |
49 |
| - date = LocalDateTime.now(), |
50 |
| - bookingEndTime = LocalDateTime.now() + Duration.ofHours(2), |
51 |
| - bookingStartTime = LocalDateTime.now() + Duration.ofHours(1), |
52 |
| - maxAttendees = 10 |
53 |
| - ) |
54 |
| - private val sampleBooking: Booking = Booking( |
55 |
| - id = 1, |
56 |
| - user = sampleUser, |
57 |
| - performance = samplePerformance |
58 |
| - ) |
59 |
| - |
60 |
| - // createBooking |
61 |
| - @Test |
62 |
| - fun `POST bookings should return created booking`() { |
63 |
| - every { bookingService.createBooking(1, 1) } returns sampleBooking |
64 |
| - |
65 |
| - mockMvc.perform( |
66 |
| - post("/bookings") |
67 |
| - .contentType(MediaType.APPLICATION_JSON) |
68 |
| - .content(Gson().toJson(sampleBookingCreateRequest)) |
69 |
| - ) |
70 |
| - .andExpect(status().isOk) |
71 |
| - .andExpect(content().contentType(MediaType.APPLICATION_JSON)) |
72 |
| - .andExpect(jsonPath("$.id").value(sampleBooking.id)) |
73 |
| - .andExpect(jsonPath("$.userId").value(sampleBooking.user.id)) |
74 |
| - .andExpect(jsonPath("$.performanceId").value(sampleBooking.performance.id)) |
75 |
| - } |
76 |
| - |
77 |
| - @Test |
78 |
| - fun `GET bookings should return booking`() { |
79 |
| - every { bookingService.getBooking(1) } returns sampleBooking |
80 |
| - |
81 |
| - mockMvc.perform( |
82 |
| - get("/bookings/1") |
83 |
| - .contentType(MediaType.APPLICATION_JSON) |
84 |
| - ) |
85 |
| - .andExpect(status().isOk) |
86 |
| - .andExpect(content().contentType(MediaType.APPLICATION_JSON)) |
87 |
| - .andExpect(jsonPath("$.id").value(sampleBooking.id)) |
88 |
| - .andExpect(jsonPath("$.userId").value(sampleBooking.user.id)) |
89 |
| - .andExpect(jsonPath("$.performanceId").value(sampleBooking.performance.id)) |
90 |
| - } |
91 |
| - |
92 |
| - @Test |
93 |
| - fun `PUT bookings should return updated booking`() { |
94 |
| - val bookingUpdateRequest = BookingUpdateRequest( |
95 |
| - performanceId = 2 |
96 |
| - ) |
97 |
| - val updatedBooking = Booking( |
98 |
| - id = 1, |
99 |
| - user = sampleUser, |
100 |
| - performance = Performance( |
101 |
| - id = 2, |
102 |
| - title = "test title 2", |
103 |
| - date = LocalDateTime.now(), |
104 |
| - bookingEndTime = LocalDateTime.now() + Duration.ofHours(2), |
105 |
| - bookingStartTime = LocalDateTime.now() + Duration.ofHours(1), |
106 |
| - maxAttendees = 10 |
107 |
| - ) |
108 |
| - ) |
109 |
| - every { bookingService.updateBooking(1, 2) } returns updatedBooking |
110 |
| - |
111 |
| - mockMvc.perform( |
112 |
| - put("/bookings/1") |
113 |
| - .contentType(MediaType.APPLICATION_JSON) |
114 |
| - .content(Gson().toJson(bookingUpdateRequest)) |
115 |
| - ) |
116 |
| - .andExpect(status().isOk) |
117 |
| - .andExpect(content().contentType(MediaType.APPLICATION_JSON)) |
118 |
| - .andExpect(jsonPath("$.id").value(updatedBooking.id)) |
119 |
| - .andExpect(jsonPath("$.userId").value(updatedBooking.user.id)) |
120 |
| - .andExpect(jsonPath("$.performanceId").value(updatedBooking.performance.id)) |
121 |
| - } |
122 |
| - |
123 |
| - @Test |
124 |
| - fun `DELETE bookings should return no content`() { |
125 |
| - every { bookingService.deleteBooking(1) } returns Unit |
126 |
| - |
127 |
| - mockMvc.perform( |
128 |
| - delete("/bookings/${sampleBookingDeleteRequest.id}") |
129 |
| - .contentType(MediaType.APPLICATION_JSON) |
130 |
| - ) |
131 |
| - .andExpect(status().isNoContent) |
132 |
| - } |
133 |
| -} |
| 1 | +package com.group4.ticketingservice.controller |
| 2 | + |
| 3 | +import com.google.gson.Gson |
| 4 | +import com.group4.ticketingservice.dto.BookingCreateRequest |
| 5 | +import com.group4.ticketingservice.dto.BookingDeleteRequest |
| 6 | +import com.group4.ticketingservice.dto.BookingUpdateRequest |
| 7 | +import com.group4.ticketingservice.entity.Booking |
| 8 | +import com.group4.ticketingservice.entity.Performance |
| 9 | +import com.group4.ticketingservice.entity.User |
| 10 | +import com.group4.ticketingservice.service.BookingService |
| 11 | +import com.ninjasquad.springmockk.MockkBean |
| 12 | +import io.mockk.every |
| 13 | +import io.mockk.junit5.MockKExtension |
| 14 | +import org.junit.jupiter.api.Test |
| 15 | +import org.junit.jupiter.api.extension.ExtendWith |
| 16 | +import org.springframework.beans.factory.annotation.Autowired |
| 17 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest |
| 18 | +import org.springframework.http.MediaType |
| 19 | +import org.springframework.test.web.servlet.MockMvc |
| 20 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete |
| 21 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get |
| 22 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post |
| 23 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put |
| 24 | +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content |
| 25 | +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath |
| 26 | +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status |
| 27 | +import java.time.Duration |
| 28 | +import java.time.LocalDateTime |
| 29 | + |
| 30 | +@ExtendWith(MockKExtension::class) |
| 31 | +@WebMvcTest(BookingController::class) |
| 32 | +class BookingControllerTest( |
| 33 | + @Autowired val mockMvc: MockMvc |
| 34 | +) { |
| 35 | + @MockkBean |
| 36 | + private lateinit var bookingService: BookingService |
| 37 | + |
| 38 | + private val sampleBookingCreateRequest = BookingCreateRequest( |
| 39 | + performanceId = 1, |
| 40 | + userId = 1 |
| 41 | + ) |
| 42 | + private val sampleBookingDeleteRequest = BookingDeleteRequest( |
| 43 | + id = 1 |
| 44 | + ) |
| 45 | + private val sampleUser : User = User(id = 1, name = "John Doe", email = "[email protected]") |
| 46 | + private val samplePerformance: Performance = Performance( |
| 47 | + id = 1, |
| 48 | + title = "test title", |
| 49 | + date = LocalDateTime.now(), |
| 50 | + bookingEndTime = LocalDateTime.now() + Duration.ofHours(2), |
| 51 | + bookingStartTime = LocalDateTime.now() + Duration.ofHours(1), |
| 52 | + maxAttendees = 10 |
| 53 | + ) |
| 54 | + private val sampleBooking: Booking = Booking( |
| 55 | + id = 1, |
| 56 | + user = sampleUser, |
| 57 | + performance = samplePerformance |
| 58 | + ) |
| 59 | + |
| 60 | + // createBooking |
| 61 | + @Test |
| 62 | + fun `POST bookings should return created booking`() { |
| 63 | + every { bookingService.createBooking(1, 1) } returns sampleBooking |
| 64 | + |
| 65 | + mockMvc.perform( |
| 66 | + post("/bookings") |
| 67 | + .contentType(MediaType.APPLICATION_JSON) |
| 68 | + .content(Gson().toJson(sampleBookingCreateRequest)) |
| 69 | + ) |
| 70 | + .andExpect(status().isOk) |
| 71 | + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) |
| 72 | + .andExpect(jsonPath("$.id").value(sampleBooking.id)) |
| 73 | + .andExpect(jsonPath("$.userId").value(sampleBooking.user.id)) |
| 74 | + .andExpect(jsonPath("$.performanceId").value(sampleBooking.performance.id)) |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun `GET bookings should return booking`() { |
| 79 | + every { bookingService.getBooking(1) } returns sampleBooking |
| 80 | + |
| 81 | + mockMvc.perform( |
| 82 | + get("/bookings/1") |
| 83 | + .contentType(MediaType.APPLICATION_JSON) |
| 84 | + ) |
| 85 | + .andExpect(status().isOk) |
| 86 | + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) |
| 87 | + .andExpect(jsonPath("$.id").value(sampleBooking.id)) |
| 88 | + .andExpect(jsonPath("$.userId").value(sampleBooking.user.id)) |
| 89 | + .andExpect(jsonPath("$.performanceId").value(sampleBooking.performance.id)) |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun `PUT bookings should return updated booking`() { |
| 94 | + val bookingUpdateRequest = BookingUpdateRequest( |
| 95 | + performanceId = 2 |
| 96 | + ) |
| 97 | + val updatedBooking = Booking( |
| 98 | + id = 1, |
| 99 | + user = sampleUser, |
| 100 | + performance = Performance( |
| 101 | + id = 2, |
| 102 | + title = "test title 2", |
| 103 | + date = LocalDateTime.now(), |
| 104 | + bookingEndTime = LocalDateTime.now() + Duration.ofHours(2), |
| 105 | + bookingStartTime = LocalDateTime.now() + Duration.ofHours(1), |
| 106 | + maxAttendees = 10 |
| 107 | + ) |
| 108 | + ) |
| 109 | + every { bookingService.updateBooking(1, 2) } returns updatedBooking |
| 110 | + |
| 111 | + mockMvc.perform( |
| 112 | + put("/bookings/1") |
| 113 | + .contentType(MediaType.APPLICATION_JSON) |
| 114 | + .content(Gson().toJson(bookingUpdateRequest)) |
| 115 | + ) |
| 116 | + .andExpect(status().isOk) |
| 117 | + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) |
| 118 | + .andExpect(jsonPath("$.id").value(updatedBooking.id)) |
| 119 | + .andExpect(jsonPath("$.userId").value(updatedBooking.user.id)) |
| 120 | + .andExpect(jsonPath("$.performanceId").value(updatedBooking.performance.id)) |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + fun `DELETE bookings should return no content`() { |
| 125 | + every { bookingService.deleteBooking(1) } returns Unit |
| 126 | + |
| 127 | + mockMvc.perform( |
| 128 | + delete("/bookings/${sampleBookingDeleteRequest.id}") |
| 129 | + .contentType(MediaType.APPLICATION_JSON) |
| 130 | + ) |
| 131 | + .andExpect(status().isNoContent) |
| 132 | + } |
| 133 | +} |
0 commit comments