Add REST controller with RFC 9457 error handling
EventController for POST /events, GlobalExceptionHandler mapping validation and business exceptions to problem+json responses. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
package de.fete.adapter.in.web;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import de.fete.TestcontainersConfig;
|
||||
import java.time.LocalDate;
|
||||
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.context.annotation.Import;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@Import(TestcontainersConfig.class)
|
||||
class EventControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
void createEventWithValidBody() throws Exception {
|
||||
String body =
|
||||
"""
|
||||
{
|
||||
"title": "Birthday Party",
|
||||
"description": "Come celebrate!",
|
||||
"dateTime": "2026-06-15T20:00:00+02:00",
|
||||
"location": "Berlin",
|
||||
"expiryDate": "%s"
|
||||
}
|
||||
""".formatted(LocalDate.now().plusDays(30));
|
||||
|
||||
mockMvc.perform(post("/api/events")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(body))
|
||||
.andExpect(status().isCreated())
|
||||
.andExpect(jsonPath("$.eventToken").isNotEmpty())
|
||||
.andExpect(jsonPath("$.organizerToken").isNotEmpty())
|
||||
.andExpect(jsonPath("$.title").value("Birthday Party"))
|
||||
.andExpect(jsonPath("$.dateTime").isNotEmpty())
|
||||
.andExpect(jsonPath("$.expiryDate").isNotEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createEventWithOptionalFieldsNull() throws Exception {
|
||||
String body =
|
||||
"""
|
||||
{
|
||||
"title": "Minimal Event",
|
||||
"dateTime": "2026-06-15T20:00:00+02:00",
|
||||
"expiryDate": "%s"
|
||||
}
|
||||
""".formatted(LocalDate.now().plusDays(30));
|
||||
|
||||
mockMvc.perform(post("/api/events")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(body))
|
||||
.andExpect(status().isCreated())
|
||||
.andExpect(jsonPath("$.eventToken").isNotEmpty())
|
||||
.andExpect(jsonPath("$.organizerToken").isNotEmpty())
|
||||
.andExpect(jsonPath("$.title").value("Minimal Event"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createEventMissingTitleReturns400() throws Exception {
|
||||
String body =
|
||||
"""
|
||||
{
|
||||
"dateTime": "2026-06-15T20:00:00+02:00",
|
||||
"expiryDate": "%s"
|
||||
}
|
||||
""".formatted(LocalDate.now().plusDays(30));
|
||||
|
||||
mockMvc.perform(post("/api/events")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(body))
|
||||
.andExpect(status().isBadRequest())
|
||||
.andExpect(content().contentTypeCompatibleWith("application/problem+json"))
|
||||
.andExpect(jsonPath("$.title").value("Validation Failed"))
|
||||
.andExpect(jsonPath("$.fieldErrors").isArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createEventMissingDateTimeReturns400() throws Exception {
|
||||
String body =
|
||||
"""
|
||||
{
|
||||
"title": "No Date",
|
||||
"expiryDate": "%s"
|
||||
}
|
||||
""".formatted(LocalDate.now().plusDays(30));
|
||||
|
||||
mockMvc.perform(post("/api/events")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(body))
|
||||
.andExpect(status().isBadRequest())
|
||||
.andExpect(content().contentTypeCompatibleWith("application/problem+json"))
|
||||
.andExpect(jsonPath("$.fieldErrors").isArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createEventMissingExpiryDateReturns400() throws Exception {
|
||||
String body =
|
||||
"""
|
||||
{
|
||||
"title": "No Expiry",
|
||||
"dateTime": "2026-06-15T20:00:00+02:00"
|
||||
}
|
||||
""";
|
||||
|
||||
mockMvc.perform(post("/api/events")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(body))
|
||||
.andExpect(status().isBadRequest())
|
||||
.andExpect(content().contentTypeCompatibleWith("application/problem+json"))
|
||||
.andExpect(jsonPath("$.fieldErrors").isArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createEventExpiryDateInPastReturns400() throws Exception {
|
||||
String body =
|
||||
"""
|
||||
{
|
||||
"title": "Past Expiry",
|
||||
"dateTime": "2026-06-15T20:00:00+02:00",
|
||||
"expiryDate": "2025-01-01"
|
||||
}
|
||||
""";
|
||||
|
||||
mockMvc.perform(post("/api/events")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(body))
|
||||
.andExpect(status().isBadRequest())
|
||||
.andExpect(content().contentTypeCompatibleWith("application/problem+json"))
|
||||
.andExpect(jsonPath("$.type").value("urn:problem-type:expiry-date-in-past"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createEventExpiryDateTodayReturns400() throws Exception {
|
||||
String body =
|
||||
"""
|
||||
{
|
||||
"title": "Today Expiry",
|
||||
"dateTime": "2026-06-15T20:00:00+02:00",
|
||||
"expiryDate": "%s"
|
||||
}
|
||||
""".formatted(LocalDate.now());
|
||||
|
||||
mockMvc.perform(post("/api/events")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(body))
|
||||
.andExpect(status().isBadRequest())
|
||||
.andExpect(content().contentTypeCompatibleWith("application/problem+json"))
|
||||
.andExpect(jsonPath("$.type").value("urn:problem-type:expiry-date-in-past"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void errorResponseContentTypeIsProblemJson() throws Exception {
|
||||
String body =
|
||||
"""
|
||||
{
|
||||
"title": "",
|
||||
"dateTime": "2026-06-15T20:00:00+02:00",
|
||||
"expiryDate": "%s"
|
||||
}
|
||||
""".formatted(LocalDate.now().plusDays(30));
|
||||
|
||||
mockMvc.perform(post("/api/events")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(body))
|
||||
.andExpect(status().isBadRequest())
|
||||
.andExpect(content().contentTypeCompatibleWith("application/problem+json"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user