Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes Add null checks for JSON stream #154 #156

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 31 additions & 27 deletions sfdx-source/apex-mocks/main/classes/fflib_ApexMocksUtils.cls
Original file line number Diff line number Diff line change
Expand Up @@ -189,39 +189,43 @@ public class fflib_ApexMocksUtils
private static void streamTokens(JSONParser fromStream, JSONGenerator toStream, JSONParserEvents events)
{
Integer depth = 0;
while (fromStream.nextToken()!=null)
while (fromStream.nextToken() != null)
{
// Give event handler chance to inject
if(events!=null)
if (events != null) {
events.nextToken(fromStream, depth, toStream);
// Forward to output stream
JSONToken currentToken = fromStream.getCurrentToken();
if(currentToken == JSONToken.START_ARRAY) {
toStream.writeStartArray();
depth++;
}
else if(currentToken == JSONToken.START_OBJECT) {
toStream.writeStartObject();
depth++;
}
else if(currentToken == JSONToken.FIELD_NAME)
toStream.writeFieldName(fromStream.getCurrentName());
else if(currentToken == JSONToken.VALUE_STRING ||
currentToken == JSONToken.VALUE_FALSE ||
currentToken == JSONToken.VALUE_TRUE ||
currentToken == JSONToken.VALUE_NUMBER_FLOAT ||
currentToken == JSONToken.VALUE_NUMBER_INT)
toStream.writeString(fromStream.getText());
else if(currentToken == JSONToken.END_OBJECT) {
toStream.writeEndObject();
depth--;
}
else if(currentToken == JSONToken.END_ARRAY) {
toStream.writeEndArray();
depth--;

// Forward to output stream
switch on fromStream.getCurrentToken() {
when START_ARRAY {
toStream.writeStartArray();
depth++;
}
when START_OBJECT {
toStream.writeStartObject();
depth++;
}
when FIELD_NAME {
toStream.writeFieldName(fromStream.getCurrentName());
}
when VALUE_STRING, VALUE_FALSE, VALUE_TRUE, VALUE_NUMBER_FLOAT, VALUE_NUMBER_INT {
toStream.writeString(fromStream.getText());
}
when VALUE_NULL {
toStream.writeNull();
}
when END_OBJECT {
toStream.writeEndObject();
depth--;
}
when END_ARRAY {
toStream.writeEndArray();
depth--;
}
}
// Don't continue to stream beyond the initial starting point
if(depth==0)
if (depth == 0)
break;
}
}
Expand Down
76 changes: 69 additions & 7 deletions sfdx-source/apex-mocks/test/classes/fflib_ApexMocksUtilsTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public class fflib_ApexMocksUtilsTest
}

@isTest
private static void makeRelationship_GenericOverload_ReturnsObjectsWithRelationFieldSet() {
private static void makeRelationship_GenericOverload_ReturnsObjectsWithRelationFieldSet()
{
//Given
SObject acc = Schema.getGlobalDescribe().get('Account').newSObject();
acc.put('Id', fflib_IDGenerator.generate(acc.getSObjectType()));
Expand Down Expand Up @@ -128,8 +129,8 @@ public class fflib_ApexMocksUtilsTest
}

@isTest
private static void makeRelationship_GenericOverload_ThrowsErrorOnInvalidParentType() {

private static void makeRelationship_GenericOverload_ThrowsErrorOnInvalidParentType()
{
// Setup parent object
SObject acc = Schema.getGlobalDescribe().get('Account').newSObject();
acc.put('Id', fflib_IDGenerator.generate(acc.getSObjectType()));
Expand All @@ -155,8 +156,8 @@ public class fflib_ApexMocksUtilsTest
}

@isTest
private static void makeRelationship_GenericOverload_ThrowsErrorOnInvalidChildType() {

private static void makeRelationship_GenericOverload_ThrowsErrorOnInvalidChildType()
{
// Setup parent object
SObject acc = Schema.getGlobalDescribe().get('Account').newSObject();
acc.put('Id', fflib_IDGenerator.generate(acc.getSObjectType()));
Expand All @@ -182,8 +183,8 @@ public class fflib_ApexMocksUtilsTest
}

@isTest
private static void makeRelationship_GenericOverload_ThrowsErrorOnInvalidFieldName() {

private static void makeRelationship_GenericOverload_ThrowsErrorOnInvalidFieldName()
{
// Setup parent object
SObject acc = Schema.getGlobalDescribe().get('Account').newSObject();
acc.put('Id', fflib_IDGenerator.generate(acc.getSObjectType()));
Expand All @@ -208,6 +209,67 @@ public class fflib_ApexMocksUtilsTest
System.Assert.areEqual('SObject field not found: MyInvalidField', errorMessage);
}

@IsTest
private static void makeRelationship_ObjectWithNull_DoesNotThrowErrorOnJSONExceptionCanNotWriteAFieldNameExpectingAValue()
{
// Given
Product2 prod1 = new Product2(
Id = fflib_IDGenerator.generate(Product2.SObjectType),
Name = 'Product1',
ProductCode = 'P1',
Description = null,
StockKeepingUnit = 'P1'
);

Product2 prod2 = new Product2(
Id = fflib_IDGenerator.generate(Product2.SObjectType),
Name = 'Product2',
ProductCode = 'P2',
Description = 'this is another product',
StockKeepingUnit = 'P2'
);

OrderItem oi1 = new OrderItem(
Id = fflib_IDGenerator.generate(OrderItem.SObjectType),
Product2Id = prod1.Id,
Product2 = prod1,
UnitPrice = 10,
Quantity = 1
);

OrderItem oi2 = new OrderItem(
Id = fflib_IDGenerator.generate(OrderItem.SObjectType),
Product2Id = prod2.Id,
Product2 = prod2,
UnitPrice = 10,
Quantity = 1
);

Order order = new Order();

Exception exceptionThatWasCalled = null;

// When
Test.startTest();

try {
fflib_ApexMocksUtils.makeRelationship(
List<Order>.class,
new List<Order>{ order },
OrderItem.OrderId,
new List<List<OrderItem>>{ new List<OrderItem>{oi1, oi2} }
);
} catch (JSONException e) {
exceptionThatWasCalled = e;
}

Test.stopTest();

// Then
System.debug(exceptionThatWasCalled);
Assert.isNull(exceptionThatWasCalled, 'Exception should not have been called');
}

@isTest
static void setReadOnlyFields_CreatedByIdSetToCurrentUserId_IdFieldSetSuccessfully() {

Expand Down
Loading