Skip to content

Commit

Permalink
#1032 [Test] Increase coverage to over 80%
Browse files Browse the repository at this point in the history
  • Loading branch information
nashtech-tuannguyenhuu1 committed Sep 18, 2024
1 parent aacf57a commit d10c700
Show file tree
Hide file tree
Showing 29 changed files with 3,822 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public InputStream getFile(String filePath) {
}
}

private Path buildFilePath(String filename) throws IOException {
private Path buildFilePath(String filename) {
// Validate the filename
if (filename.contains("..") || filename.contains("/") || filename.contains("\\")) {
throw new IllegalArgumentException("Invalid filename");
Expand Down
15 changes: 13 additions & 2 deletions media/src/test/java/com/yas/media/FileSystemRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.when;

import com.yas.media.config.FilesystemConfig;
import com.yas.media.repository.FileSystemRepository;
Expand All @@ -12,7 +12,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -68,6 +67,18 @@ void testPersistFile_whenDirectoryNotExist_thenThrowsException() {
assertThrows(IllegalStateException.class, () -> fileSystemRepository.persistFile(filename, content));
}

@Test
void testPersistFile_filePathNotContainsDirectory() {

String filename = "test-file.png";
byte[] content = "test-content".getBytes();

File directory = new File(TEST_URL);
directory.mkdirs();
when(filesystemConfig.getDirectory()).thenReturn(TEST_URL);
assertThrows(IllegalArgumentException.class, () -> fileSystemRepository.persistFile(filename, content));
}

@Test
void testGetFile_whenDirectIsExist_thenReturnFile() throws IOException {
String filename = "test-file.png";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package com.yas.order.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.yas.order.OrderApplication;
import com.yas.order.service.CheckoutService;
import com.yas.order.viewmodel.checkout.CheckoutItemPostVm;
import com.yas.order.viewmodel.checkout.CheckoutItemVm;
import com.yas.order.viewmodel.checkout.CheckoutPostVm;
import com.yas.order.viewmodel.checkout.CheckoutStatusPutVm;
import com.yas.order.viewmodel.checkout.CheckoutVm;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
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.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = CheckoutController.class)
@ContextConfiguration(classes = OrderApplication.class)
@AutoConfigureMockMvc(addFilters = false)
class CheckoutControllerTest {

@MockBean
private CheckoutService checkoutService;

@Autowired
private MockMvc mockMvc;

private ObjectWriter objectWriter;

@BeforeEach
void setUp() {
objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter();
}

@Test
void testCreateCheckout_whenRequestIsValid_thenReturnCheckoutVm() throws Exception {

CheckoutVm response = getCheckoutVm();
when(checkoutService.createCheckout(any(CheckoutPostVm.class))).thenReturn(response);

List<CheckoutItemPostVm> items = getCheckoutItemPostVms();
CheckoutPostVm request = new CheckoutPostVm(
"customer@example.com",
"Please deliver before noon.",
"SUMMER2024",
items
);

mockMvc.perform(post("/storefront/checkouts")
.contentType(MediaType.APPLICATION_JSON)
.content(objectWriter.writeValueAsString(request)))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.content().json(objectWriter.writeValueAsString(response)));

}

@Test
void testUpdateCheckoutStatus_whenRequestIsValid_thenReturnLong() throws Exception {

CheckoutStatusPutVm request = new CheckoutStatusPutVm("1", "2");
Long response = 123L;
when(checkoutService.updateCheckoutStatus(any(CheckoutStatusPutVm.class))).thenReturn(response);

mockMvc.perform(put("/storefront/checkouts/status")
.contentType(MediaType.APPLICATION_JSON)
.content(objectWriter.writeValueAsString(request)))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.content().json(objectWriter.writeValueAsString(response)));
}

@Test
void testGetOrderWithItemsById_whenRequestIsValid_thenReturnCheckoutVm() throws Exception {

String id = "123";
CheckoutVm response = getCheckoutVm();
when(checkoutService.getCheckoutPendingStateWithItemsById(id)).thenReturn(response);

mockMvc.perform(get("/storefront/checkouts/{id}", id)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.content().json(objectWriter.writeValueAsString(response)));
}


private static @NotNull List<CheckoutItemPostVm> getCheckoutItemPostVms() {
CheckoutItemPostVm item1 = new CheckoutItemPostVm(
101L,
"Product One",
3,
new BigDecimal("29.99"),
"First item note",
new BigDecimal("5.00"),
new BigDecimal("2.50"),
new BigDecimal("8.5")
);

CheckoutItemPostVm item2 = new CheckoutItemPostVm(
102L,
"Product Two",
1,
new BigDecimal("49.99"),
"Second item note",
new BigDecimal("10.00"),
new BigDecimal("5.00"),
new BigDecimal("10.0")
);

return List.of(item1, item2);
}

private CheckoutVm getCheckoutVm() {
CheckoutItemVm item1 = CheckoutItemVm.builder()
.id(1L)
.productId(101L)
.productName("Product 1")
.quantity(2)
.productPrice(new BigDecimal("19.99"))
.note("First item note")
.discountAmount(new BigDecimal("2.00"))
.taxAmount(new BigDecimal("1.50"))
.taxPercent(new BigDecimal("5.0"))
.checkoutId("checkout123")
.build();

CheckoutItemVm item2 = CheckoutItemVm.builder()
.id(2L)
.productId(102L)
.productName("Product 2")
.quantity(1)
.productPrice(new BigDecimal("9.99"))
.note("Second item note")
.discountAmount(new BigDecimal("1.00"))
.taxAmount(new BigDecimal("0.75"))
.taxPercent(new BigDecimal("5.0"))
.checkoutId("checkout123")
.build();

Set<CheckoutItemVm> checkoutItemVms = new HashSet<>();
checkoutItemVms.add(item1);
checkoutItemVms.add(item2);

return new CheckoutVm(
"checkout123",
"user@example.com",
"Please deliver after 5 PM",
"DISCOUNT20",
checkoutItemVms
);
}
}
Loading

0 comments on commit d10c700

Please sign in to comment.