diff --git a/demo/src/test/java/com/example/demo/DemoApplicationTests/CustomerTests.java b/demo/src/test/java/com/example/demo/DemoApplicationTests/CustomerTests.java new file mode 100644 index 0000000..01c9493 --- /dev/null +++ b/demo/src/test/java/com/example/demo/DemoApplicationTests/CustomerTests.java @@ -0,0 +1,63 @@ +package com.example.demo.DemoApplicationTests; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.ArrayList; + + @SpringBootTest + class PostsServiceTest { + @Autowired + private PostService newsService; + + @Autowired + private TageService tageService; + + List tages = new ArrayList<>(); + + @AfterEach + void removeData() { + newsService.getAll().forEach(item -> newsService.delete(item.getId())); + tageService.getAll().forEach(i -> tageService.delete(i.getId())); + } + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> newsService.get(0L)); + } + + @Test + void CreateUpdateDelete() { + //create + removeData(); + + var tage1 = tageService.create(new TageEntity("праздник")); + var tage2 = tageService.create(new TageEntity("сессия")); + tages.add(tage1); + tages.add(tage2); + + // create + var post1 = newsService.create(new PostEntity( + "Название", new Date(), "текст новости", tages)); + var post2 = newsService.create(new PostEntity( + "9 апреля", new Date(), "просто наступил апрель", tages)); + + Assertions.assertEquals(2L, newsService.getAll().size()); + + //update + var post2new = newsService.update(post2.getId(), new PostEntity( + "2 июня", new Date(), "просто наступил ИЮНЬ" + )); + Assertions.assertEquals(2L, newsService.getAll().size()); + + // delete + newsService.delete(post2new.getId()); + Assertions.assertEquals(1L, newsService.getAll().size()); + final var lastNew = newsService.get(post1.getId()); + Assertions.assertEquals(lastNew.getText(), post1.getText()); + } + } +}