From 9950dd6b4d9cd3e5ff7c8c18a5b3cea7ed7f625a Mon Sep 17 00:00:00 2001 From: Vlasosik <128188585+Vlasosik@users.noreply.github.com> Date: Sun, 21 Apr 2024 12:39:02 +0300 Subject: [PATCH 1/6] added dependency 'okhttp' --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index b6cc7ad..af6d9f7 100644 --- a/build.gradle +++ b/build.gradle @@ -42,6 +42,7 @@ dependencies { testImplementation 'org.springframework.security:spring-security-test' implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.3.0' implementation 'org.springdoc:springdoc-openapi-starter-webflux-ui:2.3.0' + implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.12.0' testImplementation 'org.testcontainers:postgresql' testImplementation 'org.springframework.boot:spring-boot-testcontainers' testImplementation 'org.testcontainers:junit-jupiter' From 594f7264c64c376b7613e18312da8e0cc9559895 Mon Sep 17 00:00:00 2001 From: Vlasosik <128188585+Vlasosik@users.noreply.github.com> Date: Sun, 21 Apr 2024 12:39:28 +0300 Subject: [PATCH 2/6] added test class implementation EmailValidatorImpl. --- .../validation/EmailValidatorImplTest.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/test/java/com/linkurlshorter/urlshortener/auth/validation/EmailValidatorImplTest.java diff --git a/src/test/java/com/linkurlshorter/urlshortener/auth/validation/EmailValidatorImplTest.java b/src/test/java/com/linkurlshorter/urlshortener/auth/validation/EmailValidatorImplTest.java new file mode 100644 index 0000000..f355493 --- /dev/null +++ b/src/test/java/com/linkurlshorter/urlshortener/auth/validation/EmailValidatorImplTest.java @@ -0,0 +1,51 @@ +package com.linkurlshorter.urlshortener.auth.validation; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + + +/** + * Test class for {@link EmailValidatorImpl}. + * This class contains unit tests to verify the functionality of the {@link EmailValidatorImpl} class. + */ +class EmailValidatorImplTest { + + private EmailValidatorImpl emailValidator; + + /** + * Set up method to initialize the {@link EmailValidatorImpl} instance before each test method. + */ + @BeforeEach + void setUp() { + emailValidator = new EmailValidatorImpl(); + } + + /** + * Test method to verify the validation of a valid email address. + * The email address "test@gmail.com" is considered valid. + */ + @Test + void verificationOfValidEmail() { + assertThat(emailValidator.isValid("test@gmail.com", null)).isTrue(); + } + + /** + * Test method to verify the validation of an invalid email address. + * The email address "test.gmail.com" is considered invalid. + */ + @Test + void verificationOfInvalidEmail() { + assertThat(emailValidator.isValid("test.gmail.com", null)).isFalse(); + } + + /** + * Test method to verify the behavior when an empty email address is provided. + * An empty email address is considered invalid. + */ + @Test + void checkingEmptyEmail() { + assertThat(emailValidator.isValid(null, null)).isFalse(); + } +} From 8ad0741475ee3411296e33e19748f211d0cd1b5c Mon Sep 17 00:00:00 2001 From: Vlasosik <128188585+Vlasosik@users.noreply.github.com> Date: Sun, 21 Apr 2024 12:39:41 +0300 Subject: [PATCH 3/6] added test class implementation PasswordValidatorImpl. --- .../validation/PasswordValidatorImplTest.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/test/java/com/linkurlshorter/urlshortener/auth/validation/PasswordValidatorImplTest.java diff --git a/src/test/java/com/linkurlshorter/urlshortener/auth/validation/PasswordValidatorImplTest.java b/src/test/java/com/linkurlshorter/urlshortener/auth/validation/PasswordValidatorImplTest.java new file mode 100644 index 0000000..84e57d0 --- /dev/null +++ b/src/test/java/com/linkurlshorter/urlshortener/auth/validation/PasswordValidatorImplTest.java @@ -0,0 +1,51 @@ +package com.linkurlshorter.urlshortener.auth.validation; + + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +/** + * Test class for{@link PasswordValidatorImpl}. + * This class contains unit tests to verify the functionality of the {@link PasswordValidatorImpl} class. + * + * @author Vlas Pototskyi + */ +class PasswordValidatorImplTest { + private PasswordValidatorImpl passwordValidator; + + /** + * Set up method to initialize the {@link PasswordValidatorImpl} instance before each test method. + */ + @BeforeEach + void setUp() { + passwordValidator = new PasswordValidatorImpl(); + } + + /** + * Test method to verify the validation of a valid password. + * The password "Hogvards_15F" is considered valid. + */ + @Test + void verificationOfValidPassword() { + assertThat(passwordValidator.isValid("Hogvards_15F", null)).isTrue(); + } + + /** + * Test method to verify the validation of an invalid password. + * The password "password" is considered invalid. + */ + @Test + void verificationOfInvalidPassword() { + assertThat(passwordValidator.isValid("password", null)).isFalse(); + } + + /** + * Test method to verify the behavior when an empty password is provided. + * An empty password is considered invalid. + */ + @Test + void checkingEmptyPassword() { + assertThat(passwordValidator.isValid("null", null)).isFalse(); + } +} From 560494ba75715c39e84bc4a28de90883ef6ec706 Mon Sep 17 00:00:00 2001 From: Vlasosik <128188585+Vlasosik@users.noreply.github.com> Date: Sun, 21 Apr 2024 12:40:53 +0300 Subject: [PATCH 4/6] added a null check to the ConstraintValidatorContext. --- .../auth/validation/EmailValidatorImpl.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/linkurlshorter/urlshortener/auth/validation/EmailValidatorImpl.java b/src/main/java/com/linkurlshorter/urlshortener/auth/validation/EmailValidatorImpl.java index 0ed90e6..7e9351c 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/auth/validation/EmailValidatorImpl.java +++ b/src/main/java/com/linkurlshorter/urlshortener/auth/validation/EmailValidatorImpl.java @@ -3,6 +3,8 @@ import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidatorContext; +import java.util.regex.Pattern; + /** * The EmailValidatorImpl class implements {@link ConstraintValidator} the ConstraintValidator interface to * validate email addresses. It checks whether an email address is valid according to a specified regular @@ -11,6 +13,12 @@ * @author Vlas Pototskyi */ public class EmailValidatorImpl implements ConstraintValidator { + /** + * Regular expression pattern for validating email addresses. + */ + private static final Pattern EMAIL_PATTERN = Pattern. + compile("^[A-Za-z0-9]+[._+-]?[A-Za-z0-9]+@[A-Za-z0-9]+[._-]?[A-Za-z0-9]+\\.[A-Za-z]{2,}$"); + /** * Checks if the specified email address matches the specified regular expression pattern. * @@ -20,13 +28,16 @@ public class EmailValidatorImpl implements ConstraintValidator Date: Sun, 21 Apr 2024 12:41:06 +0300 Subject: [PATCH 5/6] added a null check to the ConstraintValidatorContext. --- .../validation/PasswordValidatorImpl.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/linkurlshorter/urlshortener/auth/validation/PasswordValidatorImpl.java b/src/main/java/com/linkurlshorter/urlshortener/auth/validation/PasswordValidatorImpl.java index 8845993..61c99c1 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/auth/validation/PasswordValidatorImpl.java +++ b/src/main/java/com/linkurlshorter/urlshortener/auth/validation/PasswordValidatorImpl.java @@ -1,8 +1,11 @@ + package com.linkurlshorter.urlshortener.auth.validation; import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidatorContext; +import java.util.regex.Pattern; + /** * Implementation {@link ConstraintValidator} of a validator to check the password format. * Validates password format using a regular expression. @@ -10,6 +13,12 @@ * @author Vlas Pototskyi */ public class PasswordValidatorImpl implements ConstraintValidator { + /** + * Regular expression pattern for validating password format. + */ + private static final Pattern PASSWORD_PATTERN = Pattern. + compile("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[^ ]{8,64}$"); + /** * Checks if the entered string matches the password format. * @@ -19,12 +28,16 @@ public class PasswordValidatorImpl implements ConstraintValidator Date: Sun, 21 Apr 2024 12:42:41 +0300 Subject: [PATCH 6/6] small fix method isUrlActive. --- .../link/UrlLongFormatValidatorImpl.java | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/UrlLongFormatValidatorImpl.java b/src/main/java/com/linkurlshorter/urlshortener/link/UrlLongFormatValidatorImpl.java index f83a6a5..af203cd 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/UrlLongFormatValidatorImpl.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/UrlLongFormatValidatorImpl.java @@ -2,10 +2,11 @@ import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidatorContext; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; -import java.net.HttpURLConnection; -import java.net.URI; -import java.net.URL; +import java.io.IOException; /** * Implementation {@link ConstraintValidator} of a validator to check the URL format. @@ -31,10 +32,18 @@ public boolean isValid(String url, ConstraintValidatorContext context) { return false; } if (validateUrl(url)) { + context.buildConstraintViolationWithTemplate("Invalid URL format!") + .addConstraintViolation(); + context.disableDefaultConstraintViolation(); + return false; + } + if (!isUrlActive(url)) { + context.buildConstraintViolationWithTemplate("Url not active!") + .addConstraintViolation(); context.disableDefaultConstraintViolation(); return false; } - return isUrlActive(url); + return true; } /** @@ -65,23 +74,14 @@ private static boolean isUrlNullOrEmpty(String url) { * @return {@code true} if the URL is active (responds with a 200 status code), {@code false} otherwise. */ private boolean isUrlActive(String urlStr) { - HttpURLConnection connection = null; - try { - URL url = new URI(urlStr).toURL(); - connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("GET"); - connection.setConnectTimeout(3000); - connection.setReadTimeout(3000); - connection.connect(); - int responseCode = connection.getResponseCode(); - return (responseCode == 200); - } catch (Exception e) { + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder() + .url(urlStr) + .build(); + try (Response response = client.newCall(request).execute()) { + return response.isSuccessful(); + } catch (IOException e) { return false; - } finally { - if (connection != null) { - connection.disconnect(); - } } } } -