Skip to content

Commit

Permalink
it-test - add test case for shopper api
Browse files Browse the repository at this point in the history
  • Loading branch information
chinnawatsut committed Mar 22, 2024
1 parent 9e98ae3 commit 0f9cf88
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
1 change: 1 addition & 0 deletions kbazaar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
70 changes: 65 additions & 5 deletions kbazaar/src/test/java/com/kampus/kbazaar/KBazaarApplicationIT.java
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();
}
}
17 changes: 17 additions & 0 deletions kbazaar/src/test/resources/application-ittest.properties
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

0 comments on commit 0f9cf88

Please sign in to comment.