/***********************************************************************************************************
 * Name         : SubscriptionTriggerHelperTest
 * Purpose      : Test class for SubscriptionTriggerHelper
 **********************************************************************************************************/

@isTest
private class SubscriptionTriggerHelperTest {
    @TestSetup
    static void makeData() {
        TestFactory.createMailingList();
    }

    /************************************************************************************
     * Method: insertSubscriptionHistory
     * APEX method tested : SubscriptionTriggerHelper.insertSubscriptionHistory
     * Description : Verifies that a Subscription History object is created for each
     *  new subscription
     *************************************************************************************/
    @isTest
    static void insertSubscriptionHistory() {
        Mailing_List__c mailingList = [SELECT Id, TLA_ID__c FROM Mailing_List__c LIMIT 1];

        Subscription__c subscription = new Subscription__c();
        subscription.Mailing_List_ID__c = mailingList.Id;
        subscription.Source__c = 'SMF';
        subscription.DSP__c = 'DSP TEST';
        subscription.Preferred_Genre__c = 'POP';

        Test.startTest();
        insert subscription;
        Test.stopTest();

        List<Subscription_History__c> subsHistory = [
            SELECT
                Id,
                Mailing_List_ID__c,
                Preferred_Genre__c,
                Source__c,
                DSP__c,
                Subscription_ID__c,
                TLA_ID__c,
                Action__c
            FROM Subscription_History__c
        ];

        System.assertEquals(1, subsHistory.size(), 'The Subscription_History__c must be created at this point');

        System.assertEquals(
            mailingList.Id,
            subsHistory[0].Mailing_List_ID__c,
            'The subscription history must have the correct Mailing List Id'
        );
        System.assertEquals(
            'POP',
            subsHistory[0].Preferred_Genre__c,
            'The subscription history must have the correct Preferred_Genre__c value: "POP"'
        );
        System.assertEquals(
            'SMF',
            subsHistory[0].Source__c,
            'The subscription history must have the correct Source__c value: "SMF"'
        );
        System.assertEquals(
            'DSP TEST',
            subsHistory[0].DSP__c,
            'The subscription history must have the correct DSP__c value: "DSP TEST"'
        );
        System.assertEquals(
            subscription.Id,
            subsHistory[0].Subscription_ID__c,
            'The subscription history must have the correct Subscription_ID__c value'
        );
        System.assertEquals(
            mailingList.TLA_ID__c,
            subsHistory[0].TLA_ID__c,
            'The subscription history must have the correct TLA_ID__c value'
        );
        System.assertEquals(
            UtilConstants.SH_ACTION_SIGN_UP,
            subsHistory[0].Action__c,
            'The subscription history must have the correct Action__c value: "Sign up"'
        );
    }

    /************************************************************************************
     * Method: insertSubscriptionHistoryTFRfileUpload
     * APEX method tested : SubscriptionTriggerHelper.insertSubscriptionHistory
     * Description : Verifies that a Subscription History object is created for
     *  each new subscription, but without updating Preferred_Genre__c field
     *************************************************************************************/
    @isTest
    static void insertSubscriptionHistoryTFRfileUpload() {
        Mailing_List__c mailingList = [SELECT Id, TLA_ID__c FROM Mailing_List__c LIMIT 1];

        Subscription__c subscription = new Subscription__c();
        subscription.Mailing_List_ID__c = mailingList.Id;
        subscription.Source__c = UtilConstants.FILE_UPLOAD_TOOL;
        subscription.DSP__c = 'DSP TEST';
        subscription.Preferred_Genre__c = 'POP';

        Test.startTest();
        insert subscription;
        Test.stopTest();

        List<Subscription_History__c> subsHistory = [
            SELECT
                Id,
                Mailing_List_ID__c,
                Preferred_Genre__c,
                Source__c,
                DSP__c,
                Subscription_ID__c,
                TLA_ID__c,
                Action__c
            FROM Subscription_History__c
        ];

        System.assertEquals(1, subsHistory.size(), 'The Subscription_History__c must be created at this point');

        System.assertEquals(
            mailingList.Id,
            subsHistory[0].Mailing_List_ID__c,
            'The subscription history must have the correct Mailing List Id'
        );
        System.assertEquals(
            null,
            subsHistory[0].Preferred_Genre__c,
            'The subscription history must have the correct Preferred_Genre__c value: "null" (because it should not be updated)'
        );
        System.assertEquals(
            UtilConstants.FILE_UPLOAD_TOOL,
            subsHistory[0].Source__c,
            'The subscription history must have the correct Source__c value: "File Upload Tool"'
        );
        System.assertEquals(
            'DSP TEST',
            subsHistory[0].DSP__c,
            'The subscription history must have the correct DSP__c value: "DSP TEST"'
        );
        System.assertEquals(
            subscription.Id,
            subsHistory[0].Subscription_ID__c,
            'The subscription history must have the correct Subscription_ID__c value'
        );
        System.assertEquals(
            mailingList.TLA_ID__c,
            subsHistory[0].TLA_ID__c,
            'The subscription history must have the correct TLA_ID__c value'
        );
        System.assertEquals(
            UtilConstants.SH_ACTION_SIGN_UP,
            subsHistory[0].Action__c,
            'The subscription history must have the correct Action__c value: "Sign up"'
        );
    }
}
