Починил тесты для постов

This commit is contained in:
Никита Потапов 2024-04-29 12:06:45 +04:00
parent 2c033cd280
commit 39f8b460d1

View File

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