|
| 1 | +package apartment.example.backend.controller.Integrationtest; |
| 2 | + |
| 3 | + |
| 4 | +import apartment.example.backend.entity.Unit; |
| 5 | +import apartment.example.backend.entity.enums.UnitStatus; |
| 6 | +import apartment.example.backend.repository.UnitRepository; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; |
| 13 | +import org.springframework.http.MediaType; |
| 14 | +import org.springframework.security.test.context.support.WithMockUser; |
| 15 | +import org.springframework.test.context.ActiveProfiles; |
| 16 | +import org.springframework.test.context.DynamicPropertyRegistry; |
| 17 | +import org.springframework.test.context.DynamicPropertySource; |
| 18 | +import org.springframework.test.web.servlet.MockMvc; |
| 19 | + |
| 20 | +import java.math.BigDecimal; |
| 21 | + |
| 22 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 23 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 24 | + |
| 25 | +@SpringBootTest |
| 26 | +@AutoConfigureMockMvc |
| 27 | +@ActiveProfiles("test") |
| 28 | +@WithMockUser(username = "admin", roles = {"ADMIN"}) |
| 29 | +public class UnitControllertest { |
| 30 | + |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private MockMvc mockMvc; |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private UnitRepository unitRepository; |
| 37 | + |
| 38 | + @Autowired |
| 39 | + private ObjectMapper objectMapper; |
| 40 | + |
| 41 | + private Long savedId; |
| 42 | + |
| 43 | + @DynamicPropertySource |
| 44 | + static void jwtProperties(DynamicPropertyRegistry registry) { |
| 45 | + // Override JWT properties for testing |
| 46 | + registry.add("jwt.secret", () -> "dGVzdHNlY3JldA=="); |
| 47 | + registry.add("jwt.expiration", () -> "3600000"); |
| 48 | + |
| 49 | + // Override CORS properties for testing |
| 50 | + registry.add("cors.allowed-origins", () -> "http://localhost:5173"); |
| 51 | + |
| 52 | + // Override database properties for Testcontainers |
| 53 | + registry.add("spring.datasource.url", () -> "jdbc:tc:mysql:8.0:///testdb"); |
| 54 | + registry.add("spring.datasource.driver-class-name", () -> "org.testcontainers.jdbc.ContainerDatabaseDriver"); |
| 55 | + registry.add("spring.jpa.hibernate.ddl-auto", () -> "update"); |
| 56 | + registry.add("spring.sql.init.mode", () -> "never"); |
| 57 | + |
| 58 | + registry.add("spring.jpa.show-sql", () -> "true"); |
| 59 | + } |
| 60 | + |
| 61 | + @BeforeEach |
| 62 | + void setup() { |
| 63 | + unitRepository.deleteAll(); |
| 64 | + |
| 65 | + Unit u = new Unit(); |
| 66 | + u.setRoomNumber("A101"); |
| 67 | + u.setFloor(1); |
| 68 | + u.setStatus(UnitStatus.AVAILABLE); |
| 69 | + u.setRentAmount(BigDecimal.valueOf(8000)); |
| 70 | + u.setUnitType("Standard"); |
| 71 | + savedId = unitRepository.saveAndFlush(u).getId(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testGetAllUnits() throws Exception { |
| 76 | + mockMvc.perform(get("/units")) |
| 77 | + .andExpect(status().isOk()) |
| 78 | + .andExpect(jsonPath("$[0].roomNumber").value("A101")); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testGetUnitById() throws Exception { |
| 83 | + mockMvc.perform(get("/units/" + savedId)) |
| 84 | + .andExpect(status().isOk()) |
| 85 | + .andExpect(jsonPath("$.roomNumber").value("A101")); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testCreateUnit() throws Exception { |
| 90 | + Unit newUnit = new Unit(); |
| 91 | + newUnit.setRoomNumber("102"); |
| 92 | + newUnit.setFloor(1); |
| 93 | + newUnit.setStatus(UnitStatus.AVAILABLE); |
| 94 | + newUnit.setRentAmount(BigDecimal.valueOf(9000)); |
| 95 | + |
| 96 | + // --- เพิ่มบรรทัดนี้ --- |
| 97 | + newUnit.setUnitType("Standard"); |
| 98 | + // ------------------- |
| 99 | + |
| 100 | + mockMvc.perform(post("/units") |
| 101 | + .contentType(MediaType.APPLICATION_JSON) |
| 102 | + .content(objectMapper.writeValueAsString(newUnit))) |
| 103 | + .andExpect(status().isCreated()) |
| 104 | + .andExpect(jsonPath("$.roomNumber").value("102")); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void testUpdateUnit() throws Exception { |
| 109 | + Unit update = new Unit(); |
| 110 | + update.setRoomNumber("101-UPDATE"); |
| 111 | + update.setFloor(1); |
| 112 | + update.setStatus(UnitStatus.MAINTENANCE); |
| 113 | + update.setRentAmount(BigDecimal.valueOf(7000)); |
| 114 | + |
| 115 | + // --- เพิ่มบรรทัดนี้ --- |
| 116 | + update.setUnitType("Standard"); |
| 117 | + // ------------------- |
| 118 | + |
| 119 | + mockMvc.perform(put("/units/" + savedId) |
| 120 | + .contentType(MediaType.APPLICATION_JSON) |
| 121 | + .content(objectMapper.writeValueAsString(update))) |
| 122 | + .andExpect(status().isOk()) |
| 123 | + .andExpect(jsonPath("$.roomNumber").value("101-UPDATE")) |
| 124 | + .andExpect(jsonPath("$.status").value("MAINTENANCE")); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void testPatchStatus() throws Exception { |
| 129 | + String json = """ |
| 130 | + { "status": "OCCUPIED" } |
| 131 | + """; |
| 132 | + |
| 133 | + mockMvc.perform(patch("/units/" + savedId + "/status") |
| 134 | + .contentType(MediaType.APPLICATION_JSON) |
| 135 | + .content(json)) |
| 136 | + .andExpect(status().isOk()) |
| 137 | + .andExpect(jsonPath("$.status").value("OCCUPIED")); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void testDeleteUnit() throws Exception { |
| 142 | + mockMvc.perform(delete("/units/" + savedId)) |
| 143 | + .andExpect(status().isNoContent()); |
| 144 | + |
| 145 | + mockMvc.perform(get("/units/" + savedId)) |
| 146 | + .andExpect(status().isNotFound()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void testDashboard() throws Exception { |
| 151 | + mockMvc.perform(get("/units/dashboard")) |
| 152 | + .andExpect(status().isOk()) |
| 153 | + .andExpect(jsonPath("$.totalUnits").value(1)) |
| 154 | + .andExpect(jsonPath("$.availableUnits").value(1)); |
| 155 | + } |
| 156 | +} |
0 commit comments