|
1 | 1 | package hexlet.code; |
2 | 2 |
|
| 3 | +import hexlet.code.controller.UrlsController; |
3 | 4 | import hexlet.code.model.Url; |
4 | 5 | import hexlet.code.repository.UrlRepository; |
5 | 6 | import hexlet.code.util.NamedRoutes; |
6 | 7 | import io.javalin.Javalin; |
7 | | -import io.javalin.http.NotFoundResponse; |
| 8 | +import io.javalin.http.Context; |
8 | 9 | import io.javalin.testtools.JavalinTest; |
9 | 10 | import mockwebserver3.MockResponse; |
10 | 11 | import mockwebserver3.MockWebServer; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 14 | +import static org.mockito.Mockito.mock; |
| 15 | +import static org.mockito.Mockito.times; |
| 16 | +import static org.mockito.Mockito.verify; |
| 17 | +import static org.mockito.Mockito.when; |
11 | 18 | import okhttp3.Headers; |
12 | 19 | import org.junit.jupiter.api.AfterAll; |
13 | 20 | import org.junit.jupiter.api.BeforeEach; |
|
20 | 27 | import static org.junit.jupiter.api.Assertions.assertTrue; |
21 | 28 |
|
22 | 29 | public final class AppTest { |
| 30 | + private Context ctx; |
23 | 31 | private static MockWebServer server; |
| 32 | + private String rawUrl; |
24 | 33 | private Javalin app; |
25 | 34 |
|
26 | 35 | @BeforeEach |
27 | 36 | public void setUp() throws SQLException, IOException { |
| 37 | + ctx = mock(Context.class); |
| 38 | + |
28 | 39 | server = new MockWebServer(); |
29 | 40 | var response = new MockResponse(200, new Headers.Builder().build(), "i'm so tired"); |
30 | 41 | server.enqueue(response); |
31 | 42 | server.start(); |
32 | 43 |
|
| 44 | + rawUrl = server.url("/").toString(); |
| 45 | + |
33 | 46 | app = App.getApp(); |
34 | 47 | } |
35 | 48 |
|
@@ -86,31 +99,44 @@ public void testUrlNotFound() { |
86 | 99 | } |
87 | 100 |
|
88 | 101 | @Test |
89 | | - public void testSaveUrl() { |
90 | | - var urlName = "https://one.com"; |
| 102 | + public void testInvalidUrl() { |
| 103 | + JavalinTest.test(app, (server, client) -> { |
| 104 | + when(ctx.formParam("url")).thenReturn(rawUrl); |
91 | 105 |
|
| 106 | + UrlsController.create(ctx); |
| 107 | + verify(ctx).sessionAttribute("flash", "Страница успешно добавлена"); |
| 108 | + verify(ctx).sessionAttribute("flash-type", "alert alert-success"); |
| 109 | + |
| 110 | + assertDoesNotThrow(() -> UrlsController.create(ctx)); |
| 111 | + verify(ctx).sessionAttribute("flash", "Страница уже существует"); |
| 112 | + verify(ctx).sessionAttribute("flash-type", "alert alert-danger"); |
| 113 | + |
| 114 | + verify(ctx, times(2)).redirect(NamedRoutes.rootPath()); |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testPostUrl() { |
92 | 120 | JavalinTest.test(app, (server, client) -> { |
93 | | - var requestBody = "url=" + urlName; |
| 121 | + var requestBody = "url=" + rawUrl; |
94 | 122 | var postResponse = client.post(NamedRoutes.urlsPath(), requestBody); |
95 | | - var url = UrlRepository.findByName(urlName).orElseThrow(NotFoundResponse::new); |
96 | | - |
97 | | - assertEquals(urlName, url.getName()); |
98 | 123 | assertEquals(200, postResponse.code()); |
99 | 124 | assertTrue(postResponse.body().string().contains("Главная страница")); |
100 | 125 |
|
| 126 | + var urlName = UrlsController.normalizeUrlName(rawUrl); |
101 | 127 | var getResponse = client.get(NamedRoutes.urlsPath()); |
102 | 128 | assertTrue(getResponse.body().string().contains(urlName)); |
103 | 129 | }); |
104 | 130 | } |
105 | 131 |
|
106 | 132 | @Test |
107 | | - public void testUrlCheck() throws SQLException { |
108 | | - var urlName = server.url("/").toString(); |
109 | | - var url = new Url(urlName); |
110 | | - UrlRepository.save(url); |
111 | | - var urlId = UrlRepository.findByName(urlName).get().getId(); |
112 | | - |
| 133 | + public void testPostUrlCheck() { |
113 | 134 | JavalinTest.test(app, (server, client) -> { |
| 135 | + var urlName = UrlsController.normalizeUrlName(rawUrl); |
| 136 | + var url = new Url(urlName); |
| 137 | + UrlRepository.save(url); |
| 138 | + var urlId = UrlRepository.findByName(urlName).get().getId(); |
| 139 | + |
114 | 140 | var postResponse = client.post(NamedRoutes.urlChecksPath(urlId)); |
115 | 141 | assertEquals(200, postResponse.code()); |
116 | 142 | }); |
|
0 commit comments