BAEL-5692 Validate UUID String in Java (#12635)

* BAEL-5692 Validate UUID String in Java

* cleaned up the Main method

* added unit test for uuid validation

* renamed unit test method name

* formatted unit test

* removed unneeded file

* replaced gradle by maven

* removed gradle project

* moved project

* added readme file

* segregated unit tests by validator type.

* inlined the validating logic inside the unit tests

* moved the UUIDValidatorTest.java to core-java-uuid module

* renamed test

Co-authored-by: andrei-mandris <andrei.mandris.ext@deliveryhero.com>
This commit is contained in:
AndreiMandris 2022-09-23 19:44:52 +03:00 committed by GitHub
parent aa9c3271a9
commit d5ad2e0ed8
1 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package com.baeldung.uuid;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.UUID;
import java.util.regex.Pattern;
public class UUIDValidatorUnitTest {
@Test
public void whenValidUUIDStringIsValidated_thenValidationSucceeds() {
String validUUID = "26929514-237c-11ed-861d-0242ac120002";
Assertions.assertEquals(UUID.fromString(validUUID).toString(), validUUID);
String invalidUUID = "invalid-uuid";
Assertions.assertThrows(IllegalArgumentException.class, () -> UUID.fromString(invalidUUID));
}
@Test
public void whenUUIDIsValidatedUsingRegex_thenValidationSucceeds() {
Pattern UUID_REGEX =
Pattern.compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");
Assertions.assertTrue(UUID_REGEX.matcher("26929514-237c-11ed-861d-0242ac120002").matches());
Assertions.assertFalse(UUID_REGEX.matcher("invalid-uuid").matches());
}
}