/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class SelectProduct2ExtensionTest {

    static testMethod void myUnitTest() {
        // 製品
        Product2 pr1 = new Product2(Name = 'aiueo製品1', IsActive = true, Asset_Model_No__c = 'aiueo1', SFDA_Status__c = '不要');
        Product2 pr2 = new Product2(Name = 'aiueo製品2', IsActive = true, Asset_Model_No__c = 'aiueo2', SFDA_Status__c = '有効');
        insert new Product2[]{pr1, pr2};
        
        // SFDA証明書
        Product_Material__c pm = new Product_Material__c(Name = 'aiueoSFDA証明書');
        insert pm;
        
    	SelectProduct2Extension target = new SelectProduct2Extension();
    	
    	target.productMaterialId = pm.Id;
    	target.init();
    	System.assertEquals(0, target.productRecords.size());
    	
    	target.assetModelNo = 'aiueo';
    	target.searchProduct();
    	System.assertEquals(2, target.productRecords.size());
    	System.assertEquals('aiueo製品1', target.productRecords[0].rec.Name);
    	
    	target.sortTable();
    	System.assertEquals('aiueo製品2', target.productRecords[0].rec.Name);
    	
    	target.productRecords[1].check = true;
    	target.save();
    	
    	Material_For__c mf = [select Product__c from Material_For__c where Material__c = :pm.Id];
    	System.assertEquals(pr1.Id, mf.Product__c);
    }
    
    static testMethod void initTest1() {
        // 製品
        Product2 pr1 = new Product2(Name = 'aiueo製品1', IsActive = true, Asset_Model_No__c = 'aiueo1', SFDA_Status__c = '有効');
        Product2 pr2 = new Product2(Name = 'aiueo製品2', IsActive = true, Asset_Model_No__c = 'aiueo2', SFDA_Status__c = '有効');
        insert new Product2[]{pr1, pr2};
        
        Product_Material__c pm = new Product_Material__c(Name = 'test pm');
        insert pm;
        Material_For__c mf1 = new Material_For__c(Material__c = pm.Id, Product__c = pr1.Id);
        Material_For__c mf2 = new Material_For__c(Material__c = pm.Id, Product__c = pr2.Id);
        insert new Material_For__c[] {mf1, mf2};
        
        ApexPages.currentPage().getParameters().put( 'pmid', pm.Id);
        
        SelectProduct2Extension target = new SelectProduct2Extension();
        
        target.init();
        System.assertEquals(2, target.productRecords.size());
        
        target.assetModelNo = 'aiueo';
        target.searchProduct();
        System.assertEquals(2, target.productRecords.size());
        System.assertEquals('aiueo製品1', target.productRecords[0].rec.Name);
        
        target.sortTable();
        System.assertEquals('aiueo製品2', target.productRecords[0].rec.Name);
        target.sortKey = '1';
        target.sortTable();
        
        target.productRecords[0].check = false;
        target.save();
        
    }
    
    static testMethod void initTest2() {
        // 製品
        Product2 pr1 = new Product2(Name = 'aiueo製品1', IsActive = true, Asset_Model_No__c = 'aiueo1', SFDA_Status__c = '有効');
        Product2 pr2 = new Product2(Name = 'aiueo製品2', IsActive = true, Asset_Model_No__c = 'aiueo2', SFDA_Status__c = '有効');
        insert new Product2[]{pr1, pr2};
        
        Product_Documentation__c pd = new Product_Documentation__c(Name = 'test pd');
        insert pd;
        Product_and_document_middle_table__c pdm1 = new Product_and_document_middle_table__c(Product_document__c = pd.Id, Product_name__c = pr1.Id);
        Product_and_document_middle_table__c pdm2 = new Product_and_document_middle_table__c(Product_document__c = pd.Id, Product_name__c = pr2.Id);
        insert new Product_and_document_middle_table__c[] {pdm1, pdm2};
        
        ApexPages.currentPage().getParameters().put( 'pdmid', pd.Id);
        
        SelectProduct2Extension target = new SelectProduct2Extension();
        
        target.init();
        System.assertEquals(2, target.productRecords.size());
        
        target.assetModelNo = 'aiueo';
        target.searchProduct();
        System.assertEquals(2, target.productRecords.size());
        System.assertEquals('aiueo製品1', target.productRecords[0].rec.Name);
        
        target.sortTable();
        System.assertEquals('aiueo製品2', target.productRecords[0].rec.Name);
        
        target.productRecords[0].check = false;
        target.saveType = '1';
        target.save();
        
    }
    
    static testMethod void initTest3() {
        // 製品
        Product2 pr1 = new Product2(Name = 'aiueo製品1', IsActive = true, Asset_Model_No__c = 'aiueo1', SFDA_Status__c = '有効');
        Product2 pr2 = new Product2(Name = 'aiueo製品2', IsActive = true, Asset_Model_No__c = 'aiueo2', SFDA_Status__c = '有効');
        insert new Product2[]{pr1, pr2};
        
        Case cic = new Case();
        insert cic;
        CIC_case_and_product_middle_table__c cpm1 = new CIC_case_and_product_middle_table__c(CIC__c = cic.Id, Product__c = pr1.Id);
        CIC_case_and_product_middle_table__c cpm2 = new CIC_case_and_product_middle_table__c(CIC__c = cic.Id, Product__c = pr2.Id);
        insert new CIC_case_and_product_middle_table__c[] {cpm1, cpm2};
        
        ApexPages.currentPage().getParameters().put( 'cicid', cic.Id);
        
        SelectProduct2Extension target = new SelectProduct2Extension();
        
        target.init();
        System.assertEquals(2, target.productRecords.size());
        
        target.assetModelNo = 'aiueo';
        target.searchProduct();
        System.assertEquals(2, target.productRecords.size());
        System.assertEquals('aiueo製品1', target.productRecords[0].rec.Name);
        
        target.sortTable();
        System.assertEquals('aiueo製品2', target.productRecords[0].rec.Name);
        
        target.productRecords[1].check = false;
        target.saveType = '2';
        target.save();
        
    }
    
    static testMethod void initTest4() {
        // 製品
        Product2 pr1 = new Product2(Name = 'aiueo製品1', IsActive = true, Asset_Model_No__c = 'aiueo1', SFDA_Status__c = '有効');
        Product2 pr2 = new Product2(Name = 'aiueo製品2', IsActive = true, Asset_Model_No__c = 'aiueo2', SFDA_Status__c = '有効');
        insert new Product2[]{pr1, pr2};
        
        Product_Documentation__c pd = new Product_Documentation__c(Name = 'test pd');
        insert pd;
        
        ApexPages.currentPage().getParameters().put( 'pdmid', pd.Id);
        
        SelectProduct2Extension target = new SelectProduct2Extension();
        
        target.init();
        System.assertEquals(0, target.productRecords.size());
        
        target.assetModelNo = 'aiueo';
        target.searchProduct();
        System.assertEquals(2, target.productRecords.size());
        System.assertEquals('aiueo製品1', target.productRecords[0].rec.Name);
        
        target.sortTable();
        System.assertEquals('aiueo製品2', target.productRecords[0].rec.Name);
        
        target.productRecords[0].check = true;
        target.save();
        
    }
    
    static testMethod void initTest5() {
        // 製品
        Product2 pr1 = new Product2(Name = 'aiueo製品1', IsActive = true, Asset_Model_No__c = 'aiueo1', SFDA_Status__c = '有効');
        Product2 pr2 = new Product2(Name = 'aiueo製品2', IsActive = true, Asset_Model_No__c = 'aiueo2', SFDA_Status__c = '有効');
        insert new Product2[]{pr1, pr2};
        
        Case cic = new Case();
        insert cic;
        
        ApexPages.currentPage().getParameters().put( 'cicid', cic.Id);
        
        SelectProduct2Extension target = new SelectProduct2Extension();
        
        target.init();
        System.assertEquals(0, target.productRecords.size());
        
        target.assetModelNo = 'aiueo';
        target.searchProduct();
        System.assertEquals(2, target.productRecords.size());
        System.assertEquals('aiueo製品1', target.productRecords[0].rec.Name);
        
        target.sortTable();
        System.assertEquals('aiueo製品2', target.productRecords[0].rec.Name);
        
        target.productRecords[0].check = true;
        target.save();
        
    }
}