From 02169d2eaa6853e42435e2aebf80c0993fa16bc4 Mon Sep 17 00:00:00 2001 From: antoc0der <1@DESKTOP-K1L8ND3> Date: Thu, 7 Mar 2024 10:28:30 +0400 Subject: [PATCH] debug --- .../example/demo/reviews/api/ReviewDto.java | 7 ++- .../demo/reviews/model/ReviewEntity.java | 7 ++- .../demo/users/api/UserController.java | 10 +-- .../com/example/demo/users/api/UserDto.java | 2 +- .../example/demo/users/model/UserEntity.java | 2 +- .../com/example/demo/ItemServiceTests.java | 61 +++++++++++++++++++ .../com/example/demo/ReviewServiceTests.java | 61 +++++++++++++++++++ .../com/example/demo/UserServiceTests.java | 61 +++++++++++++++++++ 8 files changed, 198 insertions(+), 13 deletions(-) create mode 100644 lab2.2/Лекция2-src/src/test/java/com/example/demo/ItemServiceTests.java create mode 100644 lab2.2/Лекция2-src/src/test/java/com/example/demo/ReviewServiceTests.java create mode 100644 lab2.2/Лекция2-src/src/test/java/com/example/demo/UserServiceTests.java diff --git a/lab2.2/Лекция2-src/src/main/java/com/example/demo/reviews/api/ReviewDto.java b/lab2.2/Лекция2-src/src/main/java/com/example/demo/reviews/api/ReviewDto.java index a8d48a6..8e00d06 100644 --- a/lab2.2/Лекция2-src/src/main/java/com/example/demo/reviews/api/ReviewDto.java +++ b/lab2.2/Лекция2-src/src/main/java/com/example/demo/reviews/api/ReviewDto.java @@ -1,11 +1,12 @@ package com.example.demo.reviews.api; import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.Date; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; -public class ItemDto { +public class ReviewDto { private Long id; @NotNull @Min(1) @@ -46,7 +47,7 @@ public class ItemDto { return text; } - public void setText(String date) { - this.date = date; + public void setText(String text) { + this.text = text; } } diff --git a/lab2.2/Лекция2-src/src/main/java/com/example/demo/reviews/model/ReviewEntity.java b/lab2.2/Лекция2-src/src/main/java/com/example/demo/reviews/model/ReviewEntity.java index 7002d5d..9d5a1ff 100644 --- a/lab2.2/Лекция2-src/src/main/java/com/example/demo/reviews/model/ReviewEntity.java +++ b/lab2.2/Лекция2-src/src/main/java/com/example/demo/reviews/model/ReviewEntity.java @@ -1,12 +1,13 @@ package com.example.demo.reviews.model; import java.util.Objects; +import java.util.Date; import com.example.demo.core.model.BaseEntity; import com.example.demo.users.model.UserEntity; public class ReviewEntity extends BaseEntity { - private UserEntity type; + private UserEntity user; private Date date; private String text; @@ -41,8 +42,8 @@ public class ReviewEntity extends BaseEntity { return text; } - public void setText(String date) { - this.date = date; + public void setText(String text) { + this.text = text; } @Override diff --git a/lab2.2/Лекция2-src/src/main/java/com/example/demo/users/api/UserController.java b/lab2.2/Лекция2-src/src/main/java/com/example/demo/users/api/UserController.java index 8ada30e..33babee 100644 --- a/lab2.2/Лекция2-src/src/main/java/com/example/demo/users/api/UserController.java +++ b/lab2.2/Лекция2-src/src/main/java/com/example/demo/users/api/UserController.java @@ -1,4 +1,4 @@ -package com.example.demo.items.api; +package com.example.demo.users.api; import java.util.List; @@ -21,11 +21,11 @@ import jakarta.validation.Valid; @RestController @RequestMapping(Constants.API_URL + "/user") -public class ItemController { - private final UserService UserService; +public class UserController { + private final UserService userService; private final ModelMapper modelMapper; - public ItemController(UserService userService, ModelMapper modelMapper) { + public UserController(UserService userService, ModelMapper modelMapper) { this.userService = userService; this.modelMapper = modelMapper; } @@ -39,7 +39,7 @@ public class ItemController { } @GetMapping - public List getAll() { + public List getAll() { return userService.getAll().stream().map(this::toDto).toList(); } diff --git a/lab2.2/Лекция2-src/src/main/java/com/example/demo/users/api/UserDto.java b/lab2.2/Лекция2-src/src/main/java/com/example/demo/users/api/UserDto.java index 49febe0..d3dcd83 100644 --- a/lab2.2/Лекция2-src/src/main/java/com/example/demo/users/api/UserDto.java +++ b/lab2.2/Лекция2-src/src/main/java/com/example/demo/users/api/UserDto.java @@ -27,7 +27,7 @@ public class UserDto { } public String getHandle() { - return typeId; + return handle; } public void setHandle(String handle) { diff --git a/lab2.2/Лекция2-src/src/main/java/com/example/demo/users/model/UserEntity.java b/lab2.2/Лекция2-src/src/main/java/com/example/demo/users/model/UserEntity.java index 2f9fd1b..6fd8e44 100644 --- a/lab2.2/Лекция2-src/src/main/java/com/example/demo/users/model/UserEntity.java +++ b/lab2.2/Лекция2-src/src/main/java/com/example/demo/users/model/UserEntity.java @@ -56,7 +56,7 @@ public class UserEntity extends BaseEntity { return true; if (obj == null || getClass() != obj.getClass()) return false; - final ItemEntity other = (ItemEntity) obj; + final UserEntity other = (UserEntity) obj; return Objects.equals(other.getId(), id) && Objects.equals(other.getHandle(), handle) && Objects.equals(other.getEmail(), email) diff --git a/lab2.2/Лекция2-src/src/test/java/com/example/demo/ItemServiceTests.java b/lab2.2/Лекция2-src/src/test/java/com/example/demo/ItemServiceTests.java new file mode 100644 index 0000000..409d4d9 --- /dev/null +++ b/lab2.2/Лекция2-src/src/test/java/com/example/demo/ItemServiceTests.java @@ -0,0 +1,61 @@ +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.types.model.TypeEntity; +import com.example.demo.types.service.TypeService; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +class TypeServiceTests { + @Autowired + private TypeService typeService; + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L)); + } + + @Test + @Order(1) + void createTest() { + typeService.create(new TypeEntity(null, "Ноутбук")); + typeService.create(new TypeEntity(null, "Телефон")); + final TypeEntity last = typeService.create(new TypeEntity(null, "Игровая приставка")); + Assertions.assertEquals(3, typeService.getAll().size()); + Assertions.assertEquals(last, typeService.get(3L)); + } + + @Test + @Order(2) + void updateTest() { + final String test = "TEST"; + final TypeEntity entity = typeService.get(3L); + final String oldName = entity.getName(); + final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test)); + Assertions.assertEquals(3, typeService.getAll().size()); + Assertions.assertEquals(newEntity, typeService.get(3L)); + Assertions.assertEquals(test, newEntity.getName()); + Assertions.assertNotEquals(oldName, newEntity.getName()); + } + + @Test + @Order(3) + void deleteTest() { + typeService.delete(3L); + Assertions.assertEquals(2, typeService.getAll().size()); + final TypeEntity last = typeService.get(2L); + Assertions.assertEquals(2L, last.getId()); + + final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Игровая приставка")); + Assertions.assertEquals(3, typeService.getAll().size()); + Assertions.assertEquals(4L, newEntity.getId()); + } +} diff --git a/lab2.2/Лекция2-src/src/test/java/com/example/demo/ReviewServiceTests.java b/lab2.2/Лекция2-src/src/test/java/com/example/demo/ReviewServiceTests.java new file mode 100644 index 0000000..409d4d9 --- /dev/null +++ b/lab2.2/Лекция2-src/src/test/java/com/example/demo/ReviewServiceTests.java @@ -0,0 +1,61 @@ +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.types.model.TypeEntity; +import com.example.demo.types.service.TypeService; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +class TypeServiceTests { + @Autowired + private TypeService typeService; + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L)); + } + + @Test + @Order(1) + void createTest() { + typeService.create(new TypeEntity(null, "Ноутбук")); + typeService.create(new TypeEntity(null, "Телефон")); + final TypeEntity last = typeService.create(new TypeEntity(null, "Игровая приставка")); + Assertions.assertEquals(3, typeService.getAll().size()); + Assertions.assertEquals(last, typeService.get(3L)); + } + + @Test + @Order(2) + void updateTest() { + final String test = "TEST"; + final TypeEntity entity = typeService.get(3L); + final String oldName = entity.getName(); + final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test)); + Assertions.assertEquals(3, typeService.getAll().size()); + Assertions.assertEquals(newEntity, typeService.get(3L)); + Assertions.assertEquals(test, newEntity.getName()); + Assertions.assertNotEquals(oldName, newEntity.getName()); + } + + @Test + @Order(3) + void deleteTest() { + typeService.delete(3L); + Assertions.assertEquals(2, typeService.getAll().size()); + final TypeEntity last = typeService.get(2L); + Assertions.assertEquals(2L, last.getId()); + + final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Игровая приставка")); + Assertions.assertEquals(3, typeService.getAll().size()); + Assertions.assertEquals(4L, newEntity.getId()); + } +} diff --git a/lab2.2/Лекция2-src/src/test/java/com/example/demo/UserServiceTests.java b/lab2.2/Лекция2-src/src/test/java/com/example/demo/UserServiceTests.java new file mode 100644 index 0000000..409d4d9 --- /dev/null +++ b/lab2.2/Лекция2-src/src/test/java/com/example/demo/UserServiceTests.java @@ -0,0 +1,61 @@ +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.types.model.TypeEntity; +import com.example.demo.types.service.TypeService; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +class TypeServiceTests { + @Autowired + private TypeService typeService; + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L)); + } + + @Test + @Order(1) + void createTest() { + typeService.create(new TypeEntity(null, "Ноутбук")); + typeService.create(new TypeEntity(null, "Телефон")); + final TypeEntity last = typeService.create(new TypeEntity(null, "Игровая приставка")); + Assertions.assertEquals(3, typeService.getAll().size()); + Assertions.assertEquals(last, typeService.get(3L)); + } + + @Test + @Order(2) + void updateTest() { + final String test = "TEST"; + final TypeEntity entity = typeService.get(3L); + final String oldName = entity.getName(); + final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test)); + Assertions.assertEquals(3, typeService.getAll().size()); + Assertions.assertEquals(newEntity, typeService.get(3L)); + Assertions.assertEquals(test, newEntity.getName()); + Assertions.assertNotEquals(oldName, newEntity.getName()); + } + + @Test + @Order(3) + void deleteTest() { + typeService.delete(3L); + Assertions.assertEquals(2, typeService.getAll().size()); + final TypeEntity last = typeService.get(2L); + Assertions.assertEquals(2L, last.getId()); + + final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Игровая приставка")); + Assertions.assertEquals(3, typeService.getAll().size()); + Assertions.assertEquals(4L, newEntity.getId()); + } +}