Skip to content
This repository has been archived by the owner on Dec 7, 2024. It is now read-only.

Commit

Permalink
feat(IntegrationTest): all integrations tests finished
Browse files Browse the repository at this point in the history
  • Loading branch information
alexZ7000 committed Oct 15, 2024
1 parent 5086135 commit 33ec935
Show file tree
Hide file tree
Showing 5 changed files with 484 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Category>> getAllCategories() {
final List<Category> categories = categoryService.findAll();
List<Category> categories = categoryService.findAll();
return ResponseEntity.ok(categories);
}

@GetMapping("/{id}")
public ResponseEntity<Category> getCategoryById(@PathVariable final UUID id) {
final Optional<Category> category = Optional.ofNullable(categoryService.findById(id));
return category.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
public ResponseEntity<Category> getCategoryById(@PathVariable UUID id) {
return ResponseEntity.ok(categoryService.findById(id));
}

@PostMapping
public ResponseEntity<Category> createCategory(@RequestBody final CategoryDTO categoryDTO) {
final Category savedCategory = categoryService.save(categoryDTO.toEntity());
return ResponseEntity.ok(savedCategory);
public ResponseEntity<Category> createCategory(@RequestBody CategoryDTO categoryDTO) {
Category category = categoryService.save(categoryDTO.toEntity());
return ResponseEntity.ok(category);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteCategory(@PathVariable final UUID id) {
public ResponseEntity<Void> deleteCategory(@PathVariable UUID id) {
categoryService.delete(id);
return ResponseEntity.noContent().build();
}

@PutMapping("/{id}")
public ResponseEntity<Category> updateCategory(@PathVariable final UUID id, @RequestBody final CategoryDTO categoryDTO) {
final Category updatedCategory = categoryService.update(id, categoryDTO.toEntity());
return ResponseEntity.ok(updatedCategory);
}
}
}
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());
}
}
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());
}
}
Loading

0 comments on commit 33ec935

Please sign in to comment.