diff --git a/nekontakte/src/test/java/com/example/nekontakte/PostServiceTests.java b/nekontakte/src/test/java/com/example/nekontakte/PostServiceTests.java index a50afd1..5b5dc6e 100644 --- a/nekontakte/src/test/java/com/example/nekontakte/PostServiceTests.java +++ b/nekontakte/src/test/java/com/example/nekontakte/PostServiceTests.java @@ -22,119 +22,119 @@ import org.junit.jupiter.api.TestMethodOrder; @SpringBootTest @TestMethodOrder(OrderAnnotation.class) public class PostServiceTests { - @Autowired - private PostService postService; + @Autowired + private PostService postService; - @Test - void getTest() { - Assertions.assertThrows(NotFoundException.class, () -> postService.get(0)); - } + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> postService.get(0)); + } - @Test - @Order(1) - void createTest() throws ParseException, BadRequestException { - SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy", Locale.ENGLISH); - SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss", Locale.ENGLISH); + @Test + @Order(1) + void createTest() throws ParseException, BadRequestException { + SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy", Locale.ENGLISH); + SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss", Locale.ENGLISH); - final UserEntity firstUser = new UserEntity( - null, - "nspotapov", - "pass123456", - true, - "Никита", - "Потапов", - dateFormatter.parse("17.02.2003"), - "Ульяновск", - null, - "Я здесь админ"); - final UserEntity secondUser = new UserEntity( - null, - "ekallin", - "pass87654321", - false, - "Елена", - "Каллин", - dateFormatter.parse("14.06.2005"), - "Новоульяновск", - null, - null); + final UserEntity firstUser = new UserEntity(); + firstUser.setUsername("nspotapov"); + firstUser.setPassword("pass123456"); + firstUser.setIsAdmin(true); + firstUser.setName("Никита"); + firstUser.setSurname("Потапов"); + firstUser.setCity("Ульяновск"); + firstUser.setStatus("Я здесь админ"); + firstUser.setBirthday(dateFormatter.parse("17.02.2003")); - final PostEntity firstPost = postService - .create(new PostEntity( - null, - firstUser, - dateTimeFormatter.parse("01.04.2024, 17:02:34"), - null, "Первый тестовый пост")); + final UserEntity secondUser = new UserEntity(); + secondUser.setUsername("ekallin"); + secondUser.setPassword("pass87654321"); + secondUser.setIsAdmin(false); + secondUser.setName("Елена"); + secondUser.setSurname("Каллин"); + secondUser.setCity("Новоульяновск"); + secondUser.setBirthday(dateFormatter.parse("14.06.2005")); - postService.create(new PostEntity( - null, - firstUser, - dateTimeFormatter.parse("22.03.2024, 09:34:01"), - "myimage.jpg", "Второй тестовый пост")); + PostEntity firstPost = new PostEntity(); + firstPost.setUser(firstUser); + firstPost.setPubDate(dateTimeFormatter.parse("01.04.2024, 17:02:34")); + firstPost.setHtml("Первый тестовый пост"); - postService.create(new PostEntity( - null, - secondUser, - dateTimeFormatter.parse("13.02.2024, 18:01:01"), - "ohmypost.jpg", "Первый постовый пост")); + firstPost = postService.create(firstPost); - Assertions.assertThrows(BadRequestException.class, () -> postService - .create(new PostEntity( - null, - secondUser, - dateTimeFormatter.parse("13.02.2024, 18:01:01"), - null, null))); + PostEntity secondPost = new PostEntity(); + secondPost.setUser(firstUser); + secondPost.setPubDate(dateTimeFormatter.parse("22.03.2024, 09:34:01")); + secondPost.setImage("myimage.jpg"); + secondPost.setHtml("Второй тестовый пост"); - Assertions.assertEquals(firstPost, postService.get(1)); - Assertions.assertEquals(firstUser, postService.get(1).getUser()); - Assertions.assertEquals(secondUser, postService.get(3).getUser()); - Assertions.assertEquals(3, postService.getAll().size()); - Assertions.assertEquals(2, postService.getAllByUser(firstUser).size()); - } + postService.create(secondPost); - @Test - @Order(2) - void update() throws BadRequestException, ParseException { - SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy", Locale.ENGLISH); - SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss", Locale.ENGLISH); + PostEntity thirdPost = new PostEntity(); + thirdPost.setUser(secondUser); + thirdPost.setPubDate(dateTimeFormatter.parse("13.02.2024, 18:01:01")); + thirdPost.setImage("ohmypost.jpg"); + thirdPost.setHtml("Третий постовый пост"); - final UserEntity firstUser = new UserEntity( - null, - "nspotapov", - "pass123456", - true, - "Никита", - "Потапов", - dateFormatter.parse("17.02.2003"), - "Ульяновск", - null, - "Я здесь админ"); + postService.create(thirdPost); - postService.create(new PostEntity( - null, - firstUser, - dateTimeFormatter.parse("01.04.2024, 17:02:34"), - null, "Первый тестовый пост")); + PostEntity fourthPost = new PostEntity(); + fourthPost.setUser(secondUser); + fourthPost.setPubDate(dateTimeFormatter.parse("13.02.2024, 18:01:01")); - Integer postId = 1; - PostEntity oldPost = postService.get(postId); - String oldImage = oldPost.getImage(); - PostEntity newPost = new PostEntity( - oldPost.getId(), - oldPost.getUser(), - oldPost.getPubDate(), - "new image", - oldPost.getHtml()); - postService.update(postId, newPost); - Assertions.assertNotEquals( - postService.get(postId).getImage(), oldImage); - } + Assertions.assertThrows(BadRequestException.class, + () -> postService.create(fourthPost)); - @Test - @Order(3) - void delete() { - final int oldCount = postService.getAll().size(); - postService.delete(1); - Assertions.assertEquals(oldCount - 1, postService.getAll().size()); - } + Assertions.assertEquals(firstPost, postService.get(1)); + Assertions.assertEquals(firstUser, postService.get(1).getUser()); + Assertions.assertEquals(secondUser, postService.get(3).getUser()); + Assertions.assertEquals(3, postService.getAll().size()); + Assertions.assertEquals(2, postService.getAllByUser(firstUser).size()); + } + + @Test + @Order(2) + void update() throws BadRequestException, ParseException { + SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy", Locale.ENGLISH); + SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss", Locale.ENGLISH); + + final UserEntity firstUser = new UserEntity(); + firstUser.setUsername("nspotapov"); + firstUser.setPassword("pass123456"); + firstUser.setIsAdmin(true); + firstUser.setName("Никита"); + firstUser.setSurname("Потапов"); + firstUser.setCity("Ульяновск"); + firstUser.setStatus("Я здесь админ"); + firstUser.setBirthday(dateFormatter.parse("17.02.2003")); + + PostEntity firstPost = new PostEntity(); + firstPost.setUser(firstUser); + firstPost.setPubDate(dateTimeFormatter.parse("01.04.2024, 17:02:34")); + firstPost.setHtml("Первый тестовый пост"); + + firstPost = postService.create(firstPost); + + Integer postId = 1; + PostEntity oldPost = postService.get(postId); + String oldImage = oldPost.getImage(); + + PostEntity newPost = new PostEntity(); + newPost.setId(oldPost.getId()); + newPost.setUser(oldPost.getUser()); + newPost.setPubDate(oldPost.getPubDate()); + newPost.setImage("new image"); + newPost.setHtml(oldPost.getHtml()); + + postService.update(postId, newPost); + Assertions.assertNotEquals(postService.get(postId).getImage(), oldImage); + } + + @Test + @Order(3) + void delete() { + final int oldCount = postService.getAll().size(); + postService.delete(1); + Assertions.assertEquals(oldCount - 1, postService.getAll().size()); + } }