This commit is contained in:
revengel66 2024-04-09 15:21:11 +04:00
parent 2d53d7dc86
commit b9859be707
9 changed files with 319 additions and 45 deletions

View File

@ -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));
}
}

View File

@ -23,13 +23,13 @@ import jakarta.validation.Valid;
@RestController
@RequestMapping(Constants.API_URL + "/Products")
public class ProductsController {
private final ProductsService ProductsService;
private final CategoriesService CategoriesService;
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;
public ProductsController(ProductsService productsService, CategoriesService categoriesService, ModelMapper modelMapper) {
this.productsService = productsService;
this.categoriesService = categoriesService;
this.modelMapper = modelMapper;
}
@ -39,32 +39,32 @@ public class ProductsController {
private ProductsEntity toEntity(ProductsDto dto) {
final ProductsEntity entity = modelMapper.map(dto, ProductsEntity.class);
entity.setType(CategoriesService.get(dto.getTypeId()));
entity.setType(categoriesService.get(dto.getTypeId()));
return entity;
}
@GetMapping
public List<ProductsDto> getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId) {
return ProductsService.getAll(typeId).stream().map(this::toDto).toList();
return productsService.getAll().stream().map(this::toDto).toList();
}
@GetMapping("/{id}")
public ProductsDto get(@PathVariable(name = "id") Long id) {
return toDto(ProductsService.get(id));
return toDto(productsService.get(id));
}
@PostMapping
public ProductsDto create(@RequestBody @Valid ProductsDto dto) {
return toDto(ProductsService.create(toEntity(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)));
return toDto(productsService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public ProductsDto delete(@PathVariable(name = "id") Long id) {
return toDto(ProductsService.delete(id));
return toDto(productsService.delete(id));
}
}

View File

@ -24,6 +24,7 @@ public class ProductsEntity extends BaseEntity{
public CategoriesEntity getType() {
return type;
}
public void setType(CategoriesEntity type) {
this.type = type;
}
@ -40,7 +41,6 @@ public class ProductsEntity extends BaseEntity{
return name;
}
@Override
public int hashCode() {
return Objects.hash(id, type, name, price);

View File

@ -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));

View File

@ -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));
}
}

View File

@ -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());
}
}

View 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());
}
}

View 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());
}
}

View 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());
}
}