Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public with sharing class Callout {
- public static void runProccess(Case[] records) {
- Database.executeBatch(new CalloutBatch(records), 10);
- }
- public static void send(Case[] records) {
- Case[] toUpdate = new Case[]{};
- for (case record : records) {
- HttpRequest req = new HttpRequest();
- req.setEndpoint('callout:test/SOME_URL');
- req.setMethod('POST');
- req.setHeader('Content-Type', 'application/json');
- req.setBody('');
- Http http = new Http();
- HTTPResponse res = http.send(req);
- Map<String, Object> response = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
- toUpdate.add(new Case(Id = (Id) response.get('Id'), Status = (String) response.get('Status')));
- }
- update toUpdate;
- }
- global with sharing class CalloutBatch implements Database.Batchable<Case>, Database.AllowsCallouts{
- private final Case[] records;
- global CalloutBatch (Case[] records) {
- this.records = records;
- }
- global Iterable<Case> start(Database.BatchableContext BC) {
- return records;
- }
- global void execute(Database.BatchableContext BC, Case[] scope) {
- Callout.send(scope);
- }
- global void finish(Database.BatchableContext BC) {
- }
- @IsTest
- static void shouldReaciveData() {
- String status = 'Closed';
- Case caseRecord = new Case(Type = 'Web', Status = 'New');
- insert caseRecord;
- //insert new FeedItem(ParentId = caseRecord.Id, Visibility='AllUsers', Type = 'TextPost', Body = '');
- System.assertEquals(0, [SELECT Count() FROM Case WHERE Id = :caseRecord.Id AND Status = :status]);
- Test.startTest();
- Test.setMock(HttpCalloutMock.class, new CalloutTestMock(caseRecord.Id, status));
- Callout.runProccess(new Case[]{caseRecord});
- Test.stopTest();
- System.assertEquals(1, [SELECT Count() FROM Case WHERE Id = :caseRecord.Id AND Status = :status]);
- }
- private Id recordId;
- private String status;
- public CalloutTestMock(Id recordId, String status) {
- this.recordId = recordId;
- this.status = status;
- }
- public HTTPResponse respond(HTTPRequest req) {
- HttpResponse response = new HttpResponse();
- response.setStatusCode(200);
- response.setBody('{"Id": "' + recordId + '", "Status": "' + status + '"}');
- return response;
- }
Add Comment
Please, Sign In to add comment