|
| 1 | +package hexlet.code; |
| 2 | + |
| 3 | +import hexlet.code.model.Url; |
| 4 | +import hexlet.code.repository.UrlRepository; |
| 5 | +import hexlet.code.util.NamedRoutes; |
| 6 | +import io.javalin.Javalin; |
| 7 | +import io.javalin.testtools.JavalinTest; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.sql.SQLException; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 16 | + |
| 17 | +public final class AppTest { |
| 18 | + private Javalin app; |
| 19 | + |
| 20 | + @BeforeEach |
| 21 | + public void setUp() throws SQLException, IOException { |
| 22 | + app = App.getApp(); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testMainPage() { |
| 27 | + JavalinTest.test(app, (server, client) -> { |
| 28 | + var response = client.get(NamedRoutes.rootPath()); |
| 29 | + assertEquals(200, response.code()); |
| 30 | + assertTrue((response.body().string()).contains("Главная страница")); |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testUrlsPage() { |
| 36 | + JavalinTest.test(app, (server, client) -> { |
| 37 | + var response = client.get(NamedRoutes.urlsPath()); |
| 38 | + assertEquals(200, response.code()); |
| 39 | + assertTrue((response.body().string()).contains("Добавленные сайты")); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testUrlPage() { |
| 45 | + JavalinTest.test(app, (server, client) -> { |
| 46 | + |
| 47 | + var url1 = new Url("https://one.com"); |
| 48 | + var url2 = new Url("https://two.com:7070"); |
| 49 | + |
| 50 | + UrlRepository.save(url1); |
| 51 | + UrlRepository.save(url2); |
| 52 | + |
| 53 | + var response1 = client.get(NamedRoutes.urlPath(url1.getId())); |
| 54 | + var response2 = client.get(NamedRoutes.urlPath(url2.getId())); |
| 55 | + |
| 56 | + assertEquals(200, response1.code()); |
| 57 | + assertTrue((response1.body().string()).contains("Сайт")); |
| 58 | + |
| 59 | + assertEquals(200, response2.code()); |
| 60 | + assertTrue((response2.body().string()).contains("Сайт")); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testUrlNotFound() { |
| 66 | + JavalinTest.test(app, (server, client) -> { |
| 67 | + var response = client.get("/urls/1"); |
| 68 | + assertEquals(404, response.code()); |
| 69 | + }); |
| 70 | + } |
| 71 | +} |
0 commit comments