Skip to content

Commit

Permalink
Fix issue with State attribute types rasied by srauda in Codeplex, see
Browse files Browse the repository at this point in the history
  • Loading branch information
ramon.tebar committed Sep 19, 2017
1 parent 836ae4a commit 6bd4b73
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ private object RetrieveAttributeValue(DynamicExpression attributeExpression, Ent
if (!record.Contains(expressionValue))
return string.Empty;

object crmAttribute = record[expressionValue];
var crmAttribute = record[expressionValue];

if (attributeExpression.ChildExpressions.Count == 0)
{
if (crmAttribute.GetType() == typeof(Money))
Type attributeType = crmAttribute.GetType();

if (attributeType == typeof(Money))
return ((Money)crmAttribute).Value;
else if (crmAttribute.GetType() == typeof(EntityReference))
else if (attributeType == typeof(EntityReference))
return ((EntityReference)crmAttribute).Name;
else if (crmAttribute.GetType() == typeof(OptionSetValue))
else if (attributeType == typeof(OptionSetValue))
return RetrieveOptionSetLabel(recordReference, expressionValue, record[expressionValue] as OptionSetValue, organizationService);
else
return crmAttribute;
Expand Down Expand Up @@ -94,20 +96,26 @@ private string RetrieveOptionSetLabel(EntityReference entityReference, string at

RetrieveAttributeResponse attributeResponse = organizationService.Execute(attributeRequest) as RetrieveAttributeResponse;

OptionMetadataCollection options = ((PicklistAttributeMetadata)attributeResponse.AttributeMetadata).OptionSet.Options;
foreach (OptionMetadata option in options)
if (attributeResponse.AttributeMetadata is EnumAttributeMetadata)
{
if (option != null && option.Value == optionSetValue.Value)
OptionMetadataCollection options = ((EnumAttributeMetadata)attributeResponse.AttributeMetadata).OptionSet?.Options;
if (options != null)
{
if (option.Label == null
|| option.Label.UserLocalizedLabel == null)
return string.Empty;
else
return option.Label.UserLocalizedLabel.Label;
foreach (OptionMetadata option in options)
{
if (option != null && option.Value == optionSetValue.Value)
{
if (option.Label == null
|| option.Label.UserLocalizedLabel == null)
return string.Empty;
else
return option.Label.UserLocalizedLabel.Label;
}
}
}
}

return string.Empty;
return string.Empty; //Unexpected type
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public void TestTextprocessor_BasicTypes()

//Test Money type
Assert.AreEqual(((Money)payment["dxtools_amount"]).Value.ToString(), textProcessor.Process("{dxtools_amount}", payment.ToEntityReference()));

//Test State (statecode) type
Assert.AreEqual(Enum.GetName(typeof(State), State.Active), textProcessor.Process("{statecode}", payment.ToEntityReference()));
}

private Entity CreateContact()
Expand Down
6 changes: 6 additions & 0 deletions DXTools.CRM.Solutions.CustomEmails.IntegrationTests/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ internal enum PaymentDirections
{
IN = 503530000
}

internal enum State
{
Active = 0,
Inactive = 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DXTools.CRM.Solutions.CustomEmails.Common.TextProcessing;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Fakes;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Metadata.Fakes;
using Microsoft.QualityTools.Testing.Fakes;

namespace DXTools.CRM.Solutions.CustomEmails.UnitTests.Common.TextProcessing
{
[TestClass]
public class TextProcessorTest_Status
{
[TestMethod]
public void TestTextProcessor_Status()
{
using (ShimsContext.Create())
{
Entity recordContext = new Entity("dxtools_payment");
recordContext["statecode"] = new OptionSet;
recordContext.Id = Guid.NewGuid();

IOrganizationService stubOrganisationService = SetupIOrganisationService(recordContext, "createdon", AttributeTypeCode.DateTime);

ITextProcessor dynTextProcessor = new TextProcessor(stubOrganisationService);

string inputText = "Payment Status (statecode) is {statecode}.";
string expected = "Your payment was created on " + DateTime.Now + ", thanks. ";

string resultOuput = dynTextProcessor.Process(inputText, recordContext.ToEntityReference());

Assert.AreEqual(expected, resultOuput);
}

}

private IOrganizationService SetupIOrganisationService(Entity recordContext, string attributeName, AttributeTypeCode attributeType)
{
StubIOrganizationService stubIOrganisationService = new StubIOrganizationService();
stubIOrganisationService.RetrieveStringGuidColumnSet = (entityLogicalName, recordId, columnSet)
=>
{
return recordContext;
};

stubIOrganisationService.ExecuteOrganizationRequest = (request)
=>
{
if (request.RequestName == "RetrieveEntity")
{
RetrieveEntityResponse retrieveEntityResponse = new RetrieveEntityResponse();
EntityMetadata entityMetadata = new EntityMetadata();
retrieveEntityResponse["EntityMetadata"] = entityMetadata;
return retrieveEntityResponse;
}
return null;
};

Microsoft.Xrm.Sdk.Metadata.Fakes.ShimEntityMetadata.AllInstances.AttributesGet =
(item) =>
{
return new AttributeMetadata[]
{
new StubAttributeMetadata(attributeType)
{
LogicalName = attributeName
}
};
};

return stubIOrganisationService;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DXTools.CRM.Solutions.CustomEmails.Common;
using Microsoft.Xrm.Sdk.Fakes;
using Microsoft.Xrm.Sdk;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Metadata.Fakes;
using Microsoft.QualityTools.Testing.Fakes;
using DXTools.CRM.Solutions.CustomEmails.Common.TextProcessing;

namespace DXTools.CRM.Solutions.CustomEmails.UnitTests.Common.TextProcessing
{
[TestClass]
public class DuplicateAttributesTest
{
[TestMethod]
public void TestTextProcessor_DuplicateAttributes()
{
string fullname = "John Smith";

Entity recordContext = new Entity("dxtools_payment");
recordContext["fullname"] = fullname;
recordContext.Id = Guid.NewGuid();

IOrganizationService stubIOrganizationService = SetupIOrganisationService(recordContext);

ITextProcessor crmTextProcessor = new TextProcessor(stubIOrganizationService);

string textToBeProcessed = "Hello dear {fullname}. Now this is a duplicate {fullname}";
string expected = string.Format("Hello dear {0}. Now this is a duplicate {0}", fullname);
string actualResult = crmTextProcessor.Process(textToBeProcessed, recordContext.ToEntityReference());

Assert.AreEqual(expected, actualResult);
}

private IOrganizationService SetupIOrganisationService(Entity recordContext)
{
StubIOrganizationService stubIOrganisationService = new StubIOrganizationService();
stubIOrganisationService.RetrieveStringGuidColumnSet = (entityLogicalName, recordId, columnSet)
=>
{
return recordContext;
};

return stubIOrganisationService;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
</Choose>
<ItemGroup>
<Compile Include="Common\TextProcessing\Functions\TextProcessorTest_ToLocalDateTimeFunction.cs" />
<Compile Include="Common\TextProcessing\DuplicateAttributesTest.cs" />
<Compile Include="StubData.cs" />
<Compile Include="Common\TextProcessing\Functions\TextProcessorTest_ToStringFunction.cs" />
<Compile Include="Common\TextProcessing\BasicTypes\TextProcessorTest_OptionSet.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.11")]
[assembly: AssemblyFileVersion("1.0.0.11")]
[assembly: AssemblyVersion("1.0.0.12")]
[assembly: AssemblyFileVersion("1.0.0.12")]

0 comments on commit 6bd4b73

Please sign in to comment.