This repository has been archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(IntegrationTest): all integrations tests finished
- Loading branch information
Showing
5 changed files
with
484 additions
and
28 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
121 changes: 121 additions & 0 deletions
121
...st/java/com/example/comerce/integration/controller/CategoryControllerIntegrationTest.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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.example.comerce.integration.controller; | ||
|
||
import com.example.comerce.core.dto.CategoryDTO; | ||
import com.example.comerce.core.entities.Category; | ||
import com.example.comerce.core.services.CategoryService; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockitoAnnotations; | ||
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.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.util.Collections; | ||
import java.util.UUID; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
final class CategoryControllerIntegrationTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private CategoryService categoryService; | ||
|
||
private ObjectMapper objectMapper; | ||
|
||
private AutoCloseable closeable; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
closeable = MockitoAnnotations.openMocks(this); | ||
objectMapper = new ObjectMapper(); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
if (closeable != null) { | ||
try { | ||
closeable.close(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Error closing resources", e); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "alessandro.lima@example.com", roles = {"USER"}) | ||
public void testGetAllCategories() throws Exception { | ||
final Category category = new Category(); | ||
category.setCategory_id(UUID.randomUUID()); | ||
category.setDescription("Test Category"); | ||
|
||
when(categoryService.findAll()).thenReturn(Collections.singletonList(category)); | ||
|
||
mockMvc.perform(get("/api/categories") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(jsonPath("$[0].description").value("Test Category")); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "alessandro.lima@example.com", roles = {"USER"}) | ||
public void testGetCategoryById() throws Exception { | ||
final UUID categoryId = UUID.randomUUID(); | ||
final Category category = new Category(); | ||
category.setCategory_id(categoryId); | ||
category.setDescription("Test Category"); | ||
|
||
when(categoryService.findById(categoryId)).thenReturn(category); | ||
|
||
mockMvc.perform(get("/api/categories/{id}", categoryId) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(jsonPath("$.description").value("Test Category")); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "alessandro.lima@example.com", roles = {"USER"}) | ||
public void testCreateCategory() throws Exception { | ||
final CategoryDTO categoryDTO = new CategoryDTO(); | ||
categoryDTO.setDescription("Test Category"); | ||
|
||
final Category category = categoryDTO.toEntity(); | ||
category.setCategory_id(UUID.randomUUID()); | ||
|
||
when(categoryService.save(any(Category.class))).thenReturn(category); | ||
|
||
mockMvc.perform(post("/api/categories") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(categoryDTO))) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(jsonPath("$.description").value("Test Category")); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "alessandro.lima@example.com", roles = {"USER"}) | ||
public void testDeleteCategory() throws Exception { | ||
final UUID categoryId = UUID.randomUUID(); | ||
|
||
doNothing().when(categoryService).delete(categoryId); | ||
|
||
mockMvc.perform(delete("/api/categories/{id}", categoryId) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isNoContent()); | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
src/test/java/com/example/comerce/integration/controller/OrderControllerIntegrationTest.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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package com.example.comerce.integration.controller; | ||
|
||
import com.example.comerce.core.dto.OrderDTO; | ||
import com.example.comerce.core.entities.Order; | ||
import com.example.comerce.core.services.OrderService; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockitoAnnotations; | ||
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.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.util.Collections; | ||
import java.util.Date; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
final class OrderControllerIntegrationTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private OrderService orderService; | ||
|
||
private ObjectMapper objectMapper; | ||
|
||
private AutoCloseable closeable; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
closeable = MockitoAnnotations.openMocks(this); | ||
objectMapper = new ObjectMapper(); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
if (closeable != null) { | ||
try { | ||
closeable.close(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Error closing resources", e); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "test@example.com", roles = {"USER"}) | ||
public void testGetAllOrders() throws Exception { | ||
final Order order = new Order(); | ||
order.setOrder_id(UUID.randomUUID()); | ||
order.setDate(new Date()); | ||
order.setTotal_price(100.0); | ||
|
||
when(orderService.findAll()).thenReturn(Collections.singletonList(order)); | ||
|
||
mockMvc.perform(get("/api/orders") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(jsonPath("$[0].total_price").value(100.0)); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "test@example.com", roles = {"USER"}) | ||
public void testGetOrderById() throws Exception { | ||
final UUID orderId = UUID.randomUUID(); | ||
final Order order = new Order(); | ||
order.setOrder_id(orderId); | ||
order.setDate(new Date()); | ||
order.setTotal_price(100.0); | ||
|
||
when(orderService.findById(orderId)).thenReturn(Optional.of(order)); | ||
|
||
mockMvc.perform(get("/api/orders/{id}", orderId) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(jsonPath("$.total_price").value(100.0)); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "test@example.com", roles = {"USER"}) | ||
public void testCreateOrder() throws Exception { | ||
final OrderDTO orderDTO = new OrderDTO(); | ||
orderDTO.setDate(new Date()); | ||
orderDTO.setTotal_price(100.0); | ||
|
||
final Order order = orderDTO.toEntity(); | ||
order.setOrder_id(UUID.randomUUID()); | ||
|
||
when(orderService.save(any(OrderDTO.class))).thenReturn(order); | ||
|
||
mockMvc.perform(post("/api/orders") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(orderDTO))) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(jsonPath("$.total_price").value(100.0)); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "test@example.com", roles = {"USER"}) | ||
public void testDeleteOrder() throws Exception { | ||
final UUID orderId = UUID.randomUUID(); | ||
|
||
doNothing().when(orderService).delete(orderId); | ||
|
||
mockMvc.perform(delete("/api/orders/{id}", orderId) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isNoContent()); | ||
} | ||
} |
Oops, something went wrong.