-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
it-test - add test case for shopper api
- Loading branch information
1 parent
9e98ae3
commit 0f9cf88
Showing
3 changed files
with
83 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 65 additions & 5 deletions
70
kbazaar/src/test/java/com/kampus/kbazaar/KBazaarApplicationIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |