Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ plugins {
application
checkstyle
jacoco

id("com.github.johnrengelman.shadow") version "8.1.1"

id("se.patrikerdes.use-latest-versions") version "0.2.18"
id("com.github.ben-manes.versions") version "0.52.0"

id("org.sonarqube") version "6.2.0.5505"
}

Expand All @@ -31,51 +28,39 @@ dependencies {
implementation("io.javalin:javalin:6.7.0")
implementation("io.javalin:javalin-rendering:6.7.0")
implementation("gg.jte:jte:3.2.1")

implementation("org.slf4j:slf4j-simple:2.1.0-alpha1")

implementation("com.fasterxml.jackson.core:jackson-databind:2.19.1")

// LOMBOK
compileOnly("org.projectlombok:lombok:1.18.38")
annotationProcessor("org.projectlombok:lombok:1.18.38")

testCompileOnly("org.projectlombok:lombok:1.18.38")
testAnnotationProcessor("org.projectlombok:lombok:1.18.38")

// CheckStyle
implementation("com.puppycrawl.tools:checkstyle:10.26.0")

implementation("com.puppycrawl.tools:checkstyle:10.26.1")
// Special for PostgreSQL
implementation("org.postgresql:postgresql:42.7.7")

// database H2 & HikariCP
implementation("com.h2database:h2:2.3.232")
implementation("com.zaxxer:HikariCP:6.3.0")

// Tests
testImplementation("org.junit.jupiter:junit-jupiter:5.13.2")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.13.2")

//testImplementation(platform("org.junit:junit-bom:5.13.2"))
//testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.junit.jupiter:junit-jupiter:6.0.0-M1")
testImplementation(platform("org.junit:junit-bom:6.0.0-M1"))
testImplementation("io.javalin:javalin-testtools:6.7.0")
testImplementation("org.assertj:assertj-core:4.0.0-M1")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:6.0.0-M1")
}

checkstyle {
toolVersion = "10.26.0"
toolVersion = "10.26.1"
configFile = file("config/checkstyle/checkstyle.xml")
}

jacoco {
toolVersion = "0.8.12"
}


// Solution for warning "Recompile with -Xlint:unchecked for details"
tasks.withType<JavaCompile> {
options.compilerArgs.addAll(listOf("-Xlint:unchecked", "-Xlint:deprecation")) // Add other desired flags
}


tasks.jacocoTestReport {
reports {
xml.required = true
Expand All @@ -84,6 +69,14 @@ tasks.jacocoTestReport {

tasks.test {
useJUnitPlatform()
testLogging {
exceptionFormat = TestExceptionFormat.FULL
events = mutableSetOf(TestLogEvent.FAILED, TestLogEvent.PASSED, TestLogEvent.SKIPPED)
// showStackTraces = true
// showCauses = true
showStandardStreams = true
}

}

sonar {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/hexlet/code/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ private static String getDatabaseUrl() {
// Получаю url базы данных из переменной окружения DATABASE_URL
// Если она не установлена, используем базу в памяти
// • Use the JDBC URL: jdbc:pgsql://<server>[:<port>]/<database>
// String jdbcUrl = System.getenv().getOrDefault("JDBC_DATABASE_URL", "jdbc:h2:mem:project;DB_CLOSE_DELAY=-1;");
String jdbcUrl = System.getenv().getOrDefault("JDBC_DATABASE_URL", "jdbc:h2:mem:project;DB_CLOSE_DELAY=-1;");
// String jdbcUrl = System.getenv().getOrDefault("JDBC_DATABASE_URL", "jdbc:h2:mem:project");

// Формат записи URL у базы данных должен быть таким:
// jdbc:postgresql://<Hostname>:<Port>/<Database>?password=<Password>&user=<Username>
String jdbcUrl = System.getenv().getOrDefault("JDBC_DATABASE_URL",
"jdbc:postgresql://localhost:5432/project72?password=123z&user=prjct72_user");
// String jdbcUrl = System.getenv().getOrDefault("JDBC_DATABASE_URL",
// "jdbc:postgresql://localhost:5432/project72?password=123z&user=prjct72_user");

log.info(jdbcUrl);
return jdbcUrl;
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/hexlet/code/repository/UrlRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,14 @@ public static List<Url> getEntities() throws SQLException {
return result;
}
}

public static void clear() {
String sql = "DELETE FROM urls;";
try (Connection conn = dataSource.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException("Failed to clear the Url database", e);
}
}
}
63 changes: 59 additions & 4 deletions app/src/test/java/hexlet/code/AppTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,67 @@
package hexlet.code;

import hexlet.code.model.Url;
import hexlet.code.repository.UrlRepository;

import io.javalin.Javalin;
import io.javalin.testtools.JavalinTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import java.sql.SQLException;
import java.time.LocalDateTime;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

class AppTest {
Javalin app;

@BeforeEach
public final void setUp() throws SQLException, IOException {
app = App.getApp();
// Очистка базы данных перед каждым тестом
UrlRepository.clear();
}

@Test
public void testMainPage() {
JavalinTest.test(app, (server, client) -> {
var response = client.get("/");
assertThat(response.code()).isEqualTo(200);
assertThat(response.body().string()).contains("Анализатор страниц");
});
}

//save URL in BD
@Test
public void testUrlSave() throws SQLException {
var url = new Url("https://mail.ru/", LocalDateTime.now());
UrlRepository.save(url);
JavalinTest.test(app, (server, client) -> {
var response = client.get("/urls/" + url.getId());
assertThat(response.code()).isEqualTo(200);
});
}

@Test
public void testCreateUrl() {
JavalinTest.test(app, (server, client) -> {
String requestedBody = "url=https://ya.ru/";
var response = client.post("/urls", requestedBody);
// var response = client.post(NamedRoutes.urlsPath(), requestBody);
var url = UrlRepository.findByName("https://ya.ru");
assertThat(url.get().getName()).isEqualTo("https://ya.ru");
assertThat(response.code()).isEqualTo(200);
assertThat(response.body().string()).contains("https://ya.ru");
});
}

public class AppTest {
@Test
void mainTest() {
assertEquals(true, true);
public void testAllUrls() {
JavalinTest.test(app, (server, client) -> {
var response = client.get("/urls");
assertThat(response.code()).isEqualTo(200);
});
}
}
Loading