Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ppi 12 implementar pedido repository #12

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.produtopedidoitens.api.adapters.persistence.repositories;

import com.produtopedidoitens.api.application.domain.entities.OrderEntity;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.UUID;

public interface OrderRepository extends JpaRepository<OrderEntity, UUID> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import com.produtopedidoitens.api.adapters.web.responses.ErrorResponseDTO;
import com.produtopedidoitens.api.application.exceptions.BadRequestException;
import com.produtopedidoitens.api.application.exceptions.ProductNotFoundException;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.stream.Collectors;

@ControllerAdvice
public class ProductExceptionHandler {

Expand All @@ -20,4 +24,14 @@ public ResponseEntity<ErrorResponseDTO> handleBadRequestException(BadRequestExce
public ResponseEntity<ErrorResponseDTO> handleProductNotFoundException(ProductNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponseDTO(e.getMessage()));
}

@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<ErrorResponseDTO> handleConstraintViolationException(ConstraintViolationException e) {
String messages = e.getConstraintViolations()
.stream()
.map(ConstraintViolation::getMessage)
.collect(Collectors.joining(", "));
return ResponseEntity.badRequest().body(new ErrorResponseDTO(messages));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class OrderEntity {
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
private List<OrderItemEntity> items = new ArrayList<>();

@Column(name = "grossstotal")
private BigDecimal grosssTotal;
@Column(name = "grosstotal")
private BigDecimal grossTotal;

@Positive(message = MessagesConstants.ORDER_DISCOUNT_POSITIVE)
@Column(name = "discount")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.produtopedidoitens.api.adapters.persistence.repositories;

import com.produtopedidoitens.api.application.domain.entities.OrderEntity;
import com.produtopedidoitens.api.application.domain.enums.EnumOrderStatus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.ActiveProfiles;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

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

@DataJpaTest
@ActiveProfiles("test")
class OrderRepositoryTest {

@Autowired
private OrderRepository orderRepository;

private OrderEntity orderEntity;

@BeforeEach
void setUp() {
orderEntity = OrderEntity.builder()
.orderDate(LocalDate.now())
.status(EnumOrderStatus.OPEN)
.items(new ArrayList<>())
.grossTotal(BigDecimal.valueOf(1000.00))
.discount(null)
.netTotal(BigDecimal.valueOf(1000.00))
.build();
}

@Test
@DisplayName("Deve persistir um pedido no banco de dados")
void testSaveOrder() {
OrderEntity orderSaved = assertDoesNotThrow(() -> orderRepository.save(orderEntity));

assertNotNull(orderSaved);
assertEquals(orderEntity.getOrderDate(), orderSaved.getOrderDate());
assertEquals(orderEntity.getStatus(), orderSaved.getStatus());
assertEquals(orderEntity.getItems(), orderSaved.getItems());
assertEquals(orderEntity.getGrossTotal(), orderSaved.getGrossTotal());
assertEquals(orderEntity.getDiscount(), orderSaved.getDiscount());
assertEquals(orderEntity.getNetTotal(), orderSaved.getNetTotal());
}

@Test
@DisplayName("Deve listar todos os pedidos")
void testListOrders() {
OrderEntity orderSaved = assertDoesNotThrow(() -> orderRepository.save(orderEntity));

List<OrderEntity> orders = assertDoesNotThrow(() -> orderRepository.findAll());
assertNotNull(orders);
assertFalse(orders.isEmpty());
assertTrue(orders.contains(orderSaved));
assertEquals(1, orders.size());
}

@Test
@DisplayName("Deve retornar um pedido pelo id")
void testFindOrderById() {
OrderEntity orderSaved = assertDoesNotThrow(() -> orderRepository.save(orderEntity));

OrderEntity order = assertDoesNotThrow(() -> orderRepository.findById(orderSaved.getId()).orElse(null));
assertNotNull(order);
assertEquals(orderSaved, order);
}

@Test
@DisplayName("Deve atualizar um pedido")
void testUpdateOrder() {
OrderEntity orderSaved = assertDoesNotThrow(() -> orderRepository.save(orderEntity));

orderSaved.setStatus(EnumOrderStatus.CLOSED);
OrderEntity orderUpdated = assertDoesNotThrow(() -> orderRepository.save(orderSaved));

assertNotNull(orderUpdated);
assertEquals(orderSaved.getStatus(), orderUpdated.getStatus());
}

@Test
@DisplayName("Deve deletar um pedido")
void testDeleteOrder() {
OrderEntity orderSaved = assertDoesNotThrow(() -> orderRepository.save(orderEntity));

assertDoesNotThrow(() -> orderRepository.delete(orderSaved));

OrderEntity order = assertDoesNotThrow(() -> orderRepository.findById(orderSaved.getId()).orElse(null));
assertNull(order);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ CREATE TABLE tbproduct (
dthreg TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
dthalt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
version BIGINT,
CONSTRAINT tbwine_pk PRIMARY KEY (idproduct)
CONSTRAINT tbproduct_pk PRIMARY KEY (idproduct)
);
12 changes: 12 additions & 0 deletions src/test/resources/db/migration/test/V2__create_order_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE tborder (
idorder UUID,
orderdate DATE NOT NULL,
status VARCHAR(255) NOT NULL,
grossstotal NUMERIC(19,2),
discount NUMERIC(19,2) CHECK (discount >= 0),
nettotal NUMERIC(19,2),
dthreg TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
dthalt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
version BIGINT,
CONSTRAINT tborder_pk PRIMARY KEY (idorder)
);
Loading