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

feat: add product tests #4

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class CreateOrderRequestDTO {

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class Item {
private Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import br.com.grupo63.serviceorder.gateway.product.ProductJpaRepository;
import br.com.grupo63.serviceorder.gateway.product.entity.ProductPersistenceEntity;
import br.com.grupo63.serviceorder.usecase.order.OrderUseCase;
import br.com.grupo63.techchallenge.common.api.controller.dto.DefaultResponseDTO;
import jakarta.servlet.http.HttpServletRequest;
import lombok.SneakyThrows;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -30,8 +31,7 @@

import java.util.*;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.times;
Expand All @@ -54,14 +54,12 @@ public class OrderIntegrationTest {

private final Category defaultCategory = new Category(1L, false, "category");
private final Product defaultProduct = new Product(1L, false, "product", 10.0, defaultCategory);
private final CreateOrderRequestDTO.Item defaultItem = new CreateOrderRequestDTO.Item(1L, 1L);
private final Order defaultOrder = new Order(1L, false, 10.0, "hash", List.of());
private final OrderItem orderItem = new OrderItem(1L, false, 1L, 10.0, null, defaultProduct);
private final Order defaultOrder = new Order(1L, false, 10.0, "hash", List.of(orderItem));

private final OrderPersistenceEntity defaultOrderPersistenceEntity =
new OrderPersistenceEntity(defaultOrder);
private final OrderPersistenceEntity defaultOrderPersistenceEntity = new OrderPersistenceEntity(defaultOrder);
private final ProductPersistenceEntity defaultProductPersistenceEntity = new ProductPersistenceEntity(defaultProduct);

private final CreateOrderRequestDTO defaultCreateOrderRequestDTO = new CreateOrderRequestDTO(List.of(defaultItem));
private final ClientDTO defaultClientDTO = new ClientDTO();

@BeforeEach
Expand Down Expand Up @@ -102,11 +100,34 @@ void testCreate_EndToEnd() {
when(productJpaRepository.findByIdAndDeletedFalse(1L)).thenReturn(Optional.of(defaultProductPersistenceEntity));
when(orderJpaRepository.saveAndFlush(any())).thenReturn(defaultOrderPersistenceEntity);

ResponseEntity<OrderControllerDTO> response = orderAPIController.create(defaultCreateOrderRequestDTO, mockedRequest);
CreateOrderRequestDTO.Item item = new CreateOrderRequestDTO.Item();
item.setId(1L);
item.setQuantity(1L);

CreateOrderRequestDTO.Item secondItem = new CreateOrderRequestDTO.Item(1L, 1L);

CreateOrderRequestDTO createOrderRequestDTO = new CreateOrderRequestDTO(List.of(item, secondItem));

ResponseEntity<OrderControllerDTO> response = orderAPIController.create(createOrderRequestDTO, mockedRequest);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertNotNull(response.getBody());
assertEquals(response.getBody().getId(), 1L);
verify(orderJpaRepository, times(1)).saveAndFlush(any());
}

@SneakyThrows
@Test
public void testDelete_EndToEnd() {
when(orderJpaRepository.findByIdAndDeletedFalse(defaultOrderPersistenceEntity.getId())).thenReturn(Optional.of(defaultOrderPersistenceEntity));
when(orderJpaRepository.saveAndFlush(any())).thenReturn(new OrderPersistenceEntity(defaultOrderPersistenceEntity.getTotalPrice(), defaultOrderPersistenceEntity.getClientId(),
defaultOrderPersistenceEntity.getItems()));

ResponseEntity<DefaultResponseDTO> response = orderAPIController.delete(defaultOrderPersistenceEntity.getId());

assertEquals(HttpStatus.OK, response.getStatusCode());
assertNull(response.getBody());
verify(orderJpaRepository, times(1)).findByIdAndDeletedFalse(any());
verify(orderJpaRepository, times(1)).saveAndFlush(any());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package br.com.grupo63.serviceorder.product;

import br.com.grupo63.serviceorder.api.controller.product.ProductAPIController;
import br.com.grupo63.serviceorder.controller.ProductController;
import br.com.grupo63.serviceorder.controller.dto.ProductControllerDTO;
import br.com.grupo63.serviceorder.entity.product.Category;
import br.com.grupo63.serviceorder.entity.product.Product;
import br.com.grupo63.serviceorder.gateway.product.ProductJpaAdapter;
import br.com.grupo63.serviceorder.gateway.product.ProductJpaRepository;
import br.com.grupo63.serviceorder.gateway.product.entity.ProductPersistenceEntity;
import br.com.grupo63.serviceorder.usecase.product.ProductUseCase;
import br.com.grupo63.techchallenge.common.api.controller.dto.DefaultResponseDTO;
import lombok.SneakyThrows;
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.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.*;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.times;

public class ProductIntegrationTest {

@Mock
private ProductJpaRepository productJpaRepository;
@InjectMocks
private ProductJpaAdapter productJpaAdapter;
private ProductUseCase productUseCase;
private ProductController productController;
private ProductAPIController productAPIController;

private final Category defaultCategory = new Category(1L, false, "category");
private final Product defaultProduct = new Product(1L, false, "product", 10.0, defaultCategory);
private final ProductPersistenceEntity defaultProductPersistenceEntity = new ProductPersistenceEntity(defaultProduct);
private final ProductControllerDTO defaultProductControllerDTO = new ProductControllerDTO("product", 10.0, 1L);

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
productUseCase = new ProductUseCase();
productController = new ProductController(productUseCase, productJpaAdapter);
productAPIController = new ProductAPIController(productController);
}

@SneakyThrows
@Test
void testRead_EndToEnd() {
when(productJpaRepository.findByIdAndDeletedFalse(1L)).thenReturn(Optional.of(defaultProductPersistenceEntity));

ResponseEntity<ProductControllerDTO> response = productAPIController.read(1L);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(Objects.requireNonNull(response.getBody()).getId(), 1L);
}

@SneakyThrows
@Test
void testList_EndToEnd() {
when(productJpaRepository.findByDeletedFalse()).thenReturn(List.of(defaultProductPersistenceEntity));

ResponseEntity<List<ProductControllerDTO>> response = productAPIController.list();
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(Objects.requireNonNull(response.getBody()).get(0).getId(), 1L);
}

@SneakyThrows
@Test
void testListByCategory_EndToEnd() {
when(productJpaRepository.findByDeletedFalseAndCategory_Name("category")).thenReturn(List.of(defaultProductPersistenceEntity));

ResponseEntity<List<ProductControllerDTO>> response = productAPIController.listByCategoryName("category");
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(Objects.requireNonNull(response.getBody()).get(0).getId(), 1L);
}

@SneakyThrows
@Test
void testCreate_EndToEnd() {
when(productJpaRepository.saveAndFlush(any())).thenReturn(defaultProductPersistenceEntity);

ResponseEntity<ProductControllerDTO> response = productAPIController.create(defaultProductControllerDTO);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertNotNull(response.getBody());
assertEquals(response.getBody().getId(), 1L);
verify(productJpaRepository, times(1)).saveAndFlush(any());
}

@SneakyThrows
@Test
public void testDelete_EndToEnd() {
when(productJpaRepository.findByIdAndDeletedFalse(defaultProductPersistenceEntity.getId())).thenReturn(Optional.of(defaultProductPersistenceEntity));
when(productJpaRepository.saveAndFlush(any())).thenReturn(new ProductPersistenceEntity(defaultProductPersistenceEntity.getName(), defaultProductPersistenceEntity.getPrice(), defaultProductPersistenceEntity.getCategory()));

ResponseEntity<DefaultResponseDTO> response = productAPIController.delete(defaultProductPersistenceEntity.getId());

assertEquals(HttpStatus.OK, response.getStatusCode());
assertNull(response.getBody());
verify(productJpaRepository, times(1)).findByIdAndDeletedFalse(any());
verify(productJpaRepository, times(1)).saveAndFlush(any());
}

}
Loading