Extract AdminOrderWorkflowService and status rules API; split AdminPage into composables and components; share order status constants; update tests. Co-authored-by: Cursor <cursoragent@cursor.com>
176 lines
7.7 KiB
Java
176 lines
7.7 KiB
Java
package se.bilhalsning.controller;
|
|
|
|
import static org.mockito.ArgumentMatchers.anyBoolean;
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
import static org.mockito.Mockito.when;
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.time.Instant;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.security.test.context.support.WithMockUser;
|
|
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
import se.bilhalsning.entity.Order;
|
|
import se.bilhalsning.entity.OrderStatus;
|
|
import se.bilhalsning.entity.User;
|
|
import se.bilhalsning.exception.InvalidOrderStateException;
|
|
import se.bilhalsning.exception.OrderNotFoundException;
|
|
import se.bilhalsning.service.AdminOrderWorkflowService;
|
|
import se.bilhalsning.service.OrderService;
|
|
|
|
@SpringBootTest
|
|
@AutoConfigureMockMvc
|
|
class AdminControllerTest {
|
|
|
|
@Autowired
|
|
private MockMvc mockMvc;
|
|
|
|
@MockitoBean
|
|
private OrderService orderService;
|
|
|
|
@MockitoBean
|
|
private AdminOrderWorkflowService adminOrderWorkflowService;
|
|
|
|
@Test
|
|
void shouldReturn403WhenNotAuthenticated() throws Exception {
|
|
mockMvc.perform(get("/api/admin/orders"))
|
|
.andExpect(status().isForbidden());
|
|
}
|
|
|
|
@Test
|
|
@WithMockUser(username = "test@bilhej.se", roles = "USER")
|
|
void shouldReturn403ForNonAdminUser() throws Exception {
|
|
mockMvc.perform(get("/api/admin/orders"))
|
|
.andExpect(status().isForbidden());
|
|
}
|
|
|
|
@Test
|
|
@WithMockUser(username = "admin@bilhalsning.se", roles = "ADMIN")
|
|
void shouldReturnAllOrdersForAdmin() throws Exception {
|
|
Order order = createOrder(UUID.randomUUID(), "ABC123", "test@bilhej.se", OrderStatus.SENT);
|
|
when(orderService.getAllOrders()).thenReturn(List.of(order));
|
|
|
|
mockMvc.perform(get("/api/admin/orders"))
|
|
.andExpect(status().isOk())
|
|
.andExpect(jsonPath("$").isArray())
|
|
.andExpect(jsonPath("$[0].id").value(order.getId().toString()))
|
|
.andExpect(jsonPath("$[0].email").value("test@bilhej.se"))
|
|
.andExpect(jsonPath("$[0].plate").value("ABC123"))
|
|
.andExpect(jsonPath("$[0].status").value("sent"))
|
|
.andExpect(jsonPath("$[0].allowedStatuses").isArray())
|
|
.andExpect(jsonPath("$[0].canRegisterShipment").value(true));
|
|
}
|
|
|
|
@Test
|
|
@WithMockUser(username = "admin@bilhalsning.se", roles = "ADMIN")
|
|
void shouldUpdateOrderStatusSuccessfully() throws Exception {
|
|
UUID orderId = UUID.fromString("c1eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
|
|
Order order = createOrder(orderId, "ABC123", "test@bilhej.se", OrderStatus.FAILED);
|
|
|
|
when(adminOrderWorkflowService.updateOrderStatus(eq(orderId), eq("failed")))
|
|
.thenReturn(order);
|
|
|
|
mockMvc.perform(patch("/api/admin/orders/{id}/status", orderId)
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.content("{\"status\":\"failed\"}"))
|
|
.andExpect(status().isOk())
|
|
.andExpect(jsonPath("$.status").value("failed"));
|
|
}
|
|
|
|
@Test
|
|
@WithMockUser(username = "admin@bilhalsning.se", roles = "ADMIN")
|
|
void shouldReturn409WhenStatusTransitionInvalid() throws Exception {
|
|
UUID orderId = UUID.fromString("c1eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
|
|
when(adminOrderWorkflowService.updateOrderStatus(eq(orderId), eq("delivered")))
|
|
.thenThrow(new InvalidOrderStateException("Ogiltig övergång"));
|
|
|
|
mockMvc.perform(patch("/api/admin/orders/{id}/status", orderId)
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.content("{\"status\":\"delivered\"}"))
|
|
.andExpect(status().isConflict());
|
|
}
|
|
|
|
@Test
|
|
@WithMockUser(username = "admin@bilhalsning.se", roles = "ADMIN")
|
|
void shouldRegisterShipmentSuccessfully() throws Exception {
|
|
UUID orderId = UUID.fromString("c1eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
|
|
Order order = createOrder(orderId, "ABC123", "test@bilhej.se", OrderStatus.SENT);
|
|
order.setTrackingId("PN123456789");
|
|
order.setShippedAt(Instant.parse("2026-05-13T12:00:00Z"));
|
|
|
|
when(adminOrderWorkflowService.registerShipment(eq(orderId), eq("PN123456789"), eq(true)))
|
|
.thenReturn(order);
|
|
|
|
mockMvc.perform(patch("/api/admin/orders/{id}/register-shipment", orderId)
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.content("{\"trackingInput\":\"PN123456789\",\"notifyCustomer\":true}"))
|
|
.andExpect(status().isOk())
|
|
.andExpect(jsonPath("$.trackingId").value("PN123456789"))
|
|
.andExpect(jsonPath("$.status").value("sent"));
|
|
}
|
|
|
|
@Test
|
|
@WithMockUser(username = "admin@bilhalsning.se", roles = "ADMIN")
|
|
void shouldReturn400WhenTrackingInputBlank() throws Exception {
|
|
UUID orderId = UUID.fromString("c1eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
|
|
|
|
mockMvc.perform(patch("/api/admin/orders/{id}/register-shipment", orderId)
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.content("{\"trackingInput\":\"\"}"))
|
|
.andExpect(status().isBadRequest());
|
|
}
|
|
|
|
@Test
|
|
@WithMockUser(username = "admin@bilhalsning.se", roles = "ADMIN")
|
|
void shouldUpdateAdminNotes() throws Exception {
|
|
UUID orderId = UUID.fromString("c1eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
|
|
Order order = createOrder(orderId, "ABC123", "test@bilhej.se", OrderStatus.PROCESSING);
|
|
order.setAdminNotes("Kontaktat TS");
|
|
|
|
when(adminOrderWorkflowService.updateAdminNotes(orderId, "Kontaktat TS")).thenReturn(order);
|
|
|
|
mockMvc.perform(patch("/api/admin/orders/{id}/notes", orderId)
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.content("{\"adminNotes\":\"Kontaktat TS\"}"))
|
|
.andExpect(status().isOk())
|
|
.andExpect(jsonPath("$.adminNotes").value("Kontaktat TS"));
|
|
}
|
|
|
|
@Test
|
|
@WithMockUser(username = "admin@bilhalsning.se", roles = "ADMIN")
|
|
void shouldReturn404WhenOrderNotFoundForRegisterShipment() throws Exception {
|
|
UUID orderId = UUID.fromString("c1eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
|
|
when(adminOrderWorkflowService.registerShipment(eq(orderId), eq("PN123"), anyBoolean()))
|
|
.thenThrow(new OrderNotFoundException(orderId));
|
|
|
|
mockMvc.perform(patch("/api/admin/orders/{id}/register-shipment", orderId)
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.content("{\"trackingInput\":\"PN123\"}"))
|
|
.andExpect(status().isNotFound());
|
|
}
|
|
|
|
private Order createOrder(UUID orderId, String plate, String email, OrderStatus status) {
|
|
User user = new User();
|
|
user.setEmail(email);
|
|
|
|
Order order = new Order();
|
|
order.setId(orderId);
|
|
order.setUser(user);
|
|
order.setPlate(plate);
|
|
order.setLetterText("Test letter");
|
|
order.setStatus(status);
|
|
order.setAmountPaid(new BigDecimal("49.00"));
|
|
|
|
return order;
|
|
}
|
|
}
|