Skip to content

Commit 88f22c2

Browse files
committed
fix some issues noted by SonarQube
1 parent f4b91a8 commit 88f22c2

File tree

8 files changed

+42
-69
lines changed

8 files changed

+42
-69
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
import java.sql.SQLException;
1616

1717
public final class UrlChecksController {
18+
19+
private UrlChecksController() {
20+
throw new AssertionError("Util class cannot be instantiated");
21+
}
22+
1823
public static void create(Context ctx) throws SQLException {
1924

2025
var urlId = ctx.pathParamAsClass("id", Long.class).get();

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
public final class UrlsController {
2020

21+
private UrlsController() {
22+
throw new AssertionError("Util class cannot be instantiated");
23+
}
24+
2125
public static void index(Context ctx) throws SQLException {
2226
var urls = UrlRepository.getEntities();
2327
var latestChecks = UrlCheckRepository.getLatestChecks();
@@ -43,7 +47,7 @@ public static void show(Context ctx) throws SQLException {
4347

4448
public static void create(Context ctx) throws SQLException {
4549

46-
var inputUrl = ctx.formParam("url");
50+
var inputUrl = ctx.formParamAsClass("url", String.class).getOrThrow(NotFoundResponse::new);
4751
URI parsedUrl;
4852
try {
4953
parsedUrl = new URI(inputUrl);

app/src/main/java/hexlet/code/dto/BasePage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
@Getter
77
@Setter
8-
public class BasePage {
8+
public abstract class BasePage {
99
private String flash;
1010
private String flashType;
1111
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
import com.zaxxer.hikari.HikariDataSource;
44

5-
public class BaseRepository {
5+
public abstract class BaseRepository {
66
public static HikariDataSource dataSource;
77
}

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

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
public final class UrlCheckRepository extends BaseRepository {
1515

16+
private UrlCheckRepository() {
17+
throw new AssertionError("Util class cannot be instantiated");
18+
}
19+
1620
public static void save(UrlCheck check) throws SQLException {
1721
var sql = """
1822
INSERT INTO url_checks
@@ -40,29 +44,6 @@ public static void save(UrlCheck check) throws SQLException {
4044
}
4145
}
4246

43-
// public static Optional<UrlCheck> find(Long id) throws SQLException {
44-
// var sql = "SELECT * FROM url_checks WHERE id = ?";
45-
// try (
46-
// var conn = dataSource.getConnection();
47-
// var stmt = conn.prepareStatement(sql)
48-
// ) {
49-
// stmt.setLong(1, id);
50-
//
51-
// var rs = stmt.executeQuery();
52-
// if (rs.next()) {
53-
// var check = UrlCheck.builder()
54-
// .id(id)
55-
// .statusCode(rs.getInt("status_code"))
56-
// .urlId(rs.getLong("url_id"))
57-
// .createdAt(rs.getTimestamp("created_at"))
58-
// .build();
59-
// return Optional.of(check);
60-
// } else {
61-
// return Optional.empty();
62-
// }
63-
// }
64-
// }
65-
6647
public static List<UrlCheck> findByUrlId(Long urlId) throws SQLException {
6748
var sql = "SELECT * FROM url_checks WHERE url_id = ? ORDER BY id";
6849
try (
@@ -117,29 +98,4 @@ SELECT DISTINCT ON (url_id) *
11798
return result;
11899
}
119100
}
120-
121-
// public static List<UrlCheck> getEntities() throws SQLException {
122-
// var sql = "SELECT * FROM url_checks";
123-
// try (
124-
// var conn = dataSource.getConnection();
125-
// var stmt = conn.prepareStatement(sql)
126-
// ) {
127-
// var rs = stmt.executeQuery();
128-
// var result = new ArrayList<UrlCheck>();
129-
// while (rs.next()) {
130-
// var check = UrlCheck.builder()
131-
// .id(rs.getLong("id"))
132-
// .statusCode(rs.getInt("status_code"))
133-
// .title(rs.getString("title"))
134-
// .h1(rs.getString("h1"))
135-
// .description(rs.getString("description"))
136-
// .urlId(rs.getLong("url_id"))
137-
// .createdAt(rs.getTimestamp("created_at").toLocalDateTime())
138-
// .build();
139-
// result.add(check);
140-
// }
141-
//
142-
// return result;
143-
// }
144-
// }
145101
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
public final class UrlRepository extends BaseRepository {
1414

15+
private UrlRepository() {
16+
throw new AssertionError("Util class cannot be instantiated");
17+
}
18+
1519
public static void save(Url url) throws SQLException {
1620
var sql = "INSERT INTO urls (name, created_at) VALUES (?, ?)";
1721
try (var conn = dataSource.getConnection();

app/src/main/java/hexlet/code/util/NamedRoutes.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
public final class NamedRoutes {
44

5+
private NamedRoutes() {
6+
throw new AssertionError("Util class cannot be instantiated");
7+
}
8+
59
public static String rootPath() {
610
return "/";
711
}

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
import static org.junit.jupiter.api.Assertions.assertEquals;
3333
import static org.junit.jupiter.api.Assertions.assertTrue;
3434

35-
public final class AppTest {
35+
final class AppTest {
3636
private Javalin app;
3737
private Context ctx;
38-
private static MockWebServer server;
38+
private static MockWebServer mockWebServer;
3939
private String rawUrl;
4040

4141
private static Document html;
@@ -44,7 +44,7 @@ public final class AppTest {
4444
private static String description = "description example";
4545

4646
@BeforeAll
47-
public static void mockHtml() {
47+
static void mockHtml() {
4848
title = "title example";
4949
h1 = "h1 example";
5050
description = "description example";
@@ -61,25 +61,25 @@ public static void mockHtml() {
6161
}
6262

6363
@BeforeEach
64-
public void setUp() throws SQLException, IOException {
65-
server = new MockWebServer();
64+
void setUp() throws SQLException, IOException {
65+
mockWebServer = new MockWebServer();
6666
var response = new MockResponse(200, new Headers.Builder().build(), html.toString());
67-
server.enqueue(response);
68-
server.start();
67+
mockWebServer.enqueue(response);
68+
mockWebServer.start();
6969

70-
rawUrl = server.url("/").toString();
70+
rawUrl = mockWebServer.url("/").toString();
7171

7272
app = App.getApp();
7373
ctx = mock(Context.class);
7474
}
7575

7676
@AfterAll
77-
public static void tearDown() {
78-
server.close();
77+
static void tearDown() {
78+
mockWebServer.close();
7979
}
8080

8181
@Test
82-
public void testMainPage() {
82+
void testMainPage() {
8383
JavalinTest.test(app, (server, client) -> {
8484
var response = client.get(NamedRoutes.rootPath());
8585
assertEquals(200, response.code());
@@ -88,7 +88,7 @@ public void testMainPage() {
8888
}
8989

9090
@Test
91-
public void testUrlsPage() {
91+
void testUrlsPage() {
9292
JavalinTest.test(app, (server, client) -> {
9393
var response = client.get(NamedRoutes.urlsPath());
9494
assertEquals(200, response.code());
@@ -97,7 +97,7 @@ public void testUrlsPage() {
9797
}
9898

9999
@Test
100-
public void testUrlPage() {
100+
void testUrlPage() {
101101
JavalinTest.test(app, (server, client) -> {
102102

103103
var url1 = new Url("https://one.com");
@@ -118,15 +118,15 @@ public void testUrlPage() {
118118
}
119119

120120
@Test
121-
public void testUrlNotFound() {
121+
void testUrlNotFound() {
122122
JavalinTest.test(app, (server, client) -> {
123123
var response = client.get(NamedRoutes.urlPath("1"));
124124
assertEquals(404, response.code());
125125
});
126126
}
127127

128128
@Test
129-
public void testUrlDuplicate() {
129+
void testUrlDuplicate() {
130130
JavalinTest.test(app, (server, client) -> {
131131
when(ctx.formParam("url")).thenReturn(rawUrl);
132132

@@ -143,7 +143,7 @@ public void testUrlDuplicate() {
143143
}
144144

145145
@Test
146-
public void testUrlIncorrect() {
146+
void testUrlIncorrect() {
147147
JavalinTest.test(app, (server, client) -> {
148148
when(ctx.formParam("url")).thenReturn(" " + rawUrl);
149149
assertDoesNotThrow(() -> UrlsController.create(ctx));
@@ -154,7 +154,7 @@ public void testUrlIncorrect() {
154154
}
155155

156156
@Test
157-
public void testPostUrl() {
157+
void testPostUrl() {
158158
JavalinTest.test(app, (server, client) -> {
159159
var requestBody = "url=" + rawUrl;
160160
try (var postResponse = client.post(NamedRoutes.urlsPath(), requestBody)) {
@@ -170,7 +170,7 @@ public void testPostUrl() {
170170
}
171171

172172
@Test
173-
public void testPostUrlCheck() {
173+
void testPostUrlCheck() {
174174
JavalinTest.test(app, (server, client) -> {
175175
var parsedUrl = new URI(rawUrl);
176176
var urlName = UrlsController.normalizeUrlName(parsedUrl);

0 commit comments

Comments
 (0)