package com.gnx.telemedicine.util;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

/**
 * Unit tests for InputSanitizer utility class.
 */
class InputSanitizerTest {

    @Test
    void testSanitizeHtml() {
        String input = "<script>alert('XSS')</script><p>Safe content</p>";
        String result = InputSanitizer.sanitizeHtml(input);
        assertFalse(result.contains("<script>"), "Should remove script tags");
        assertTrue(result.contains("Safe content"), "Should preserve safe content");
    }

    @Test
    void testSanitizeText() {
        String input = "<p>Text with <b>HTML</b> tags</p>";
        String result = InputSanitizer.sanitizeText(input);
        assertFalse(result.contains("<"), "Should remove all HTML tags");
        assertTrue(result.contains("Text with HTML tags"), "Should preserve text content");
    }

    @Test
    void testSanitizeEmail() {
        String validEmail = "test@example.com";
        String result = InputSanitizer.sanitizeEmail(validEmail);
        assertEquals(validEmail, result, "Should accept valid email");
        
        String invalidEmail = "invalid-email";
        String result2 = InputSanitizer.sanitizeEmail(invalidEmail);
        assertNull(result2, "Should reject invalid email");
    }

    @Test
    void testSanitizePhone() {
        String phone = "+1 (555) 123-4567";
        String result = InputSanitizer.sanitizePhone(phone);
        assertTrue(result.contains("1"), "Should preserve digits");
        assertTrue(result.contains("555"), "Should preserve digits");
    }

    @Test
    void testSanitizeUuid() {
        String validUuid = "123e4567-e89b-12d3-a456-426614174000";
        String result = InputSanitizer.sanitizeUuid(validUuid);
        assertEquals(validUuid, result, "Should accept valid UUID");
        
        String invalidUuid = "invalid-uuid";
        String result2 = InputSanitizer.sanitizeUuid(invalidUuid);
        assertNull(result2, "Should reject invalid UUID");
    }

    @Test
    void testContainsSqlInjection() {
        assertTrue(InputSanitizer.containsSqlInjection("SELECT * FROM users"), "Should detect SQL injection");
        assertTrue(InputSanitizer.containsSqlInjection("' OR '1'='1"), "Should detect SQL injection");
        assertFalse(InputSanitizer.containsSqlInjection("normal text"), "Should not flag normal text");
    }

    @Test
    void testContainsXss() {
        assertTrue(InputSanitizer.containsXss("<script>alert('XSS')</script>"), "Should detect XSS");
        assertTrue(InputSanitizer.containsXss("onclick='malicious()'"), "Should detect event handlers");
        assertFalse(InputSanitizer.containsXss("normal text"), "Should not flag normal text");
    }

    @Test
    void testSanitizeFilePath() {
        String path = "../../../etc/passwd";
        String result = InputSanitizer.sanitizeFilePath(path);
        assertFalse(result.contains(".."), "Should remove directory traversal");
        
        String safePath = "uploads/file.jpg";
        String result2 = InputSanitizer.sanitizeFilePath(safePath);
        assertTrue(result2.contains("file.jpg"), "Should preserve safe path");
    }

    @Test
    void testSanitizeLength() {
        String input = "test";
        String result = InputSanitizer.sanitizeLength(input, 10);
        assertEquals(input, result, "Should accept valid length");
        
        String longInput = "a".repeat(101);
        String result2 = InputSanitizer.sanitizeLength(longInput, 100);
        assertNull(result2, "Should reject input exceeding max length");
    }

    @Test
    void testSanitizeNumeric() {
        String validNumber = "123.45";
        String result = InputSanitizer.sanitizeNumeric(validNumber);
        assertEquals(validNumber, result, "Should accept valid number");
        
        String invalidNumber = "abc123";
        String result2 = InputSanitizer.sanitizeNumeric(invalidNumber);
        assertNull(result2, "Should reject invalid number");
    }
}

