Skip to content

Commit

Permalink
test unitarios
Browse files Browse the repository at this point in the history
  • Loading branch information
lgtoledo committed Oct 11, 2023
1 parent 622bd00 commit 6b1f719
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/test/java/com/lgtoledo/CodeGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
77 changes: 77 additions & 0 deletions src/test/java/com/lgtoledo/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -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, ""));
}

}

0 comments on commit 6b1f719

Please sign in to comment.