22
33import hexlet .code .controller .UrlsController ;
44import hexlet .code .model .Url ;
5+ import hexlet .code .repository .UrlCheckRepository ;
56import hexlet .code .repository .UrlRepository ;
67import hexlet .code .util .NamedRoutes ;
78import io .javalin .Javalin ;
89import io .javalin .http .Context ;
10+ import io .javalin .http .NotFoundResponse ;
911import io .javalin .testtools .JavalinTest ;
1012import mockwebserver3 .MockResponse ;
1113import mockwebserver3 .MockWebServer ;
1618import static org .mockito .Mockito .verify ;
1719import static org .mockito .Mockito .when ;
1820import okhttp3 .Headers ;
21+ import org .jsoup .Jsoup ;
22+ import org .jsoup .nodes .Document ;
1923import org .junit .jupiter .api .AfterAll ;
24+ import org .junit .jupiter .api .BeforeAll ;
2025import org .junit .jupiter .api .BeforeEach ;
2126import org .junit .jupiter .api .Test ;
2227
2833import static org .junit .jupiter .api .Assertions .assertTrue ;
2934
3035public final class AppTest {
36+ private Javalin app ;
3137 private Context ctx ;
3238 private static MockWebServer server ;
3339 private String rawUrl ;
34- private Javalin app ;
40+
41+ private static Document html ;
42+ private static String title = "title example" ;
43+ private static String h1 = "h1 example" ;
44+ private static String description = "description example" ;
45+
46+ @ BeforeAll
47+ public static void mockHtml () {
48+ title = "title example" ;
49+ h1 = "h1 example" ;
50+ description = "description example" ;
51+
52+ html = Jsoup .parse ("" );
53+ html .title (title );
54+ var head = html .appendElement ("html" ).appendElement ("head" );
55+ head .appendElement ("meta" )
56+ .attr ("name" , "description" )
57+ .attr ("content" , description );
58+
59+ var body = html .appendElement ("body" );
60+ body .appendElement ("h1" ).text (h1 );
61+ }
3562
3663 @ BeforeEach
3764 public void setUp () throws SQLException , IOException {
38- ctx = mock (Context .class );
39-
4065 server = new MockWebServer ();
41- var response = new MockResponse (200 , new Headers .Builder ().build (), "i'm so tired" );
66+ var response = new MockResponse (200 , new Headers .Builder ().build (), html . toString () );
4267 server .enqueue (response );
4368 server .start ();
4469
4570 rawUrl = server .url ("/" ).toString ();
4671
4772 app = App .getApp ();
73+ ctx = mock (Context .class );
4874 }
4975
5076 @ AfterAll
@@ -131,9 +157,10 @@ public void testUrlIncorrect() {
131157 public void testPostUrl () {
132158 JavalinTest .test (app , (server , client ) -> {
133159 var requestBody = "url=" + rawUrl ;
134- var postResponse = client .post (NamedRoutes .urlsPath (), requestBody );
135- assertEquals (200 , postResponse .code ());
136- assertTrue (postResponse .body ().string ().contains ("Главная страница" ));
160+ try (var postResponse = client .post (NamedRoutes .urlsPath (), requestBody )) {
161+ assertEquals (200 , postResponse .code ());
162+ assertTrue (postResponse .body ().string ().contains ("Главная страница" ));
163+ }
137164
138165 var parsedUrl = new URI (rawUrl );
139166 var urlName = UrlsController .normalizeUrlName (parsedUrl );
@@ -149,10 +176,17 @@ public void testPostUrlCheck() {
149176 var urlName = UrlsController .normalizeUrlName (parsedUrl );
150177 var url = new Url (urlName );
151178 UrlRepository .save (url );
152- var urlId = UrlRepository .findByName (urlName ).get ().getId ();
179+ var urlId = UrlRepository .findByName (urlName ).orElseThrow (NotFoundResponse ::new ).getId ();
180+
181+ try (var postResponse = client .post (NamedRoutes .urlChecksPath (urlId ))) {
182+ assertEquals (200 , postResponse .code ());
183+ }
153184
154- var postResponse = client .post (NamedRoutes .urlChecksPath (urlId ));
155- assertEquals (200 , postResponse .code ());
185+ var check = UrlCheckRepository .findByUrlId (urlId ).getLast ();
186+ assertEquals (200 , check .getStatusCode ());
187+ assertEquals (title , check .getTitle ());
188+ assertEquals (h1 , check .getH1 ());
189+ assertEquals (description , check .getDescription ());
156190 });
157191 }
158192}
0 commit comments