Skip to content

Commit 68906c3

Browse files
Refactor after review
1 parent 508c4ea commit 68906c3

6 files changed

Lines changed: 28 additions & 31 deletions

File tree

app/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,7 @@ sonar {
101101
property("sonar.projectKey", "VictorGotsenko_java-project-72")
102102
property("sonar.organization", "victorgotsenko")
103103
property("sonar.host.url", "https://sonarcloud.io")
104+
// Отключаем проверку зависимостей
105+
property("sonar.dependencyVerification.enabled", "false")
104106
}
105107
}

app/src/main/java/hexlet/code/controller/UrlCheckController.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import io.javalin.http.NotFoundResponse;
2020

2121
import java.sql.SQLException;
22-
import java.time.LocalDateTime;
2322

2423
import kong.unirest.HttpResponse;
2524
import kong.unirest.Unirest;
@@ -50,7 +49,6 @@ public static void check(Context ctx) throws SQLException {
5049
String description = document.select("meta[name=description]").attr("content");
5150

5251
UrlCheck urlCheck = new UrlCheck(statusCode, title, h1, description, urlId);
53-
urlCheck.setCreatedAt(LocalDateTime.now());
5452
log.info("urlCheck created");
5553
UrlCheckRepository.save(urlCheck);
5654
log.info("check saved");
@@ -60,12 +58,10 @@ public static void check(Context ctx) throws SQLException {
6058
log.info("Error in UrlCheckController.check - UnirestException: ", e);
6159
ctx.sessionAttribute(FLASH, URL_BAD);
6260
ctx.sessionAttribute(FLASH_TYPE, FLASH_DANGER);
63-
// ctx.redirect(NamedRoutes.urlPath(urlId))
6461
} catch (SQLException e) {
6562
log.info("Error in UrlCheckController.check - SQLException", e);
6663
ctx.sessionAttribute(FLASH, CHECK_ERROR + e.getMessage());
6764
ctx.sessionAttribute(FLASH_TYPE, FLASH_DANGER);
68-
// ctx.redirect(NamedRoutes.urlPath(urlId))
6965
}
7066
ctx.redirect(NamedRoutes.urlPath(urlId));
7167
}

app/src/main/java/hexlet/code/controller/UrlController.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.net.MalformedURLException;
3232

3333
import java.sql.SQLException;
34-
import java.time.LocalDateTime;
3534
import java.util.List;
3635
import java.util.Map;
3736

@@ -77,15 +76,14 @@ public static void create(Context ctx) throws SQLException, URISyntaxException,
7776

7877
if (UrlRepository.findByName(String.valueOf(url)).isEmpty()) {
7978
Url newUrl = new Url(String.valueOf(url));
80-
newUrl.setCreatedAt(LocalDateTime.now());
8179
UrlRepository.save(newUrl);
80+
log.info("newUrl saveed");
8281
} else {
8382
ctx.sessionAttribute(FLASH, PAGE_EXIST);
8483
ctx.sessionAttribute(FLASH_TYPE, FLASH_INFO);
8584
ctx.redirect(NamedRoutes.urlsPath());
8685
return;
8786
}
88-
8987
ctx.sessionAttribute(FLASH, PAGE_ADDED);
9088
ctx.sessionAttribute(FLASH_TYPE, FLASH_SUCCESS);
9189
ctx.redirect(NamedRoutes.urlsPath());

app/src/main/java/hexlet/code/repository/UrlCheckRepository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.sql.SQLException;
1212
import java.sql.Statement;
1313
import java.sql.Timestamp;
14+
import java.time.LocalDateTime;
1415
import java.util.ArrayList;
1516
import java.util.HashMap;
1617
import java.util.List;
@@ -30,7 +31,7 @@ public static void save(UrlCheck urlCheck) throws SQLException {
3031
preparedStatement.setString(3, urlCheck.getH1());
3132
preparedStatement.setString(4, urlCheck.getDescription());
3233
preparedStatement.setLong(5, urlCheck.getUrlId());
33-
preparedStatement.setTimestamp(6, Timestamp.valueOf(urlCheck.getCreatedAt()));
34+
preparedStatement.setTimestamp(6, Timestamp.valueOf(LocalDateTime.now()));
3435

3536
preparedStatement.executeUpdate();
3637
var generatedKeys = preparedStatement.getGeneratedKeys();

app/src/main/java/hexlet/code/repository/UrlRepository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.sql.SQLException;
99
import java.sql.Statement;
1010
import java.sql.Timestamp;
11+
import java.time.LocalDateTime;
1112
import java.util.ArrayList;
1213
import java.util.List;
1314
import java.util.Optional;
@@ -25,7 +26,7 @@ public static void save(Url url) throws SQLException {
2526
try (Connection conn = BaseRepository.getDataSource().getConnection();
2627
PreparedStatement preparedStatement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
2728
preparedStatement.setString(1, url.getName());
28-
preparedStatement.setTimestamp(2, Timestamp.valueOf(url.getCreatedAt()));
29+
preparedStatement.setTimestamp(2, Timestamp.valueOf(LocalDateTime.now()));
2930
preparedStatement.executeUpdate();
3031
var generatedKeys = preparedStatement.getGeneratedKeys();
3132
if (generatedKeys.next()) {

app/src/test/java/hexlet/code/AppTest.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
import okhttp3.Response;
1212
import okhttp3.mockwebserver.MockResponse;
1313
import okhttp3.mockwebserver.MockWebServer;
14-
import org.junit.jupiter.api.AfterAll;
15-
import org.junit.jupiter.api.BeforeAll;
14+
import org.junit.jupiter.api.AfterEach;
1615
import org.junit.jupiter.api.BeforeEach;
1716
import org.junit.jupiter.api.Test;
1817

@@ -21,7 +20,6 @@
2120
import java.nio.file.Path;
2221
import java.nio.file.Paths;
2322
import java.sql.SQLException;
24-
import java.time.LocalDateTime;
2523
import java.util.List;
2624
import java.util.Map;
2725
import java.util.Optional;
@@ -37,32 +35,27 @@ public static String readFixture(String fileName) throws IOException {
3735
return new String(Files.readAllBytes(filePath));
3836
}
3937

40-
@BeforeAll
41-
static void beforeAll() throws IOException {
42-
mockServer = new MockWebServer();
43-
MockResponse mockedResponse = new MockResponse()
44-
.setBody(readFixture("test.html"));
45-
mockServer.enqueue(mockedResponse);
46-
mockServer.start();
47-
}
48-
4938
@BeforeEach
5039
final void beforeEach() throws SQLException, IOException {
5140
app = App.getApp();
5241
// Очистка базы данных перед каждым тестом
5342
UrlRepository.clear();
43+
// Start
44+
mockServer = new MockWebServer();
45+
MockResponse mockedResponse = new MockResponse().setBody(readFixture("test.html"));
46+
mockServer.enqueue(mockedResponse);
47+
mockServer.start();
5448
}
5549

56-
@AfterAll
57-
static void afterAll() throws IOException {
50+
@AfterEach
51+
void afterEach() throws IOException {
5852
mockServer.shutdown();
5953
}
6054

6155
// Main page
6256
@Test
6357
void testMainPage() {
6458
JavalinTest.test(app, (server, client) -> {
65-
// Response response = client.get("/")
6659
assertThat(client.get("/").code()).isEqualTo(200);
6760
assertThat(client.get("/").body().string()).contains("Анализатор страниц");
6861
});
@@ -72,7 +65,6 @@ void testMainPage() {
7265
@Test
7366
void testUrlSave() throws SQLException {
7467
Url url = new Url("https://mail.ru/");
75-
url.setCreatedAt(LocalDateTime.now());
7668
UrlRepository.save(url);
7769
JavalinTest.test(app, (server, client) -> {
7870
Response response = client.get("/urls/" + url.getId());
@@ -114,7 +106,6 @@ void testAllUrls() {
114106
void testCheck() throws SQLException {
115107
String mockUrl = mockServer.url("/").toString();
116108
Url url = new Url(mockUrl);
117-
url.setCreatedAt(LocalDateTime.now());
118109
UrlRepository.save(url);
119110

120111
JavalinTest.test(app, (server, client) -> {
@@ -128,22 +119,30 @@ void testCheck() throws SQLException {
128119
assertThat(urlCheck.getTitle().equals("Test page"));
129120
assertThat(urlCheck.getDescription().equals("just a simple test page"));
130121
}
131-
Map<Long, UrlCheck> latestChecks = UrlCheckRepository.getLatestChecks();
132-
assertThat(!latestChecks.isEmpty());
133122
});
134123
}
135124

136-
137125
// everything below is for coverage test SonarQube
126+
@Test
127+
void testLatestChecks() throws SQLException {
128+
String mockUrl = mockServer.url("/").toString();
129+
Url url = new Url(mockUrl);
130+
UrlRepository.save(url);
138131

132+
JavalinTest.test(app, (server, client) -> {
133+
Url savedUrl = UrlRepository.findByName(mockServer.url("/").toString()).orElseThrow();
134+
Response response = client.post(NamedRoutes.urlPathForChecks(savedUrl.getId()));
135+
assertThat(response.code()).isEqualTo(200);
136+
Map<Long, UrlCheck> latestChecks = UrlCheckRepository.getLatestChecks();
137+
assertThat(!latestChecks.isEmpty());
138+
});
139+
}
139140

140141
@Test
141142
void testGetEntities() throws SQLException {
142143
Url url = new Url("https://mail.ru/");
143-
url.setCreatedAt(LocalDateTime.now());
144144
UrlRepository.save(url);
145145
url = new Url("https://ya.ru/");
146-
url.setCreatedAt(LocalDateTime.now());
147146
UrlRepository.save(url);
148147

149148
List<Url> getEntities = UrlRepository.getEntities();

0 commit comments

Comments
 (0)