Compare commits
2 Commits
431abe9c99
...
b9859be707
Author | SHA1 | Date | |
---|---|---|---|
|
b9859be707 | ||
|
2d53d7dc86 |
@ -21,11 +21,11 @@ import jakarta.validation.Valid;
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/Categories")
|
||||
public class CategoriesController {
|
||||
private final CategoriesService CategoriesService;
|
||||
private final CategoriesService categoriesService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public CategoriesController(CategoriesService CategoriesService, ModelMapper modelMapper) {
|
||||
this.CategoriesService = CategoriesService;
|
||||
public CategoriesController(CategoriesService categoriesService, ModelMapper modelMapper) {
|
||||
this.categoriesService = categoriesService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
@ -39,26 +39,26 @@ public class CategoriesController {
|
||||
|
||||
@GetMapping
|
||||
public List<CategoriesDto> getAll() {
|
||||
return CategoriesService.getAll().stream().map(this::toDto).toList();
|
||||
return categoriesService.getAll().stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public CategoriesDto get(@PathVariable(name = "id") Long id) {
|
||||
return toDto(CategoriesService.get(id));
|
||||
return toDto(categoriesService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public CategoriesDto create(@RequestBody @Valid CategoriesDto dto) {
|
||||
return toDto(CategoriesService.create(toEntity(dto)));
|
||||
return toDto(categoriesService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public CategoriesDto update(@PathVariable(name = "id") Long id, @RequestBody CategoriesDto dto) {
|
||||
return toDto(CategoriesService.update(id, toEntity(dto)));
|
||||
return toDto(categoriesService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public CategoriesDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(CategoriesService.delete(id));
|
||||
return toDto(categoriesService.delete(id));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,71 @@
|
||||
package com.example.demo.itemOrders.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
import com.example.demo.itemOrders.model.OrdersEntity;
|
||||
import com.example.demo.itemOrders.service.OrdersService;
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
import com.example.demo.itemProducts.service.ProductsService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/Orders")
|
||||
public class OrdersController {
|
||||
private final OrdersService ordersService;
|
||||
private final ProductsService productsService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public OrdersController(OrdersService ordersService, ProductsService productsService, ModelMapper modelMapper) {
|
||||
this.ordersService = ordersService;
|
||||
this.productsService = productsService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private OrdersDto toDto(OrdersEntity entity) {
|
||||
return modelMapper.map(entity, OrdersDto.class);
|
||||
}
|
||||
|
||||
private OrdersEntity toEntity(OrdersDto dto) {
|
||||
final OrdersEntity entity = modelMapper.map(dto, OrdersEntity.class);
|
||||
entity.setProduct(productsService.get(dto.getProductId()));
|
||||
return entity;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<OrdersDto> getAll(@RequestParam(name = "typeId", defaultValue = "0") ProductsEntity product) {
|
||||
return ordersService.getAll(product.getId()).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public OrdersDto get(@PathVariable(name = "id") Long id) {
|
||||
return toDto(ordersService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public OrdersDto create(@RequestBody @Valid OrdersDto dto) {
|
||||
return toDto(ordersService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public OrdersDto update(@PathVariable(name = "id") Long id, @RequestBody OrdersDto dto) {
|
||||
return toDto(ordersService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public OrdersDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(ordersService.delete(id));
|
||||
}
|
||||
}
|
60
src/main/java/com/example/demo/itemOrders/api/OrdersDto.java
Normal file
60
src/main/java/com/example/demo/itemOrders/api/OrdersDto.java
Normal file
@ -0,0 +1,60 @@
|
||||
package com.example.demo.itemOrders.api;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class OrdersDto {
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
private Long id;
|
||||
@NotNull
|
||||
private ProductsEntity product;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Integer count;
|
||||
@NotNull
|
||||
private SimpleDateFormat date;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public ProductsEntity getProduct() {
|
||||
return product;
|
||||
}
|
||||
public void setProduct(ProductsEntity product) {
|
||||
this.product = product;
|
||||
}
|
||||
public Long getProductId() {
|
||||
return product.getId();
|
||||
}
|
||||
public void setProductId(Long productId) {
|
||||
productId = product.getId();
|
||||
}
|
||||
|
||||
public Integer getCount() {
|
||||
return count;
|
||||
}
|
||||
public void setCount(Integer count) {
|
||||
this.count = count;
|
||||
}
|
||||
public SimpleDateFormat getDate() {
|
||||
return date;
|
||||
}
|
||||
public void setDate(SimpleDateFormat date) {
|
||||
this.date = date;
|
||||
}
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Double getSum() {
|
||||
return count * product.getPrice();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package com.example.demo.itemProducts.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
import com.example.demo.itemProducts.service.ProductsService;
|
||||
import com.example.demo.itemCategories.service.CategoriesService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/Products")
|
||||
public class ProductsController {
|
||||
private final ProductsService productsService;
|
||||
private final CategoriesService categoriesService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public ProductsController(ProductsService productsService, CategoriesService categoriesService, ModelMapper modelMapper) {
|
||||
this.productsService = productsService;
|
||||
this.categoriesService = categoriesService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private ProductsDto toDto(ProductsEntity entity) {
|
||||
return modelMapper.map(entity, ProductsDto.class);
|
||||
}
|
||||
|
||||
private ProductsEntity toEntity(ProductsDto dto) {
|
||||
final ProductsEntity entity = modelMapper.map(dto, ProductsEntity.class);
|
||||
entity.setType(categoriesService.get(dto.getTypeId()));
|
||||
return entity;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<ProductsDto> getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId) {
|
||||
return productsService.getAll().stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ProductsDto get(@PathVariable(name = "id") Long id) {
|
||||
return toDto(productsService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ProductsDto create(@RequestBody @Valid ProductsDto dto) {
|
||||
return toDto(productsService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ProductsDto update(@PathVariable(name = "id") Long id, @RequestBody ProductsDto dto) {
|
||||
return toDto(productsService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ProductsDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(productsService.delete(id));
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.example.demo.itemProducts.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class ProductsDto {
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
private Long id;
|
||||
@NotBlank
|
||||
private String name;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Long typeId;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Double price;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getTypeId() {
|
||||
return typeId;
|
||||
}
|
||||
|
||||
public void setTypeId(Long typeId) {
|
||||
this.typeId = typeId;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
@ -31,7 +31,9 @@ public class ProductsEntity extends BaseEntity{
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
@ -39,9 +41,6 @@ public class ProductsEntity extends BaseEntity{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, type, name, price);
|
||||
|
@ -17,13 +17,17 @@ public class ProductsService {
|
||||
public ProductsService(ProductsRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
/*
|
||||
public List<ProductsEntity> getAll(Long typeId) {
|
||||
if (Objects.equals(typeId, 0L)) {
|
||||
return repository.getAll();
|
||||
}
|
||||
return repository.getAll().stream().filter(item -> item.getType().getId().equals(typeId)).toList();
|
||||
}
|
||||
*/
|
||||
public List<ProductsEntity> getAll() {
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public ProductsEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
|
||||
|
@ -21,11 +21,11 @@ import jakarta.validation.Valid;
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/Users")
|
||||
public class UsersController {
|
||||
private final UsersService UsersService;
|
||||
private final UsersService usersService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public UsersController(UsersService UsersService, ModelMapper modelMapper) {
|
||||
this.UsersService = UsersService;
|
||||
public UsersController(UsersService usersService, ModelMapper modelMapper) {
|
||||
this.usersService = usersService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
@ -39,26 +39,26 @@ public class UsersController {
|
||||
|
||||
@GetMapping
|
||||
public List<UsersDto> getAll() {
|
||||
return UsersService.getAll().stream().map(this::toDto).toList();
|
||||
return usersService.getAll().stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public UsersDto get(@PathVariable(name = "id") Long id) {
|
||||
return toDto(UsersService.get(id));
|
||||
return toDto(usersService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public UsersDto create(@RequestBody @Valid UsersDto dto) {
|
||||
return toDto(UsersService.create(toEntity(dto)));
|
||||
return toDto(usersService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public UsersDto update(@PathVariable(name = "id") Long id, @RequestBody UsersDto dto) {
|
||||
return toDto(UsersService.update(id, toEntity(dto)));
|
||||
return toDto(usersService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public UsersDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(UsersService.delete(id));
|
||||
return toDto(usersService.delete(id));
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.example.demo.itemUsers.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -16,31 +16,31 @@ import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class CategoriesServiceTests {
|
||||
@Autowired
|
||||
private CategoriesService CategoriesService;
|
||||
|
||||
private CategoriesService categoriesService;
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> CategoriesService.get(0L));
|
||||
Assertions.assertThrows(NotFoundException.class, () -> categoriesService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
CategoriesService.create(new CategoriesEntity(null, "Ноутбуки"));
|
||||
final CategoriesEntity last = CategoriesService.create(new CategoriesEntity(null, "Телефоны"));
|
||||
Assertions.assertEquals(3, CategoriesService.getAll().size());
|
||||
Assertions.assertEquals(last, CategoriesService.get(3L));
|
||||
categoriesService.create(new CategoriesEntity(null, "Ноутбук"));
|
||||
categoriesService.create(new CategoriesEntity(null, "Телефон"));
|
||||
final CategoriesEntity last = categoriesService.create(new CategoriesEntity(null, "Игровая приставка"));
|
||||
Assertions.assertEquals(3, categoriesService.getAll().size());
|
||||
Assertions.assertEquals(last, categoriesService.get(3L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateTest() {
|
||||
final String test = "TEST";
|
||||
final CategoriesEntity entity = CategoriesService.get(3L);
|
||||
final CategoriesEntity entity = categoriesService.get(3L);
|
||||
final String oldName = entity.getName();
|
||||
final CategoriesEntity newEntity = CategoriesService.update(3L, new CategoriesEntity(1L, test));
|
||||
Assertions.assertEquals(3, CategoriesService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, CategoriesService.get(3L));
|
||||
final CategoriesEntity newEntity = categoriesService.update(3L, new CategoriesEntity(1L, test));
|
||||
Assertions.assertEquals(3, categoriesService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, categoriesService.get(3L));
|
||||
Assertions.assertEquals(test, newEntity.getName());
|
||||
Assertions.assertNotEquals(oldName, newEntity.getName());
|
||||
}
|
||||
@ -48,13 +48,13 @@ class CategoriesServiceTests {
|
||||
@Test
|
||||
@Order(3)
|
||||
void deleteTest() {
|
||||
CategoriesService.delete(3L);
|
||||
Assertions.assertEquals(2, CategoriesService.getAll().size());
|
||||
final CategoriesEntity last = CategoriesService.get(2L);
|
||||
categoriesService.delete(3L);
|
||||
Assertions.assertEquals(2, categoriesService.getAll().size());
|
||||
final CategoriesEntity last = categoriesService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
|
||||
final CategoriesEntity newEntity = CategoriesService.create(new CategoriesEntity(null, "Игровая приставка"));
|
||||
Assertions.assertEquals(3, CategoriesService.getAll().size());
|
||||
final CategoriesEntity newEntity = categoriesService.create(new CategoriesEntity(null, "Игровая приставка"));
|
||||
Assertions.assertEquals(3, categoriesService.getAll().size());
|
||||
Assertions.assertEquals(4L, newEntity.getId());
|
||||
}
|
||||
}
|
||||
|
65
src/test/java/com/example/demo/ProductsServiceTests.java
Normal file
65
src/test/java/com/example/demo/ProductsServiceTests.java
Normal file
@ -0,0 +1,65 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||
import com.example.demo.itemCategories.service.CategoriesService;
|
||||
import com.example.demo.itemProducts.service.ProductsService;
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class ProductsServiceTests {
|
||||
@Autowired
|
||||
private ProductsService productsService;
|
||||
private CategoriesService categoriesService;
|
||||
CategoriesEntity category1 = categoriesService.create(new CategoriesEntity(null, "Телефон"));
|
||||
CategoriesEntity category2 = categoriesService.create(new CategoriesEntity(null, "Игровая приставка"));
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> productsService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
final CategoriesEntity category1 = categoriesService.create(new CategoriesEntity(null, "Ноутбук"));
|
||||
final CategoriesEntity category2 = categoriesService.create(new CategoriesEntity(null, "Телефон"));
|
||||
|
||||
productsService.create(new ProductsEntity(null, category1, "Lenovo IDEA PAD 13", 15232.00));
|
||||
productsService.create(new ProductsEntity(null, category1, "Acer", 20300.00));
|
||||
final ProductsEntity last = productsService.create(new ProductsEntity (null, category2, "Iphone 13", 150000.00));
|
||||
|
||||
Assertions.assertEquals(3, productsService.getAll().size());
|
||||
Assertions.assertEquals(last, productsService.get(3L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateTest() {
|
||||
final Double newPrice = 20000.00;
|
||||
final ProductsEntity entity = productsService.get(1L);
|
||||
final Double oldPrice = entity.getPrice();
|
||||
final ProductsEntity newEntity = productsService.update(1L, new ProductsEntity(1L,category1, "Lenovo IDEA PAD 13", newPrice));
|
||||
Assertions.assertEquals(3, productsService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, productsService.get(1L));
|
||||
Assertions.assertEquals(newPrice, newEntity.getPrice());
|
||||
Assertions.assertNotEquals(oldPrice, newEntity.getPrice());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void deleteTest() {
|
||||
productsService.delete(3L);
|
||||
Assertions.assertEquals(2, productsService.getAll().size());
|
||||
final ProductsEntity last = productsService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
}
|
||||
}
|
148
src/test/java/com/example/demo/Test.java
Normal file
148
src/test/java/com/example/demo/Test.java
Normal file
@ -0,0 +1,148 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||
import com.example.demo.itemCategories.service.CategoriesService;
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
import com.example.demo.itemProducts.service.ProductsService;
|
||||
import com.example.demo.itemUsers.service.UsersService;
|
||||
import com.example.demo.itemUsers.model.UsersEntity;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class UsersServiceTests {
|
||||
@Autowired
|
||||
private UsersService usersService;
|
||||
private CategoriesService categoriesService;
|
||||
private ProductsService productsService;
|
||||
|
||||
@Test
|
||||
void getUsers() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> usersService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createUsers() {
|
||||
usersService.create(new UsersEntity(null, "Natalia", "1234"));
|
||||
final UsersEntity last = usersService.create(new UsersEntity(null, "Revengel", "4567"));
|
||||
Assertions.assertEquals(2, usersService.getAll().size());
|
||||
Assertions.assertEquals(last, usersService.get(2L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateUsers() {
|
||||
final String newPassword = "0000";
|
||||
final UsersEntity entity = usersService.get(2L);
|
||||
final String login = entity.getLogin();
|
||||
final String oldPassword = entity.getPassword();
|
||||
final UsersEntity newEntity = usersService.update(2L, new UsersEntity(1L, login, newPassword));
|
||||
Assertions.assertEquals(2, usersService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, usersService.get(2L));
|
||||
Assertions.assertEquals(newPassword, newEntity.getPassword());
|
||||
Assertions.assertNotEquals(oldPassword, newEntity.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void deleteUsers() {
|
||||
usersService.delete(2L);
|
||||
Assertions.assertEquals(1, usersService.getAll().size());
|
||||
final UsersEntity last = usersService.get(1L);
|
||||
Assertions.assertEquals(1L, last.getId());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void getCategories() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> categoriesService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void createCategories() {
|
||||
CategoriesEntity category1 = categoriesService.create(new CategoriesEntity(null, "Телефон"));
|
||||
CategoriesEntity category2 = categoriesService.create(new CategoriesEntity(null, "Игровая приставка"));
|
||||
final CategoriesEntity last = categoriesService.create(new CategoriesEntity(null, "Ноутбук"));
|
||||
Assertions.assertEquals(3, categoriesService.getAll().size());
|
||||
Assertions.assertEquals(last, categoriesService.get(3L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void updateCategories() {
|
||||
final String test = "TEST";
|
||||
final CategoriesEntity entity = categoriesService.get(3L);
|
||||
final String oldName = entity.getName();
|
||||
final CategoriesEntity newEntity = categoriesService.update(3L, new CategoriesEntity(1L, test));
|
||||
Assertions.assertEquals(3, categoriesService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, categoriesService.get(3L));
|
||||
Assertions.assertEquals(test, newEntity.getName());
|
||||
Assertions.assertNotEquals(oldName, newEntity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void deleteCategories() {
|
||||
categoriesService.delete(3L);
|
||||
Assertions.assertEquals(2, categoriesService.getAll().size());
|
||||
final CategoriesEntity last = categoriesService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
|
||||
final CategoriesEntity newEntity = categoriesService.create(new CategoriesEntity(null, "Игровая приставка"));
|
||||
Assertions.assertEquals(3, categoriesService.getAll().size());
|
||||
Assertions.assertEquals(4L, newEntity.getId());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void getTest() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> productsService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void createTest() {
|
||||
productsService.create(new ProductsEntity(null, category1, "Lenovo IDEA PAD 13", 15232.00));
|
||||
productsService.create(new ProductsEntity(null, category1, "Acer", 20300.00));
|
||||
final ProductsEntity last = productsService.create(new ProductsEntity (null, category2, "Iphone 13", 150000.00));
|
||||
|
||||
Assertions.assertEquals(3, productsService.getAll().size());
|
||||
Assertions.assertEquals(last, productsService.get(3L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void updateTest() {
|
||||
final Double newPrice = 20000.00;
|
||||
final ProductsEntity entity = productsService.get(1L);
|
||||
final Double oldPrice = entity.getPrice();
|
||||
final ProductsEntity newEntity = productsService.update(1L, new ProductsEntity(1L,category1, "Lenovo IDEA PAD 13", newPrice));
|
||||
Assertions.assertEquals(3, productsService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, productsService.get(1L));
|
||||
Assertions.assertEquals(newPrice, newEntity.getPrice());
|
||||
Assertions.assertNotEquals(oldPrice, newEntity.getPrice());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void deleteTest() {
|
||||
productsService.delete(3L);
|
||||
Assertions.assertEquals(2, productsService.getAll().size());
|
||||
final ProductsEntity last = productsService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
}
|
||||
}
|
57
src/test/java/com/example/demo/UsersServiceTests.java
Normal file
57
src/test/java/com/example/demo/UsersServiceTests.java
Normal file
@ -0,0 +1,57 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.itemUsers.service.UsersService;
|
||||
import com.example.demo.itemUsers.model.UsersEntity;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class UsersServiceTests {
|
||||
@Autowired
|
||||
private UsersService usersService;
|
||||
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> usersService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
usersService.create(new UsersEntity(null, "Natalia", "1234"));
|
||||
final UsersEntity last = usersService.create(new UsersEntity(null, "Revengel", "4567"));
|
||||
Assertions.assertEquals(2, usersService.getAll().size());
|
||||
Assertions.assertEquals(last, usersService.get(2L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateTest() {
|
||||
final String newPassword = "0000";
|
||||
final UsersEntity entity = usersService.get(2L);
|
||||
final String login = entity.getLogin();
|
||||
final String oldPassword = entity.getPassword();
|
||||
final UsersEntity newEntity = usersService.update(2L, new UsersEntity(1L, login, newPassword));
|
||||
Assertions.assertEquals(2, usersService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, usersService.get(2L));
|
||||
Assertions.assertEquals(newPassword, newEntity.getPassword());
|
||||
Assertions.assertNotEquals(oldPassword, newEntity.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void deleteTest() {
|
||||
usersService.delete(1L);
|
||||
Assertions.assertEquals(1, usersService.getAll().size());
|
||||
final UsersEntity last = usersService.get(0L);
|
||||
Assertions.assertEquals(1L, last.getId());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user