// *****************************************************************************
// CLASS: ListingEligibilityCheckAPITest
// *****************************************************************************
//
// Author: Vertiba
// Date: 03/08/16
// Description: Test class for ListingEligibilityCheckAPI.cls and ListingService.cls
//
// *

@isTest
private class ListingEligibilityCheckAPITest {
	
    private static List<Properties__c> lProperties;
    private static Account acc;
    private static Contact con;
    private static User usr;
    private static AMI__c ami;
    static  ID listingID, listing1ID ;
    static List<Id> listListingId;
    static String preferenceName ;
    
    static void setupTestData() {
        Id rtId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Household').getRecordTypeId();
        acc = new Account();
        acc.Name = 'Test Account';
        acc.RecordTypeId = rtId;
        acc.AccountNumber = '12345';
        insert acc;
        
        Development__c dev = new Development__c();
        dev.Name_of_Development__c = 'amli';
        dev.Project_ID__c = '123';
        insert dev;
      
        Building__c build = new Building__c();
        build.Development__c = dev.Id;
        build.Developer__c = acc.Id;
        build.Maximum_waitlist_size__c = 10;
        build.Number_of_people_currently_on_waitlist__c = 5;
        build.hasWaitlist__c = true;
       	insert build;
        
        Listing__c listing = new Listing__c();
        listing.name='somelisting';
        listing.Building__c  = build.id;
        listing.Account__c = acc.Id;
        listing.Status__c = 'Active';
        listing.Does_Match__c = true;
        insert listing;
        
        listingID = listing.id;
        listListingId = new List<Id>();
        listListingId.add(listingID);
        
        Unit__c unit = new Unit__c();
        unit.property__c  = build.id;
        unit.BMR_Rent_Monthly__c = 400;
        unit.Rent_Multiple__c=3;
        unit.of_AMI_for_Pricing_Unit__c=50;
        unit.Listing__c = listing.id;
        unit.Unit_Type__c = 'Studio';
        insert unit;
        
        Unit__c unit1 = new Unit__c();
        unit1.property__c  = build.id;
        unit1.BMR_Rent_Monthly__c = 400;
        unit1.Rent_Multiple__c=3;
        unit1.of_AMI_for_Pricing_Unit__c=50;
        unit1.Listing__c = listing.id;
        unit1.Unit_Type__c = '1 BR';
        insert unit1;
        
        ami = new AMI__c();
        ami.name='Test Lottery Preference';
        ami.amount__c = 53850;
        ami.Num_of_HouseHold__c=4;
        ami.Percent_of_Median__c=50;
        insert ami;
        
    }
        
    static testMethod void doGetById() {
        setupTestData();
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        req.requestURI = '/Listing/EligibilityCheck/'+listingID;  
        req.httpMethod = 'GET';
        req.addParameter('householdsize', '4');
        RestContext.request = req;
        RestContext.response = res;
        system.assert(ListingEligibilityCheckAPI.doGet().ListingId == listingID);
    }
    
    static testMethod void doGetByIdParam() {
        setupTestData();
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        req.requestURI = '/Listing/EligibilityCheck';  
        req.addParameter('id', listingID);
        req.addParameter('householdsize', '4');
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response = res;
        system.assert(ListingEligibilityCheckAPI.doGet().ListingId == listingID);
    }
    
    /*
     * check if listing matches criteria
     */
    static testMethod void doGetByCriteriaMatch() {
         setupTestData();
         RestRequest req = new RestRequest(); 
         RestResponse res = new RestResponse();
         req.requestURI = '/Listing/EligibilityCheck';  
         req.addParameter('id', listingID);
         req.addParameter('incomelevel', '53850');
         req.addParameter('householdsize', '4');
         req.addParameter('childrenUnder6', '2');
         req.httpMethod = 'GET';
         RestContext.request = req;
         RestContext.response = res;
         ListingService.Eligibility listingEligibility = ListingEligibilityCheckAPI.doGet();
         
         //verify if eligibility matches
         system.assert(listingEligibility.ListingId == listingID);
		 system.assert(listingEligibility.householdEligibilityResult == 'Match');
		// system.assert(listingEligibility.incomeEligibilityResult == 'Match');
		 system.assert(listingEligibility.householdMatch == true);
     //    system.assert(listingEligibility.incomeMatch == true);
        
     }
    
    /*
     * check if eligibility returns too big
     */
    static testMethod void doGetByCriteriaTooBig() {
         setupTestData();
         RestRequest req = new RestRequest(); 
         RestResponse res = new RestResponse();
         req.requestURI = '/Listing/EligibilityCheck';  
         req.addParameter('id', listingID);
         req.addParameter('incomelevel', '100000');
         req.addParameter('householdsize', '6');
         req.addParameter('childrenUnder6', '2');
         req.httpMethod = 'GET';
         RestContext.request = req;
         RestContext.response = res;
         ListingService.Eligibility listingEligibility = ListingEligibilityCheckAPI.doGet();
         
         //verify if eligibility returns too big
         system.assert(listingEligibility.ListingId == listingID);
		 system.assert(listingEligibility.householdEligibilityResult == 'Too Big');
		 system.assert(listingEligibility.incomeEligibilityResult == 'Too High');
		 system.assert(listingEligibility.householdMatch == false);
         system.assert(listingEligibility.incomeMatch == false);
        
     }
    
    /*
     * check if eligibility return too small
     */
    static testMethod void doGetByCriteriaToSmall() {
         setupTestData();
         RestRequest req = new RestRequest(); 
         RestResponse res = new RestResponse();
         req.requestURI = '/Listing/EligibilityCheck';  
         req.addParameter('id', listingID);
         req.addParameter('incomelevel', '10000');
         req.addParameter('householdsize', '0');
         req.addParameter('childrenUnder6', '2');
         req.httpMethod = 'GET';
         RestContext.request = req;
         RestContext.response = res;
         ListingService.Eligibility listingEligibility = ListingEligibilityCheckAPI.doGet();
         
         //verify if eligibility returns too small 
         system.assert(listingEligibility.ListingId == listingID);
		 system.assert(listingEligibility.householdEligibilityResult == 'Too Small');
		 system.assert(listingEligibility.incomeEligibilityResult == 'Too Low');
		 system.assert(listingEligibility.householdMatch == false);
         system.assert(listingEligibility.incomeMatch == false);
        
     }
}