0

Apex class:

public void post(){           
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http(); 
        req.setMethod('POST' ); // Method Type
        req.setEndpoint('http://www.nicomatic.com/_servicesWebDev/getCsvFromSalesforce.php'); // Server Url
       // Blob headerValue = Blob.valueOf('salesforce'+:+'salesforce12345');
     String username = 'salesforce';
     String password = 'salesforce12345'; 
     Blob headerValue = Blob.valueOf(username + ':' + password);
     String authorizationHeader = 'BASIC ' +
     EncodingUtil.base64Encode(headerValue);
     req.setHeader('Authorization', authorizationHeader);
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); // Content Type
        List<order_batch__c> bcList = new List<order_batch__c>();
       bcList= [select Or_sub_po_number__c,Or_customer_PO_Number__c,Or_customerid__c,Or_Account_name__c,Or_invoiceadd1__c,Or_invoiceadd2__c,Or_invoiceadd3__c,Or_invoiceadd4__c,Or_invoiceadd5__c,Or_contact_Phone__c,Or_contact_Fax__c,Or_Subcontact_family_name__c,Or_Subcontact_first_name__c,Or_contact_Name__c,Or_contact_Email__c,Or_Carrier__c,Order_Date__c,Or_Comments__c,Order_numbering__c,Or_clientpn__c,Or_Nicomatic_p_n__c,Quantity_Ordered__c,Requested_Date__c,Discount__c,Unit_price__c,Or_Quote_number__c,currencyisocode,id from order_batch__c  where  Quote_Line_Item__r.Quote1__c= : Apexpages.currentPage().getParameters().get('Id') order by Order_numbering__c];
      // system.debug(bcList);
     // String JSONString = JSON.serialize(bcList);
      //  system.debug(JSONString);
      String JSONString = '';
     for (order_batch__c order : bcList) {
     String orderJSON = '{\n' +
    ' "Or_sub_po_number__c" :"' + order.Or_sub_po_number__c + '",\n' +
    ' "Or_customer_PO_Number__c" :"' + order.Or_customer_PO_Number__c + '",\n' +
    ' "Or_customerid__c" :"' + order.Or_customerid__c + '",\n' +
    ' "Or_Account_name__c" :"' + order.Or_Account_name__c + '",\n' +
    ' "Or_invoiceadd1__c" :"' + order.Or_invoiceadd1__c + '",\n' +
    ' "Or_invoiceadd2__c" :"' + order.Or_invoiceadd2__c + '",\n' +
    ' "Or_invoiceadd3__c" :"' + order.Or_invoiceadd3__c + '",\n' +
    ' "Or_invoiceadd4__c" :"' + order.Or_invoiceadd4__c + '",\n' +
    ' "Or_invoiceadd5__c" :"' + order.Or_invoiceadd5__c + '",\n' +
    ' "Or_contact_Phone__c" :"' + order.Or_contact_Phone__c + '",\n' +
    ' "Or_contact_Fax__c" :"' + order.Or_contact_Fax__c + '",\n' +
    ' "Or_Subcontact_family_name__c" :"' + order.Or_Subcontact_family_name__c + '",\n' +
    ' "Or_Subcontact_first_name__c" :"' + order.Or_Subcontact_first_name__c + '",\n' +
    ' "Or_contact_Name__c" :"' + order.Or_contact_Name__c + '",\n' +
    ' "Or_contact_Email__c" :"' + order.Or_contact_Email__c + '",\n' +
    ' "Or_Carrier__c" :"' + order.Or_Carrier__c + '",\n' +
    ' "Order_Date__c" :"' + order.Order_Date__c + '",\n' +
    ' "Or_Comments__c" :"' + order.Or_Comments__c + '",\n' +
    ' "Order_numbering__c" :"' + order.Order_numbering__c + '",\n' +
    ' "Or_clientpn__c" :"' + order.Or_clientpn__c + '",\n' +
    ' "Or_Nicomatic_p_n__c" :"' + order.Or_Nicomatic_p_n__c + '",\n' +
    ' "Quantity_Ordered__c" :"' + order.Quantity_Ordered__c + '",\n' +
    ' "Requested_Date__c" :"' + order.Requested_Date__c + '",\n' +
    ' "Discount__c" :"' + order.Discount__c + '",\n' +
    ' "Unit_price__c" :"' + order.Unit_price__c + '",\n' +
    ' "Or_Quote_number__c" :"' + order.Or_Quote_number__c + '"' +
    '},';
      JSONString +=orderJSON ;
         }     
        req.setBody('csvcontent=' + EncodingUtil.urlEncode('['+ JSONString.substring(0,JSONString.length()-1) +']', 'UTF-8' ); // Request Parameters
       try {
        system.debug( EncodingUtil.urlEncode(JSONString, 'UTF-8'));
            res = http.send(req);
            if(res.getBody() != null){
                system.debug(res.getbody()); //Parse Response
                res1=res.getbody();
            }
        } catch(Exception e) {
            System.debug('error: '+ e);
       }
} 

Test Apex Class:

@IsTest
private class TestFinalorderreviw 
{

 static testmethod void TestFinalorderreviw()
 {
   Account A= new Account(Name='test', BillingCountry ='India');
  insert A;
   Contact con=new Contact();
                con.FirstName='Test';
                con.LastName='testwe';
                con.AccountId=a.id;
                con.Fax='8906745632';
                con.Phone='8906745632';
                con.Email='test@gmail.com';
                con.LastName='Jena';
                con.Primary_Contact__c=true;
                con.MailingCountry = 'India';
                try{
                    Insert con;
                }catch(DMLException de){
                    System.debug('*********cont********'+de);
                }
  Opportunity op= new opportunity(Name='test',CloseDate=date.today(),StageName='Qualification',Product_Type__c='DPI',Accountid=A.Id);
  insert op;

  Quote__c q1 = New Quote__c (Opportunity_Name__c = op.Id,Account_Name__c = A.Id);
  insert q1; 

  Quote_Line_Item__c  qll = new Quote_Line_Item__c (Name='test',Quote1__c = q1.id);
  insert qll;
order_batch__c b1 =new order_batch__c( Quantity_Ordered__c =1111,Quote_Line_Item__c = qll .id);
            insert b1;
           finalorderreview obj=new finalorderreview();
           obj.cancel1(); 
           obj.save();
           obj.post();
           obj.getob1();
 }
 }

Deploy error : System.StringException: Ending position out of bounds: -1 Stack Trace: Class.finalorderreview.post: line 79, column 1 Class.TestFinalorderreviw.TestFinalorderreviw: line 37, column 1

  req.setBody('csvcontent=' + EncodingUtil.urlEncode('['+ JSONString.substring(0,JSONString.length()-1) +']', 'UTF-8' ); // Request Parameters

    obj.post();
4

Add this to your test:

PageReference pageRef = Page.YourPageName;
pageRef.getParameters().put('Id', q1.Id);
Test.setCurrentPage(pageRef);

before you create finalorderreview.

The query in the post method references:

ApexPages.currentPage().getParameters().get('Id')

and so your test needs to supply that value, otherwise the query will return no results and the JSONString will remain at length zero.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.