Last active
December 14, 2015 11:29
-
-
Save joshbirk/5079255 to your computer and use it in GitHub Desktop.
WarehousePullData Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class WarehousePullData { | |
public WarehousePullData() {} | |
//WarehousePullData.pullData(); | |
public static String pullData() { | |
// Instantiate a new http object | |
Http h = new Http(); | |
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint | |
HttpRequest req = new HttpRequest(); | |
req.setEndpoint('http://warehousedatapush.herokuapp.com/'); | |
req.setMethod('GET'); | |
// Send the request, and return a response | |
HttpResponse res = h.send(req); | |
Map<String, Object> data = (Map<String, Object>)JSON.deserializeUntyped(res.getBody()); | |
List<Object> items = (List<Object>)data.get('Items'); | |
List<Merchandise__c> new_items = new List<Merchandise__c>(); | |
for(Object item : items) { | |
Map<String, Object> item_data = (Map<String, Object>)item; | |
Merchandise__c merch = new Merchandise__c(); | |
merch.Name = (String)item_data.get('Name'); | |
merch.Quantity__c = (Decimal)item_data.get('Quantity'); | |
merch.Price__c = (Decimal)item_data.get('Price'); | |
new_items.add(merch); | |
} | |
insert new_items; | |
return 'Done'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
public class WarehousePullTest { | |
@isTest | |
public static void testPullData() { | |
List<Merchandise__c> merch = [SELECT ID from Merchandise__c WHERE Name = 'Super Laptop']; | |
System.assertEquals(merch.size(),1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static public Boolean createBaselineData() { | |
Merchandise__c m = new Merchandise__c(Name='Rack Server',Price__c=1245.99,Quantity__c=500); | |
insert m; | |
Invoice__c i = new Invoice__c(); | |
insert i; | |
Line_Item__c li = new Line_Item__c(Name='1',Quantity__c=10,Merchandise__c=m.Id,Invoice__c=i.Id); | |
insert li; | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NAME,Price__c,Quantity__c | |
Super Laptop,1299.00,170 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WarehousePullData.createBaselineData(); | |
List<Merchandise__c> merchandise = Test.loadData(Merchandise__c.sObjectType, 'MerchData'); | |
update merchandise; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
global class WarehouseMock implements HttpCalloutMock { | |
// Implement this interface method | |
global HTTPResponse respond(HTTPRequest req) { | |
HttpResponse res = new HttpResponse(); | |
res.setHeader('Content-Type', 'application/json'); | |
res.setBody('{ "Items": [ { "Name": "Laptop From Endpoint", "Price": 2999.99, "Quantity": 4 }, { "Name": "Super Duper Laptop", "Price": 3999.99, "Quantity": 4 } ] }'); | |
res.setStatusCode(200); | |
return res; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Test.startTest(); | |
// Set mock callout class | |
Test.setMock(HttpCalloutMock.class, new WarehouseMock()); | |
// Call class that utilizes this endpoint | |
WarehousePullData.pullData(); | |
Test.stopTest(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Merchandise__c> merch = [SELECT ID from Merchandise__c WHERE Name = 'Laptop From Endpoint']; | |
System.assertEquals(merch.size(),1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class WarehousePullData { | |
public WarehousePullData() {} | |
//WarehousePullData.pullData(); | |
public static String pullData() { | |
// Instantiate a new http object | |
Http h = new Http(); | |
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint | |
HttpRequest req = new HttpRequest(); | |
req.setEndpoint('http://warehousedatapush.herokuapp.com/'); | |
req.setMethod('GET'); | |
// Send the request, and return a response | |
HttpResponse res = h.send(req); | |
Map<String, Object> data = (Map<String, Object>)JSON.deserializeUntyped(res.getBody()); | |
List<Object> items = (List<Object>)data.get('Items'); | |
List<Merchandise__c> new_items = new List<Merchandise__c>(); | |
for(Object item : items) { | |
Map<String, Object> item_data = (Map<String, Object>)item; | |
Merchandise__c merch = new Merchandise__c(); | |
merch.Name = (String)item_data.get('Name'); | |
merch.Quantity__c = (Decimal)item_data.get('Quantity'); | |
merch.Price__c = (Decimal)item_data.get('Price'); | |
new_items.add(merch); | |
} | |
insert new_items; | |
return 'Done'; | |
} | |
static public Boolean createBaselineData() { | |
Merchandise__c m = new Merchandise__c(Name='Rack Server',Price__c=1245.99,Quantity__c=500); | |
insert m; | |
Invoice__c i = new Invoice__c(); | |
insert i; | |
Line_Item__c li = new Line_Item__c(Name='1',Quantity__c=10,Merchandise__c=m.Id,Invoice__c=i.Id); | |
insert li; | |
return true; | |
} | |
@isTest | |
public static void testPullData() { | |
WarehousePullData.createBaselineData(); | |
List<Merchandise__c> merchandise = Test.loadData(Merchandise__c.sObjectType, 'MerchData'); | |
update merchandise; | |
Test.startTest(); | |
// Set mock callout class | |
Test.setMock(HttpCalloutMock.class, new WarehouseMock()); | |
// Call class that utilizes this endpoint | |
WarehousePullData.pullData(); | |
Test.stopTest(); | |
List<Merchandise__c> merch = [SELECT ID from Merchandise__c WHERE Name = 'Laptop From Endpoint']; | |
System.assertEquals(merch.size(),1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
public class WarehousePullTest { | |
static public Boolean createBaselineData() { | |
Merchandise__c m = new Merchandise__c(Name='Rack Server',Price__c=1245.99,Quantity__c=500); | |
insert m; | |
Invoice__c i = new Invoice__c(); | |
insert i; | |
Line_Item__c li = new Line_Item__c(Name='1',Quantity__c=10,Merchandise__c=m.Id,Invoice__c=i.Id); | |
insert li; | |
return true; | |
} | |
@isTest | |
public static void testPullData() { | |
WarehousePullTest.createBaselineData(); | |
List<Merchandise__c> merchandise = Test.loadData(Merchandise__c.sObjectType, 'MerchData'); | |
update merchandise; | |
Test.startTest(); | |
// Set mock callout class | |
Test.setMock(HttpCalloutMock.class, new WarehouseMock()); | |
// Call class that utilizes this endpoint | |
WarehousePullData.pullData(); | |
Test.stopTest(); | |
List<Merchandise__c> merch = [SELECT ID from Merchandise__c WHERE Name = 'Laptop From Endpoint']; | |
System.assertEquals(merch.size(),1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment