From 0cc31e6fc1a9738b83809f1d40f82742c885daf9 Mon Sep 17 00:00:00 2001 From: Tatjana Date: Thu, 9 Apr 2026 22:53:44 +0200 Subject: [PATCH 1/3] Testtest: add validation tests for VetRequest DTO * Added unit tests to verify Bean Validation constraints on VetRequest * Covered required fields (@NotNull, @NotBlank) and size limits (@Size) * Ensured valid input passes without violations * Verified invalid inputs produce expected validation errors * Improves API input validation reliability and test coverage --- .../dto/request/vet/VetRequestTest.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/test/java/org/example/vet1177/dto/request/vet/VetRequestTest.java diff --git a/src/test/java/org/example/vet1177/dto/request/vet/VetRequestTest.java b/src/test/java/org/example/vet1177/dto/request/vet/VetRequestTest.java new file mode 100644 index 00000000..a32a06e2 --- /dev/null +++ b/src/test/java/org/example/vet1177/dto/request/vet/VetRequestTest.java @@ -0,0 +1,79 @@ +package org.example.vet1177.dto.request.vet; + +import jakarta.validation.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Set; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +class VetRequestTest { + + private Validator validator; + + @BeforeEach + void setUp() { + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); + validator = factory.getValidator(); + } + + @Test + void should_pass_validation_when_valid_input() { + VetRequest request = new VetRequest( + UUID.randomUUID(), + "LIC123", + "Surgery", + "Available weekdays" + ); + + Set> violations = validator.validate(request); + + assertTrue(violations.isEmpty()); + } + + @Test + void should_fail_when_userId_is_null() { + VetRequest request = new VetRequest( + null, + "LIC123", + "Surgery", + "Info" + ); + + Set> violations = validator.validate(request); + + assertFalse(violations.isEmpty()); + } + + @Test + void should_fail_when_licenseId_is_blank() { + VetRequest request = new VetRequest( + UUID.randomUUID(), + "", + "Surgery", + "Info" + ); + + Set> violations = validator.validate(request); + + assertFalse(violations.isEmpty()); + } + + @Test + void should_fail_when_licenseId_too_long() { + String longLicense = "A".repeat(51); + + VetRequest request = new VetRequest( + UUID.randomUUID(), + longLicense, + "Surgery", + "Info" + ); + + Set> violations = validator.validate(request); + + assertFalse(violations.isEmpty()); + } +} From b15ae6d766a1cc86aee5e2f5d8dc269a77712755 Mon Sep 17 00:00:00 2001 From: Tatjana Date: Thu, 9 Apr 2026 22:54:12 +0200 Subject: [PATCH 2/3] Test: add validation tests for VetRequest DTO * Added unit tests to verify Bean Validation constraints on VetRequest * Covered required fields (@NotNull, @NotBlank) and size limits (@Size) * Ensured valid input passes without violations * Verified invalid inputs produce expected validation errors * Improves API input validation reliability and test coverage --- .../org/example/vet1177/dto/request/vet/VetRequestTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/example/vet1177/dto/request/vet/VetRequestTest.java b/src/test/java/org/example/vet1177/dto/request/vet/VetRequestTest.java index a32a06e2..cf01b26b 100644 --- a/src/test/java/org/example/vet1177/dto/request/vet/VetRequestTest.java +++ b/src/test/java/org/example/vet1177/dto/request/vet/VetRequestTest.java @@ -76,4 +76,4 @@ void should_fail_when_licenseId_too_long() { assertFalse(violations.isEmpty()); } -} +} \ No newline at end of file From 7716ec3c6ed2639266b7cf44f8a5e9956af1a286 Mon Sep 17 00:00:00 2001 From: Tatjana Date: Thu, 9 Apr 2026 23:58:11 +0200 Subject: [PATCH 3/3] Test: improve VetRequest validation tests with field-level assertions * Added helper method to assert specific constraint violations per field * Replaced generic "violations not empty" checks with precise field validation * Ensures tests fail only when incorrect fields are validated * Improves robustness and accuracy of validation test coverage --- .../vet1177/dto/request/vet/VetRequestTest.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/example/vet1177/dto/request/vet/VetRequestTest.java b/src/test/java/org/example/vet1177/dto/request/vet/VetRequestTest.java index cf01b26b..03ceb2ea 100644 --- a/src/test/java/org/example/vet1177/dto/request/vet/VetRequestTest.java +++ b/src/test/java/org/example/vet1177/dto/request/vet/VetRequestTest.java @@ -19,6 +19,11 @@ void setUp() { validator = factory.getValidator(); } + private void assertHasViolation(Set> violations, String field) { + assertTrue( violations.stream() + .anyMatch(v -> v.getPropertyPath() + .toString().equals(field)), "Expected violation on field: " + field ); } + @Test void should_pass_validation_when_valid_input() { VetRequest request = new VetRequest( @@ -44,7 +49,7 @@ void should_fail_when_userId_is_null() { Set> violations = validator.validate(request); - assertFalse(violations.isEmpty()); + assertHasViolation(violations, "userId"); } @Test @@ -58,7 +63,7 @@ void should_fail_when_licenseId_is_blank() { Set> violations = validator.validate(request); - assertFalse(violations.isEmpty()); + assertHasViolation(violations, "licenseId"); } @Test @@ -74,6 +79,6 @@ void should_fail_when_licenseId_too_long() { Set> violations = validator.validate(request); - assertFalse(violations.isEmpty()); + assertHasViolation(violations, "licenseId"); } } \ No newline at end of file