diff --git a/src/test/java/com/lgtoledo/CodeGeneratorTest.java b/src/test/java/com/lgtoledo/CodeGeneratorTest.java new file mode 100644 index 0000000..e371a34 --- /dev/null +++ b/src/test/java/com/lgtoledo/CodeGeneratorTest.java @@ -0,0 +1,25 @@ +package com.lgtoledo; + +import org.junit.jupiter.api.Test; + +import com.lgtoledo.utils.CodeGenerator; + +import static org.junit.jupiter.api.Assertions.*; + +public class CodeGeneratorTest { + + @Test + public void testGenerateCode() { + String code = CodeGenerator.generateCode(6); + assertNotNull(code); + assertEquals(6, code.length()); + } + + @Test + public void testGenerateCodeWithZeroLength() { + String code = CodeGenerator.generateCode(0); + assertNotNull(code); + assertEquals("", code); + } + +} \ No newline at end of file diff --git a/src/test/java/com/lgtoledo/UtilsTest.java b/src/test/java/com/lgtoledo/UtilsTest.java new file mode 100644 index 0000000..5a30c38 --- /dev/null +++ b/src/test/java/com/lgtoledo/UtilsTest.java @@ -0,0 +1,77 @@ +package com.lgtoledo; + +import org.junit.jupiter.api.Test; + +import com.lgtoledo.utils.Utils; + +import java.time.LocalDateTime; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +public class UtilsTest { + + @Test + public void testIsValidUrl() { + String longUrlRegex = "^https://.*$"; + assertFalse(Utils.isValidUrl(Optional.of("http://www.mercadolibre.com"), longUrlRegex)); + assertTrue(Utils.isValidUrl(Optional.of("https://www.mercadolibre.com"), longUrlRegex)); + assertFalse(Utils.isValidUrl(Optional.of("www.mercadolibre.com"), longUrlRegex)); + assertFalse(Utils.isValidUrl(Optional.of("http://www.mercadolibre"), longUrlRegex)); + assertFalse(Utils.isValidUrl(Optional.of("http://www.mercadolibre."), longUrlRegex)); + assertFalse(Utils.isValidUrl(Optional.of("http://www.mercadolibre.com/"), longUrlRegex)); + assertFalse(Utils.isValidUrl(Optional.empty(), longUrlRegex)); + } + + @Test + public void testExtractLastPart() { + String shortUrl = "http://localhost:8080/abc123"; + String lastPart = Utils.extractLastPart(shortUrl); + assertEquals("abc123", lastPart); + } + + @Test + public void testGetCurrentUtcDateTime() { + LocalDateTime dateTime = Utils.getCurrentUtcDateTime(); + assertNotNull(dateTime); + } + + @Test + public void testValidShortLink() { + String baseUrl = "https://short.link"; + assertTrue(Utils.isShortLinkValid(baseUrl, "https://short.link/l/abc123")); + } + + @Test + public void testInvalidShortLinkWrongLength() { + String baseUrl = "https://short.link"; + assertFalse(Utils.isShortLinkValid(baseUrl, "https://short.link/l/abc12345")); + } + + @Test + public void testInvalidShortLinkWrongChars() { + String baseUrl = "https://short.link"; + assertFalse(Utils.isShortLinkValid(baseUrl, "https://short.link/l/abc_!@")); + } + + @Test + public void testInvalidShortLinkWithoutL() { + String baseUrl = "https://short.link"; + assertFalse(Utils.isShortLinkValid(baseUrl, "https://short.link/abc123")); + } + + // test if short link is null + @Test + public void testInvalidShortLinkNull() { + String baseUrl = "https://short.link"; + assertFalse(Utils.isShortLinkValid(baseUrl, null)); + } + + // test if short link is empty + @Test + public void testInvalidShortLinkEmpty() { + String baseUrl = "https://short.link"; + assertFalse(Utils.isShortLinkValid(baseUrl, "")); + } + +} \ No newline at end of file