Skip to content

Commit

Permalink
Merge pull request #18 from nhkhai/main
Browse files Browse the repository at this point in the history
Added skeleton for test case setup and cleanup.
  • Loading branch information
nhkhai authored Mar 24, 2024
2 parents 3b8c020 + c3bf1b5 commit 1d19abc
Show file tree
Hide file tree
Showing 15 changed files with 319 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/main/java/sg/com/smartinventory/utility/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@ public DataLoader(CustomerRepository customerRepository, ProductRepository produ

@PostConstruct
public void loadData() {
// Clear all data.
clearTableData();

// Create fake data.
generateFakeData();
}

public void clearTableData() {
// Clear all data.
customerRepository.deleteAll();
productRepository.deleteAll();
reviewRepository.deleteAll();
}

public void generateFakeData() {
// Create fake data.
customerRepository.save(new Customer("John", "Doe", "USA", "123 Main St", 123456, 12345678,
"[email protected]"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.slf4j.Logger;
Expand All @@ -19,6 +23,27 @@ class SmartInventoryApplicationTests {
// trace(), debug(), info(), warn(), error().
private static final Logger test_logger = LoggerFactory.getLogger(SmartInventoryApplicationTests.class);

// Test Setup and Teardown configuration.
@BeforeAll
static void initAll() {

}

@AfterAll
static void teardownAll() {

}

@BeforeEach
void init() {

}

@AfterEach
void teardown() {

}

@Test
void contextLoads() {
test_logger.info("Starting test: " + getCurrentMethodName() + ". ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -42,6 +44,16 @@ public class CustomerControllerTest {
private static final Logger test_logger = LoggerFactory.getLogger(CustomerControllerTest.class);

// Test Setup and Teardown configuration.
@BeforeAll
static void initAll() {

}

@AfterAll
static void teardownAll() {

}

@BeforeEach
void init() {

Expand All @@ -57,7 +69,7 @@ void teardown() {
public void createCustomerTest() throws Exception {
test_logger.info("Starting test: " + getCurrentMethodName() + ". ");

// Step 1: Create a Customer object
// Step 1: Create a Customer object.
Customer newCustomer = Customer.builder().firstName("Jackie").lastName("Chan").country("Hong Kong")
.address("123 HK St").postalCode(654321).phoneNumber(87654321)
.email("[email protected]").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -42,6 +44,16 @@ public class ProductControllerTest {
private static final Logger test_logger = LoggerFactory.getLogger(ProductControllerTest.class);

// Test Setup and Teardown configuration.
@BeforeAll
static void initAll() {

}

@AfterAll
static void teardownAll() {

}

@BeforeEach
void init() {

Expand All @@ -57,7 +69,7 @@ void teardown() {
public void createProductTest() throws Exception {
test_logger.info("Starting test: " + getCurrentMethodName() + ". ");

// Step 1: Create a Product object
// Step 1: Create a Product object.
Product newProduct = Product.builder().category("Electronics").name("Smartphone")
.description("High-end smartphone with advanced features. ")
.price(999.99).stockQuantity(100).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -42,6 +44,16 @@ public class ReviewControllerTest {
private static final Logger test_logger = LoggerFactory.getLogger(ReviewControllerTest.class);

// Test Setup and Teardown configuration.
@BeforeAll
static void initAll() {

}

@AfterAll
static void teardownAll() {

}

@BeforeEach
void init() {

Expand All @@ -62,7 +74,7 @@ void teardown() {
public void createReviewTest() throws Exception {
test_logger.info("Starting test: " + getCurrentMethodName() + ". ");

// Step 1: Create a Review object
// Step 1: Create a Review object.
Review newReview = Review.builder().category("Electronics")
.reviewContent("Great smartphone with excellent features. ").rating(5).customerId(1)
.productId(2).build();
Expand Down Expand Up @@ -93,4 +105,124 @@ public void createReviewTest() throws Exception {
// test_logger.error("Ending test: createReviewTest. ");
test_logger.info("Ending test: " + getCurrentMethodName() + ". ");
}

@DisplayName("Get All Reviews")
@Test
public void getAllReviewsTest() throws Exception {
test_logger.info("Starting test: " + getCurrentMethodName() + ". ");

// Step 1: Create a Review objects.
Review newReview = Review.builder().category("Electronics")
.reviewContent("Great smartphone with excellent features. ").rating(5).customerId(1)
.productId(2).build();

// Step 2: Convert the Java object to JSON using ObjectMapper.
String newReviewAsJSON = objectMapper.writeValueAsString(newReview);

// Step 3: Build the request.
RequestBuilder request = MockMvcRequestBuilders.post("/reviews")
.contentType(MediaType.APPLICATION_JSON)
.content(newReviewAsJSON);

// Step 4: Perform the request and get the response and assert.
mockMvc.perform(request)
// MvcResult Result = mockMvc.perform(request)
// .andDo(print -> test_logger.error("Starting Request: createReviewTest. "))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.category").value("Electronics"))
.andExpect(jsonPath("$.rating").value(5));

test_logger.info("Ending test: " + getCurrentMethodName() + ". ");
}

@DisplayName("Get One Review")
@Test
public void getOneReviewTest() throws Exception {
test_logger.info("Starting test: " + getCurrentMethodName() + ". ");

// Step 1: Create a Review object.
Review newReview = Review.builder().category("Electronics")
.reviewContent("Great smartphone with excellent features. ").rating(5).customerId(1)
.productId(2).build();

// Step 2: Convert the Java object to JSON using ObjectMapper.
String newReviewAsJSON = objectMapper.writeValueAsString(newReview);

// Step 3: Build the request.
RequestBuilder request = MockMvcRequestBuilders.post("/reviews")
.contentType(MediaType.APPLICATION_JSON)
.content(newReviewAsJSON);

// Step 4: Perform the request and get the response and assert.
mockMvc.perform(request)
// MvcResult Result = mockMvc.perform(request)
// .andDo(print -> test_logger.error("Starting Request: createReviewTest. "))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.category").value("Electronics"))
.andExpect(jsonPath("$.rating").value(5));

test_logger.info("Ending test: " + getCurrentMethodName() + ". ");
}

@DisplayName("Update Review")
@Test
public void updateReviewTest() throws Exception {
test_logger.info("Starting test: " + getCurrentMethodName() + ". ");

// Step 1: Create a Review object.
Review newReview = Review.builder().category("Electronics")
.reviewContent("Great smartphone with excellent features. ").rating(5).customerId(1)
.productId(2).build();

// Step 2: Convert the Java object to JSON using ObjectMapper.
String newReviewAsJSON = objectMapper.writeValueAsString(newReview);

// Step 3: Build the request.
RequestBuilder request = MockMvcRequestBuilders.post("/reviews")
.contentType(MediaType.APPLICATION_JSON)
.content(newReviewAsJSON);

// Step 4: Perform the request and get the response and assert.
mockMvc.perform(request)
// MvcResult Result = mockMvc.perform(request)
// .andDo(print -> test_logger.error("Starting Request: createReviewTest. "))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.category").value("Electronics"))
.andExpect(jsonPath("$.rating").value(5));

test_logger.info("Ending test: " + getCurrentMethodName() + ". ");
}

@DisplayName("Delete Review")
@Test
public void deleteReviewTest() throws Exception {
test_logger.info("Starting test: " + getCurrentMethodName() + ". ");

// Step 1: Create a Review object.
Review newReview = Review.builder().category("Electronics")
.reviewContent("Great smartphone with excellent features. ").rating(5).customerId(1)
.productId(2).build();

// Step 2: Convert the Java object to JSON using ObjectMapper.
String newReviewAsJSON = objectMapper.writeValueAsString(newReview);

// Step 3: Build the request.
RequestBuilder request = MockMvcRequestBuilders.post("/reviews")
.contentType(MediaType.APPLICATION_JSON)
.content(newReviewAsJSON);

// Step 4: Perform the request and get the response and assert.
mockMvc.perform(request)
// MvcResult Result = mockMvc.perform(request)
// .andDo(print -> test_logger.error("Starting Request: createReviewTest. "))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.category").value("Electronics"))
.andExpect(jsonPath("$.rating").value(5));

test_logger.info("Ending test: " + getCurrentMethodName() + ". ");
}
}
12 changes: 12 additions & 0 deletions src/test/java/sg/com/smartinventory/entities/CustomerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -32,6 +34,16 @@ public class CustomerTest {
private static final Logger test_logger = LoggerFactory.getLogger(CustomerTest.class);

// Test Setup and Teardown configuration.
@BeforeAll
static void initAll() {

}

@AfterAll
static void teardownAll() {

}

@BeforeEach
void init() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -23,6 +25,16 @@ public class ErrorResponseTest {
private static final Logger test_logger = LoggerFactory.getLogger(ErrorResponseTest.class);

// Test Setup and Teardown configuration.
@BeforeAll
static void initAll() {

}

@AfterAll
static void teardownAll() {

}

@BeforeEach
void init() {

Expand Down
12 changes: 12 additions & 0 deletions src/test/java/sg/com/smartinventory/entities/ProductTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -32,6 +34,16 @@ public class ProductTest {
private static final Logger test_logger = LoggerFactory.getLogger(ProductTest.class);

// Test Setup and Teardown configuration.
@BeforeAll
static void initAll() {

}

@AfterAll
static void teardownAll() {

}

@BeforeEach
void init() {

Expand Down
Loading

0 comments on commit 1d19abc

Please sign in to comment.