From 11b9f698dd7fb87ba268b42967c809a04c056663 Mon Sep 17 00:00:00 2001 From: bedroomghost Date: Mon, 21 Apr 2025 20:13:44 +0200 Subject: [PATCH] test: Add error handling tests for PriceControllerAdapter --- .../adapters/PriceControllerAdapterTest.java | 72 ++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/infrastructure/in/rest-api/src/test/java/com/techivw/webprice/infrastructure/in/controllers/adapters/PriceControllerAdapterTest.java b/infrastructure/in/rest-api/src/test/java/com/techivw/webprice/infrastructure/in/controllers/adapters/PriceControllerAdapterTest.java index d2b6079..64acfb7 100644 --- a/infrastructure/in/rest-api/src/test/java/com/techivw/webprice/infrastructure/in/controllers/adapters/PriceControllerAdapterTest.java +++ b/infrastructure/in/rest-api/src/test/java/com/techivw/webprice/infrastructure/in/controllers/adapters/PriceControllerAdapterTest.java @@ -1,12 +1,16 @@ package com.techivw.webprice.infrastructure.in.controllers.adapters; +import com.techivw.webprice.application.exceptions.NotFoundException; import com.techivw.webprice.application.ports.in.PriceServicePort; import com.techivw.webprice.domain.Price; +import com.techivw.webprice.infrastructure.in.controllers.handlers.AdapterExceptionHandler; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.context.annotation.Import; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.bean.override.mockito.MockitoBean; @@ -23,6 +27,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @WebMvcTest @ContextConfiguration(classes = PriceControllerAdapter.class) +@Import(AdapterExceptionHandler.class) @ActiveProfiles("test") public class PriceControllerAdapterTest { @@ -35,8 +40,71 @@ public class PriceControllerAdapterTest { private static final Long PRODUCT_ID = 35455L; private static final Long BRAND_ID = 1L; + @Test + void testNotFound() throws Exception { + LocalDateTime date = LocalDateTime.parse("2020-06-14T10:00:00"); + + when(priceServicePort.getPriceWithHighestPriorityByDateTimeAndProductIdAndBrandId(date, PRODUCT_ID, BRAND_ID)) + .thenThrow(new NotFoundException("")); + + mockMvc.perform(get("/price") + .param("dateTime", date.toString()) + .param("productId", String.valueOf(PRODUCT_ID)) + .param("brandId", String.valueOf(BRAND_ID))) + .andExpect(status().isNotFound()) + .andExpect(jsonPath("$.status").value(404)) + .andExpect(jsonPath("$.error").value("Not Found")); + } + + @Test + void testMissingParameter() throws Exception { + mockMvc.perform(get("/price") + .param("productId", String.valueOf(PRODUCT_ID)) + .param("brandId", String.valueOf(BRAND_ID))) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.status").value(400)) + .andExpect(jsonPath("$.error").value("Bad Request")) + .andExpect(jsonPath("$.message").value("Required parameter 'dateTime' is missing")); + } + + @Test + void testInvalidDateFormat() throws Exception { + mockMvc.perform(get("/price") + .param("dateTime", "2020-06T17:00") + .param("productId", String.valueOf(PRODUCT_ID)) + .param("brandId", String.valueOf(BRAND_ID))) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.status").value(400)) + .andExpect(jsonPath("$.error").value("Bad Request")) + .andExpect(jsonPath("$.message").value("Parameter 'dateTime' has invalid format.")); + } + + @Test + void testInvalidProductIdFormat() throws Exception { + mockMvc.perform(get("/price") + .param("dateTime", "2020-06-14T10:00:00") + .param("productId", "testNaN") + .param("brandId", String.valueOf(BRAND_ID))) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.status").value(400)) + .andExpect(jsonPath("$.error").value("Bad Request")) + .andExpect(jsonPath("$.message").value("Parameter 'productId' has invalid format.")); + } + + @Test + void testInvalidBrandIdFormat() throws Exception { + mockMvc.perform(get("/price") + .param("dateTime", "2020-06-14T10:00:00") + .param("productId", String.valueOf(PRODUCT_ID)) + .param("brandId", "not-a-number")) // Invalid brandId format + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.status").value(400)) + .andExpect(jsonPath("$.error").value("Bad Request")) + .andExpect(jsonPath("$.message").value("Parameter 'brandId' has invalid format.")); + } + @ParameterizedTest - @MethodSource("testCases") + @MethodSource("successfulTestCases") void testGetPrice(String testName, String dateTime, double expectedPrice) throws Exception { LocalDateTime date = LocalDateTime.parse(dateTime); @@ -52,7 +120,7 @@ public class PriceControllerAdapterTest { .andExpect(jsonPath("$.price").value(expectedPrice)); } - private static Stream testCases() { + private static Stream successfulTestCases() { return Stream.of( Arguments.of("Test 1", "2020-06-14T10:00:00", 35.50), Arguments.of("Test 2", "2020-06-14T16:00:00", 25.45),