diff --git a/infrastructure/in/rest-api/pom.xml b/infrastructure/in/rest-api/pom.xml index de49e08..9ad6546 100644 --- a/infrastructure/in/rest-api/pom.xml +++ b/infrastructure/in/rest-api/pom.xml @@ -16,6 +16,12 @@ rest-api + + org.springframework.boot + spring-boot-starter-test + test + + com.techivw domain @@ -28,4 +34,16 @@ + + + + org.apache.maven.plugins + maven-surefire-plugin + + -javaagent:${settings.localRepository}/net/bytebuddy/byte-buddy-agent/1.15.11/byte-buddy-agent-1.15.11.jar + + + + + 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 new file mode 100644 index 0000000..d2b6079 --- /dev/null +++ b/infrastructure/in/rest-api/src/test/java/com/techivw/webprice/infrastructure/in/controllers/adapters/PriceControllerAdapterTest.java @@ -0,0 +1,77 @@ +package com.techivw.webprice.infrastructure.in.controllers.adapters; + +import com.techivw.webprice.application.ports.in.PriceServicePort; +import com.techivw.webprice.domain.Price; +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.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.bean.override.mockito.MockitoBean; +import org.springframework.test.web.servlet.MockMvc; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.stream.Stream; + +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@WebMvcTest +@ContextConfiguration(classes = PriceControllerAdapter.class) +@ActiveProfiles("test") +public class PriceControllerAdapterTest { + + @Autowired + private MockMvc mockMvc; + + @MockitoBean + private PriceServicePort priceServicePort; + + private static final Long PRODUCT_ID = 35455L; + private static final Long BRAND_ID = 1L; + + @ParameterizedTest + @MethodSource("testCases") + void testGetPrice(String testName, String dateTime, double expectedPrice) throws Exception { + LocalDateTime date = LocalDateTime.parse(dateTime); + + Price mockPrice = createPrice(date.minusHours(1), date.plusHours(1), 1, expectedPrice, 1); + when(priceServicePort.getPriceWithHighestPriorityByDateTimeAndProductIdAndBrandId(date, PRODUCT_ID, BRAND_ID)) + .thenReturn(mockPrice); + + mockMvc.perform(get("/price") + .param("dateTime", dateTime) + .param("productId", String.valueOf(PRODUCT_ID)) + .param("brandId", String.valueOf(BRAND_ID))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.price").value(expectedPrice)); + } + + private static Stream testCases() { + 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), + Arguments.of("Test 3", "2020-06-14T21:00:00", 35.50), + Arguments.of("Test 4", "2020-06-15T10:00:00", 30.50), + Arguments.of("Test 5", "2020-06-16T21:00:00", 38.95) + ); + } + + private Price createPrice(LocalDateTime startDate, LocalDateTime endDate, long priceList, double price, int priority) { + return Price.builder() + .brandId(BRAND_ID) + .productId(PRODUCT_ID) + .startDate(startDate) + .endDate(endDate) + .priceList(priceList) + .price(BigDecimal.valueOf(price)) + .priority(priority) + .currency("EUR") + .build(); + } +} \ No newline at end of file