-- MARS-828: Test archival strategies
-- Author: Grzegorz Michalski
-- Date: 2026-01-27
-- Description: Comprehensive test scenarios for all archival strategies

SET SERVEROUTPUT ON SIZE UNLIMITED

PROMPT ========================================
PROMPT MARS-828: Testing Archival Strategies
PROMPT ========================================

-- Test 1: THRESHOLD_BASED (backward compatibility)
PROMPT
PROMPT Test 1: THRESHOLD_BASED strategy
DECLARE
    vTestKey NUMBER := -1000;
BEGIN
    INSERT INTO CT_MRDS.A_SOURCE_FILE_CONFIG (
        A_SOURCE_FILE_CONFIG_KEY,
        A_SOURCE_KEY,
        SOURCE_FILE_TYPE,
        SOURCE_FILE_ID,
        SOURCE_FILE_DESC,
        SOURCE_FILE_NAME_PATTERN,
        TABLE_ID,
        TEMPLATE_TABLE_NAME,
        ARCHIVAL_STRATEGY,
        DAYS_FOR_ARCHIVE_THRESHOLD
    ) VALUES (
        vTestKey,
        'TEST1',
        'INPUT',
        'TEST_THRESHOLD',
        'Test threshold-based',
        'test*.csv',
        'TEST_TABLE',
        'CT_ET_TEMPLATES.TEST',
        'THRESHOLD_BASED',
        30
    );
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: THRESHOLD_BASED strategy configured');
    ROLLBACK;
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('FAILED: ' || SQLERRM);
        ROLLBACK;
END;
/

-- Test 2: CURRENT_MONTH_ONLY
PROMPT
PROMPT Test 2: CURRENT_MONTH_ONLY strategy
DECLARE
    vTestKey NUMBER := -1001;
BEGIN
    INSERT INTO CT_MRDS.A_SOURCE_FILE_CONFIG (
        A_SOURCE_FILE_CONFIG_KEY,
        A_SOURCE_KEY,
        SOURCE_FILE_TYPE,
        SOURCE_FILE_ID,
        SOURCE_FILE_DESC,
        SOURCE_FILE_NAME_PATTERN,
        TABLE_ID,
        TEMPLATE_TABLE_NAME,
        ARCHIVAL_STRATEGY
    ) VALUES (
        vTestKey,
        'TEST2',
        'INPUT',
        'TEST_CURRENT_MONTH',
        'Test current month only',
        'test*.csv',
        'TEST_TABLE',
        'CT_ET_TEMPLATES.TEST',
        'CURRENT_MONTH_ONLY'
    );
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: CURRENT_MONTH_ONLY strategy configured');
    ROLLBACK;
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('FAILED: ' || SQLERRM);
        ROLLBACK;
END;
/

-- Test 3: MINIMUM_AGE_MONTHS (should succeed)
PROMPT
PROMPT Test 3: MINIMUM_AGE_MONTHS strategy with valid config
DECLARE
    vTestKey NUMBER := -1002;
BEGIN
    INSERT INTO CT_MRDS.A_SOURCE_FILE_CONFIG (
        A_SOURCE_FILE_CONFIG_KEY,
        A_SOURCE_KEY,
        SOURCE_FILE_TYPE,
        SOURCE_FILE_ID,
        SOURCE_FILE_DESC,
        SOURCE_FILE_NAME_PATTERN,
        TABLE_ID,
        TEMPLATE_TABLE_NAME,
        ARCHIVAL_STRATEGY,
        MINIMUM_AGE_MONTHS
    ) VALUES (
        vTestKey,
        'TEST3',
        'INPUT',
        'TEST_MIN_AGE',
        'Test minimum age months',
        'test*.csv',
        'TEST_TABLE',
        'CT_ET_TEMPLATES.TEST',
        'MINIMUM_AGE_MONTHS',
        6
    );
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: MINIMUM_AGE_MONTHS strategy configured with 6 months');
    ROLLBACK;
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('FAILED: ' || SQLERRM);
        ROLLBACK;
END;
/

-- Test 4: MINIMUM_AGE_MONTHS without value (should fail)
PROMPT
PROMPT Test 4: MINIMUM_AGE_MONTHS strategy without value (should fail)
DECLARE
    vTestKey NUMBER := -1003;
BEGIN
    INSERT INTO CT_MRDS.A_SOURCE_FILE_CONFIG (
        A_SOURCE_FILE_CONFIG_KEY,
        A_SOURCE_KEY,
        SOURCE_FILE_TYPE,
        SOURCE_FILE_ID,
        SOURCE_FILE_DESC,
        SOURCE_FILE_NAME_PATTERN,
        TABLE_ID,
        TEMPLATE_TABLE_NAME,
        ARCHIVAL_STRATEGY,
        MINIMUM_AGE_MONTHS
    ) VALUES (
        vTestKey,
        'TEST4',
        'INPUT',
        'TEST_MIN_AGE_FAIL',
        'Test minimum age months failure',
        'test*.csv',
        'TEST_TABLE',
        'CT_ET_TEMPLATES.TEST',
        'MINIMUM_AGE_MONTHS',
        NULL  -- Should trigger error
    );
    
    DBMS_OUTPUT.PUT_LINE('FAILED: Trigger did not fire - this should have failed!');
    ROLLBACK;
EXCEPTION
    WHEN OTHERS THEN
        IF SQLCODE = -20999 THEN
            DBMS_OUTPUT.PUT_LINE('SUCCESS: Trigger correctly prevented invalid config');
        ELSE
            DBMS_OUTPUT.PUT_LINE('FAILED: Unexpected error: ' || SQLERRM);
        END IF;
        ROLLBACK;
END;
/

-- Test 5: HYBRID strategy
PROMPT
PROMPT Test 5: HYBRID strategy
DECLARE
    vTestKey NUMBER := -1004;
BEGIN
    INSERT INTO CT_MRDS.A_SOURCE_FILE_CONFIG (
        A_SOURCE_FILE_CONFIG_KEY,
        A_SOURCE_KEY,
        SOURCE_FILE_TYPE,
        SOURCE_FILE_ID,
        SOURCE_FILE_DESC,
        SOURCE_FILE_NAME_PATTERN,
        TABLE_ID,
        TEMPLATE_TABLE_NAME,
        ARCHIVAL_STRATEGY,
        MINIMUM_AGE_MONTHS
    ) VALUES (
        vTestKey,
        'TEST5',
        'INPUT',
        'TEST_HYBRID',
        'Test hybrid strategy',
        'test*.csv',
        'TEST_TABLE',
        'CT_ET_TEMPLATES.TEST',
        'HYBRID',
        3
    );
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: HYBRID strategy configured');
    ROLLBACK;
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('FAILED: ' || SQLERRM);
        ROLLBACK;
END;
/

-- Test 6: Invalid strategy (should fail)
PROMPT
PROMPT Test 6: Invalid strategy (should fail)
DECLARE
    vTestKey NUMBER := -1005;
BEGIN
    INSERT INTO CT_MRDS.A_SOURCE_FILE_CONFIG (
        A_SOURCE_FILE_CONFIG_KEY,
        A_SOURCE_KEY,
        SOURCE_FILE_TYPE,
        SOURCE_FILE_ID,
        SOURCE_FILE_DESC,
        SOURCE_FILE_NAME_PATTERN,
        TABLE_ID,
        TEMPLATE_TABLE_NAME,
        ARCHIVAL_STRATEGY
    ) VALUES (
        vTestKey,
        'TEST6',
        'INPUT',
        'TEST_INVALID',
        'Test invalid strategy',
        'test*.csv',
        'TEST_TABLE',
        'CT_ET_TEMPLATES.TEST',
        'INVALID_STRATEGY'
    );
    
    DBMS_OUTPUT.PUT_LINE('FAILED: Check constraint did not fire!');
    ROLLBACK;
EXCEPTION
    WHEN OTHERS THEN
        IF SQLCODE = -2290 THEN
            DBMS_OUTPUT.PUT_LINE('SUCCESS: Check constraint prevented invalid strategy');
        ELSE
            DBMS_OUTPUT.PUT_LINE('FAILED: Unexpected error: ' || SQLERRM);
        END IF;
        ROLLBACK;
END;
/

PROMPT
PROMPT ========================================
PROMPT MARS-828: Testing Complete
PROMPT ========================================
