Saturday, 18 March 2017

Microsoft Fakes

=================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Client.Services;

namespace ConsoleApplication4
{
    public class OrganizationServiceMethods
    {
        private IOrganizationService _service;

        public OrganizationServiceMethods(IOrganizationService service)
        {
            _service = service;
        }

        public Guid CreateAccount(string name)
        {
            var entity = new Entity("account")
            {

                Attributes = new Microsoft.Xrm.Sdk.AttributeCollection
                {
                    { "name", name },
                    { "telephone1", "89998888" }
                }
            };

            Guid Id = _service.Create(entity);
            return Id;
        }


        public void Update(string DT)
        {
            Entity Acc = _service.Retrieve("account", new Guid(DT), new Microsoft.Xrm.Sdk.Query.ColumnSet("name"));

            Acc.Attributes["name"] = "Testrecords";
            _service.Update(Acc);

        }

        //public void Retrievemultiple()
        //{
        //    // Retrieve Entities.
        //    OrganizationService.RetrieveStringGuidColumnSet = (entityName, entityId, columns) =>
        //    {
        //        if (entityName == "product")
        //        {
        //            Entity entAccount = new Entity("product");
        //            entAccount.Id = new Guid();
        //            entAccount.Attributes["sse_complexitythreshold"] = 2;
        //            entAccount.Attributes["sse_productcategory"] = new OptionSetValue(867690000);
        //            return entAccount;
        //        }
        //        if (entityName == "opportunity")
        //        {
        //            Entity entAccount = new Entity("opportunity");
        //            entAccount.Id = new Guid();
        //            entAccount.Attributes["sse_terminmonths"] = 16;
        //            //entAccount.Attributes["sse_productcategory"] = new OptionSetValue(867690000);
        //            return entAccount;
        //        }
        //        return null;
        //    };
        //}

        //public void Methods()
        //{
        //    // Create entity
        //    service.CreateEntity = delegate(Entity record)
        //    {
        //        Task objTask = new Task();
        //        objTask.Id = new Guid();
        //        objTask.Description = "Task Description";
        //        objTask.Subject = "Task Subject";
        //        objTask.OwnerId = new EntityReference("systemuser", Guid.NewGuid());
        //        objTask.RegardingObjectId = new EntityReference("account", Guid.NewGuid());
        //        objTask.sse_taskcategory = new OptionSetValue(2);
        //        objTask.sse_sladate = DateTime.Now;
        //        return objTask.Id;
        //    };
        //}

        //public Entity test()
        //{
        //    // Create entity
        //    service.CreateEntity = delegate(Entity record)
        //    {
        //        Task objTask = new Task();
        //        objTask.Id = new Guid();
        //        objTask.Description = "Task Description";
        //        objTask.Subject = "Task Subject";
        //        objTask.OwnerId = new EntityReference("systemuser", Guid.NewGuid());
        //        objTask.RegardingObjectId = new EntityReference("account", Guid.NewGuid());
        //        objTask.sse_taskcategory = new OptionSetValue(2);
        //        objTask.sse_sladate = DateTime.Now;
        //        return objTask.Id;
        //    };
        //}


        //public void GetOrgService()
        //{
        //    StubIOrganizationService organizationService = new StubIOrganizationService();

        //    using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create())
        //    {
        //        this.ShimCrmConnection = () => organizationService;
        //        Microsoft.Xrm.Sdk.Fakes.StubIOrganizationServiceFactory organizationServiceFactory = new Microsoft.Xrm.Sdk.Fakes.StubIOrganizationServiceFactory();
        //        organizationService = new StubIOrganizationService();
        //        organizationServiceFactory.CreateOrganizationServiceNullableOfGuid = delegate(Guid? userid)
        //        {
        //            return organizationService;
        //        };
        //    }
        //}

        public static void get()
        {
            ClientCredentials credentials = new ClientCredentials();
            credentials.UserName.UserName = "UserNmae";
            credentials.UserName.Password = "pwd";
            Uri serviceUri = new Uri("https://xyz.api.crm8.dynamics.com/XRMServices/2011/Organization.svc");
            OrganizationServiceProxy organizationServiceProxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
            IOrganizationService getOrganizationService = (IOrganizationService)organizationServiceProxy;
            for (int i = 0; i < 2000; i++)
            {
                Entity entiy = new Entity("new_student");
                entiy.Attributes["new_name"] = "Dummy1" + i;
                entiy.Attributes["new_gender"] = false;
                entiy.Attributes["new_rollnumber"] = 900;
                getOrganizationService.Create(entiy);
            }
        }
    }
}

=================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Xrm.Sdk.Fakes;
using Microsoft.Xrm.Sdk;
using System.Runtime.Serialization;
using System.ServiceModel.Fakes;
using ConsoleApplication4.Fakes;


namespace ConsoleApplication4.Tests
{

    [TestClass()]
    public class OrganizationServiceMethodsTests
    {
        /// <summary>
        /// Gets Shim CRM Connection
        /// </summary>
        public Func<object> ShimCrmConnection { get; private set; }


        /// <summary>
        /// Gets or sets Stub IPlugin Execution Context Plugin Execution Context
        /// </summary>
        // private static StubIPluginExecutionContext Context { get; set; }

        /// <summary>
        /// Gets or sets StubIOrganizationService OrganizationService
        /// </summary>
        private static StubIOrganizationService OrganizationService { get; set; }

        /// <summary>
        /// Gets or sets Initialization of the Class.
        /// </summary>
        /// <param name="textContext">Test Context</param>
        [ClassInitialize]
        public static void ClassInit(TestContext textContext)
        {
            var tracingService = new StubITracingService();
            var orgFactory = new StubIOrganizationServiceFactory();
            OrganizationService = new StubIOrganizationService();
            orgFactory.CreateOrganizationServiceNullableOfGuid = (userId) => OrganizationService;
        }

        [TestMethod()]
        public void CreateAccountTest()
        {



            StubIOrganizationService organizationService = new StubIOrganizationService();
            using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create())
            {
                this.ShimCrmConnection = () => organizationService;
                Microsoft.Xrm.Sdk.Fakes.StubIOrganizationServiceFactory organizationServiceFactory = new Microsoft.Xrm.Sdk.Fakes.StubIOrganizationServiceFactory();
                organizationService = new StubIOrganizationService();
                organizationServiceFactory.CreateOrganizationServiceNullableOfGuid = delegate(Guid? userid)
                {
                    return organizationService;
                };
            }
            string accountName = "abcabcabc";
            Guid actual;
            Guid expected = Guid.NewGuid();
            int callCount = 0;        
            organizationService.CreateEntity = delegate(Entity record)
            {
                Entity tast = new Entity("account");
                tast.Id = Guid.NewGuid();
                tast.Attributes["name"] = "Test";
                tast.Attributes["telephone1"] = "234323545345";                return tast.Id;
            };

        OrganizationServiceMethods target = new OrganizationServiceMethods(organizationService);

            //
            // Act
            //
            actual = target.CreateAccount(accountName);

            //
            // Assert
            //
            Assert.AreEqual(callCount, 1); // verify OrganizationServiceContext.Create is called once
          //  Assert.IsNotNull(entity); // verify OrganizationServiceContext.Create is called with not null object
           // Assert.AreEqual(entity.LogicalName, "account"); // verify OrganizationServiceContext.Create is called with entity with proper entity name
           // Assert.AreEqual(entity.GetAttributeValue<string>("name"), accountName); // verify OrganizationServiceContext.Create is called with entity with proper value set on name attribute

            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void UpdateTest()
        {
            var accountId = Guid.NewGuid();
            var previousNumber = "Test123";
            var expected = "Testrecords";
            string actual = "1004";

            // IOrganizationService
            var service = new Microsoft.Xrm.Sdk.Fakes.StubIOrganizationService();
            service.RetrieveStringGuidColumnSet = (entityName, id, columns) =>
            {
                return new Microsoft.Xrm.Sdk.Entity("account")
                {
                    Id = accountId,
                    Attributes = { { "name", previousNumber } }
                };
            };
            service.UpdateEntity = (entity) =>
            {
                actual = entity.GetAttributeValue<string>("name");
            };
            OrganizationServiceMethods target = new OrganizationServiceMethods(service);
            target.Update(accountId.ToString());

            Assert.AreEqual(expected, actual);
        }




    }
}