daily pastebin goal
8%
SHARE
TWEET

Untitled

a guest Jan 12th, 2018 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class CloneOfferLEXComponentController {
  2.  
  3.     @AuraEnabled
  4.     public static String serverCloneOffer(String offerId) {  
  5.  
  6.         pba__Offer__c  offer = [SELECT Id, Buyer_Email_Address__c, Buyer__c, Coop_Agent__c, Offer_Type__c, pba__OfferedAmount_pb__c, pba__Listing__c FROM pba__Offer__c WHERE Id = :offerId];
  7.  
  8.         String response;    // response after trying to save the cloned offer
  9.  
  10.         // if the offer doesn't have a listing, we can't clone the offer
  11.         if(offer.pba__Listing__c != null) {
  12.             pba__Listing__c listing = [SELECT Id, Auction__c, Bid_Coordinator__c, Stage__c FROM pba__Listing__c WHERE Id =:offer.pba__Listing__c];
  13.  
  14.             pba__Offer__c clonedOffer = new pba__Offer__c(
  15.                 Buyer__c                 = offer.Buyer__c,
  16.                 Coop_Agent__c            = offer.Coop_Agent__c,
  17.                 Stage__c                 = 'Pending Review',
  18.                 pba__OfferedAmount_pb__c = offer.pba__OfferedAmount_pb__c,
  19.                 Offer_Type__c            = listing.Stage__c,
  20.                 pba__Listing__c          = listing.Id,
  21.                 Bid_Coordinator__c       = listing.Bid_Coordinator__c,
  22.                 Auction__c               = listing.Auction__c,
  23.                 Recaptured_Buyer__c      = true
  24.             );
  25.  
  26.             // this is done to cause the cloning to fail, for code coverage
  27.             if(Test.isRunningTest() && offer.Buyer_Email_Address__c == 'fail1@email.com') {
  28.                 clonedOffer.Stage__c        = 'Reviewed';
  29.                 clonedOffer.pba__Status__c  = 'Subject To';
  30.             }
  31.  
  32.             Database.SaveResult sr = Database.insert(clonedOffer, false);
  33.  
  34.             if (sr.isSuccess()) {
  35.                 response = sr.getId();
  36.             } else {  
  37.                 String failureDetail;
  38.                 for(Database.Error err : sr.getErrors()) {
  39.                     String errorFields = '';
  40.                     for(String eF: err.getFields()) {
  41.                         errorFields = errorFields + ef + 'n';
  42.                     }
  43.                     failureDetail = err.getStatusCode() + ': ' + err.getMessage() + '. n' + errorFields;
  44.                 }
  45.  
  46.                 AuraHandledException e = new AuraHandledException(failureDetail);
  47.                 e.setMessage(failureDetail);
  48.                 throw e;
  49.             }            
  50.         } else {
  51.             AuraHandledException e = new AuraHandledException('There is no listing associated with this offer.');
  52.             e.setMessage('There is no listing associated with this offer.');
  53.             throw e;
  54.         }
  55.  
  56.         return response;        
  57.     }
  58.  
  59. }
  60.    
  61. @isTest
  62. public class CloneOfferLEXComponentControllerTest {
  63.  
  64.     @isTest static void cloningTest() {
  65.  
  66.         Account seller = new Account (Name = 'Test Seller');
  67.  
  68.         insert seller;
  69.  
  70.         Contact c = new Contact(LastName = 'lastNameTest', Email = 'testBuyerEmail@test.com');
  71.  
  72.         insert c;
  73.  
  74.         List<pba__Listing__c> listings = new List<pba__Listing__c>();
  75.         listings.add(new pba__Listing__c (
  76.             Stage__c  = 'Pre-Auction',
  77.             pba__Status__c = 'Available',
  78.             Seller__c = seller.Id,
  79.             pba__PostalCode_pb__c = '12345'
  80.         ));  
  81.  
  82.         insert listings;
  83.  
  84.         List<Auction__c> auctions = new List<Auction__c>();
  85.         auctions.add(new Auction__c (
  86.             Auction_Program__c = 'Dual Path',
  87.             Listing__c = listings[0].Id,
  88.             Auction_Number__c = '5'
  89.         ));
  90.  
  91.         insert auctions;
  92.  
  93.         List<pba__Offer__c> offers = new List<pba__Offer__c>();
  94.  
  95.         // this offer should clone successfully
  96.         offers.add(new pba__Offer__c (
  97.             Auction__c               = auctions[0].Id,
  98.             pba__Listing__c          = listings[0].Id,
  99.             Buyer_Email_Address__c   = 'succeed1@email.com',
  100.             Stage__c                 = 'Cancelled',
  101.             pba__Status__c           = 'Withdrawal',
  102.             Buyer__c                 = c.Id,
  103.             pba__OfferedAmount_pb__c = 100
  104.         ));
  105.  
  106.         // this offer will not clone, because we are changing the stage/status
  107.         // in the controller.
  108.         offers.add(new pba__Offer__c (
  109.             Auction__c               = auctions[0].Id,
  110.             Buyer_Email_Address__c   = 'fail1@email.com',
  111.             pba__Listing__c          = listings[0].Id,
  112.             Stage__c                 = 'Cancelled',
  113.             pba__Status__c           = 'Withdrawal',
  114.             Buyer__c                 = c.Id,
  115.             pba__OfferedAmount_pb__c = 100
  116.         ));
  117.  
  118.         // this offer will not clone, because it has no listing
  119.         offers.add(new pba__Offer__c (
  120.             Auction__c               = auctions[0].Id,
  121.             Buyer_Email_Address__c   = 'fail2@email.com',
  122.             Stage__c                 = 'Cancelled',
  123.             pba__Status__c           = 'Withdrawal',
  124.             Buyer__c                 = c.Id,
  125.             pba__OfferedAmount_pb__c = 100,
  126.             Offer_Type__c            = 'Pre-Auction'
  127.         ));  
  128.  
  129.         insert offers;
  130.  
  131.         Test.startTest();
  132.  
  133.         CloneOfferLEXComponentController.serverCloneOffer(offers[0].Id);
  134.  
  135.         try{
  136.             CloneOfferLEXComponentController.serverCloneOffer(offers[1].Id);  
  137.         }catch (Exception e) {
  138.             Boolean error =  e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION') ? true : false;
  139.             System.AssertEquals(error, true);
  140.         }
  141.  
  142.         try {
  143.             CloneOfferLEXComponentController.serverCloneOffer(offers[2].Id);    
  144.         }catch (Exception e) {
  145.             Boolean error =  e.getMessage().contains('There is no listing associated with this offer') ? true : false;
  146.             System.AssertEquals(error, true);
  147.         }
  148.  
  149.         Test.stopTest();
  150.  
  151.         // should be the 3 original test offers, and 1 clone, so 4 offers total.
  152.         System.assertEquals(4, [SELECT count() FROM pba__Offer__c]);
  153.         // on an offer, Recaptured_Buyer__c = true signifies the offer was cloned. There should only be one.
  154.         System.assertEquals(1, [SELECT count() FROM pba__Offer__c WHERE Recaptured_Buyer__c = true]);
  155.     }
  156.  
  157. }
  158.    
  159. try{
  160.     CloneOfferLEXComponentController.serverCloneOffer(offers[1].Id);  
  161. }catch (Exception e) {
  162.     Boolean error =  e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION') ? true : false;
  163.     System.AssertEquals(error, true);
  164. }
RAW Paste Data
Top