diff --git a/src/main/java/com/example/comerce/core/controller/CategoryController.java b/src/main/java/com/example/comerce/core/controller/CategoryController.java index 8e4f447..e51caaa 100644 --- a/src/main/java/com/example/comerce/core/controller/CategoryController.java +++ b/src/main/java/com/example/comerce/core/controller/CategoryController.java @@ -8,47 +8,35 @@ import org.springframework.web.bind.annotation.*; import java.util.List; -import java.util.Optional; import java.util.UUID; @RestController -@RequestMapping("/api/category") -public final class CategoryController { - - private final CategoryService categoryService; +@RequestMapping("/api/categories") +public class CategoryController { @Autowired - public CategoryController(final CategoryService categoryService) { - this.categoryService = categoryService; - } + private CategoryService categoryService; @GetMapping public ResponseEntity> getAllCategories() { - final List categories = categoryService.findAll(); + List categories = categoryService.findAll(); return ResponseEntity.ok(categories); } @GetMapping("/{id}") - public ResponseEntity getCategoryById(@PathVariable final UUID id) { - final Optional category = Optional.ofNullable(categoryService.findById(id)); - return category.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); + public ResponseEntity getCategoryById(@PathVariable UUID id) { + return ResponseEntity.ok(categoryService.findById(id)); } @PostMapping - public ResponseEntity createCategory(@RequestBody final CategoryDTO categoryDTO) { - final Category savedCategory = categoryService.save(categoryDTO.toEntity()); - return ResponseEntity.ok(savedCategory); + public ResponseEntity createCategory(@RequestBody CategoryDTO categoryDTO) { + Category category = categoryService.save(categoryDTO.toEntity()); + return ResponseEntity.ok(category); } @DeleteMapping("/{id}") - public ResponseEntity deleteCategory(@PathVariable final UUID id) { + public ResponseEntity deleteCategory(@PathVariable UUID id) { categoryService.delete(id); return ResponseEntity.noContent().build(); } - - @PutMapping("/{id}") - public ResponseEntity updateCategory(@PathVariable final UUID id, @RequestBody final CategoryDTO categoryDTO) { - final Category updatedCategory = categoryService.update(id, categoryDTO.toEntity()); - return ResponseEntity.ok(updatedCategory); - } -} +} \ No newline at end of file diff --git a/src/test/java/com/example/comerce/integration/controller/CategoryControllerIntegrationTest.java b/src/test/java/com/example/comerce/integration/controller/CategoryControllerIntegrationTest.java new file mode 100644 index 0000000..af85c24 --- /dev/null +++ b/src/test/java/com/example/comerce/integration/controller/CategoryControllerIntegrationTest.java @@ -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()); + } +} \ No newline at end of file diff --git a/src/test/java/com/example/comerce/integration/controller/OrderControllerIntegrationTest.java b/src/test/java/com/example/comerce/integration/controller/OrderControllerIntegrationTest.java new file mode 100644 index 0000000..d305638 --- /dev/null +++ b/src/test/java/com/example/comerce/integration/controller/OrderControllerIntegrationTest.java @@ -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()); + } +} \ No newline at end of file diff --git a/src/test/java/com/example/comerce/integration/controller/ProductControllerIntegrationTest.java b/src/test/java/com/example/comerce/integration/controller/ProductControllerIntegrationTest.java new file mode 100644 index 0000000..3abfc7f --- /dev/null +++ b/src/test/java/com/example/comerce/integration/controller/ProductControllerIntegrationTest.java @@ -0,0 +1,128 @@ +package com.example.comerce.integration.controller; + +import com.example.comerce.core.dto.ProductDTO; +import com.example.comerce.core.entities.Product; +import com.example.comerce.core.services.ProductService; +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.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 ProductControllerIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private ProductService productService; + + 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 testGetAllProducts() throws Exception { + final Product product = new Product(); + product.setProduct_id(UUID.randomUUID()); + product.setName("Test Product"); + product.setPrice(100.0); + + when(productService.findAll()).thenReturn(Collections.singletonList(product)); + + mockMvc.perform(get("/api/products") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$[0].name").value("Test Product")) + .andExpect(jsonPath("$[0].price").value(100.0)); + } + + @Test + @WithMockUser(username = "test@example.com", roles = {"USER"}) + public void testGetProductById() throws Exception { + final UUID productId = UUID.randomUUID(); + final Product product = new Product(); + product.setProduct_id(productId); + product.setName("Test Product"); + product.setPrice(100.0); + + when(productService.findById(productId)).thenReturn(Optional.of(product)); + + mockMvc.perform(get("/api/products/{id}", productId) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.name").value("Test Product")) + .andExpect(jsonPath("$.price").value(100.0)); + } + + @Test + @WithMockUser(username = "test@example.com", roles = {"USER"}) + public void testCreateProduct() throws Exception { + final ProductDTO productDTO = new ProductDTO(); + productDTO.setName("Test Product"); + productDTO.setPrice(100.0); + + final Product product = productDTO.toEntity(); + product.setProduct_id(UUID.randomUUID()); + + when(productService.save(any(ProductDTO.class))).thenReturn(product); + + mockMvc.perform(post("/api/products") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(productDTO))) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.name").value("Test Product")) + .andExpect(jsonPath("$.price").value(100.0)); + } + + @Test + @WithMockUser(username = "test@example.com", roles = {"USER"}) + public void testDeleteProduct() throws Exception { + final UUID productId = UUID.randomUUID(); + + doNothing().when(productService).delete(productId); + + mockMvc.perform(delete("/api/products/{id}", productId) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNoContent()); + } +} \ No newline at end of file diff --git a/src/test/java/com/example/comerce/integration/controller/UserControllerIntegrationTest.java b/src/test/java/com/example/comerce/integration/controller/UserControllerIntegrationTest.java index e40ab02..66e7bb0 100644 --- a/src/test/java/com/example/comerce/integration/controller/UserControllerIntegrationTest.java +++ b/src/test/java/com/example/comerce/integration/controller/UserControllerIntegrationTest.java @@ -1,6 +1,8 @@ package com.example.comerce.integration.controller; -import com.example.comerce.core.controller.UserController; +import com.example.comerce.core.dto.AddressDTO; +import com.example.comerce.core.dto.OrderDTO; +import com.example.comerce.core.dto.UserDTO; import com.example.comerce.core.entities.LoginRequest; import com.example.comerce.core.entities.LoginResponse; import com.example.comerce.core.entities.User; @@ -9,17 +11,18 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; 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 static org.hamcrest.Matchers.containsString; -import java.util.UUID; + +import java.util.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; @@ -33,7 +36,7 @@ final class UserControllerIntegrationTest { @Autowired private MockMvc mockMvc; - @Mock + @MockBean private UserService userService; private ObjectMapper objectMapper; @@ -58,6 +61,7 @@ public void tearDown() { } @Test + @WithMockUser(username = "alessandro.lima@example.com") public void testLogin() throws Exception { final User user = new User(); user.setUser_id(UUID.fromString("81b9aef2-c202-4a44-9a75-d3a11afdb714")); @@ -81,4 +85,93 @@ public void testLogin() throws Exception { .andExpect(jsonPath("$.token").value(containsString("Bearer "))) .andExpect(jsonPath("$.user.email").value("alessandro.lima@example.com")); } + + @Test + @WithMockUser(username = "alessandro.lima@example.com") + public void testGetAllUsers() throws Exception { + final User user = new User(); + user.setUser_id(UUID.randomUUID()); + user.setEmail("test@example.com"); + user.setName("Test User"); + + when(userService.findAll()).thenReturn(Collections.singletonList(user)); + + mockMvc.perform(get("/api/users") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$[0].email").value("test@example.com")) + .andExpect(jsonPath("$[0].name").value("Test User")); + } + + @Test + @WithMockUser(username = "alessandro.lima@example.com") + public void testGetUserById() throws Exception { + final UUID userId = UUID.randomUUID(); + final User user = new User(); + user.setUser_id(userId); + user.setEmail("test@example.com"); + user.setName("Test User"); + + when(userService.findById(userId)).thenReturn(Optional.of(user)); + + mockMvc.perform(get("/api/users/{id}", userId) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.email").value("test@example.com")) + .andExpect(jsonPath("$.name").value("Test User")); + } + + @Test + @WithMockUser(username = "alessandro.lima@example.com") + public void testCreateUser() throws Exception { + final UserDTO userDTO = new UserDTO(); + userDTO.setName("Test User"); + userDTO.setEmail("test@example.com"); + userDTO.setCpf("12345678901"); + userDTO.setTelephone("12345678901"); + userDTO.setPassword("password"); + + final AddressDTO address = new AddressDTO(); + address.setPostal_code("12345678"); + address.setStreet("Rua Exemplo"); + address.setNumber("123"); + address.setNeighborhood("Bairro Exemplo"); + address.setComplement("Apto 456"); + address.setCity("Cidade Exemplo"); + address.setState("Estado Exemplo"); + userDTO.setAddress(address); + + final OrderDTO order = new OrderDTO(); + order.setDate(new Date()); + order.setDiscount(10.0); + order.setTotal_price(100.0); + userDTO.setOrders(List.of(order)); + + final User user = userDTO.toEntity(); + user.setUser_id(UUID.randomUUID()); + + when(userService.save(any(UserDTO.class))).thenReturn(user); + + mockMvc.perform(post("/api/users") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(userDTO))) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.email").value("test@example.com")) + .andExpect(jsonPath("$.name").value("Test User")); + } + + @Test + @WithMockUser(username = "alessandro.lima@example.com") + public void testDeleteUser() throws Exception { + final UUID userId = UUID.randomUUID(); + + doNothing().when(userService).delete(userId); + + mockMvc.perform(delete("/api/users/{id}", userId) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNoContent()); + } }