Skip to content

Instantly share code, notes, and snippets.

View sachi-d's full-sized avatar

Sachithra Dangalla sachi-d

View GitHub Profile
@sachi-d
sachi-d / Animal.java
Last active December 4, 2020 12:19
Mockito verify examples
public class Animal {
boolean amIBird;
public Animal(boolean amIBird) {
this.amIBird = amIBird;
}
public void move(int distance) {
boolean isBird = amIBird();
if (isBird) {
@RunWith(Parameterized.class)
public class AdderTest extends TestCase {
@Parameterized.Parameters(name = "{index}: add[{0}, {1}]={2}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][]{
{0, 0, 0},
{1, 1, 2},
{2, 1, 3},
{100, 400, 500},
import junit.framework.TestCase;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
public class DogTest extends TestCase {
@Test
@sachi-d
sachi-d / Find existence of duplicates before insertion
Created October 31, 2020 13:57
If you want to check for duplicates before trying to insert, that too can be done using the FindDuplicates class in Datacloud namespace.
Class__c classToCreate = new Class__c(Name='Science', Grade__c=8);
List<Class__c> duplicates = findDuplicateRecords(classToCreate);
if(duplicates.size() == 0){
//no duplicates
insert classToCreate;
} else{
//desired update
}
@sachi-d
sachi-d / Insert and capture error
Created October 31, 2020 13:56
The duplicate rule can be captured by the errors thrown at insertion as follows.
Class__c classToCreate = new Class__c(Name='Science', Grade__c=8);
Database.SaveResult saveResult = Database.insert(classToCreate, false);
if (!saveResult.isSuccess()) {
for (Database.Error error : saveResult.getErrors()) {
if (error instanceof Database.DuplicateError) {
Database.DuplicateError duplicateError = (Database.DuplicateError)error;
Datacloud.DuplicateResult duplicateResult = duplicateError.getDuplicateResult();
@sachi-d
sachi-d / Enrolments
Last active October 31, 2020 13:23
Apex code stub for Enrolments class
public class Enrolments {
@InvocableMethod(label='Get Enrolments' description='Iterate over students, classes and payments and create junction records')
public static List<EnrolmentsResult> createEnrolments(List<EnrolmentsRequest> request){
//parse inputs and variables
List<Account> students = request.get(0).students;
List<Class__c> classes = request.get(0).classes;
List<Payment__c> payments = request.get(0).payments;
List<Enrolment__c> enrolments = new List<Enrolment__c>();
public class BadRequestException extends Exception {
private String errorCode = 'BAD_REQUEST';
private Integer statusCode = 400;
public String getErrorCode() {
return this.errorCode;
}
public Integer getStatusCode() {
return this.statusCode;
@sachi-d
sachi-d / PaymentAPI.yaml
Created April 19, 2020 05:37
Sample yaml file for Payment API
swagger: "2.0"
info:
description: |
API documentation for Sample API.
version: "0.0.1"
title: "Sample API"
host: "payment.swagger.io"
basePath: "/v1"
consumes:
@sachi-d
sachi-d / swagger.yaml
Last active February 18, 2020 04:42
Weather App Files
swagger: "2.0"
info:
description: "This is a sample weather forecast server."
version: "1.0.0"
title: "Weather Forecast"
host: "localhost:3000"
schemes:
- "https"
- "http"
tags: