Skip to content

Commit

Permalink
Started to add testing around logging in the CachedSoqlExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Apr 7, 2022
1 parent 0870a6d commit 145d9f6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ public with sharing class TestLoggerUtils
{
LoggerService.config = new Logging_Configuration__c();
}

public static TestLoggerService registerTestLoggerService()
{
TestLoggerService testLogger = new TestLoggerService();
Application.SERVICE.setMock( ILoggerService.class, testLogger );
return testLogger;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public inherited sharing class CachedSoqlExecutor //NOPMD: incorrect report of t
{
if ( cacheWrapper.isACache() )
{
LoggerService.log( LoggerService.Level.ERROR, 'Opportunity to use Platform Cache skipped since user does not have required permission' ); // TODO: can we get the permission name into this?
LoggerService.log( LoggerService.Level.INFO, 'Opportunity to use Platform Cache skipped since user does not have required permission' ); // TODO: can we get the permission name into this?
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ private without sharing class CachedSoqlExecutorTest
@isTest
private static void query_whenCalledTwiceByAUserWithAccessToTheCache_onlyIssuesOneSoqlStatement() // NOPMD: Test method name format
{
TestLoggerUtils.switchLoggingOn();
TestLoggerService logger = TestLoggerUtils.registerTestLoggerService();

String soqlStatement = 'SELECT Id FROM Account';

setupAccessToSoqlCache( true );
Expand All @@ -19,11 +22,20 @@ private without sharing class CachedSoqlExecutorTest

System.assertEquals( 1, soqlCalls, 'query, when called twice by a user with access to the cache, will only issue one SOQL statement' );
System.assertEquals( originalResults, secondResults, 'query, when called twice by a user with access to the cache, returns the same results in both calls' );

System.assertEquals( 1, logger.methods.size(), 'query, when called twice by a user with access to the cache, will log once' );
System.assertEquals( 'log', logger.methods[0], 'query, when called twice by a user with access to the cache, will log once' );
System.assertEquals( LoggerService.Level.INFO, (LoggerService.Level)logger.parameters[0][0], 'query, when called twice by a user with access to the cache, will log once with an info message' );
ortoo_Asserts.assertContains( 'Platform Cache miss when running the SOQL', (String)logger.parameters[0][1], 'query, when called twice by a user with access to the cache, will log once to say there was a cache miss' );
ortoo_Asserts.assertContains( soqlStatement, (String)logger.parameters[0][1], 'query, when called twice by a user with access to the cache, will log once to say there was a cache miss and reference the SOQL statement' );
}

@isTest
private static void query_org_whenCalledTwiceByAUserWithAccessToTheCache_onlyIssuesOneSoqlStatement() // NOPMD: Test method name format
{
TestLoggerUtils.switchLoggingOn();
TestLoggerService logger = TestLoggerUtils.registerTestLoggerService();

String soqlStatement = 'SELECT Id FROM Account';

setupAccessToSoqlCache( true );
Expand All @@ -38,11 +50,21 @@ private without sharing class CachedSoqlExecutorTest

System.assertEquals( 1, soqlCalls, 'query, when called twice by a user with access to the cache, will only issue one SOQL statement' );
System.assertEquals( originalResults, secondResults, 'query, when called twice by a user with access to the cache, returns the same results in both calls' );

System.assertEquals( 1, logger.methods.size(), 'query against an org cache, when called twice by a user with access to the cache, will log once' );
System.assertEquals( 'log', logger.methods[0], 'query against an org cache, when called twice by a user with access to the cache, will log once' );
System.assertEquals( LoggerService.Level.INFO, (LoggerService.Level)logger.parameters[0][0], 'query against an org cache, when called twice by a user with access to the cache, will log once with an info message' );
ortoo_Asserts.assertContains( 'Platform Cache miss when running the SOQL', (String)logger.parameters[0][1], 'query against an org cache, when called twice by a user with access to the cache, will log once to say there was a cache miss' );
ortoo_Asserts.assertContains( soqlStatement, (String)logger.parameters[0][1], 'query against an org cache, when called twice by a user with access to the cache, will log once to say there was a cache miss and reference the SOQL statement' );

}

@isTest
private static void query_whenCalledTwiceByAUserWithoutAccessToTheCache_issuesTwoSoqlStatements() // NOPMD: Test method name format
{
TestLoggerUtils.switchLoggingOn();
TestLoggerService logger = TestLoggerUtils.registerTestLoggerService();

String soqlStatement = 'SELECT Id FROM Account';

setupAccessToSoqlCache( false );
Expand All @@ -57,6 +79,11 @@ private without sharing class CachedSoqlExecutorTest

System.assertEquals( 2, soqlCalls, 'query, when called twice by a user with no access to the cache, will issue two SOQL statements' );
System.assertEquals( originalResults, secondResults, 'query, when called twice by a user with not access to the cache, returns the same results in both calls' );

System.assertEquals( 2, logger.methods.size(), 'query, when called twice by a user with access to the cache, will log once' );

//TODO: continue here:

}

@isTest
Expand Down

0 comments on commit 145d9f6

Please sign in to comment.