test: Add error handling tests for PriceControllerAdapter
This commit is contained in:
parent
0258f2b5d1
commit
11b9f698dd
@ -1,12 +1,16 @@
|
|||||||
package com.techivw.webprice.infrastructure.in.controllers.adapters;
|
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.application.ports.in.PriceServicePort;
|
||||||
import com.techivw.webprice.domain.Price;
|
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.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.Arguments;
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
import org.junit.jupiter.params.provider.MethodSource;
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
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.ActiveProfiles;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||||
@ -23,6 +27,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||||||
|
|
||||||
@WebMvcTest
|
@WebMvcTest
|
||||||
@ContextConfiguration(classes = PriceControllerAdapter.class)
|
@ContextConfiguration(classes = PriceControllerAdapter.class)
|
||||||
|
@Import(AdapterExceptionHandler.class)
|
||||||
@ActiveProfiles("test")
|
@ActiveProfiles("test")
|
||||||
public class PriceControllerAdapterTest {
|
public class PriceControllerAdapterTest {
|
||||||
|
|
||||||
@ -35,8 +40,71 @@ public class PriceControllerAdapterTest {
|
|||||||
private static final Long PRODUCT_ID = 35455L;
|
private static final Long PRODUCT_ID = 35455L;
|
||||||
private static final Long BRAND_ID = 1L;
|
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
|
@ParameterizedTest
|
||||||
@MethodSource("testCases")
|
@MethodSource("successfulTestCases")
|
||||||
void testGetPrice(String testName, String dateTime, double expectedPrice) throws Exception {
|
void testGetPrice(String testName, String dateTime, double expectedPrice) throws Exception {
|
||||||
LocalDateTime date = LocalDateTime.parse(dateTime);
|
LocalDateTime date = LocalDateTime.parse(dateTime);
|
||||||
|
|
||||||
@ -52,7 +120,7 @@ public class PriceControllerAdapterTest {
|
|||||||
.andExpect(jsonPath("$.price").value(expectedPrice));
|
.andExpect(jsonPath("$.price").value(expectedPrice));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Stream<Arguments> testCases() {
|
private static Stream<Arguments> successfulTestCases() {
|
||||||
return Stream.of(
|
return Stream.of(
|
||||||
Arguments.of("Test 1", "2020-06-14T10:00:00", 35.50),
|
Arguments.of("Test 1", "2020-06-14T10:00:00", 35.50),
|
||||||
Arguments.of("Test 2", "2020-06-14T16:00:00", 25.45),
|
Arguments.of("Test 2", "2020-06-14T16:00:00", 25.45),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user