From 66de182e1f9b723465ccf8ced608a9c7f6e04169 Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Sat, 6 Nov 2021 15:13:35 +0300 Subject: [PATCH 1/2] create tests --- .../springmvc/SpringMvcApplication.java | 3 - .../configuration/BeanConfiguration.java | 14 - .../mvcLayer/controller/OrderController.java | 6 - .../mvcLayer/controller/UserController.java | 2 - .../domain/search/ProductSearchCondition.java | 5 - .../repository/CategoryRepository.java | 4 - .../repository/ProductRepository.java | 5 - .../mvcLayer/repository/ReviewRepository.java | 1 - .../mvcLayer/service/OrderService.java | 3 +- .../mvcLayer/service/ProductService.java | 3 - .../mvcLayer/service/ReviewService.java | 1 - .../service/impl/OrderServiceImpl.java | 3 - .../service/impl/ProductServiceImpl.java | 49 +-- .../service/impl/ReviewServiceImpl.java | 5 - .../mvcLayer/component/ShoppingCartTest.java | 251 +++++++++++ .../configuration/MvcConfigurationTest.java | 32 ++ .../SecurityConfigurationTest.java | 50 +++ .../controller/CategoryControllerTest.java | 111 +++++ .../controller/OrderControllerTest.java | 29 ++ .../controller/ProductControllerTest.java | 240 +++++++++++ .../ProductReviewControllerTest.java | 117 ++++++ .../ShoppingCartControllerTest.java | 392 ++++++++++++++++++ .../controller/UserControllerTest.java | 127 ++++++ .../mvcLayer/domain/cart/CartItemTest.java | 32 ++ .../service/impl/CategoryServiceImplTest.java | 283 +++++++++++++ .../service/impl/FileServiceImplTest.java | 22 + .../service/impl/OrderServiceImplTest.java | 114 +++++ .../service/impl/ProductServiceImplTest.java | 378 +++++++++++++++++ .../service/impl/ReviewServiceImplTest.java | 222 ++++++++++ .../impl/security/RoleServiceImplTest.java | 49 +++ .../security/UserDetailsServiceImplTest.java | 64 +++ .../impl/security/UserServiceImplTest.java | 150 +++++++ 32 files changed, 2676 insertions(+), 91 deletions(-) delete mode 100644 src/main/java/com/example/springmvc/mvcLayer/configuration/BeanConfiguration.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/component/ShoppingCartTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/configuration/MvcConfigurationTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/configuration/SecurityConfigurationTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/controller/CategoryControllerTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/controller/OrderControllerTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/controller/ProductControllerTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/controller/ProductReviewControllerTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/controller/ShoppingCartControllerTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/controller/UserControllerTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/domain/cart/CartItemTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/service/impl/CategoryServiceImplTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/service/impl/FileServiceImplTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/service/impl/OrderServiceImplTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/service/impl/ProductServiceImplTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/service/impl/ReviewServiceImplTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/service/impl/security/RoleServiceImplTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/service/impl/security/UserDetailsServiceImplTest.java create mode 100644 src/test/java/com/example/springmvc/mvcLayer/service/impl/security/UserServiceImplTest.java diff --git a/src/main/java/com/example/springmvc/SpringMvcApplication.java b/src/main/java/com/example/springmvc/SpringMvcApplication.java index 002ba54..4ebd646 100644 --- a/src/main/java/com/example/springmvc/SpringMvcApplication.java +++ b/src/main/java/com/example/springmvc/SpringMvcApplication.java @@ -7,9 +7,6 @@ public class SpringMvcApplication { public static void main(String[] args) { -// Flyway flyway = Flyway.configure().dataSource( -// "jdbc:postgresql://localhost:5435/spring", "postgres", "postgrespass").load(); -// flyway.migrate(); SpringApplication.run(SpringMvcApplication.class, args); } } \ No newline at end of file diff --git a/src/main/java/com/example/springmvc/mvcLayer/configuration/BeanConfiguration.java b/src/main/java/com/example/springmvc/mvcLayer/configuration/BeanConfiguration.java deleted file mode 100644 index a3d997b..0000000 --- a/src/main/java/com/example/springmvc/mvcLayer/configuration/BeanConfiguration.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.example.springmvc.mvcLayer.configuration; - -import org.modelmapper.ModelMapper; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class BeanConfiguration { - - @Bean - public ModelMapper modelMapper(){ - return new ModelMapper(); - } -} \ No newline at end of file diff --git a/src/main/java/com/example/springmvc/mvcLayer/controller/OrderController.java b/src/main/java/com/example/springmvc/mvcLayer/controller/OrderController.java index a7bce5b..58c1917 100644 --- a/src/main/java/com/example/springmvc/mvcLayer/controller/OrderController.java +++ b/src/main/java/com/example/springmvc/mvcLayer/controller/OrderController.java @@ -43,10 +43,4 @@ public String createOrder(@ModelAttribute Address address, orderService.save(shoppingCart, address, name); return "redirect:/order/list"; } - -// @PostMapping(FORM) -// public String updateStatusOrder(@ModelAttribute Address address) { -// String name = SecurityContextHolder.getContext().getAuthentication().getName(); -// return "redirect:/order/list"; -// } } diff --git a/src/main/java/com/example/springmvc/mvcLayer/controller/UserController.java b/src/main/java/com/example/springmvc/mvcLayer/controller/UserController.java index 2dc478e..77f7b39 100644 --- a/src/main/java/com/example/springmvc/mvcLayer/controller/UserController.java +++ b/src/main/java/com/example/springmvc/mvcLayer/controller/UserController.java @@ -57,6 +57,4 @@ public String getUserAccount(Model model) { model.addAttribute("user", user); return "user/personalAccount"; } - - } \ No newline at end of file diff --git a/src/main/java/com/example/springmvc/mvcLayer/domain/search/ProductSearchCondition.java b/src/main/java/com/example/springmvc/mvcLayer/domain/search/ProductSearchCondition.java index 93e6769..fd65f07 100644 --- a/src/main/java/com/example/springmvc/mvcLayer/domain/search/ProductSearchCondition.java +++ b/src/main/java/com/example/springmvc/mvcLayer/domain/search/ProductSearchCondition.java @@ -1,13 +1,8 @@ package com.example.springmvc.mvcLayer.domain.search; -import com.example.springmvc.mvcLayer.domain.dto.ProductDto; import lombok.Data; -import org.springframework.data.domain.Page; import org.springframework.data.domain.Sort; -import java.util.Collections; -import java.util.List; - @Data public class ProductSearchCondition { private Sort.Direction sortDirection=Sort.Direction.ASC; diff --git a/src/main/java/com/example/springmvc/mvcLayer/repository/CategoryRepository.java b/src/main/java/com/example/springmvc/mvcLayer/repository/CategoryRepository.java index e9a5473..2e65321 100644 --- a/src/main/java/com/example/springmvc/mvcLayer/repository/CategoryRepository.java +++ b/src/main/java/com/example/springmvc/mvcLayer/repository/CategoryRepository.java @@ -12,9 +12,5 @@ public interface CategoryRepository extends JpaRepository { Optional findCategoryByTitle(String title); - Set findCategoryByIdIn(Set ids); - -// Page findProductsByIdCategory(Pageable pageable); - } diff --git a/src/main/java/com/example/springmvc/mvcLayer/repository/ProductRepository.java b/src/main/java/com/example/springmvc/mvcLayer/repository/ProductRepository.java index 558931c..4d8a49f 100644 --- a/src/main/java/com/example/springmvc/mvcLayer/repository/ProductRepository.java +++ b/src/main/java/com/example/springmvc/mvcLayer/repository/ProductRepository.java @@ -10,7 +10,6 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; -import java.util.List; import java.util.Optional; @Repository @@ -30,8 +29,4 @@ Page findProductsByTitleContainingIgnoreCaseAndPriceBetween( @Modifying @Query("update Product p set p.countProduct = :count where p.id = :id") void updateCount(@Param("id")Integer id, @Param("count")Integer count); - - @Modifying - @Query("update Product p set p.countProduct = p.countProduct +1 where p.id = :id") - void plusCount(@Param("id")Integer id); } diff --git a/src/main/java/com/example/springmvc/mvcLayer/repository/ReviewRepository.java b/src/main/java/com/example/springmvc/mvcLayer/repository/ReviewRepository.java index ddba65b..9e3d0a3 100644 --- a/src/main/java/com/example/springmvc/mvcLayer/repository/ReviewRepository.java +++ b/src/main/java/com/example/springmvc/mvcLayer/repository/ReviewRepository.java @@ -11,7 +11,6 @@ public interface ReviewRepository extends JpaRepository ListfindProductReviewsByProduct_id(Integer productId); - void deleteProductReviewsByProduct_Id(Integer productId); void deleteProductReviewById(Integer id); diff --git a/src/main/java/com/example/springmvc/mvcLayer/service/OrderService.java b/src/main/java/com/example/springmvc/mvcLayer/service/OrderService.java index 8b36f26..cf65f50 100644 --- a/src/main/java/com/example/springmvc/mvcLayer/service/OrderService.java +++ b/src/main/java/com/example/springmvc/mvcLayer/service/OrderService.java @@ -8,6 +8,7 @@ public interface OrderService { - void save(ShoppingCart shoppingCart, Address address, String name ); + void save(ShoppingCart shoppingCart, Address address, String name); + List getAllOrders(String name); } diff --git a/src/main/java/com/example/springmvc/mvcLayer/service/ProductService.java b/src/main/java/com/example/springmvc/mvcLayer/service/ProductService.java index 603b1aa..66179c6 100644 --- a/src/main/java/com/example/springmvc/mvcLayer/service/ProductService.java +++ b/src/main/java/com/example/springmvc/mvcLayer/service/ProductService.java @@ -28,8 +28,5 @@ Page findProductsByTitleAndByMaxAndMinPriceBySearchConditional( void pagination(ProductSearchCondition searchCondition, Model model, Page page); - void updateCountInProduct(Integer id, Integer count); - -// void plusCountProduct(Integer productId); } \ No newline at end of file diff --git a/src/main/java/com/example/springmvc/mvcLayer/service/ReviewService.java b/src/main/java/com/example/springmvc/mvcLayer/service/ReviewService.java index 952d2aa..048bc38 100644 --- a/src/main/java/com/example/springmvc/mvcLayer/service/ReviewService.java +++ b/src/main/java/com/example/springmvc/mvcLayer/service/ReviewService.java @@ -5,7 +5,6 @@ import java.util.List; public interface ReviewService { - List findAllReview(); List findReviewByProductId(Integer id); diff --git a/src/main/java/com/example/springmvc/mvcLayer/service/impl/OrderServiceImpl.java b/src/main/java/com/example/springmvc/mvcLayer/service/impl/OrderServiceImpl.java index 25c14f9..311aac9 100644 --- a/src/main/java/com/example/springmvc/mvcLayer/service/impl/OrderServiceImpl.java +++ b/src/main/java/com/example/springmvc/mvcLayer/service/impl/OrderServiceImpl.java @@ -16,7 +16,6 @@ import java.time.LocalDate; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -25,9 +24,7 @@ public class OrderServiceImpl implements OrderService { private final OrderRepository orderRepository; - private final UserService userService; - private final AddressRepository addressRepository; @Override diff --git a/src/main/java/com/example/springmvc/mvcLayer/service/impl/ProductServiceImpl.java b/src/main/java/com/example/springmvc/mvcLayer/service/impl/ProductServiceImpl.java index 2869b2d..a149b69 100644 --- a/src/main/java/com/example/springmvc/mvcLayer/service/impl/ProductServiceImpl.java +++ b/src/main/java/com/example/springmvc/mvcLayer/service/impl/ProductServiceImpl.java @@ -10,7 +10,6 @@ import com.example.springmvc.mvcLayer.service.ProductService; import com.example.springmvc.mvcLayer.service.ReviewService; import lombok.AllArgsConstructor; -import org.modelmapper.ModelMapper; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -37,8 +36,6 @@ public class ProductServiceImpl implements ProductService { private final FileService fileService; private final ReviewService reviewService; - private final ModelMapper modelMapper; - @Override @Transactional public Product saveProductAndImage(ProductDto productDto, MultipartFile image) { @@ -63,14 +60,11 @@ private Product choosingActionOrCreatingOrUpdating(ProductDto productDto) { } private Product dtoProductConvertToProduct(ProductDto productDto) { - Product product = modelMapper.map(productDto, Product.class); - product.setCategories(categoryService.findCategoryById(productDto.getCategoryDto())); - return product; -// return Product.builder().title(productDto.getTitle()) -// .price(productDto.getPrice()) -// .categories(categoryService.findCategoryById(productDto.getCategoryDto())) -// .countProduct(productDto.getCountProduct()) -// .build(); + return Product.builder().title(productDto.getTitle()) + .price(productDto.getPrice()) + .categories(categoryService.findCategoryById(productDto.getCategoryDto())) + .countProduct(productDto.getCountProduct()) + .build(); } @Override @@ -86,23 +80,6 @@ public ProductDto findProductDtoById(Integer id) { } else throw new NoSuchElementException("Продукт был удален администратором"); } -// @Override -// @Transactional -// public void minusOneProductInCount(Integer id, Integer count) { -//// Optional byId = productRepository.findById(id); -//// byId.ifPresent(product -> productRepository.updateCount(product, count)); -// productRepository.updateCount(id, count); -// } - -// @Override -// @Transactional -// public void plusCountProduct(Integer productId) { -// Optional byId = productRepository.findById(productId); -// Integer countProduct = byId.get().getCountProduct()+1; -// productRepository.updateCount(productId, countProduct); -//// productRepository.plusCount(productId); -// } - @Override @Transactional public void updateCountInProduct(Integer id, Integer count) { @@ -110,16 +87,12 @@ public void updateCountInProduct(Integer id, Integer count) { } private ProductDto productConvertToDTOProduct(Product entity) { - ProductDto productDto = modelMapper.map(entity, ProductDto.class); - productDto.setCategoryDto(categoryService.getCategoryIdList(entity.getCategories())); - System.err.println(productDto.getCategoryDto().toString()); - return productDto; -// return ProductDto.builder().id(entity.getId()) -// .title(entity.getTitle()) -// .price(entity.getPrice()) -// .categoryDto(categoryService.getCategoryIdList(entity.getCategories())) -// .countProduct(entity.getCountProduct()) -// .build(); + return ProductDto.builder().id(entity.getId()) + .title(entity.getTitle()) + .price(entity.getPrice()) + .categoryDto(categoryService.getCategoryIdList(entity.getCategories())) + .countProduct(entity.getCountProduct()) + .build(); } @Override diff --git a/src/main/java/com/example/springmvc/mvcLayer/service/impl/ReviewServiceImpl.java b/src/main/java/com/example/springmvc/mvcLayer/service/impl/ReviewServiceImpl.java index d639cbe..23dbadf 100644 --- a/src/main/java/com/example/springmvc/mvcLayer/service/impl/ReviewServiceImpl.java +++ b/src/main/java/com/example/springmvc/mvcLayer/service/impl/ReviewServiceImpl.java @@ -19,11 +19,6 @@ public class ReviewServiceImpl implements ReviewService { private final ReviewRepository reviewRepository; private final UserService userService; - @Override - public List findAllReview() { - return reviewRepository.findAll(); - } - @Override public List findReviewByProductId(Integer id) { return reviewRepository.findProductReviewsByProduct_id(id); diff --git a/src/test/java/com/example/springmvc/mvcLayer/component/ShoppingCartTest.java b/src/test/java/com/example/springmvc/mvcLayer/component/ShoppingCartTest.java new file mode 100644 index 0000000..d64cf2d --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/component/ShoppingCartTest.java @@ -0,0 +1,251 @@ +package com.example.springmvc.mvcLayer.component; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.example.springmvc.mvcLayer.domain.dto.ProductDto; + +import java.util.HashSet; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ContextConfiguration(classes = {ShoppingCart.class}) +@ExtendWith(SpringExtension.class) +class ShoppingCartTest { + @Autowired + private ShoppingCart shoppingCart; + + @Test + @Disabled("TODO: This test is incomplete") + void testAddCartItem() { + // TODO: This test is incomplete. + // Reason: F009 Internal error. + // java.lang.AssertionError: Misuse of currentTasks stack detected: GenAssertions$Task(java.util.Map, #61208: (#1160).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@2580797f:cartItems, java.util.HashMap@c1a1750:], 2, 1) != GenAssertions$Task(java.util.Map, #61208: (#1160).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@2580797f:cartItems], 2, 1) + // Cause: java.lang.AssertionError: Misuse of currentTasks stack detected: GenAssertions$Task(java.util.Map, #61208: (#1160).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@2580797f:cartItems, java.util.HashMap@c1a1750:], 2, 1) != GenAssertions$Task(java.util.Map, #61208: (#1160).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@2580797f:cartItems], 2, 1) + // at com.diffblue.assertion.generator.core.g.b(SourceFile:212) + // at com.diffblue.assertion.generator.core.i.a(SourceFile:213) + // at com.diffblue.assertion.generator.core.n.a(SourceFile:65) + // at com.diffblue.assertion.generator.core.l.a(SourceFile:119) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:232) + // at java.util.ArrayList.forEach(ArrayList.java:1541) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:232) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:109) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:287) + // at com.diffblue.fuzztest.e.e.addAssertions(SourceFile:48) + // at com.diffblue.fuzztest.e.b.b(SourceFile:41) + // at com.diffblue.cover.sandbox.execution.WrapExceptionsInBaseException.handleException(SourceFile:55) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.lambda$run$0(SourceFile:21) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.handleThrowable(SourceFile:32) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.run(SourceFile:21) + // at com.diffblue.cover.sandbox.execution.SafeExecutor.run(SourceFile:26) + // at com.diffblue.fuzztest.e.b.a(SourceFile:40) + // at com.diffblue.fuzztest.e.c.b(SourceFile:1271) + // at com.diffblue.fuzztest.e.c.i(SourceFile:400) + // at com.diffblue.cover.sandbox.execution.WorkerThread.lambda$callWorkerThread$3(SourceFile:134) + // at com.diffblue.cover.sandbox.execution.WorkerThread.run(SourceFile:207) + // Please contact Diffblue through the appropriate support channel + // (https://www.diffblue.com/support) providing details about this error. + + ShoppingCart shoppingCart = new ShoppingCart(); + + ProductDto productDto = new ProductDto(); + productDto.setPrice(0); + shoppingCart.addCartItem(productDto); + } + + @Test + void testAddCartItem2() { + ShoppingCart shoppingCart = new ShoppingCart(); + ProductDto productDto = mock(ProductDto.class); + when(productDto.getPrice()).thenReturn(1); + when(productDto.getId()).thenReturn(1); + shoppingCart.addCartItem(productDto); + verify(productDto).getId(); + verify(productDto).getPrice(); + assertEquals(1, shoppingCart.getTotalPrice().intValue()); + assertEquals(1, shoppingCart.getCount()); + assertEquals(1, shoppingCart.getCartItems().get(1).getCount().intValue()); + } + + @Test + void testAddCartItem3() { + ShoppingCart shoppingCart = new ShoppingCart(); + shoppingCart.addCartItem(new ProductDto(1, "Dr", 1, new HashSet(), 3)); + ProductDto productDto = mock(ProductDto.class); + when(productDto.getPrice()).thenReturn(1); + when(productDto.getId()).thenReturn(1); + shoppingCart.addCartItem(productDto); + verify(productDto).getId(); + assertEquals(2, shoppingCart.getTotalPrice().intValue()); + assertEquals(2, shoppingCart.getCartItems().get(1).getCount().intValue()); + } + + @Test + @Disabled("TODO: This test is incomplete") + void testDeleteCartItem() { + // TODO: This test is incomplete. + // Reason: F009 Internal error. + // java.lang.AssertionError: Misuse of currentTasks stack detected: GenAssertions$Task(java.util.Map, #61883: (#61809).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@841a96f:cartItems, java.util.HashMap@74f7e19c:], 2, 1) != GenAssertions$Task(java.util.Map, #61883: (#61809).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@841a96f:cartItems], 2, 1) + // Cause: java.lang.AssertionError: Misuse of currentTasks stack detected: GenAssertions$Task(java.util.Map, #61883: (#61809).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@841a96f:cartItems, java.util.HashMap@74f7e19c:], 2, 1) != GenAssertions$Task(java.util.Map, #61883: (#61809).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@841a96f:cartItems], 2, 1) + // at com.diffblue.assertion.generator.core.g.b(SourceFile:212) + // at com.diffblue.assertion.generator.core.i.a(SourceFile:213) + // at com.diffblue.assertion.generator.core.n.a(SourceFile:65) + // at com.diffblue.assertion.generator.core.l.a(SourceFile:119) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:232) + // at java.util.ArrayList.forEach(ArrayList.java:1541) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:232) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:109) + // at com.diffblue.fuzztest.e.d.a.addAssertions(SourceFile:71) + // at com.diffblue.fuzztest.e.b.b(SourceFile:41) + // at com.diffblue.cover.sandbox.execution.WrapExceptionsInBaseException.handleException(SourceFile:55) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.lambda$run$0(SourceFile:21) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.handleThrowable(SourceFile:32) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.run(SourceFile:21) + // at com.diffblue.cover.sandbox.execution.SafeExecutor.run(SourceFile:26) + // at com.diffblue.fuzztest.e.b.a(SourceFile:40) + // at com.diffblue.fuzztest.e.c.b(SourceFile:1271) + // at com.diffblue.fuzztest.e.c.i(SourceFile:400) + // at com.diffblue.cover.sandbox.execution.WorkerThread.lambda$callWorkerThread$3(SourceFile:134) + // at com.diffblue.cover.sandbox.execution.WorkerThread.run(SourceFile:207) + // Please contact Diffblue through the appropriate support channel + // (https://www.diffblue.com/support) providing details about this error. + + this.shoppingCart.deleteCartItem(0); + } + + @Test + @Disabled("TODO: This test is incomplete") + void testAddSameItem() { + // TODO: This test is incomplete. + // Reason: F009 Internal error. + // java.lang.AssertionError: Misuse of currentTasks stack detected: GenAssertions$Task(java.util.Map, #61794: (#61723).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@841a96f:cartItems, java.util.HashMap@407fb5fe:], 2, 1) != GenAssertions$Task(java.util.Map, #61794: (#61723).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@841a96f:cartItems], 2, 1) + // Cause: java.lang.AssertionError: Misuse of currentTasks stack detected: GenAssertions$Task(java.util.Map, #61794: (#61723).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@841a96f:cartItems, java.util.HashMap@407fb5fe:], 2, 1) != GenAssertions$Task(java.util.Map, #61794: (#61723).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@841a96f:cartItems], 2, 1) + // at com.diffblue.assertion.generator.core.g.b(SourceFile:212) + // at com.diffblue.assertion.generator.core.i.a(SourceFile:213) + // at com.diffblue.assertion.generator.core.n.a(SourceFile:65) + // at com.diffblue.assertion.generator.core.l.a(SourceFile:119) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:232) + // at java.util.ArrayList.forEach(ArrayList.java:1541) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:232) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:109) + // at com.diffblue.fuzztest.e.d.a.addAssertions(SourceFile:71) + // at com.diffblue.fuzztest.e.b.b(SourceFile:41) + // at com.diffblue.cover.sandbox.execution.WrapExceptionsInBaseException.handleException(SourceFile:55) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.lambda$run$0(SourceFile:21) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.handleThrowable(SourceFile:32) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.run(SourceFile:21) + // at com.diffblue.cover.sandbox.execution.SafeExecutor.run(SourceFile:26) + // at com.diffblue.fuzztest.e.b.a(SourceFile:40) + // at com.diffblue.fuzztest.e.c.b(SourceFile:1271) + // at com.diffblue.fuzztest.e.c.i(SourceFile:400) + // at com.diffblue.cover.sandbox.execution.WorkerThread.lambda$callWorkerThread$3(SourceFile:134) + // at com.diffblue.cover.sandbox.execution.WorkerThread.run(SourceFile:207) + // Please contact Diffblue through the appropriate support channel + // (https://www.diffblue.com/support) providing details about this error. + + this.shoppingCart.addSameItem(2); + } + + @Test + void testDeleteSameItem() { + ShoppingCart shoppingCart = new ShoppingCart(); + shoppingCart.deleteSameItem(123); + assertEquals(0, shoppingCart.getTotalPrice().intValue()); + } + + @Test + void testDeleteSameItem2() { + ShoppingCart shoppingCart = new ShoppingCart(); + shoppingCart.addCartItem(new ProductDto(1, "Dr", 1, new HashSet(), 3)); + shoppingCart.deleteSameItem(123); + assertEquals(1, shoppingCart.getTotalPrice().intValue()); + } + + @Test + void testDeleteSameItem3() { + ShoppingCart shoppingCart = new ShoppingCart(); + shoppingCart.addCartItem(new ProductDto(123, "Dr", 1, new HashSet(), 3)); + shoppingCart.deleteSameItem(123); + assertEquals(0, shoppingCart.getTotalPrice().intValue()); + assertEquals(0, shoppingCart.getCount()); + } + + @Test + @Disabled("TODO: This test is incomplete") + void testGetCount() { + // TODO: This test is incomplete. + // Reason: F009 Internal error. + // java.lang.AssertionError: Misuse of currentTasks stack detected: GenAssertions$Task(java.util.Map, #112142: (#112131).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@841a96f:cartItems, java.util.HashMap@522e0362:], 2, 1) != GenAssertions$Task(java.util.Map, #112142: (#112131).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@841a96f:cartItems], 2, 1) + // Cause: java.lang.AssertionError: Misuse of currentTasks stack detected: GenAssertions$Task(java.util.Map, #112142: (#112131).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@841a96f:cartItems, java.util.HashMap@522e0362:], 2, 1) != GenAssertions$Task(java.util.Map, #112142: (#112131).getCartItems(), [com.example.springmvc.mvcLayer.component.ShoppingCart@841a96f:cartItems], 2, 1) + // at com.diffblue.assertion.generator.core.g.b(SourceFile:212) + // at com.diffblue.assertion.generator.core.i.a(SourceFile:213) + // at com.diffblue.assertion.generator.core.n.a(SourceFile:65) + // at com.diffblue.assertion.generator.core.l.a(SourceFile:119) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:232) + // at java.util.Collections$SingletonList.forEach(Collections.java:4856) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:232) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:109) + // at com.diffblue.fuzztest.e.d.a.addAssertions(SourceFile:71) + // at com.diffblue.fuzztest.e.b.b(SourceFile:41) + // at com.diffblue.cover.sandbox.execution.WrapExceptionsInBaseException.handleException(SourceFile:55) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.lambda$run$0(SourceFile:21) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.handleThrowable(SourceFile:32) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.run(SourceFile:21) + // at com.diffblue.cover.sandbox.execution.SafeExecutor.run(SourceFile:26) + // at com.diffblue.fuzztest.e.b.a(SourceFile:40) + // at com.diffblue.fuzztest.e.c.b(SourceFile:1271) + // at com.diffblue.fuzztest.e.c.i(SourceFile:400) + // at com.diffblue.cover.sandbox.execution.WorkerThread.lambda$callWorkerThread$3(SourceFile:134) + // at com.diffblue.cover.sandbox.execution.WorkerThread.run(SourceFile:207) + // Please contact Diffblue through the appropriate support channel + // (https://www.diffblue.com/support) providing details about this error. + + this.shoppingCart.getCount(); + } + + @Test + @Disabled("TODO: This test is incomplete") + void testGetCartItems() { + // TODO: This test is incomplete. + // Reason: F009 Internal error. + // java.lang.AssertionError: Misuse of currentTasks stack detected: GenAssertions$Task(java.util.Map, #112115: (#112112).getCartItems(), [java.util.HashMap@6c10c55d:], 1, 0) != GenAssertions$Task(java.util.Map, #112115: (#112112).getCartItems(), [], 1, 0) + // Cause: java.lang.AssertionError: Misuse of currentTasks stack detected: GenAssertions$Task(java.util.Map, #112115: (#112112).getCartItems(), [java.util.HashMap@6c10c55d:], 1, 0) != GenAssertions$Task(java.util.Map, #112115: (#112112).getCartItems(), [], 1, 0) + // at com.diffblue.assertion.generator.core.g.b(SourceFile:212) + // at com.diffblue.assertion.generator.core.i.a(SourceFile:213) + // at com.diffblue.assertion.generator.core.n.a(SourceFile:65) + // at com.diffblue.assertion.generator.core.l.a(SourceFile:102) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:232) + // at java.util.Collections$SingletonList.forEach(Collections.java:4856) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:232) + // at com.diffblue.assertion.generator.core.f.a(SourceFile:109) + // at com.diffblue.fuzztest.e.d.a.addAssertions(SourceFile:71) + // at com.diffblue.fuzztest.e.b.b(SourceFile:41) + // at com.diffblue.cover.sandbox.execution.WrapExceptionsInBaseException.handleException(SourceFile:55) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.lambda$run$0(SourceFile:21) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.handleThrowable(SourceFile:32) + // at com.diffblue.cover.sandbox.execution.WrapThrowableInBaseException.run(SourceFile:21) + // at com.diffblue.cover.sandbox.execution.SafeExecutor.run(SourceFile:26) + // at com.diffblue.fuzztest.e.b.a(SourceFile:40) + // at com.diffblue.fuzztest.e.c.b(SourceFile:1271) + // at com.diffblue.fuzztest.e.c.i(SourceFile:400) + // at com.diffblue.cover.sandbox.execution.WorkerThread.lambda$callWorkerThread$3(SourceFile:134) + // at com.diffblue.cover.sandbox.execution.WorkerThread.run(SourceFile:207) + // Please contact Diffblue through the appropriate support channel + // (https://www.diffblue.com/support) providing details about this error. + + this.shoppingCart.getCartItems(); + } + + @Test + void testConstructor() { + assertEquals(0, (new ShoppingCart()).getTotalPrice().intValue()); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/configuration/MvcConfigurationTest.java b/src/test/java/com/example/springmvc/mvcLayer/configuration/MvcConfigurationTest.java new file mode 100644 index 0000000..df260bd --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/configuration/MvcConfigurationTest.java @@ -0,0 +1,32 @@ +package com.example.springmvc.mvcLayer.configuration; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; + +@ContextConfiguration(classes = {MvcConfiguration.class}) +@ExtendWith(SpringExtension.class) +class MvcConfigurationTest { + @Autowired + private MvcConfiguration mvcConfiguration; + + @Test + void testAddResourceHandlers() { + // TODO: This test is incomplete. + // Reason: R004 No meaningful assertions found. + // Diffblue Cover was unable to create an assertion. + // Make sure that fields modified by addResourceHandlers(ResourceHandlerRegistry) + // have package-private, protected, or public getters. + // See https://diff.blue/R004 to resolve this issue. + + AnnotationConfigReactiveWebApplicationContext applicationContext = new AnnotationConfigReactiveWebApplicationContext(); + this.mvcConfiguration + .addResourceHandlers(new ResourceHandlerRegistry(applicationContext, new MockServletContext())); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/configuration/SecurityConfigurationTest.java b/src/test/java/com/example/springmvc/mvcLayer/configuration/SecurityConfigurationTest.java new file mode 100644 index 0000000..0dc11e5 --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/configuration/SecurityConfigurationTest.java @@ -0,0 +1,50 @@ +package com.example.springmvc.mvcLayer.configuration; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; +import org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl; + +class SecurityConfigurationTest { + @Test + void testPasswordEncoder() { + // TODO: This test is incomplete. + // Reason: R002 Missing observers. + // Diffblue Cover was unable to create an assertion. + // Add getters for the following fields or make them package-private: + // SecurityConfiguration.userDetailsService + // WebSecurityConfigurerAdapter.authenticationBuilder + // WebSecurityConfigurerAdapter.authenticationConfiguration + // WebSecurityConfigurerAdapter.authenticationManager + // WebSecurityConfigurerAdapter.authenticationManagerInitialized + // WebSecurityConfigurerAdapter.contentNegotiationStrategy + // WebSecurityConfigurerAdapter.context + // WebSecurityConfigurerAdapter.disableDefaults + // WebSecurityConfigurerAdapter.disableLocalConfigureAuthenticationBldr + // WebSecurityConfigurerAdapter.http + // WebSecurityConfigurerAdapter.localConfigureAuthenticationBldr + // WebSecurityConfigurerAdapter.logger + // WebSecurityConfigurerAdapter.objectPostProcessor + // WebSecurityConfigurerAdapter.trustResolver + // BCryptPasswordEncoder.BCRYPT_PATTERN + // BCryptPasswordEncoder.logger + // BCryptPasswordEncoder.random + // BCryptPasswordEncoder.strength + // BCryptPasswordEncoder.version + + (new SecurityConfiguration(new JdbcDaoImpl())).passwordEncoder(); + } + + @Test + void testAuthenticationProvider() { + DaoAuthenticationProvider actualAuthenticationProviderResult = (new SecurityConfiguration(new JdbcDaoImpl())) + .authenticationProvider(); + assertTrue(actualAuthenticationProviderResult + .getUserCache() instanceof org.springframework.security.core.userdetails.cache.NullUserCache); + assertTrue(actualAuthenticationProviderResult.isHideUserNotFoundExceptions()); + assertFalse(actualAuthenticationProviderResult.isForcePrincipalAsString()); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/controller/CategoryControllerTest.java b/src/test/java/com/example/springmvc/mvcLayer/controller/CategoryControllerTest.java new file mode 100644 index 0000000..39693d1 --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/controller/CategoryControllerTest.java @@ -0,0 +1,111 @@ +package com.example.springmvc.mvcLayer.controller; + +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import com.example.springmvc.mvcLayer.domain.Category; +import com.example.springmvc.mvcLayer.service.CategoryService; + +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@ContextConfiguration(classes = {CategoryController.class}) +@ExtendWith(SpringExtension.class) +class CategoryControllerTest { + @Autowired + private CategoryController categoryController; + + @MockBean + private CategoryService categoryService; + + @Test + void testAddViewToCreateCategory() throws Exception { + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/category/form"); + MockMvcBuilders.standaloneSetup(this.categoryController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(2)) + .andExpect(MockMvcResultMatchers.model().attributeExists("category", "error")) + .andExpect(MockMvcResultMatchers.view().name("category/form")) + .andExpect(MockMvcResultMatchers.forwardedUrl("category/form")); + } + + @Test + void testAddViewToCreateCategory2() throws Exception { + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/category/form", "Uri Vars"); + MockMvcBuilders.standaloneSetup(this.categoryController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(2)) + .andExpect(MockMvcResultMatchers.model().attributeExists("category", "error")) + .andExpect(MockMvcResultMatchers.view().name("category/form")) + .andExpect(MockMvcResultMatchers.forwardedUrl("category/form")); + } + + @Test + void testCreateCategory() throws Exception { + doNothing().when(this.categoryService).addCategory((String) any()); + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/category/form").param("title", "foo"); + MockMvcBuilders.standaloneSetup(this.categoryController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isFound()) + .andExpect(MockMvcResultMatchers.model().size(0)) + .andExpect(MockMvcResultMatchers.redirectedUrl("/category/list")); + } + + @Test + void testCreateCategory2() throws Exception { + doNothing().when(this.categoryService).addCategory((String) any()); + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/category/form").param("title", ""); + MockMvcBuilders.standaloneSetup(this.categoryController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isFound()) + .andExpect(MockMvcResultMatchers.model().size(0)) + .andExpect(MockMvcResultMatchers.redirectedUrl("/category/form")); + } + + @Test + void testFindAllCategory() throws Exception { + when(this.categoryService.findCategories()).thenReturn(new ArrayList()); + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/category/list"); + MockMvcBuilders.standaloneSetup(this.categoryController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(1)) + .andExpect(MockMvcResultMatchers.model().attributeExists("categories")) + .andExpect(MockMvcResultMatchers.view().name("category/list")) + .andExpect(MockMvcResultMatchers.forwardedUrl("category/list")); + } + + @Test + void testFindAllCategory2() throws Exception { + when(this.categoryService.findCategories()).thenReturn(new ArrayList()); + MockHttpServletRequestBuilder getResult = MockMvcRequestBuilders.get("/category/list"); + getResult.contentType("Not all who wander are lost"); + MockMvcBuilders.standaloneSetup(this.categoryController) + .build() + .perform(getResult) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(1)) + .andExpect(MockMvcResultMatchers.model().attributeExists("categories")) + .andExpect(MockMvcResultMatchers.view().name("category/list")) + .andExpect(MockMvcResultMatchers.forwardedUrl("category/list")); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/controller/OrderControllerTest.java b/src/test/java/com/example/springmvc/mvcLayer/controller/OrderControllerTest.java new file mode 100644 index 0000000..5bb8dd2 --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/controller/OrderControllerTest.java @@ -0,0 +1,29 @@ +package com.example.springmvc.mvcLayer.controller; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; + +import com.example.springmvc.mvcLayer.component.ShoppingCart; +import com.example.springmvc.mvcLayer.repository.AddressRepository; +import com.example.springmvc.mvcLayer.repository.OrderRepository; +import com.example.springmvc.mvcLayer.repository.UserRepository; +import com.example.springmvc.mvcLayer.service.RoleService; +import com.example.springmvc.mvcLayer.service.impl.OrderServiceImpl; +import com.example.springmvc.mvcLayer.service.impl.security.UserServiceImpl; +import org.junit.jupiter.api.Test; +import org.springframework.security.crypto.argon2.Argon2PasswordEncoder; +import org.springframework.ui.ConcurrentModel; + +class OrderControllerTest { + @Test + void testCreateOrderView() { + OrderRepository orderRepository = mock(OrderRepository.class); + UserRepository userRepository = mock(UserRepository.class); + RoleService roleService = mock(RoleService.class); + OrderController orderController = new OrderController(new OrderServiceImpl(orderRepository, + new UserServiceImpl(userRepository, roleService, new Argon2PasswordEncoder()), mock(AddressRepository.class))); + ConcurrentModel model = new ConcurrentModel(); + assertEquals("order/newOrder", orderController.createOrderView(model, new ShoppingCart())); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/controller/ProductControllerTest.java b/src/test/java/com/example/springmvc/mvcLayer/controller/ProductControllerTest.java new file mode 100644 index 0000000..70dbd51 --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/controller/ProductControllerTest.java @@ -0,0 +1,240 @@ +package com.example.springmvc.mvcLayer.controller; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import com.example.springmvc.mvcLayer.domain.Category; +import com.example.springmvc.mvcLayer.domain.Product; +import com.example.springmvc.mvcLayer.domain.dto.ProductDto; +import com.example.springmvc.mvcLayer.service.CategoryService; +import com.example.springmvc.mvcLayer.service.ProductService; + +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.data.domain.PageImpl; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@ContextConfiguration(classes = {ProductController.class}) +@ExtendWith(SpringExtension.class) +class ProductControllerTest { + @MockBean + private CategoryService categoryService; + + @Autowired + private ProductController productController; + + @MockBean + private ProductService productService; + + @Test + void testHandleError() { + MockHttpServletRequest req = new MockHttpServletRequest(); + assertEquals("error", this.productController.handleError(req, new Exception("An error occurred"))); + } + + @Test + void testDeleteProductById() throws Exception { + doNothing().when(this.productService).deleteProductById((Integer) any()); + MockHttpServletRequestBuilder getResult = MockMvcRequestBuilders.get("/product/delete"); + MockHttpServletRequestBuilder requestBuilder = getResult.param("id", String.valueOf(1)); + MockMvcBuilders.standaloneSetup(this.productController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isFound()) + .andExpect(MockMvcResultMatchers.model().size(0)) + .andExpect(MockMvcResultMatchers.redirectedUrl("/product/list")); + } + + @Test + void testDeleteProductById2() throws Exception { + doNothing().when(this.productService).deleteProductById((Integer) any()); + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/product/delete") + .param("id", "https://example.org/example"); + MockMvcBuilders.standaloneSetup(this.productController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(0)) + .andExpect(MockMvcResultMatchers.view().name("error")) + .andExpect(MockMvcResultMatchers.forwardedUrl("error")); + } + + @Test + void testFilterProductsByTitleAndByMaxAndMinPrice() throws Exception { + doNothing().when(this.productService) + .pagination((com.example.springmvc.mvcLayer.domain.search.ProductSearchCondition) any(), + (org.springframework.ui.Model) any(), (org.springframework.data.domain.Page) any()); + when(this.productService.findProductsByTitleAndByMaxAndMinPriceBySearchConditional( + (com.example.springmvc.mvcLayer.domain.search.ProductSearchCondition) any(), (String) any(), (Integer) any(), + (Integer) any())).thenReturn(new PageImpl(new ArrayList())); + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/product/filter"); + MockMvcBuilders.standaloneSetup(this.productController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(1)) + .andExpect(MockMvcResultMatchers.model().attributeExists("productSearchCondition")) + .andExpect(MockMvcResultMatchers.view().name("product/list")) + .andExpect(MockMvcResultMatchers.forwardedUrl("product/list")); + } + + @Test + void testFilterProductsByTitleAndByMaxAndMinPrice2() throws Exception { + doNothing().when(this.productService) + .pagination((com.example.springmvc.mvcLayer.domain.search.ProductSearchCondition) any(), + (org.springframework.ui.Model) any(), (org.springframework.data.domain.Page) any()); + when(this.productService.findProductsByTitleAndByMaxAndMinPriceBySearchConditional( + (com.example.springmvc.mvcLayer.domain.search.ProductSearchCondition) any(), (String) any(), (Integer) any(), + (Integer) any())).thenReturn(new PageImpl(new ArrayList())); + MockHttpServletRequestBuilder getResult = MockMvcRequestBuilders.get("/product/filter"); + getResult.contentType("Not all who wander are lost"); + MockMvcBuilders.standaloneSetup(this.productController) + .build() + .perform(getResult) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(1)) + .andExpect(MockMvcResultMatchers.model().attributeExists("productSearchCondition")) + .andExpect(MockMvcResultMatchers.view().name("product/list")) + .andExpect(MockMvcResultMatchers.forwardedUrl("product/list")); + } + + @Test + void testGetListProducts() throws Exception { + doNothing().when(this.productService) + .pagination((com.example.springmvc.mvcLayer.domain.search.ProductSearchCondition) any(), + (org.springframework.ui.Model) any(), (org.springframework.data.domain.Page) any()); + when(this.productService + .findAllBySearchConditional((com.example.springmvc.mvcLayer.domain.search.ProductSearchCondition) any())) + .thenReturn(new PageImpl(new ArrayList())); + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/product/list"); + MockMvcBuilders.standaloneSetup(this.productController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(1)) + .andExpect(MockMvcResultMatchers.model().attributeExists("productSearchCondition")) + .andExpect(MockMvcResultMatchers.view().name("product/list")) + .andExpect(MockMvcResultMatchers.forwardedUrl("product/list")); + } + + @Test + void testGetListProducts2() throws Exception { + doNothing().when(this.productService) + .pagination((com.example.springmvc.mvcLayer.domain.search.ProductSearchCondition) any(), + (org.springframework.ui.Model) any(), (org.springframework.data.domain.Page) any()); + when(this.productService + .findAllBySearchConditional((com.example.springmvc.mvcLayer.domain.search.ProductSearchCondition) any())) + .thenReturn(new PageImpl(new ArrayList())); + MockHttpServletRequestBuilder getResult = MockMvcRequestBuilders.get("/product/list"); + getResult.contentType("Not all who wander are lost"); + MockMvcBuilders.standaloneSetup(this.productController) + .build() + .perform(getResult) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(1)) + .andExpect(MockMvcResultMatchers.model().attributeExists("productSearchCondition")) + .andExpect(MockMvcResultMatchers.view().name("product/list")) + .andExpect(MockMvcResultMatchers.forwardedUrl("product/list")); + } + + @Test + void testGetProductForm() throws Exception { + when(this.categoryService.findCategories()).thenReturn(new ArrayList()); + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/product/form"); + MockMvcBuilders.standaloneSetup(this.productController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(3)) + .andExpect(MockMvcResultMatchers.model().attributeExists("categories", "error", "product")) + .andExpect(MockMvcResultMatchers.view().name("product/form")) + .andExpect(MockMvcResultMatchers.forwardedUrl("product/form")); + } + + @Test + void testGetProductForm2() throws Exception { + when(this.productService.findProductDtoById((Integer) any())).thenReturn(new ProductDto()); + when(this.categoryService.findCategories()).thenReturn(new ArrayList()); + MockHttpServletRequestBuilder getResult = MockMvcRequestBuilders.get("/product/form"); + MockHttpServletRequestBuilder requestBuilder = getResult.param("id", String.valueOf(1)); + MockMvcBuilders.standaloneSetup(this.productController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(3)) + .andExpect(MockMvcResultMatchers.model().attributeExists("categories", "error", "product")) + .andExpect(MockMvcResultMatchers.view().name("product/form")) + .andExpect(MockMvcResultMatchers.forwardedUrl("product/form")); + } + + @Test + void testGetProductForm3() throws Exception { + when(this.productService.findProductDtoById((Integer) any())).thenReturn(new ProductDto()); + when(this.categoryService.findCategories()).thenReturn(new ArrayList()); + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/product/form") + .param("id", "https://example.org/example"); + MockMvcBuilders.standaloneSetup(this.productController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(0)) + .andExpect(MockMvcResultMatchers.view().name("error")) + .andExpect(MockMvcResultMatchers.forwardedUrl("error")); + } + + @Test + void testGetProductsByCategoryId() throws Exception { + doNothing().when(this.productService) + .pagination((com.example.springmvc.mvcLayer.domain.search.ProductSearchCondition) any(), + (org.springframework.ui.Model) any(), (org.springframework.data.domain.Page) any()); + when(this.productService.findProductsByCategoryId((Integer) any(), + (com.example.springmvc.mvcLayer.domain.search.ProductSearchCondition) any())) + .thenReturn(new PageImpl(new ArrayList())); + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/product/list/{catId}", 123); + MockMvcBuilders.standaloneSetup(this.productController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(0)) + .andExpect(MockMvcResultMatchers.view().name("product/list")) + .andExpect(MockMvcResultMatchers.forwardedUrl("product/list")); + } + + @Test + void testSaveProduct() throws Exception { + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/product/form"); + MockMvcBuilders.standaloneSetup(this.productController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(0)) + .andExpect(MockMvcResultMatchers.view().name("error")) + .andExpect(MockMvcResultMatchers.forwardedUrl("error")); + } + + @Test + void testSaveProduct2() throws Exception { + MockHttpServletRequestBuilder postResult = MockMvcRequestBuilders.post("/product/form"); + postResult.contentType("Not all who wander are lost"); + MockMvcBuilders.standaloneSetup(this.productController) + .build() + .perform(postResult) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(0)) + .andExpect(MockMvcResultMatchers.view().name("error")) + .andExpect(MockMvcResultMatchers.forwardedUrl("error")); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/controller/ProductReviewControllerTest.java b/src/test/java/com/example/springmvc/mvcLayer/controller/ProductReviewControllerTest.java new file mode 100644 index 0000000..e0f137c --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/controller/ProductReviewControllerTest.java @@ -0,0 +1,117 @@ +package com.example.springmvc.mvcLayer.controller; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.example.springmvc.mvcLayer.domain.Category; +import com.example.springmvc.mvcLayer.domain.Product; +import com.example.springmvc.mvcLayer.domain.ProductReview; +import com.example.springmvc.mvcLayer.repository.CategoryRepository; +import com.example.springmvc.mvcLayer.repository.ProductRepository; +import com.example.springmvc.mvcLayer.repository.ReviewRepository; +import com.example.springmvc.mvcLayer.repository.UserRepository; +import com.example.springmvc.mvcLayer.service.FileService; +import com.example.springmvc.mvcLayer.service.ProductService; +import com.example.springmvc.mvcLayer.service.ReviewService; +import com.example.springmvc.mvcLayer.service.RoleService; +import com.example.springmvc.mvcLayer.service.impl.CategoryServiceImpl; +import com.example.springmvc.mvcLayer.service.impl.ProductServiceImpl; +import com.example.springmvc.mvcLayer.service.impl.ReviewServiceImpl; +import com.example.springmvc.mvcLayer.service.impl.security.UserServiceImpl; + +import java.util.ArrayList; + +import java.util.HashSet; +import java.util.Optional; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.security.crypto.argon2.Argon2PasswordEncoder; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.ui.ConcurrentModel; + +@ContextConfiguration(classes = {ProductReviewController.class}) +@ExtendWith(SpringExtension.class) +class ProductReviewControllerTest { + @Autowired + private ProductReviewController productReviewController; + + @MockBean + private ProductService productService; + + @MockBean + private ReviewService reviewService; + + @Test + void testCreateViewForReviewProduct() { + Product product = new Product(); + product.setCountProduct(3); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + ProductRepository productRepository = mock(ProductRepository.class); + when(productRepository.findById((Integer) any())).thenReturn(Optional.of(product)); + CategoryServiceImpl categoryService = new CategoryServiceImpl(mock(CategoryRepository.class)); + FileService fileService = mock(FileService.class); + ReviewRepository reviewRepository = mock(ReviewRepository.class); + ProductServiceImpl productService = new ProductServiceImpl(productRepository, categoryService, fileService, + new ReviewServiceImpl(reviewRepository, + new UserServiceImpl(mock(UserRepository.class), mock(RoleService.class), null))); + + ReviewRepository reviewRepository1 = mock(ReviewRepository.class); + UserRepository userRepository = mock(UserRepository.class); + RoleService roleService = mock(RoleService.class); + ProductReviewController productReviewController = new ProductReviewController( + new ReviewServiceImpl(reviewRepository1, + new UserServiceImpl(userRepository, roleService, new Argon2PasswordEncoder())), + productService); + ConcurrentModel concurrentModel = new ConcurrentModel(); + assertEquals("/review/form", productReviewController.createViewForReviewProduct(concurrentModel, 1)); + verify(productRepository).findById((Integer) any()); + assertSame(product, ((ProductReview) concurrentModel.get("productReview")).getProduct()); + } + + @Test + void testDeleteReview() throws Exception { + doNothing().when(this.reviewService).deleteReview((Integer) any()); + MockHttpServletRequestBuilder getResult = MockMvcRequestBuilders.get("/review/delete"); + MockHttpServletRequestBuilder requestBuilder = getResult.param("id", String.valueOf(1)); + MockMvcBuilders.standaloneSetup(this.productReviewController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(0)) + .andExpect(MockMvcResultMatchers.view().name("review/list")) + .andExpect(MockMvcResultMatchers.forwardedUrl("review/list")); + } + + @Test + void testGetReviewByProduct() throws Exception { + when(this.reviewService.findReviewByProductId((Integer) any())).thenReturn(new ArrayList()); + MockHttpServletRequestBuilder getResult = MockMvcRequestBuilders.get("/review"); + MockHttpServletRequestBuilder requestBuilder = getResult.param("id", String.valueOf(1)); + MockMvcBuilders.standaloneSetup(this.productReviewController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(1)) + .andExpect(MockMvcResultMatchers.model().attributeExists("reviewList")) + .andExpect(MockMvcResultMatchers.view().name("review/list")) + .andExpect(MockMvcResultMatchers.forwardedUrl("review/list")); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/controller/ShoppingCartControllerTest.java b/src/test/java/com/example/springmvc/mvcLayer/controller/ShoppingCartControllerTest.java new file mode 100644 index 0000000..60bacfb --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/controller/ShoppingCartControllerTest.java @@ -0,0 +1,392 @@ +package com.example.springmvc.mvcLayer.controller; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.example.springmvc.mvcLayer.component.ShoppingCart; +import com.example.springmvc.mvcLayer.domain.Category; +import com.example.springmvc.mvcLayer.domain.Product; +import com.example.springmvc.mvcLayer.domain.cart.CartItem; +import com.example.springmvc.mvcLayer.domain.dto.ProductDto; +import com.example.springmvc.mvcLayer.repository.CategoryRepository; +import com.example.springmvc.mvcLayer.repository.ProductRepository; +import com.example.springmvc.mvcLayer.repository.ReviewRepository; +import com.example.springmvc.mvcLayer.repository.UserRepository; +import com.example.springmvc.mvcLayer.service.FileService; +import com.example.springmvc.mvcLayer.service.ProductService; +import com.example.springmvc.mvcLayer.service.RoleService; +import com.example.springmvc.mvcLayer.service.impl.CategoryServiceImpl; +import com.example.springmvc.mvcLayer.service.impl.ProductServiceImpl; +import com.example.springmvc.mvcLayer.service.impl.ReviewServiceImpl; +import com.example.springmvc.mvcLayer.service.impl.security.UserServiceImpl; + +import java.util.HashSet; +import java.util.Optional; + +import org.junit.jupiter.api.Test; +import org.springframework.web.servlet.view.RedirectView; + +class ShoppingCartControllerTest { + @Test + void testGetCartList() { + ProductRepository productRepository = mock(ProductRepository.class); + CategoryServiceImpl categoryService = new CategoryServiceImpl(mock(CategoryRepository.class)); + FileService fileService = mock(FileService.class); + ReviewRepository reviewRepository = mock(ReviewRepository.class); + ShoppingCartController shoppingCartController = new ShoppingCartController( + new ProductServiceImpl(productRepository, categoryService, fileService, new ReviewServiceImpl(reviewRepository, + new UserServiceImpl(mock(UserRepository.class), mock(RoleService.class), null)))); + assertEquals("cart/list", shoppingCartController.getCartList(new ShoppingCart())); + } + + @Test + void testAddProductToCart() { + Product product = new Product(); + product.setCountProduct(3); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + Optional ofResult = Optional.of(product); + ProductRepository productRepository = mock(ProductRepository.class); + doNothing().when(productRepository).updateCount((Integer) any(), (Integer) any()); + when(productRepository.findById((Integer) any())).thenReturn(ofResult); + CategoryServiceImpl categoryService = new CategoryServiceImpl(mock(CategoryRepository.class)); + FileService fileService = mock(FileService.class); + ReviewRepository reviewRepository = mock(ReviewRepository.class); + ShoppingCartController shoppingCartController = new ShoppingCartController( + new ProductServiceImpl(productRepository, categoryService, fileService, new ReviewServiceImpl(reviewRepository, + new UserServiceImpl(mock(UserRepository.class), mock(RoleService.class), null)))); + ShoppingCart shoppingCart = new ShoppingCart(); + RedirectView actualAddProductToCartResult = shoppingCartController.addProductToCart(1, shoppingCart); + assertFalse(actualAddProductToCartResult.isPropagateQueryProperties()); + assertFalse(actualAddProductToCartResult.isExposePathVariables()); + assertEquals("/product/list", actualAddProductToCartResult.getUrl()); + assertEquals("text/html;charset=ISO-8859-1", actualAddProductToCartResult.getContentType()); + assertTrue(actualAddProductToCartResult.getAttributesMap().isEmpty()); + verify(productRepository).findById((Integer) any()); + verify(productRepository).updateCount((Integer) any(), (Integer) any()); + assertEquals(1, shoppingCart.getTotalPrice().intValue()); + assertEquals(1, shoppingCart.getCount()); + CartItem getResult = shoppingCart.getCartItems().get(1); + assertEquals(1, getResult.getCount().intValue()); + ProductDto product1 = getResult.getProduct(); + assertTrue(product1.getCategoryDto().isEmpty()); + assertEquals("Dr", product1.getTitle()); + assertEquals(1, product1.getPrice().intValue()); + assertEquals(1, product1.getId().intValue()); + assertEquals(3, product1.getCountProduct().intValue()); + } + + @Test + void testAddProductToCart2() { + Product product = new Product(); + product.setCountProduct(0); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + Optional ofResult = Optional.of(product); + ProductRepository productRepository = mock(ProductRepository.class); + doNothing().when(productRepository).updateCount((Integer) any(), (Integer) any()); + when(productRepository.findById((Integer) any())).thenReturn(ofResult); + CategoryServiceImpl categoryService = new CategoryServiceImpl(mock(CategoryRepository.class)); + FileService fileService = mock(FileService.class); + ReviewRepository reviewRepository = mock(ReviewRepository.class); + ShoppingCartController shoppingCartController = new ShoppingCartController( + new ProductServiceImpl(productRepository, categoryService, fileService, new ReviewServiceImpl(reviewRepository, + new UserServiceImpl(mock(UserRepository.class), mock(RoleService.class), null)))); + RedirectView actualAddProductToCartResult = shoppingCartController.addProductToCart(1, new ShoppingCart()); + assertFalse(actualAddProductToCartResult.isPropagateQueryProperties()); + assertFalse(actualAddProductToCartResult.isExposePathVariables()); + assertEquals("/product/list", actualAddProductToCartResult.getUrl()); + assertEquals("text/html;charset=ISO-8859-1", actualAddProductToCartResult.getContentType()); + assertTrue(actualAddProductToCartResult.getAttributesMap().isEmpty()); + verify(productRepository).findById((Integer) any()); + } + + @Test + void testAddProductToCart3() { + ProductDto productDto = new ProductDto(); + productDto.setCountProduct(0); + ProductService productService = mock(ProductService.class); + when(productService.findProductDtoById((Integer) any())).thenReturn(productDto); + ShoppingCartController shoppingCartController = new ShoppingCartController(productService); + RedirectView actualAddProductToCartResult = shoppingCartController.addProductToCart(1, new ShoppingCart()); + assertFalse(actualAddProductToCartResult.isPropagateQueryProperties()); + assertFalse(actualAddProductToCartResult.isExposePathVariables()); + assertEquals("/product/list", actualAddProductToCartResult.getUrl()); + assertEquals("text/html;charset=ISO-8859-1", actualAddProductToCartResult.getContentType()); + assertTrue(actualAddProductToCartResult.getAttributesMap().isEmpty()); + verify(productService).findProductDtoById((Integer) any()); + } + + @Test + void testDeleteProductFromCart() { + ProductRepository productRepository = mock(ProductRepository.class); + CategoryServiceImpl categoryService = new CategoryServiceImpl(mock(CategoryRepository.class)); + FileService fileService = mock(FileService.class); + ReviewRepository reviewRepository = mock(ReviewRepository.class); + ShoppingCartController shoppingCartController = new ShoppingCartController( + new ProductServiceImpl(productRepository, categoryService, fileService, new ReviewServiceImpl(reviewRepository, + new UserServiceImpl(mock(UserRepository.class), mock(RoleService.class), null)))); + RedirectView actualDeleteProductFromCartResult = shoppingCartController.deleteProductFromCart(1, + new ShoppingCart()); + assertFalse(actualDeleteProductFromCartResult.isPropagateQueryProperties()); + assertFalse(actualDeleteProductFromCartResult.isExposePathVariables()); + assertEquals("/product/list", actualDeleteProductFromCartResult.getUrl()); + assertEquals("text/html;charset=ISO-8859-1", actualDeleteProductFromCartResult.getContentType()); + assertTrue(actualDeleteProductFromCartResult.getAttributesMap().isEmpty()); + } + + @Test + void testDeleteProductFromCart2() { + Product product = new Product(); + product.setCountProduct(3); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + Optional ofResult = Optional.of(product); + ProductRepository productRepository = mock(ProductRepository.class); + doNothing().when(productRepository).updateCount((Integer) any(), (Integer) any()); + when(productRepository.findById((Integer) any())).thenReturn(ofResult); + CategoryServiceImpl categoryService = new CategoryServiceImpl(mock(CategoryRepository.class)); + FileService fileService = mock(FileService.class); + ReviewRepository reviewRepository = mock(ReviewRepository.class); + ShoppingCartController shoppingCartController = new ShoppingCartController( + new ProductServiceImpl(productRepository, categoryService, fileService, new ReviewServiceImpl(reviewRepository, + new UserServiceImpl(mock(UserRepository.class), mock(RoleService.class), null)))); + ShoppingCart shoppingCart = mock(ShoppingCart.class); + when(shoppingCart.deleteCartItem((Integer) any())).thenReturn(true); + RedirectView actualDeleteProductFromCartResult = shoppingCartController.deleteProductFromCart(1, shoppingCart); + assertFalse(actualDeleteProductFromCartResult.isPropagateQueryProperties()); + assertFalse(actualDeleteProductFromCartResult.isExposePathVariables()); + assertEquals("/product/list", actualDeleteProductFromCartResult.getUrl()); + assertEquals("text/html;charset=ISO-8859-1", actualDeleteProductFromCartResult.getContentType()); + assertTrue(actualDeleteProductFromCartResult.getAttributesMap().isEmpty()); + verify(productRepository).findById((Integer) any()); + verify(productRepository).updateCount((Integer) any(), (Integer) any()); + verify(shoppingCart).deleteCartItem((Integer) any()); + } + + @Test + void testDeleteProductFromCart3() { + ProductDto productDto = new ProductDto(); + productDto.setCountProduct(3); + ProductService productService = mock(ProductService.class); + doNothing().when(productService).updateCountInProduct((Integer) any(), (Integer) any()); + when(productService.findProductDtoById((Integer) any())).thenReturn(productDto); + ShoppingCartController shoppingCartController = new ShoppingCartController(productService); + ShoppingCart shoppingCart = mock(ShoppingCart.class); + when(shoppingCart.deleteCartItem((Integer) any())).thenReturn(true); + RedirectView actualDeleteProductFromCartResult = shoppingCartController.deleteProductFromCart(1, shoppingCart); + assertFalse(actualDeleteProductFromCartResult.isPropagateQueryProperties()); + assertFalse(actualDeleteProductFromCartResult.isExposePathVariables()); + assertEquals("/product/list", actualDeleteProductFromCartResult.getUrl()); + assertEquals("text/html;charset=ISO-8859-1", actualDeleteProductFromCartResult.getContentType()); + assertTrue(actualDeleteProductFromCartResult.getAttributesMap().isEmpty()); + verify(productService).findProductDtoById((Integer) any()); + verify(productService).updateCountInProduct((Integer) any(), (Integer) any()); + verify(shoppingCart).deleteCartItem((Integer) any()); + } + + @Test + void testDeleteProductFromCart4() { + ProductDto productDto = new ProductDto(123, "Dr", 1, new HashSet(), 3); + productDto.setCountProduct(3); + ProductService productService = mock(ProductService.class); + doNothing().when(productService).updateCountInProduct((Integer) any(), (Integer) any()); + when(productService.findProductDtoById((Integer) any())).thenReturn(productDto); + ShoppingCartController shoppingCartController = new ShoppingCartController(productService); + ShoppingCart shoppingCart = mock(ShoppingCart.class); + when(shoppingCart.deleteCartItem((Integer) any())).thenReturn(true); + RedirectView actualDeleteProductFromCartResult = shoppingCartController.deleteProductFromCart(1, shoppingCart); + assertFalse(actualDeleteProductFromCartResult.isPropagateQueryProperties()); + assertFalse(actualDeleteProductFromCartResult.isExposePathVariables()); + assertEquals("/product/list", actualDeleteProductFromCartResult.getUrl()); + assertEquals("text/html;charset=ISO-8859-1", actualDeleteProductFromCartResult.getContentType()); + assertTrue(actualDeleteProductFromCartResult.getAttributesMap().isEmpty()); + verify(productService).findProductDtoById((Integer) any()); + verify(productService).updateCountInProduct((Integer) any(), (Integer) any()); + verify(shoppingCart).deleteCartItem((Integer) any()); + } + + @Test + void testAddSameItem() { + Product product = new Product(); + product.setCountProduct(3); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + Optional ofResult = Optional.of(product); + ProductRepository productRepository = mock(ProductRepository.class); + doNothing().when(productRepository).updateCount((Integer) any(), (Integer) any()); + when(productRepository.findById((Integer) any())).thenReturn(ofResult); + CategoryServiceImpl categoryService = new CategoryServiceImpl(mock(CategoryRepository.class)); + FileService fileService = mock(FileService.class); + ReviewRepository reviewRepository = mock(ReviewRepository.class); + ShoppingCartController shoppingCartController = new ShoppingCartController( + new ProductServiceImpl(productRepository, categoryService, fileService, new ReviewServiceImpl(reviewRepository, + new UserServiceImpl(mock(UserRepository.class), mock(RoleService.class), null)))); + RedirectView actualAddSameItemResult = shoppingCartController.addSameItem(1, new ShoppingCart()); + assertFalse(actualAddSameItemResult.isPropagateQueryProperties()); + assertFalse(actualAddSameItemResult.isExposePathVariables()); + assertEquals("/cart", actualAddSameItemResult.getUrl()); + assertEquals("text/html;charset=ISO-8859-1", actualAddSameItemResult.getContentType()); + assertTrue(actualAddSameItemResult.getAttributesMap().isEmpty()); + verify(productRepository).findById((Integer) any()); + verify(productRepository).updateCount((Integer) any(), (Integer) any()); + } + + @Test + void testAddSameItem2() { + Product product = new Product(); + product.setCountProduct(0); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + Optional ofResult = Optional.of(product); + ProductRepository productRepository = mock(ProductRepository.class); + doNothing().when(productRepository).updateCount((Integer) any(), (Integer) any()); + when(productRepository.findById((Integer) any())).thenReturn(ofResult); + CategoryServiceImpl categoryService = new CategoryServiceImpl(mock(CategoryRepository.class)); + FileService fileService = mock(FileService.class); + ReviewRepository reviewRepository = mock(ReviewRepository.class); + ShoppingCartController shoppingCartController = new ShoppingCartController( + new ProductServiceImpl(productRepository, categoryService, fileService, new ReviewServiceImpl(reviewRepository, + new UserServiceImpl(mock(UserRepository.class), mock(RoleService.class), null)))); + RedirectView actualAddSameItemResult = shoppingCartController.addSameItem(1, new ShoppingCart()); + assertFalse(actualAddSameItemResult.isPropagateQueryProperties()); + assertFalse(actualAddSameItemResult.isExposePathVariables()); + assertEquals("/cart", actualAddSameItemResult.getUrl()); + assertEquals("text/html;charset=ISO-8859-1", actualAddSameItemResult.getContentType()); + assertTrue(actualAddSameItemResult.getAttributesMap().isEmpty()); + verify(productRepository).findById((Integer) any()); + } + + @Test + void testAddSameItem3() { + ProductDto productDto = new ProductDto(); + productDto.setCountProduct(3); + ProductService productService = mock(ProductService.class); + doNothing().when(productService).updateCountInProduct((Integer) any(), (Integer) any()); + when(productService.findProductDtoById((Integer) any())).thenReturn(productDto); + ShoppingCartController shoppingCartController = new ShoppingCartController(productService); + RedirectView actualAddSameItemResult = shoppingCartController.addSameItem(1, new ShoppingCart()); + assertFalse(actualAddSameItemResult.isPropagateQueryProperties()); + assertFalse(actualAddSameItemResult.isExposePathVariables()); + assertEquals("/cart", actualAddSameItemResult.getUrl()); + assertEquals("text/html;charset=ISO-8859-1", actualAddSameItemResult.getContentType()); + assertTrue(actualAddSameItemResult.getAttributesMap().isEmpty()); + verify(productService).findProductDtoById((Integer) any()); + verify(productService).updateCountInProduct((Integer) any(), (Integer) any()); + } + + @Test + void testAddSameItem4() { + ProductDto productDto = new ProductDto(); + productDto.setCountProduct(3); + ProductService productService = mock(ProductService.class); + doNothing().when(productService).updateCountInProduct((Integer) any(), (Integer) any()); + when(productService.findProductDtoById((Integer) any())).thenReturn(productDto); + ShoppingCartController shoppingCartController = new ShoppingCartController(productService); + ShoppingCart shoppingCart = mock(ShoppingCart.class); + doNothing().when(shoppingCart).addSameItem((Integer) any()); + RedirectView actualAddSameItemResult = shoppingCartController.addSameItem(1, shoppingCart); + assertFalse(actualAddSameItemResult.isPropagateQueryProperties()); + assertFalse(actualAddSameItemResult.isExposePathVariables()); + assertEquals("/cart", actualAddSameItemResult.getUrl()); + assertEquals("text/html;charset=ISO-8859-1", actualAddSameItemResult.getContentType()); + assertTrue(actualAddSameItemResult.getAttributesMap().isEmpty()); + verify(productService).findProductDtoById((Integer) any()); + verify(productService).updateCountInProduct((Integer) any(), (Integer) any()); + verify(shoppingCart).addSameItem((Integer) any()); + } + + @Test + void testDeleteSameItem() { + Product product = new Product(); + product.setCountProduct(3); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + Optional ofResult = Optional.of(product); + ProductRepository productRepository = mock(ProductRepository.class); + doNothing().when(productRepository).updateCount((Integer) any(), (Integer) any()); + when(productRepository.findById((Integer) any())).thenReturn(ofResult); + CategoryServiceImpl categoryService = new CategoryServiceImpl(mock(CategoryRepository.class)); + FileService fileService = mock(FileService.class); + ReviewRepository reviewRepository = mock(ReviewRepository.class); + ShoppingCartController shoppingCartController = new ShoppingCartController( + new ProductServiceImpl(productRepository, categoryService, fileService, new ReviewServiceImpl(reviewRepository, + new UserServiceImpl(mock(UserRepository.class), mock(RoleService.class), null)))); + ShoppingCart shoppingCart = new ShoppingCart(); + RedirectView actualDeleteSameItemResult = shoppingCartController.deleteSameItem(1, shoppingCart); + assertFalse(actualDeleteSameItemResult.isPropagateQueryProperties()); + assertFalse(actualDeleteSameItemResult.isExposePathVariables()); + assertEquals("/cart", actualDeleteSameItemResult.getUrl()); + assertEquals("text/html;charset=ISO-8859-1", actualDeleteSameItemResult.getContentType()); + assertTrue(actualDeleteSameItemResult.getAttributesMap().isEmpty()); + verify(productRepository).findById((Integer) any()); + verify(productRepository).updateCount((Integer) any(), (Integer) any()); + assertEquals(0, shoppingCart.getTotalPrice().intValue()); + } + + @Test + void testDeleteSameItem2() { + ProductDto productDto = new ProductDto(); + productDto.setCountProduct(3); + ProductService productService = mock(ProductService.class); + doNothing().when(productService).updateCountInProduct((Integer) any(), (Integer) any()); + when(productService.findProductDtoById((Integer) any())).thenReturn(productDto); + ShoppingCartController shoppingCartController = new ShoppingCartController(productService); + ShoppingCart shoppingCart = new ShoppingCart(); + RedirectView actualDeleteSameItemResult = shoppingCartController.deleteSameItem(1, shoppingCart); + assertFalse(actualDeleteSameItemResult.isPropagateQueryProperties()); + assertFalse(actualDeleteSameItemResult.isExposePathVariables()); + assertEquals("/cart", actualDeleteSameItemResult.getUrl()); + assertEquals("text/html;charset=ISO-8859-1", actualDeleteSameItemResult.getContentType()); + assertTrue(actualDeleteSameItemResult.getAttributesMap().isEmpty()); + verify(productService).findProductDtoById((Integer) any()); + verify(productService).updateCountInProduct((Integer) any(), (Integer) any()); + assertEquals(0, shoppingCart.getTotalPrice().intValue()); + } + + @Test + void testDeleteSameItem3() { + ProductDto productDto = new ProductDto(); + productDto.setCountProduct(3); + ProductService productService = mock(ProductService.class); + doNothing().when(productService).updateCountInProduct((Integer) any(), (Integer) any()); + when(productService.findProductDtoById((Integer) any())).thenReturn(productDto); + ShoppingCartController shoppingCartController = new ShoppingCartController(productService); + ShoppingCart shoppingCart = mock(ShoppingCart.class); + doNothing().when(shoppingCart).deleteSameItem((Integer) any()); + RedirectView actualDeleteSameItemResult = shoppingCartController.deleteSameItem(1, shoppingCart); + assertFalse(actualDeleteSameItemResult.isPropagateQueryProperties()); + assertFalse(actualDeleteSameItemResult.isExposePathVariables()); + assertEquals("/cart", actualDeleteSameItemResult.getUrl()); + assertEquals("text/html;charset=ISO-8859-1", actualDeleteSameItemResult.getContentType()); + assertTrue(actualDeleteSameItemResult.getAttributesMap().isEmpty()); + verify(productService).findProductDtoById((Integer) any()); + verify(productService).updateCountInProduct((Integer) any(), (Integer) any()); + verify(shoppingCart).deleteSameItem((Integer) any()); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/controller/UserControllerTest.java b/src/test/java/com/example/springmvc/mvcLayer/controller/UserControllerTest.java new file mode 100644 index 0000000..c2d10c4 --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/controller/UserControllerTest.java @@ -0,0 +1,127 @@ +package com.example.springmvc.mvcLayer.controller; + +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import com.example.springmvc.mvcLayer.domain.security.Role; +import com.example.springmvc.mvcLayer.domain.security.User; +import com.example.springmvc.mvcLayer.service.UserService; + +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.data.domain.PageImpl; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@ContextConfiguration(classes = {UserController.class}) +@ExtendWith(SpringExtension.class) +class UserControllerTest { + @Autowired + private UserController userController; + + @MockBean + private UserService userService; + + @Test + void testCreateModelRegistration() throws Exception { + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/user/registration"); + MockMvcBuilders.standaloneSetup(this.userController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(1)) + .andExpect(MockMvcResultMatchers.model().attributeExists("user")) + .andExpect(MockMvcResultMatchers.view().name("user/registration")) + .andExpect(MockMvcResultMatchers.forwardedUrl("user/registration")); + } + + @Test + void testCreateModelRegistration2() throws Exception { + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/user/registration", "Uri Vars"); + MockMvcBuilders.standaloneSetup(this.userController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(1)) + .andExpect(MockMvcResultMatchers.model().attributeExists("user")) + .andExpect(MockMvcResultMatchers.view().name("user/registration")) + .andExpect(MockMvcResultMatchers.forwardedUrl("user/registration")); + } + + @Test + void testGetAllUsers() throws Exception { + when(this.userService.findAllByPage((org.springframework.data.domain.Pageable) any())) + .thenReturn(new PageImpl(new ArrayList())); + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/user/admin"); + MockMvcBuilders.standaloneSetup(this.userController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(1)) + .andExpect(MockMvcResultMatchers.model().attributeExists("page")) + .andExpect(MockMvcResultMatchers.view().name("user/admin")) + .andExpect(MockMvcResultMatchers.forwardedUrl("user/admin")); + } + + @Test + void testGetAllUsers2() throws Exception { + when(this.userService.findAllByPage((org.springframework.data.domain.Pageable) any())) + .thenReturn(new PageImpl(new ArrayList())); + MockHttpServletRequestBuilder getResult = MockMvcRequestBuilders.get("/user/admin"); + MockHttpServletRequestBuilder requestBuilder = getResult.param("pageNum", String.valueOf(1)); + MockMvcBuilders.standaloneSetup(this.userController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.model().size(1)) + .andExpect(MockMvcResultMatchers.model().attributeExists("page")) + .andExpect(MockMvcResultMatchers.view().name("user/admin")) + .andExpect(MockMvcResultMatchers.forwardedUrl("user/admin")); + } + + @Test + void testRegistrationUser() throws Exception { + User user = new User(); + user.setPassword("iloveyou"); + user.setEmail("jane.doe@example.org"); + user.setUsername("janedoe"); + user.setId(123L); + user.setEnabled(true); + user.setRoles(new ArrayList()); + when(this.userService.saveUser((User) any())).thenReturn(user); + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/user/registration"); + MockMvcBuilders.standaloneSetup(this.userController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isFound()) + .andExpect(MockMvcResultMatchers.model().size(1)) + .andExpect(MockMvcResultMatchers.model().attributeExists("user")) + .andExpect(MockMvcResultMatchers.view().name("redirect:/login")) + .andExpect(MockMvcResultMatchers.redirectedUrl("/login")); + } + + @Test + void testSetEnableUser() throws Exception { + doNothing().when(this.userService).setEnable((Long) any(), (Boolean) any()); + MockHttpServletRequestBuilder getResult = MockMvcRequestBuilders.get("/user/enable"); + MockHttpServletRequestBuilder paramResult = getResult.param("enable", String.valueOf(true)); + MockHttpServletRequestBuilder requestBuilder = paramResult.param("userId", String.valueOf(1L)); + MockMvcBuilders.standaloneSetup(this.userController) + .build() + .perform(requestBuilder) + .andExpect(MockMvcResultMatchers.status().isFound()) + .andExpect(MockMvcResultMatchers.model().size(0)) + .andExpect(MockMvcResultMatchers.view().name("redirect:/user/admin")) + .andExpect(MockMvcResultMatchers.redirectedUrl("/user/admin")); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/domain/cart/CartItemTest.java b/src/test/java/com/example/springmvc/mvcLayer/domain/cart/CartItemTest.java new file mode 100644 index 0000000..d780862 --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/domain/cart/CartItemTest.java @@ -0,0 +1,32 @@ +package com.example.springmvc.mvcLayer.domain.cart; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; + +import com.example.springmvc.mvcLayer.domain.dto.ProductDto; +import org.junit.jupiter.api.Test; + +class CartItemTest { + @Test + void testConstructor() { + ProductDto productDto = new ProductDto(); + CartItem actualCartItem = new CartItem(productDto); + assertEquals(1, actualCartItem.getCount().intValue()); + assertSame(productDto, actualCartItem.getProduct()); + } + + @Test + void testIncreaseCount() { + CartItem cartItem = new CartItem(new ProductDto()); + cartItem.increaseCount(); + assertEquals(2, cartItem.getCount().intValue()); + } + + @Test + void testReduceCount() { + CartItem cartItem = new CartItem(new ProductDto()); + cartItem.reduceCount(); + assertEquals(0, cartItem.getCount().intValue()); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/service/impl/CategoryServiceImplTest.java b/src/test/java/com/example/springmvc/mvcLayer/service/impl/CategoryServiceImplTest.java new file mode 100644 index 0000000..dc4cc63 --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/service/impl/CategoryServiceImplTest.java @@ -0,0 +1,283 @@ +package com.example.springmvc.mvcLayer.service.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.example.springmvc.mvcLayer.domain.Category; +import com.example.springmvc.mvcLayer.domain.Product; +import com.example.springmvc.mvcLayer.repository.CategoryRepository; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ContextConfiguration(classes = {CategoryServiceImpl.class}) +@ExtendWith(SpringExtension.class) +class CategoryServiceImplTest { + @MockBean + private CategoryRepository categoryRepository; + + @Autowired + private CategoryServiceImpl categoryServiceImpl; + + @Test + void testFindCategories() { + ArrayList categoryList = new ArrayList(); + when(this.categoryRepository.findAll()).thenReturn(categoryList); + List actualFindCategoriesResult = this.categoryServiceImpl.findCategories(); + assertSame(categoryList, actualFindCategoriesResult); + assertTrue(actualFindCategoriesResult.isEmpty()); + verify(this.categoryRepository).findAll(); + } + + @Test + void testAddCategory() { + Category category = new Category(); + category.setId(1); + category.setParentCategory(new Category()); + category.setSubCategories(new HashSet()); + category.setProducts(new ArrayList()); + category.setTitle("Dr"); + + Category category1 = new Category(); + category1.setId(1); + category1.setParentCategory(category); + category1.setSubCategories(new HashSet()); + category1.setProducts(new ArrayList()); + category1.setTitle("Dr"); + + Category category2 = new Category(); + category2.setId(1); + category2.setParentCategory(category1); + category2.setSubCategories(new HashSet()); + category2.setProducts(new ArrayList()); + category2.setTitle("Dr"); + + Category category3 = new Category(); + category3.setId(1); + category3.setParentCategory(category2); + category3.setSubCategories(new HashSet()); + category3.setProducts(new ArrayList()); + category3.setTitle("Dr"); + Optional ofResult = Optional.of(category3); + when(this.categoryRepository.findCategoryByTitle((String) any())).thenReturn(ofResult); + this.categoryServiceImpl.addCategory("Dr"); + verify(this.categoryRepository).findCategoryByTitle((String) any()); + assertTrue(this.categoryServiceImpl.findCategories().isEmpty()); + } + + @Test + void testAddCategory2() { + Category category = new Category(); + category.setId(1); + category.setParentCategory(new Category()); + category.setSubCategories(new HashSet()); + category.setProducts(new ArrayList()); + category.setTitle("Dr"); + + Category category1 = new Category(); + category1.setId(1); + category1.setParentCategory(category); + category1.setSubCategories(new HashSet()); + category1.setProducts(new ArrayList()); + category1.setTitle("Dr"); + + Category category2 = new Category(); + category2.setId(1); + category2.setParentCategory(category1); + category2.setSubCategories(new HashSet()); + category2.setProducts(new ArrayList()); + category2.setTitle("Dr"); + + Category category3 = new Category(); + category3.setId(1); + category3.setParentCategory(category2); + category3.setSubCategories(new HashSet()); + category3.setProducts(new ArrayList()); + category3.setTitle("Dr"); + + Category category4 = new Category(); + category4.setId(1); + category4.setParentCategory(category3); + category4.setSubCategories(new HashSet()); + category4.setProducts(new ArrayList()); + category4.setTitle("Dr"); + when(this.categoryRepository.save((Category) any())).thenReturn(category4); + when(this.categoryRepository.findCategoryByTitle((String) any())).thenReturn(Optional.empty()); + this.categoryServiceImpl.addCategory("Dr"); + verify(this.categoryRepository).findCategoryByTitle((String) any()); + verify(this.categoryRepository).save((Category) any()); + assertTrue(this.categoryServiceImpl.findCategories().isEmpty()); + } + + @Test + void testFindCategoryById() { + HashSet categorySet = new HashSet(); + when(this.categoryRepository.findCategoryByIdIn((Set) any())).thenReturn(categorySet); + Set actualFindCategoryByIdResult = this.categoryServiceImpl.findCategoryById(new HashSet()); + assertSame(categorySet, actualFindCategoryByIdResult); + assertTrue(actualFindCategoryByIdResult.isEmpty()); + verify(this.categoryRepository).findCategoryByIdIn((Set) any()); + assertTrue(this.categoryServiceImpl.findCategories().isEmpty()); + } + + @Test + void testGetCategoryIdList() { + assertTrue(this.categoryServiceImpl.getCategoryIdList(new HashSet()).isEmpty()); + } + + @Test + void testGetCategoryIdList2() { + Category category = new Category(); + category.setId(1); + category.setParentCategory(new Category()); + category.setSubCategories(new HashSet()); + category.setProducts(new ArrayList()); + category.setTitle("Dr"); + + Category category1 = new Category(); + category1.setId(1); + category1.setParentCategory(category); + category1.setSubCategories(new HashSet()); + category1.setProducts(new ArrayList()); + category1.setTitle("Dr"); + + Category category2 = new Category(); + category2.setId(1); + category2.setParentCategory(category1); + category2.setSubCategories(new HashSet()); + category2.setProducts(new ArrayList()); + category2.setTitle("Dr"); + + Category category3 = new Category(); + category3.setId(1); + category3.setParentCategory(category2); + category3.setSubCategories(new HashSet()); + category3.setProducts(new ArrayList()); + category3.setTitle("Dr"); + + HashSet categorySet = new HashSet(); + categorySet.add(category3); + Set actualCategoryIdList = this.categoryServiceImpl.getCategoryIdList(categorySet); + assertEquals(1, actualCategoryIdList.size()); + assertTrue(actualCategoryIdList.contains(1)); + } + + @Test + void testGetCategoryIdList3() { + Category category = new Category(); + category.setId(1); + category.setParentCategory(new Category()); + category.setSubCategories(new HashSet()); + category.setProducts(new ArrayList()); + category.setTitle("Dr"); + + Category category1 = new Category(); + category1.setId(1); + category1.setParentCategory(category); + category1.setSubCategories(new HashSet()); + category1.setProducts(new ArrayList()); + category1.setTitle("Dr"); + + Category category2 = new Category(); + category2.setId(1); + category2.setParentCategory(category1); + category2.setSubCategories(new HashSet()); + category2.setProducts(new ArrayList()); + category2.setTitle("Dr"); + + Category category3 = new Category(); + category3.setId(1); + category3.setParentCategory(category2); + category3.setSubCategories(new HashSet()); + category3.setProducts(new ArrayList()); + category3.setTitle("Dr"); + + Category category4 = new Category(); + category4.setId(1); + category4.setParentCategory(new Category()); + category4.setSubCategories(new HashSet()); + category4.setProducts(new ArrayList()); + category4.setTitle("Title"); + + Category category5 = new Category(); + category5.setId(1); + category5.setParentCategory(category4); + category5.setSubCategories(new HashSet()); + category5.setProducts(new ArrayList()); + category5.setTitle("Dr"); + + Category category6 = new Category(); + category6.setId(1); + category6.setParentCategory(category5); + category6.setSubCategories(new HashSet()); + category6.setProducts(new ArrayList()); + category6.setTitle("Dr"); + + Category category7 = new Category(); + category7.setId(1); + category7.setParentCategory(category6); + category7.setSubCategories(new HashSet()); + category7.setProducts(new ArrayList()); + category7.setTitle("Dr"); + + HashSet categorySet = new HashSet(); + categorySet.add(category7); + categorySet.add(category3); + Set actualCategoryIdList = this.categoryServiceImpl.getCategoryIdList(categorySet); + assertEquals(1, actualCategoryIdList.size()); + assertTrue(actualCategoryIdList.contains(1)); + } + + @Test + void testFindCategory() { + Category category = new Category(); + category.setId(1); + category.setParentCategory(new Category()); + category.setSubCategories(new HashSet()); + category.setProducts(new ArrayList()); + category.setTitle("Dr"); + + Category category1 = new Category(); + category1.setId(1); + category1.setParentCategory(category); + category1.setSubCategories(new HashSet()); + category1.setProducts(new ArrayList()); + category1.setTitle("Dr"); + + Category category2 = new Category(); + category2.setId(1); + category2.setParentCategory(category1); + category2.setSubCategories(new HashSet()); + category2.setProducts(new ArrayList()); + category2.setTitle("Dr"); + + Category category3 = new Category(); + category3.setId(1); + category3.setParentCategory(category2); + category3.setSubCategories(new HashSet()); + category3.setProducts(new ArrayList()); + category3.setTitle("Dr"); + Optional ofResult = Optional.of(category3); + when(this.categoryRepository.findById((Integer) any())).thenReturn(ofResult); + Optional actualFindCategoryResult = this.categoryServiceImpl.findCategory(1); + assertSame(ofResult, actualFindCategoryResult); + assertTrue(actualFindCategoryResult.isPresent()); + verify(this.categoryRepository).findById((Integer) any()); + assertTrue(this.categoryServiceImpl.findCategories().isEmpty()); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/service/impl/FileServiceImplTest.java b/src/test/java/com/example/springmvc/mvcLayer/service/impl/FileServiceImplTest.java new file mode 100644 index 0000000..d67164f --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/service/impl/FileServiceImplTest.java @@ -0,0 +1,22 @@ +package com.example.springmvc.mvcLayer.service.impl; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ContextConfiguration(classes = {FileServiceImpl.class}) +@ExtendWith(SpringExtension.class) +class FileServiceImplTest { + @Autowired + private FileServiceImpl fileServiceImpl; + + @Test + void testSaveProductImage() { + assertThrows(IllegalArgumentException.class, () -> this.fileServiceImpl.saveProductImage(null)); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/service/impl/OrderServiceImplTest.java b/src/test/java/com/example/springmvc/mvcLayer/service/impl/OrderServiceImplTest.java new file mode 100644 index 0000000..32df9f9 --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/service/impl/OrderServiceImplTest.java @@ -0,0 +1,114 @@ +package com.example.springmvc.mvcLayer.service.impl; + +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.example.springmvc.mvcLayer.component.ShoppingCart; +import com.example.springmvc.mvcLayer.domain.Address; +import com.example.springmvc.mvcLayer.domain.Order; +import com.example.springmvc.mvcLayer.domain.dto.ProductDto; +import com.example.springmvc.mvcLayer.domain.security.Role; +import com.example.springmvc.mvcLayer.domain.security.User; +import com.example.springmvc.mvcLayer.repository.AddressRepository; +import com.example.springmvc.mvcLayer.repository.OrderRepository; +import com.example.springmvc.mvcLayer.service.UserService; + +import java.time.LocalDate; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ContextConfiguration(classes = {OrderServiceImpl.class, ShoppingCart.class}) +@ExtendWith(SpringExtension.class) +class OrderServiceImplTest { + @Autowired + private ShoppingCart shoppingCart; + + @MockBean + private AddressRepository addressRepository; + + @MockBean + private OrderRepository orderRepository; + + @Autowired + private OrderServiceImpl orderServiceImpl; + + @MockBean + private UserService userService; + + @Test + void testSave() { + User user = new User(); + user.setPassword("iloveyou"); + user.setEmail("jane.doe@example.org"); + user.setUsername("janedoe"); + user.setId(123L); + user.setEnabled(true); + user.setRoles(new ArrayList()); + when(this.userService.findByUsername((String) any())).thenReturn(user); + + User user1 = new User(); + user1.setPassword("iloveyou"); + user1.setEmail("jane.doe@example.org"); + user1.setUsername("janedoe"); + user1.setId(123L); + user1.setEnabled(true); + user1.setRoles(new ArrayList()); + + Address address = new Address(); + address.setId(1); + address.setCity("Oxford"); + address.setApartment(1); + address.setHouse(1); + address.setStreet("Street"); + + Order order = new Order(); + order.setDate(LocalDate.ofEpochDay(1L)); + order.setStatus("Status"); + order.setUserId(user1); + order.setId(1); + order.setAddress(address); + order.setCart(new ArrayList()); + when(this.orderRepository.save((Order) any())).thenReturn(order); + + Address address1 = new Address(); + address1.setId(1); + address1.setCity("Oxford"); + address1.setApartment(1); + address1.setHouse(1); + address1.setStreet("Street"); + when(this.addressRepository.save((Address) any())).thenReturn(address1); + + Address address2 = new Address(); + address2.setId(1); + address2.setCity("Oxford"); + address2.setApartment(1); + address2.setHouse(1); + address2.setStreet("Street"); + this.orderServiceImpl.save(this.shoppingCart, address2, "Name"); + verify(this.userService).findByUsername((String) any()); + verify(this.orderRepository).save((Order) any()); + verify(this.addressRepository).save((Address) any()); + } + + @Test + void testGetAllOrders() { + ArrayList orderList = new ArrayList(); + when(this.orderRepository.findOrdersByUserId_Username((String) any())).thenReturn(orderList); + List actualAllOrders = this.orderServiceImpl.getAllOrders("Name"); + assertSame(orderList, actualAllOrders); + assertTrue(actualAllOrders.isEmpty()); + verify(this.orderRepository).findOrdersByUserId_Username((String) any()); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/service/impl/ProductServiceImplTest.java b/src/test/java/com/example/springmvc/mvcLayer/service/impl/ProductServiceImplTest.java new file mode 100644 index 0000000..50b3939 --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/service/impl/ProductServiceImplTest.java @@ -0,0 +1,378 @@ +package com.example.springmvc.mvcLayer.service.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.example.springmvc.mvcLayer.domain.Category; +import com.example.springmvc.mvcLayer.domain.Product; +import com.example.springmvc.mvcLayer.domain.dto.ProductDto; +import com.example.springmvc.mvcLayer.domain.search.ProductSearchCondition; +import com.example.springmvc.mvcLayer.repository.ProductRepository; +import com.example.springmvc.mvcLayer.service.CategoryService; +import com.example.springmvc.mvcLayer.service.FileService; +import com.example.springmvc.mvcLayer.service.ReviewService; + +import java.io.UnsupportedEncodingException; +import java.nio.file.Paths; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.NoSuchElementException; +import java.util.Optional; +import java.util.Set; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Sort; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.ui.ConcurrentModel; +import org.springframework.web.multipart.MultipartFile; + +@ContextConfiguration(classes = {ProductServiceImpl.class}) +@ExtendWith(SpringExtension.class) +class ProductServiceImplTest { + @MockBean + private CategoryService categoryService; + + @MockBean + private FileService fileService; + + @MockBean + private ProductRepository productRepository; + + @Autowired + private ProductServiceImpl productServiceImpl; + + @MockBean + private ReviewService reviewService; + + @Test + void testSaveProductAndImage() throws UnsupportedEncodingException { + Product product = new Product(); + product.setCountProduct(3); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + when(this.productRepository.save((Product) any())).thenReturn(product); + when(this.fileService.saveProductImage((org.springframework.web.multipart.MultipartFile) any())) + .thenReturn(Paths.get(System.getProperty("java.io.tmpdir"), "test.txt")); + when(this.categoryService.findCategoryById((Set) any())).thenReturn(new HashSet()); + ProductDto productDto = new ProductDto(); + Product actualSaveProductAndImageResult = this.productServiceImpl.saveProductAndImage(productDto, + new MockMultipartFile("Name", "AAAAAAAAAAAAAAAAAAAAAAAA".getBytes("UTF-8"))); + assertSame(product, actualSaveProductAndImageResult); + String expectedImage = Paths.get(System.getProperty("java.io.tmpdir"), "test.txt").toString(); + assertEquals(expectedImage, actualSaveProductAndImageResult.getImage()); + verify(this.productRepository, atLeast(1)).save((Product) any()); + verify(this.fileService).saveProductImage((org.springframework.web.multipart.MultipartFile) any()); + verify(this.categoryService).findCategoryById((Set) any()); + } + + @Test + void testSaveProductAndImage2() { + Product product = new Product(); + product.setCountProduct(3); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + when(this.productRepository.save((Product) any())).thenReturn(product); + when(this.fileService.saveProductImage((org.springframework.web.multipart.MultipartFile) any())).thenReturn(null); + when(this.categoryService.findCategoryById((Set) any())).thenReturn(new HashSet()); + ProductDto productDto = mock(ProductDto.class); + when(productDto.getCountProduct()).thenReturn(3); + when(productDto.getCategoryDto()).thenReturn(new HashSet()); + when(productDto.getPrice()).thenReturn(1); + when(productDto.getTitle()).thenReturn("Dr"); + when(productDto.getId()).thenReturn(1); + assertSame(product, + this.productServiceImpl.saveProductAndImage(productDto, new MockMultipartFile("Name", (byte[]) null))); + verify(this.productRepository).save((Product) any()); + verify(this.categoryService).findCategoryById((Set) any()); + verify(productDto).getCategoryDto(); + verify(productDto).getCountProduct(); + verify(productDto, atLeast(1)).getId(); + verify(productDto).getPrice(); + verify(productDto).getTitle(); + } + + @Test + void testSaveProductAndImage3() { + Product product = new Product(); + product.setCountProduct(3); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + when(this.productRepository.save((Product) any())).thenReturn(product); + when(this.fileService.saveProductImage((MultipartFile) any())).thenReturn(null); + when(this.categoryService.findCategoryById((Set) any())).thenReturn(new HashSet()); + ProductDto productDto = mock(ProductDto.class); + when(productDto.getCountProduct()).thenReturn(3); + when(productDto.getCategoryDto()).thenReturn(new HashSet()); + when(productDto.getPrice()).thenReturn(1); + when(productDto.getTitle()).thenReturn("Dr"); + when(productDto.getId()).thenReturn(1); + assertSame(product, this.productServiceImpl.saveProductAndImage(productDto, null)); + verify(this.productRepository).save((Product) any()); + verify(this.categoryService).findCategoryById((Set) any()); + verify(productDto).getCategoryDto(); + verify(productDto).getCountProduct(); + verify(productDto, atLeast(1)).getId(); + verify(productDto).getPrice(); + verify(productDto).getTitle(); + } + + @Test + void testSaveProductAndImage4() { + Product product = new Product(); + product.setCountProduct(3); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + when(this.productRepository.save((Product) any())).thenReturn(product); + when(this.fileService.saveProductImage((MultipartFile) any())).thenReturn(null); + when(this.categoryService.findCategoryById((Set) any())).thenReturn(new HashSet()); + ProductDto productDto = mock(ProductDto.class); + when(productDto.getCountProduct()).thenReturn(3); + when(productDto.getCategoryDto()).thenReturn(new HashSet()); + when(productDto.getPrice()).thenReturn(1); + when(productDto.getTitle()).thenReturn("Dr"); + when(productDto.getId()).thenReturn(1); + MultipartFile multipartFile = mock(MultipartFile.class); + when(multipartFile.isEmpty()).thenReturn(true); + assertSame(product, this.productServiceImpl.saveProductAndImage(productDto, multipartFile)); + verify(this.productRepository).save((Product) any()); + verify(this.categoryService).findCategoryById((Set) any()); + verify(productDto).getCategoryDto(); + verify(productDto).getCountProduct(); + verify(productDto, atLeast(1)).getId(); + verify(productDto).getPrice(); + verify(productDto).getTitle(); + verify(multipartFile).isEmpty(); + } + + @Test + void testFindProductById() { + Product product = new Product(); + product.setCountProduct(3); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + Optional ofResult = Optional.of(product); + when(this.productRepository.findById((Integer) any())).thenReturn(ofResult); + Optional actualFindProductByIdResult = this.productServiceImpl.findProductById(1); + assertSame(ofResult, actualFindProductByIdResult); + assertTrue(actualFindProductByIdResult.isPresent()); + verify(this.productRepository).findById((Integer) any()); + } + + @Test + void testFindProductDtoById() { + Product product = new Product(); + product.setCountProduct(3); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + Optional ofResult = Optional.of(product); + when(this.productRepository.findById((Integer) any())).thenReturn(ofResult); + when(this.categoryService.getCategoryIdList((Set) any())).thenReturn(new HashSet()); + ProductDto actualFindProductDtoByIdResult = this.productServiceImpl.findProductDtoById(1); + assertTrue(actualFindProductDtoByIdResult.getCategoryDto().isEmpty()); + assertEquals("Dr", actualFindProductDtoByIdResult.getTitle()); + assertEquals(1, actualFindProductDtoByIdResult.getPrice().intValue()); + assertEquals(1, actualFindProductDtoByIdResult.getId().intValue()); + assertEquals(3, actualFindProductDtoByIdResult.getCountProduct().intValue()); + verify(this.productRepository).findById((Integer) any()); + verify(this.categoryService).getCategoryIdList((Set) any()); + } + + @Test + void testFindProductDtoById2() { + when(this.productRepository.findById((Integer) any())).thenReturn(Optional.empty()); + when(this.categoryService.getCategoryIdList((java.util.Set) any())) + .thenReturn(new HashSet()); + assertThrows(NoSuchElementException.class, () -> this.productServiceImpl.findProductDtoById(1)); + verify(this.productRepository).findById((Integer) any()); + } + + @Test + void testUpdateCountInProduct() { + doNothing().when(this.productRepository).updateCount((Integer) any(), (Integer) any()); + this.productServiceImpl.updateCountInProduct(1, 3); + verify(this.productRepository).updateCount((Integer) any(), (Integer) any()); + } + + @Test + void testDeleteProductById() { + doNothing().when(this.reviewService).deleteAllReviewByProductId((Integer) any()); + doNothing().when(this.productRepository).deleteById((Integer) any()); + this.productServiceImpl.deleteProductById(1); + verify(this.reviewService).deleteAllReviewByProductId((Integer) any()); + verify(this.productRepository).deleteById((Integer) any()); + } + + @Test + void testFindProductsByCategoryId() { + PageImpl pageImpl = new PageImpl(new ArrayList()); + when(this.productRepository.findProductsByCategories((org.springframework.data.domain.Pageable) any(), + (Category) any())).thenReturn(pageImpl); + + Category category = new Category(); + category.setId(1); + category.setParentCategory(new Category()); + category.setSubCategories(new HashSet()); + category.setProducts(new ArrayList()); + category.setTitle("Dr"); + + Category category1 = new Category(); + category1.setId(1); + category1.setParentCategory(category); + category1.setSubCategories(new HashSet()); + category1.setProducts(new ArrayList()); + category1.setTitle("Dr"); + + Category category2 = new Category(); + category2.setId(1); + category2.setParentCategory(category1); + category2.setSubCategories(new HashSet()); + category2.setProducts(new ArrayList()); + category2.setTitle("Dr"); + + Category category3 = new Category(); + category3.setId(1); + category3.setParentCategory(category2); + category3.setSubCategories(new HashSet()); + category3.setProducts(new ArrayList()); + category3.setTitle("Dr"); + Optional ofResult = Optional.of(category3); + when(this.categoryService.findCategory((Integer) any())).thenReturn(ofResult); + + ProductSearchCondition productSearchCondition = new ProductSearchCondition(); + productSearchCondition.setPageSize(3); + productSearchCondition.setSortField("Sort Field"); + productSearchCondition.setPageNum(10); + productSearchCondition.setSortDirection(Sort.Direction.ASC); + Page actualFindProductsByCategoryIdResult = this.productServiceImpl.findProductsByCategoryId(123, + productSearchCondition); + assertSame(pageImpl, actualFindProductsByCategoryIdResult); + assertTrue(actualFindProductsByCategoryIdResult.toList().isEmpty()); + verify(this.productRepository).findProductsByCategories((org.springframework.data.domain.Pageable) any(), + (Category) any()); + verify(this.categoryService).findCategory((Integer) any()); + } + + @Test + void testFindAllBySearchConditional() { + PageImpl pageImpl = new PageImpl(new ArrayList()); + when(this.productRepository.findAll((org.springframework.data.domain.Pageable) any())).thenReturn(pageImpl); + + ProductSearchCondition productSearchCondition = new ProductSearchCondition(); + productSearchCondition.setPageSize(3); + productSearchCondition.setSortField("Sort Field"); + productSearchCondition.setPageNum(10); + productSearchCondition.setSortDirection(Sort.Direction.ASC); + Page actualFindAllBySearchConditionalResult = this.productServiceImpl + .findAllBySearchConditional(productSearchCondition); + assertSame(pageImpl, actualFindAllBySearchConditionalResult); + assertTrue(actualFindAllBySearchConditionalResult.toList().isEmpty()); + verify(this.productRepository).findAll((org.springframework.data.domain.Pageable) any()); + } + + @Test + void testFindAllBySearchConditional2() { + PageImpl pageImpl = new PageImpl(new ArrayList()); + when(this.productRepository.findAll((org.springframework.data.domain.Pageable) any())).thenReturn(pageImpl); + + ProductSearchCondition productSearchCondition = new ProductSearchCondition(); + productSearchCondition.setPageSize(1); + productSearchCondition.setSortField("Sort Field"); + productSearchCondition.setPageNum(10); + productSearchCondition.setSortDirection(Sort.Direction.ASC); + Page actualFindAllBySearchConditionalResult = this.productServiceImpl + .findAllBySearchConditional(productSearchCondition); + assertSame(pageImpl, actualFindAllBySearchConditionalResult); + assertTrue(actualFindAllBySearchConditionalResult.toList().isEmpty()); + verify(this.productRepository).findAll((org.springframework.data.domain.Pageable) any()); + } + + @Test + void testFindProductsByTitleAndByMaxAndMinPriceBySearchConditional() { + PageImpl pageImpl = new PageImpl(new ArrayList()); + when(this.productRepository.findProductsByTitleContainingIgnoreCaseAndPriceBetween((String) any(), (Integer) any(), + (Integer) any(), (org.springframework.data.domain.Pageable) any())).thenReturn(pageImpl); + + ProductSearchCondition productSearchCondition = new ProductSearchCondition(); + productSearchCondition.setPageSize(3); + productSearchCondition.setSortField("Sort Field"); + productSearchCondition.setPageNum(10); + productSearchCondition.setSortDirection(Sort.Direction.ASC); + Page actualFindProductsByTitleAndByMaxAndMinPriceBySearchConditionalResult = this.productServiceImpl + .findProductsByTitleAndByMaxAndMinPriceBySearchConditional(productSearchCondition, "Dr", 1, 3); + assertSame(pageImpl, actualFindProductsByTitleAndByMaxAndMinPriceBySearchConditionalResult); + assertTrue(actualFindProductsByTitleAndByMaxAndMinPriceBySearchConditionalResult.toList().isEmpty()); + verify(this.productRepository).findProductsByTitleContainingIgnoreCaseAndPriceBetween((String) any(), + (Integer) any(), (Integer) any(), (org.springframework.data.domain.Pageable) any()); + } + + @Test + void testFindProductsByTitleAndByMaxAndMinPriceBySearchConditional2() { + PageImpl pageImpl = new PageImpl(new ArrayList()); + when(this.productRepository.findProductsByTitleContainingIgnoreCaseAndPriceBetween((String) any(), (Integer) any(), + (Integer) any(), (org.springframework.data.domain.Pageable) any())).thenReturn(pageImpl); + + ProductSearchCondition productSearchCondition = new ProductSearchCondition(); + productSearchCondition.setPageSize(1); + productSearchCondition.setSortField("Sort Field"); + productSearchCondition.setPageNum(10); + productSearchCondition.setSortDirection(Sort.Direction.ASC); + Page actualFindProductsByTitleAndByMaxAndMinPriceBySearchConditionalResult = this.productServiceImpl + .findProductsByTitleAndByMaxAndMinPriceBySearchConditional(productSearchCondition, "Dr", 1, 3); + assertSame(pageImpl, actualFindProductsByTitleAndByMaxAndMinPriceBySearchConditionalResult); + assertTrue(actualFindProductsByTitleAndByMaxAndMinPriceBySearchConditionalResult.toList().isEmpty()); + verify(this.productRepository).findProductsByTitleContainingIgnoreCaseAndPriceBetween((String) any(), + (Integer) any(), (Integer) any(), (org.springframework.data.domain.Pageable) any()); + } + + @Test + void testPagination() { + ProductSearchCondition productSearchCondition = new ProductSearchCondition(); + productSearchCondition.setPageSize(3); + productSearchCondition.setSortField("Sort Field"); + productSearchCondition.setPageNum(10); + productSearchCondition.setSortDirection(Sort.Direction.ASC); + ConcurrentModel concurrentModel = new ConcurrentModel(); + this.productServiceImpl.pagination(productSearchCondition, concurrentModel, + new PageImpl(new ArrayList())); + assertEquals(10, productSearchCondition.getPageNum()); + assertEquals("Sort Field", productSearchCondition.getSortField()); + assertEquals(Sort.Direction.ASC, productSearchCondition.getSortDirection()); + assertEquals(3, productSearchCondition.getPageSize().intValue()); + assertTrue(((PageImpl) concurrentModel.get("page")).toList().isEmpty()); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/service/impl/ReviewServiceImplTest.java b/src/test/java/com/example/springmvc/mvcLayer/service/impl/ReviewServiceImplTest.java new file mode 100644 index 0000000..d092ef1 --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/service/impl/ReviewServiceImplTest.java @@ -0,0 +1,222 @@ +package com.example.springmvc.mvcLayer.service.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.example.springmvc.mvcLayer.domain.Category; +import com.example.springmvc.mvcLayer.domain.Product; +import com.example.springmvc.mvcLayer.domain.ProductReview; +import com.example.springmvc.mvcLayer.domain.security.Role; +import com.example.springmvc.mvcLayer.domain.security.User; +import com.example.springmvc.mvcLayer.repository.ReviewRepository; +import com.example.springmvc.mvcLayer.service.UserService; + +import java.time.LocalDate; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ContextConfiguration(classes = {ReviewServiceImpl.class}) +@ExtendWith(SpringExtension.class) +class ReviewServiceImplTest { + @MockBean + private ReviewRepository reviewRepository; + + @Autowired + private ReviewServiceImpl reviewServiceImpl; + + @MockBean + private UserService userService; + + @Test + void testFindReviewByProductId() { + ArrayList productReviewList = new ArrayList(); + when(this.reviewRepository.findProductReviewsByProduct_id((Integer) any())).thenReturn(productReviewList); + List actualFindReviewByProductIdResult = this.reviewServiceImpl.findReviewByProductId(1); + assertSame(productReviewList, actualFindReviewByProductIdResult); + assertTrue(actualFindReviewByProductIdResult.isEmpty()); + verify(this.reviewRepository).findProductReviewsByProduct_id((Integer) any()); + } + + @Test + void testFindReviewByProductId2() { + ArrayList productReviewList = new ArrayList(); + when(this.reviewRepository.findProductReviewsByProduct_id((Integer) any())).thenReturn(productReviewList); + List actualFindReviewByProductIdResult = this.reviewServiceImpl.findReviewByProductId(1); + assertSame(productReviewList, actualFindReviewByProductIdResult); + assertTrue(actualFindReviewByProductIdResult.isEmpty()); + verify(this.reviewRepository).findProductReviewsByProduct_id((Integer) any()); + } + + @Test + void testSaveReview() { + User user = new User(); + user.setPassword("iloveyou"); + user.setEmail("jane.doe@example.org"); + user.setUsername("janedoe"); + user.setId(123L); + user.setEnabled(true); + user.setRoles(new ArrayList()); + when(this.userService.findByUsername((String) any())).thenReturn(user); + + User user1 = new User(); + user1.setPassword("iloveyou"); + user1.setEmail("jane.doe@example.org"); + user1.setUsername("janedoe"); + user1.setId(123L); + user1.setEnabled(true); + user1.setRoles(new ArrayList()); + + Product product = new Product(); + product.setCountProduct(3); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + + ProductReview productReview = new ProductReview(); + productReview.setRating(1); + productReview.setReview("Review"); + productReview.setId(1); + productReview.setUser(user1); + productReview.setProduct(product); + productReview.setDateCreate(LocalDate.ofEpochDay(1L)); + when(this.reviewRepository.save((ProductReview) any())).thenReturn(productReview); + + User user2 = new User(); + user2.setPassword("iloveyou"); + user2.setEmail("jane.doe@example.org"); + user2.setUsername("janedoe"); + user2.setId(123L); + user2.setEnabled(true); + user2.setRoles(new ArrayList()); + + Product product1 = new Product(); + product1.setCountProduct(3); + product1.setId(1); + product1.setTitle("Dr"); + product1.setCategories(new HashSet()); + product1.setPrice(1); + product1.setImage("Image"); + + ProductReview productReview1 = new ProductReview(); + productReview1.setRating(1); + productReview1.setReview("Review"); + productReview1.setId(1); + productReview1.setUser(user2); + productReview1.setProduct(product1); + productReview1.setDateCreate(LocalDate.ofEpochDay(1L)); + this.reviewServiceImpl.saveReview(productReview1, "Name"); + verify(this.userService).findByUsername((String) any()); + verify(this.reviewRepository).save((ProductReview) any()); + assertEquals(user2, productReview1.getUser()); + } + + @Test + void testSaveReview2() { + User user = new User(); + user.setPassword("iloveyou"); + user.setEmail("jane.doe@example.org"); + user.setUsername("janedoe"); + user.setId(123L); + user.setEnabled(true); + user.setRoles(new ArrayList()); + when(this.userService.findByUsername((String) any())).thenReturn(user); + + User user1 = new User(); + user1.setPassword("iloveyou"); + user1.setEmail("jane.doe@example.org"); + user1.setUsername("janedoe"); + user1.setId(123L); + user1.setEnabled(true); + user1.setRoles(new ArrayList()); + + Product product = new Product(); + product.setCountProduct(3); + product.setId(1); + product.setTitle("Dr"); + product.setCategories(new HashSet()); + product.setPrice(1); + product.setImage("Image"); + + ProductReview productReview = new ProductReview(); + productReview.setRating(1); + productReview.setReview("Review"); + productReview.setId(1); + productReview.setUser(user1); + productReview.setProduct(product); + productReview.setDateCreate(LocalDate.ofEpochDay(1L)); + when(this.reviewRepository.save((ProductReview) any())).thenReturn(productReview); + + User user2 = new User(); + user2.setPassword("iloveyou"); + user2.setEmail("jane.doe@example.org"); + user2.setUsername("janedoe"); + user2.setId(123L); + user2.setEnabled(true); + user2.setRoles(new ArrayList()); + + Product product1 = new Product(); + product1.setCountProduct(3); + product1.setId(1); + product1.setTitle("Dr"); + product1.setCategories(new HashSet()); + product1.setPrice(1); + product1.setImage("Image"); + + ProductReview productReview1 = new ProductReview(); + productReview1.setRating(1); + productReview1.setReview("Review"); + productReview1.setId(1); + productReview1.setUser(user2); + productReview1.setProduct(product1); + productReview1.setDateCreate(LocalDate.ofEpochDay(1L)); + this.reviewServiceImpl.saveReview(productReview1, "Name"); + verify(this.userService).findByUsername((String) any()); + verify(this.reviewRepository).save((ProductReview) any()); + assertEquals(user2, productReview1.getUser()); + } + + @Test + void testDeleteAllReviewByProductId() { + doNothing().when(this.reviewRepository).deleteProductReviewsByProduct_Id((Integer) any()); + this.reviewServiceImpl.deleteAllReviewByProductId(1); + verify(this.reviewRepository).deleteProductReviewsByProduct_Id((Integer) any()); + } + + @Test + void testDeleteAllReviewByProductId2() { + doNothing().when(this.reviewRepository).deleteProductReviewsByProduct_Id((Integer) any()); + this.reviewServiceImpl.deleteAllReviewByProductId(1); + verify(this.reviewRepository).deleteProductReviewsByProduct_Id((Integer) any()); + } + + @Test + void testDeleteReview() { + doNothing().when(this.reviewRepository).deleteProductReviewById((Integer) any()); + this.reviewServiceImpl.deleteReview(123); + verify(this.reviewRepository).deleteProductReviewById((Integer) any()); + } + + @Test + void testDeleteReview2() { + doNothing().when(this.reviewRepository).deleteProductReviewById((Integer) any()); + this.reviewServiceImpl.deleteReview(123); + verify(this.reviewRepository).deleteProductReviewById((Integer) any()); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/service/impl/security/RoleServiceImplTest.java b/src/test/java/com/example/springmvc/mvcLayer/service/impl/security/RoleServiceImplTest.java new file mode 100644 index 0000000..9da61f6 --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/service/impl/security/RoleServiceImplTest.java @@ -0,0 +1,49 @@ +package com.example.springmvc.mvcLayer.service.impl.security; + +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.example.springmvc.mvcLayer.domain.security.Role; +import com.example.springmvc.mvcLayer.repository.RoleRepository; + +import java.util.Optional; +import javax.persistence.EntityNotFoundException; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ContextConfiguration(classes = {RoleServiceImpl.class}) +@ExtendWith(SpringExtension.class) +class RoleServiceImplTest { + @MockBean + private RoleRepository roleRepository; + + @Autowired + private RoleServiceImpl roleServiceImpl; + + @Test + void testFindByName() { + Role role = new Role(); + role.setId(123L); + role.setName("Name"); + Optional ofResult = Optional.of(role); + when(this.roleRepository.findByNameIgnoreCase((String) any())).thenReturn(ofResult); + assertSame(role, this.roleServiceImpl.findByName("Name")); + verify(this.roleRepository).findByNameIgnoreCase((String) any()); + } + + @Test + void testFindByName2() { + when(this.roleRepository.findByNameIgnoreCase((String) any())).thenReturn(Optional.empty()); + assertThrows(EntityNotFoundException.class, () -> this.roleServiceImpl.findByName("Name")); + verify(this.roleRepository).findByNameIgnoreCase((String) any()); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/service/impl/security/UserDetailsServiceImplTest.java b/src/test/java/com/example/springmvc/mvcLayer/service/impl/security/UserDetailsServiceImplTest.java new file mode 100644 index 0000000..56c18db --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/service/impl/security/UserDetailsServiceImplTest.java @@ -0,0 +1,64 @@ +package com.example.springmvc.mvcLayer.service.impl.security; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.example.springmvc.mvcLayer.domain.security.Role; +import com.example.springmvc.mvcLayer.domain.security.User; +import com.example.springmvc.mvcLayer.repository.UserRepository; + +import java.util.ArrayList; +import java.util.Optional; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ContextConfiguration(classes = {UserDetailsServiceImpl.class}) +@ExtendWith(SpringExtension.class) +class UserDetailsServiceImplTest { + @Autowired + private UserDetailsServiceImpl userDetailsServiceImpl; + + @MockBean + private UserRepository userRepository; + + @Test + void testLoadUserByUsername() throws UsernameNotFoundException { + User user = new User(); + user.setPassword("iloveyou"); + user.setEmail("jane.doe@example.org"); + user.setUsername("janedoe"); + user.setId(123L); + user.setEnabled(true); + user.setRoles(new ArrayList()); + Optional ofResult = Optional.of(user); + when(this.userRepository.findByUsername((String) any())).thenReturn(ofResult); + UserDetails actualLoadUserByUsernameResult = this.userDetailsServiceImpl.loadUserByUsername("janedoe"); + assertTrue(actualLoadUserByUsernameResult.getAuthorities().isEmpty()); + assertTrue(actualLoadUserByUsernameResult.isEnabled()); + assertTrue(actualLoadUserByUsernameResult.isCredentialsNonExpired()); + assertTrue(actualLoadUserByUsernameResult.isAccountNonLocked()); + assertTrue(actualLoadUserByUsernameResult.isAccountNonExpired()); + assertEquals("janedoe", actualLoadUserByUsernameResult.getUsername()); + assertEquals("iloveyou", actualLoadUserByUsernameResult.getPassword()); + verify(this.userRepository).findByUsername((String) any()); + } + + @Test + void testLoadUserByUsername2() throws UsernameNotFoundException { + when(this.userRepository.findByUsername((String) any())).thenReturn(Optional.empty()); + assertThrows(UsernameNotFoundException.class, () -> this.userDetailsServiceImpl.loadUserByUsername("janedoe")); + verify(this.userRepository).findByUsername((String) any()); + } +} + diff --git a/src/test/java/com/example/springmvc/mvcLayer/service/impl/security/UserServiceImplTest.java b/src/test/java/com/example/springmvc/mvcLayer/service/impl/security/UserServiceImplTest.java new file mode 100644 index 0000000..901a1c0 --- /dev/null +++ b/src/test/java/com/example/springmvc/mvcLayer/service/impl/security/UserServiceImplTest.java @@ -0,0 +1,150 @@ +package com.example.springmvc.mvcLayer.service.impl.security; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.example.springmvc.mvcLayer.domain.security.Role; +import com.example.springmvc.mvcLayer.domain.security.User; +import com.example.springmvc.mvcLayer.repository.UserRepository; +import com.example.springmvc.mvcLayer.service.RoleService; + +import java.util.ArrayList; +import java.util.Optional; +import javax.persistence.EntityNotFoundException; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ContextConfiguration(classes = {UserServiceImpl.class}) +@ExtendWith(SpringExtension.class) +class UserServiceImplTest { + @MockBean + private PasswordEncoder passwordEncoder; + + @MockBean + private RoleService roleService; + + @MockBean + private UserRepository userRepository; + + @Autowired + private UserServiceImpl userServiceImpl; + + @Test + void testFindByUsername() { + User user = new User(); + user.setPassword("iloveyou"); + user.setEmail("jane.doe@example.org"); + user.setUsername("janedoe"); + user.setId(123L); + user.setEnabled(true); + user.setRoles(new ArrayList()); + Optional ofResult = Optional.of(user); + when(this.userRepository.findByUsername((String) any())).thenReturn(ofResult); + assertSame(user, this.userServiceImpl.findByUsername("janedoe")); + verify(this.userRepository).findByUsername((String) any()); + } + + @Test + void testFindByUsername2() { + when(this.userRepository.findByUsername((String) any())).thenReturn(Optional.empty()); + assertThrows(UsernameNotFoundException.class, () -> this.userServiceImpl.findByUsername("janedoe")); + verify(this.userRepository).findByUsername((String) any()); + } + + @Test + void testSaveUser() { + User user = new User(); + user.setPassword("iloveyou"); + user.setEmail("jane.doe@example.org"); + user.setUsername("janedoe"); + user.setId(123L); + user.setEnabled(true); + user.setRoles(new ArrayList()); + when(this.userRepository.save((User) any())).thenReturn(user); + + Role role = new Role(); + role.setId(123L); + role.setName("Name"); + when(this.roleService.findByName((String) any())).thenReturn(role); + when(this.passwordEncoder.encode((CharSequence) any())).thenReturn("secret"); + + User user1 = new User(); + user1.setPassword("iloveyou"); + user1.setEmail("jane.doe@example.org"); + user1.setUsername("janedoe"); + user1.setId(123L); + user1.setEnabled(true); + user1.setRoles(new ArrayList()); + assertSame(user, this.userServiceImpl.saveUser(user1)); + verify(this.userRepository).save((User) any()); + verify(this.roleService).findByName((String) any()); + verify(this.passwordEncoder).encode((CharSequence) any()); + assertEquals(1, user1.getRoles().size()); + assertEquals("secret", user1.getPassword()); + } + + @Test + void testFindAllByPage() { + PageImpl pageImpl = new PageImpl(new ArrayList()); + when(this.userRepository.findAll((org.springframework.data.domain.Pageable) any())).thenReturn(pageImpl); + Page actualFindAllByPageResult = this.userServiceImpl.findAllByPage(null); + assertSame(pageImpl, actualFindAllByPageResult); + assertTrue(actualFindAllByPageResult.toList().isEmpty()); + verify(this.userRepository).findAll((org.springframework.data.domain.Pageable) any()); + } + + @Test + void testSetEnable() { + User user = new User(); + user.setPassword("iloveyou"); + user.setEmail("jane.doe@example.org"); + user.setUsername("janedoe"); + user.setId(123L); + user.setEnabled(true); + user.setRoles(new ArrayList()); + Optional ofResult = Optional.of(user); + + User user1 = new User(); + user1.setPassword("iloveyou"); + user1.setEmail("jane.doe@example.org"); + user1.setUsername("janedoe"); + user1.setId(123L); + user1.setEnabled(true); + user1.setRoles(new ArrayList()); + when(this.userRepository.save((User) any())).thenReturn(user1); + when(this.userRepository.findById((Long) any())).thenReturn(ofResult); + this.userServiceImpl.setEnable(123L, true); + verify(this.userRepository).findById((Long) any()); + verify(this.userRepository).save((User) any()); + } + + @Test + void testSetEnable2() { + User user = new User(); + user.setPassword("iloveyou"); + user.setEmail("jane.doe@example.org"); + user.setUsername("janedoe"); + user.setId(123L); + user.setEnabled(true); + user.setRoles(new ArrayList()); + when(this.userRepository.save((User) any())).thenReturn(user); + when(this.userRepository.findById((Long) any())).thenReturn(Optional.empty()); + assertThrows(EntityNotFoundException.class, () -> this.userServiceImpl.setEnable(123L, true)); + verify(this.userRepository).findById((Long) any()); + } +} + From 65b94fb94e335d39f7afae719d1eb782d752e2b3 Mon Sep 17 00:00:00 2001 From: SerjNikitin <76908941+SerjNikitin@users.noreply.github.com> Date: Sat, 6 Nov 2021 20:57:29 +0300 Subject: [PATCH 2/2] Create README.md --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..26bc643 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +Он-лайн магазин продуктов + +Функции у пользователя: +1) Поиск товара по категориям, цене названию; +2) Добавление товара в корзину, оформление заказа; +3) В личном кобинете можно посмотреть список всех заказов; +4) Можно оставлять и смотреть коментарии к товару; + +Функции у администратора: +1) Добавление новых, редактирование и удаление товаров; +2) Можно оставлять, смотреть, удалять комментарии; +3) Можно заблокировать/разблокировать пользователей; + +Примечание: +Администратор: логин:ggg пароль:ggg +Пользователь: логин:fff пароль:fff +http://localhost:8080/product/list