diff --git a/kbazaar/build.gradle b/kbazaar/build.gradle index 1669c59..5711753 100644 --- a/kbazaar/build.gradle +++ b/kbazaar/build.gradle @@ -56,6 +56,7 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' // testImplementation 'org.springframework.security:spring-security-test' testImplementation 'com.tngtech.archunit:archunit-junit5:1.2.1' + testImplementation 'org.testcontainers:postgresql:1.15.3' } test { diff --git a/kbazaar/src/test/java/com/kampus/kbazaar/KBazaarApplicationIT.java b/kbazaar/src/test/java/com/kampus/kbazaar/KBazaarApplicationIT.java index c71c120..c83d010 100644 --- a/kbazaar/src/test/java/com/kampus/kbazaar/KBazaarApplicationIT.java +++ b/kbazaar/src/test/java/com/kampus/kbazaar/KBazaarApplicationIT.java @@ -1,20 +1,80 @@ package com.kampus.kbazaar; import static org.junit.jupiter.api.Assertions.assertEquals; +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; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +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.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.testcontainers.containers.PostgreSQLContainer; @SpringBootTest +@ExtendWith(SpringExtension.class) +@AutoConfigureMockMvc @Tag("integration-test") -class KBazaarApplicationIT { +@TestPropertySource(locations = "classpath:application-ittest.properties") +class KBazaarApplicationTests { + + @Autowired + private MockMvc mockMvc; + + private final String jwtToken = "eyJhbGciOiJIUzI1NiJ9.eyJhdXRob3JpdGllcyI6WyJST0xFX1NIT1BQRVIiXSwic3ViIjoic2hvcHBlciIsImlhdCI6MTcxMTA4MTk1OSwiZXhwIjoxNzQyNjE3OTU5fQ.hwpc_6CL_ZENHurOaZEYg3tz9FVBgOYa7ILF063stxs"; + + static PostgreSQLContainer postgresContainer = new PostgreSQLContainer<>("postgres:12-alpine"); + + @DynamicPropertySource + static void postgresqlProperties(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", postgresContainer::getJdbcUrl); + registry.add("spring.datasource.username", postgresContainer::getUsername); + registry.add("spring.datasource.password", postgresContainer::getPassword); + registry.add("security.jwt.secret", () -> "1ukPr@a1M@1T@1D3rN@NgJoNMaHenKubT@"); + } + + @BeforeAll + static void startContainer() { + postgresContainer.start(); + } + + @AfterAll + static void stopContainer() { + postgresContainer.stop(); + } + + @Test + void getShopper_shouldReturnShopperList() throws Exception { + MvcResult mvcResult = mockMvc.perform( + get("/api/v1/shoppers").header("Authorization", "Bearer "+ jwtToken) + ) + .andExpect(status().isOk()) + .andExpect(jsonPath("$").isArray()) + .andExpect(jsonPath("$.length()").value(Matchers.greaterThan(0))) + .andExpect(jsonPath("$[0].username").value("TechNinja")) + .andReturn(); + } @Test - void contextLoads() { - int want = 1; - int got = 1; + void getShopperByName_shouldReturnShopper() throws Exception { + String username = "TechNinja"; - assertEquals(want, got); + MvcResult mvcResult = mockMvc.perform( + get("/api/v1/shoppers/" + username).header("Authorization", "Bearer "+ jwtToken) + ) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.username").value(username)) + .andReturn(); } } diff --git a/kbazaar/src/test/resources/application-ittest.properties b/kbazaar/src/test/resources/application-ittest.properties new file mode 100644 index 0000000..a98e9eb --- /dev/null +++ b/kbazaar/src/test/resources/application-ittest.properties @@ -0,0 +1,17 @@ + +spring.jpa.generate-ddl=true +spring.jpa.hibernate.ddl-auto=update +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true + +spring.datasource.url= +spring.datasource.username= +spring.datasource.password= +spring.datasource.maximumPoolSize=1 + +spring.sql.init.mode=always +spring.sql.init.schema-locations=classpath:sql/schema/*.sql +spring.sql.init.data-locations=classpath:sql/data/*.sql + +# swagger +springdoc.swagger-ui.enabled=true