public void CreateVehicleCountEntry()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;

            #region VehicleCountSchedule EntityCollection
            var VehicleCountSchedule = new EntityCollection
            {
                EntityName = "gsc_iv_vehiclecountschedule",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_vehiclecountschedule",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_description",  "Put desc here."              },
                            { "gsc_status",       new OptionSetValue(100000000) },
                            { "gsc_documentdate", DateTime.Today                }
                        }
                    }
                }
            };
            #endregion

            #endregion

            #region 2. Call/Action
            var VehicleCountScheduleHandler = new VehicleCountScheduleHandler(orgService, orgTracing);
            VehicleCountScheduleHandler.CreateVehicleCountEntry(VehicleCountSchedule.Entities[0]);
            #endregion

            #region 3. Verify
            #endregion
        }
        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePostVehicleCountScheduleUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;

            Entity preImageEntity  = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
            Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;

            IOrganizationService service = localContext.OrganizationService;
            ITracingService      trace   = localContext.TracingService;
            Entity vehicleCount          = (Entity)context.InputParameters["Target"];

            if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity))
            {
                return;
            }

            if (vehicleCount.LogicalName != "gsc_iv_vehiclecountschedule")
            {
                return;
            }

            if (context.Mode == 0) //Synchronous Plug-in
            {
                string message = context.MessageName;

                try
                {
                    #region Pre-images
                    Guid preImageSite = preImageEntity.GetAttributeValue <EntityReference>("gsc_siteid") != null
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_siteid").Id
                        : Guid.Empty;

                    Guid preImageBaseModel = preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid") != null
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Id
                        : Guid.Empty;

                    Guid preImageProduct = preImageEntity.GetAttributeValue <EntityReference>("gsc_productid") != null
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_productid").Id
                        : Guid.Empty;

                    Guid preImageColor = preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid") != null
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Id
                        : Guid.Empty;

                    String preImageModel = preImageEntity.Contains("gsc_modelcode")
                        ? preImageEntity.GetAttributeValue <String>("gsc_modelcode")
                        : String.Empty;

                    String preImageOption = preImageEntity.Contains("gsc_optioncode")
                        ? preImageEntity.GetAttributeValue <String>("gsc_optioncode")
                        : String.Empty;

                    String preImageCopyFilterResult = preImageEntity.Contains("gsc_copyfilterresult")
                        ? preImageEntity.GetAttributeValue <String>("gsc_copyfilterresult")
                        : String.Empty;

                    String preImageCopyAllFilterResult = preImageEntity.Contains("gsc_copyallfilterresult")
                        ? preImageEntity.GetAttributeValue <String>("gsc_copyallfilterresult")
                        : String.Empty;

                    String preImageClearFilterResult = preImageEntity.Contains("gsc_clearfilterresult")
                        ? preImageEntity.GetAttributeValue <String>("gsc_clearfilterresult")
                        : String.Empty;

                    String preImageClearDetail = preImageEntity.Contains("gsc_cleardetail")
                        ? preImageEntity.GetAttributeValue <String>("gsc_cleardetail")
                        : String.Empty;

                    var preImageChangeStatus = preImageEntity.Contains("gsc_changestatus")
                        ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_changestatus").Value
                        : 0;

                    var preImageReuseCount = preImageEntity.Contains("gsc_reusecount")
                        ? preImageEntity.GetAttributeValue <String>("gsc_reusecount")
                        : String.Empty;
                    #endregion

                    #region Post-images
                    Guid postImageSite = postImageEntity.GetAttributeValue <EntityReference>("gsc_siteid") != null
                        ? postImageEntity.GetAttributeValue <EntityReference>("gsc_siteid").Id
                        : Guid.Empty;

                    Guid postImageBaseModel = postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid") != null
                        ? postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Id
                        : Guid.Empty;

                    Guid postImageProduct = postImageEntity.GetAttributeValue <EntityReference>("gsc_productid") != null
                        ? postImageEntity.GetAttributeValue <EntityReference>("gsc_productid").Id
                        : Guid.Empty;

                    Guid postImageColor = postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid") != null
                        ? postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Id
                        : Guid.Empty;

                    String postImageModel = postImageEntity.Contains("gsc_modelcode")
                        ? postImageEntity.GetAttributeValue <String>("gsc_modelcode")
                        : String.Empty;

                    String postImageOption = postImageEntity.Contains("gsc_optioncode")
                        ? postImageEntity.GetAttributeValue <String>("gsc_optioncode")
                        : String.Empty;

                    String postImageCopyFilterResult = postImageEntity.Contains("gsc_copyfilterresult")
                        ? postImageEntity.GetAttributeValue <String>("gsc_copyfilterresult")
                        : String.Empty;

                    String postImageCopyAllFilterResult = postImageEntity.Contains("gsc_copyallfilterresult")
                        ? postImageEntity.GetAttributeValue <String>("gsc_copyallfilterresult")
                        : String.Empty;

                    String postImageClearFilterResult = postImageEntity.Contains("gsc_clearfilterresult")
                        ? postImageEntity.GetAttributeValue <String>("gsc_clearfilterresult")
                        : String.Empty;

                    String postImageClearDetail = postImageEntity.Contains("gsc_cleardetail")
                        ? postImageEntity.GetAttributeValue <String>("gsc_cleardetail")
                        : String.Empty;

                    var postImageChangeStatus = postImageEntity.Contains("gsc_changestatus")
                        ? postImageEntity.GetAttributeValue <OptionSetValue>("gsc_changestatus").Value
                        : 0;

                    var postImageReuseCount = postImageEntity.Contains("gsc_reusecount")
                        ? postImageEntity.GetAttributeValue <String>("gsc_reusecount")
                        : String.Empty;
                    #endregion

                    VehicleCountScheduleHandler vehicleCountHandler = new VehicleCountScheduleHandler(service, trace);

                    //Functions on Invoice No change
                    if (preImageSite != postImageSite || preImageBaseModel != postImageBaseModel || preImageProduct != postImageProduct ||
                        preImageColor != postImageColor || preImageModel != postImageModel || preImageOption != postImageOption)
                    {
                        vehicleCountHandler.ApplyFilter(postImageEntity);
                    }

                    if (preImageCopyFilterResult != postImageCopyFilterResult)
                    {
                        vehicleCountHandler.ReplicateFilterResult(postImageEntity);
                    }

                    if (preImageCopyAllFilterResult != postImageCopyAllFilterResult)
                    {
                        vehicleCountHandler.ReplicateAllFilterResult(postImageEntity);
                    }

                    if (preImageClearFilterResult != postImageClearFilterResult)
                    {
                        vehicleCountHandler.ClearFilterResults(postImageEntity);
                    }

                    if (preImageClearDetail != postImageClearDetail)
                    {
                        vehicleCountHandler.ClearVehicleForCounting(postImageEntity);
                    }

                    if (preImageChangeStatus != postImageChangeStatus)
                    {
                        if (postImageChangeStatus == 100000001)
                        {
                            vehicleCountHandler.CheckDuplicateStartedSchedule(postImageEntity);
                        }

                        else if (postImageChangeStatus == 100000000)
                        {
                            vehicleCountHandler.CancelCountSchedule(postImageEntity);
                        }
                    }

                    if (preImageReuseCount != postImageReuseCount)
                    {
                        vehicleCountHandler.ReuseCountSchedule(postImageEntity);
                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(String.Concat(ex.Message));
                }
            }
        }
        public void CopyVehicleCountScheduleDetail()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;

            #region VehicleCountSchedule EntityCollection
            var VehicleCountSchedule = new EntityCollection
            {
                EntityName = "gsc_iv_vehiclecountschedule",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_vehiclecountschedule",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_description",  "Put desc here."              },
                            { "gsc_status",       new OptionSetValue(100000000) },
                            { "gsc_documentdate", DateTime.Today                }
                        }
                    }
                }
            };
            #endregion

            #region VehicleCountScheduleDetail EntityCollection
            var VehicleCountScheduleDetail = new EntityCollection
            {
                EntityName = "gsc_iv_vehiclecountscheduledetail",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_vehiclecountscheduledetail",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_vehiclecountscheduleid", new EntityReference(VehicleCountSchedule.EntityName, VehicleCountSchedule.Entities[0].Id)              },
                            { "gsc_inventoryid",            new EntityReference("inventory",                     new Guid("e0968b43-af44-e611-80da-00155d010e2d")) },
                            { "gsc_siteid",                 new EntityReference("site",                          new Guid("e0968b43-af44-e611-80da-00155d010e2c")) },
                            { "gsc_vehiclebasemodelid",     new EntityReference("basemodel",                     new Guid("e0968b43-af44-e611-80da-00155d010e2a")) },
                            { "gsc_productid",              new EntityReference("basemodel",                     new Guid("e0968b43-af44-e611-80da-00155d010e2b")) },
                            { "gsc_color",                  "Black" },
                            { "gsc_modelcode",              "0001" },
                            { "gsc_optioncode",             "002" },
                            { "gsc_lastcount",              null }
                        }
                    }
                }
            };
            #endregion

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == VehicleCountScheduleDetail.EntityName)
                                      ))).Returns(VehicleCountScheduleDetail);

            orgServiceMock.Setup(service => service.Retrieve(
                                     It.IsAny <string>(),
                                     It.IsAny <Guid>(),
                                     It.IsAny <ColumnSet>())).Returns(VehicleCountScheduleDetail.Entities[0]);

            orgServiceMock.Setup((service => service.Update(It.Is <Entity>(entity => entity.LogicalName == VehicleCountScheduleDetail.EntityName)))).Callback <Entity>(s => VehicleCountScheduleDetail.Entities[0] = s);

            #endregion

            #region 2. Call/Action
            var    VehicleCountScheduleHandler = new VehicleCountScheduleHandler(orgService, orgTracing);
            Entity returnEntity = VehicleCountScheduleHandler.CopyVehicleCountScheduleDetail(VehicleCountSchedule.Entities[0], new Guid("e0968b43-af44-e611-80da-00155d010e2d"));
            #endregion

            #region 3. Verify
            var schedDetail = VehicleCountScheduleDetail.Entities[0];

            Assert.AreEqual(schedDetail.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Name, returnEntity.GetAttributeValue <String>("gsc_basemodel"));
            Assert.AreEqual(schedDetail.GetAttributeValue <EntityReference>("gsc_productid").Name, returnEntity.GetAttributeValue <String>("gsc_vehiclecountentrydetailspn"));
            Assert.AreEqual(schedDetail.GetAttributeValue <EntityReference>("gsc_siteid").Name, returnEntity.GetAttributeValue <String>("gsc_site"));
            Assert.AreEqual(schedDetail.GetAttributeValue <String>("gsc_color"), returnEntity.GetAttributeValue <String>("gsc_color"));
            Assert.AreEqual(schedDetail.GetAttributeValue <String>("gsc_modelcode"), returnEntity.GetAttributeValue <String>("gsc_modelcode"));
            Assert.AreEqual(schedDetail.GetAttributeValue <String>("gsc_optioncode"), returnEntity.GetAttributeValue <String>("gsc_optioncode"));
            Assert.AreEqual(DateTime.Today.ToString("MM-dd-yyyy"), schedDetail.GetAttributeValue <DateTime>("gsc_lastcount"));

            #endregion
        }
        public void CreateVehicleCountBreakdown()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;

            #region Inventory EntityCollection
            var Inventory = new EntityCollection
            {
                EntityName = "gsc_iv_inventory",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_inventory",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_productionno", "Prod1"   },
                            { "gsc_csno",         "CS1"     },
                            { "gsc_engineno",     "Engine1" },
                            { "gsc_vinno",        "VIN1"    },
                            { "gsc_color",        "Black"   }
                        }
                    }
                }
            };
            #endregion

            #region VehicleCountSchedule EntityCollection
            var VehicleCountSchedule = new EntityCollection
            {
                EntityName = "gsc_iv_vehiclecountschedule",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_vehiclecountschedule",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_description",  "Put desc here."              },
                            { "gsc_status",       new OptionSetValue(100000000) },
                            { "gsc_documentdate", DateTime.Today                }
                        }
                    }
                }
            };
            #endregion

            #region VehicleCountEntry EntityCollection
            var VehicleCountEntry = new EntityCollection
            {
                EntityName = "gsc_iv_vehiclecountentry",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_vehiclecountentry",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                        }
                    }
                }
            };
            #endregion

            #region VehicleCountEntryDetail EntityCollection
            var VehicleCountEntryDetail = new EntityCollection
            {
                EntityName = "gsc_iv_vehiclecountentrydetails",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_vehiclecountentrydetails",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_vehiclecountentryid", new EntityReference(VehicleCountEntry.EntityName, VehicleCountEntry.Entities[0].Id) },
                            { "gsc_inventoryid",         new EntityReference(Inventory.EntityName,         Inventory.Entities[0].Id)         },
                            { "gsc_site",                "Site 1" },
                            { "gsc_modelcode",           "0001" },
                            { "gsc_optioncode",          "002" }
                        }
                    }
                }
            };
            #endregion

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == VehicleCountEntryDetail.EntityName)
                                      ))).Returns(VehicleCountEntryDetail);

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == Inventory.EntityName)
                                      ))).Returns(Inventory);

            #endregion

            #region 2. Call/Action
            var           VehicleCountScheduleHandler = new VehicleCountScheduleHandler(orgService, orgTracing);
            List <Entity> records = VehicleCountScheduleHandler.CreateVehicleCountBreakdown(VehicleCountSchedule.Entities[0], VehicleCountEntry.Entities[0].Id);
            #endregion

            #region 3. Verify
            var schedDetail  = VehicleCountEntryDetail.Entities[0];
            var inventory    = Inventory.Entities[0];
            var returnEntity = records[0];

            Assert.AreEqual(schedDetail.GetAttributeValue <String>("site"), returnEntity.GetAttributeValue <String>("site"));
            Assert.AreEqual(schedDetail.GetAttributeValue <String>("gsc_modelcode"), returnEntity.GetAttributeValue <String>("gsc_modelcode"));
            Assert.AreEqual(schedDetail.GetAttributeValue <String>("gsc_optioncode"), returnEntity.GetAttributeValue <String>("gsc_optioncode"));

            Assert.AreEqual(inventory.GetAttributeValue <String>("gsc_productionno"), returnEntity.GetAttributeValue <String>("gsc_productionno"));
            Assert.AreEqual(inventory.GetAttributeValue <String>("gsc_csno"), returnEntity.GetAttributeValue <String>("gsc_csno"));
            Assert.AreEqual(inventory.GetAttributeValue <String>("gsc_engineno"), returnEntity.GetAttributeValue <String>("gsc_engineno"));
            Assert.AreEqual(inventory.GetAttributeValue <String>("gsc_vinno"), returnEntity.GetAttributeValue <String>("gsc_vinno"));
            Assert.AreEqual(inventory.GetAttributeValue <String>("gsc_color"), returnEntity.GetAttributeValue <String>("gsc_color"));

            #endregion
        }
        public void FilterResultDuplicate()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;

            #region VehicleCountSchedule EntityCollection
            var VehicleCountSchedule = new EntityCollection
            {
                EntityName = "gsc_iv_vehiclecountschedule",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_vehiclecountschedule",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                        }
                    }
                }
            };
            #endregion

            #region VehicleCountScheduleFilter EntityCollection
            var VehicleCountScheduleFilter = new EntityCollection
            {
                EntityName = "gsc_iv_vehiclecountschedulefilter",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_vehiclecountschedulefilter",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_vehiclecountscheduleid", new EntityReference(VehicleCountSchedule.EntityName, VehicleCountSchedule.Entities[0].Id)              },
                            { "gsc_inventoryid",            new EntityReference("inventory",                     new Guid("e0968b43-af44-e611-80da-00155d010e2d")) },
                            { "gsc_siteid",                 new EntityReference("site",                          new Guid("e0968b43-af44-e611-80da-00155d010e2c")) },
                            { "gsc_vehiclebasemodelid",     new EntityReference("basemodel",                     new Guid("e0968b43-af44-e611-80da-00155d010e2a")) },
                            { "gsc_productid",              new EntityReference("basemodel",                     new Guid("e0968b43-af44-e611-80da-00155d010e2b")) },
                            { "gsc_color",                  "Black" },
                            { "gsc_modelcode",              "0001" },
                            { "gsc_optioncode",             "002" }
                        }
                    }
                }
            };
            #endregion

            #region VehicleCountScheduleDetail EntityCollection
            var VehicleCountScheduleDetail = new EntityCollection
            {
                EntityName = "gsc_iv_vehiclecountscheduledetail",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_vehiclecountscheduledetail",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_vehiclecountscheduleid", new EntityReference(VehicleCountSchedule.EntityName, VehicleCountSchedule.Entities[0].Id)              },
                            { "gsc_inventoryid",            new EntityReference("inventory",                     new Guid("e0968b43-af44-e611-80da-00155d010e2d")) },
                            { "gsc_siteid",                 new EntityReference("site",                          new Guid("e0968b43-af44-e611-80da-00155d010e2c")) },
                            { "gsc_vehiclebasemodelid",     new EntityReference("basemodel",                     new Guid("e0968b43-af44-e611-80da-00155d010e2a")) },
                            { "gsc_productid",              new EntityReference("basemodel",                     new Guid("e0968b43-af44-e611-80da-00155d010e2b")) },
                            { "gsc_color",                  "Black" },
                            { "gsc_modelcode",              "0001" },
                            { "gsc_optioncode",             "002" }
                        }
                    }
                }
            };
            #endregion

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == VehicleCountScheduleFilter.EntityName)
                                      ))).Returns(VehicleCountScheduleFilter);

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == VehicleCountScheduleDetail.EntityName)
                                      ))).Returns(VehicleCountScheduleDetail);

            #endregion

            #region 2. Call/Action
            var           VehicleCountScheduleHandler = new VehicleCountScheduleHandler(orgService, orgTracing);
            List <Entity> records = VehicleCountScheduleHandler.ReplicateFilterResult(VehicleCountSchedule.Entities[0]);
            #endregion

            #region 3. Verify
            var filter = VehicleCountScheduleFilter.Entities[0];

            Assert.AreEqual(0, records.Count);

            #endregion
        }
        public void FilterResultEmpty()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;

            #region VehicleCountSchedule EntityCollection
            var VehicleCountSchedule = new EntityCollection
            {
                EntityName = "gsc_iv_vehiclecountschedule",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_vehiclecountschedule",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {/*
                          * {"gsc_siteid", new EntityReference("site", new Guid("e0968b43-af44-e611-80da-00155d010e2c"))},
                          * {"gsc_vehiclebasemodelid", new EntityReference("basemodel", new Guid("e0968b43-af44-e611-80da-00155d010e2a"))},
                          * {"gsc_productid", new EntityReference("basemodel", new Guid("e0968b43-af44-e611-80da-00155d010e2b"))},
                          * {"gsc_vehiclecolorid", new EntityReference("color", Guid.NewGuid()) { Name = "Blue"}},
                          * {"gsc_modelcode", "0001"},
                          * {"gsc_optioncode", "002"}*/
                            { "gsc_siteid",             new EntityReference("site", new Guid("e0968b43-af44-e611-80da-00155d010e2c")) },
                            { "gsc_vehiclebasemodelid", null },
                            { "gsc_productid",          null },
                            { "gsc_colo",               "" },
                            { "gsc_modelcode",          "" },
                            { "gsc_optioncode",         "" }
                        }
                    }
                }
            };
            #endregion

            #region Product EntityCollection
            var ProductCollection = new EntityCollection
            {
                EntityName = "product",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "product",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_modelcode",  "0001" },
                            { "gsc_optioncode", "0002" }
                        }
                    }
                }
            };
            #endregion

            #region Product Quantity EntityCollection
            var ProductQuantityCollection = new EntityCollection
            {
                EntityName = "gsc_iv_productquantity",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_productquantity",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_siteid",         new EntityReference("site",                       new Guid("e0968b43-af44-e611-80da-00155d010e2c")) },
                            { "gsc_vehiclemodelid", new EntityReference("basemodel",                  new Guid("e0968b43-af44-e611-80da-00155d010e2a")) },
                            { "gsc_vehiclecolorid", new EntityReference("vehiclecolor",               new Guid("e0968b43-af44-e611-80da-00155d010e1a")) },
                            { "gsc_productid",      new EntityReference(ProductCollection.EntityName, ProductCollection.Entities[0].Id)                 }
                        }
                    }
                }
            };
            #endregion

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == ProductQuantityCollection.EntityName)
                                      ))).Returns(ProductQuantityCollection);

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == ProductCollection.EntityName)
                                      ))).Returns(ProductCollection);

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == VehicleCountSchedule.EntityName)
                                      ))).Returns(VehicleCountSchedule);

            #endregion

            #region 2. Call/Action
            var    VehicleCountScheduleHandler = new VehicleCountScheduleHandler(orgService, orgTracing);
            Entity filterResult = VehicleCountScheduleHandler.ApplyFilter(VehicleCountSchedule.Entities[0]);
            #endregion

            #region 3. Verify
            var ProductQuantityEntity  = ProductQuantityCollection.Entities[0];
            var InventorySchedulEntity = ProductCollection.Entities[0];
            Assert.AreEqual(ProductQuantityEntity.GetAttributeValue <EntityReference>("gsc_vehiclemodelid").Id, filterResult.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Id);
            Assert.AreEqual(ProductQuantityEntity.GetAttributeValue <EntityReference>("gsc_productid").Id, filterResult.GetAttributeValue <EntityReference>("gsc_productid").Id);
            Assert.AreEqual(ProductQuantityEntity.GetAttributeValue <EntityReference>("gsc_siteid").Id, filterResult.GetAttributeValue <EntityReference>("gsc_siteid").Id);
            Assert.AreEqual(ProductQuantityEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Id, filterResult.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Id);
            Assert.AreEqual(InventorySchedulEntity.GetAttributeValue <String>("gsc_modelcode"), filterResult.GetAttributeValue <String>("gsc_modelcode"));
            Assert.AreEqual(InventorySchedulEntity.GetAttributeValue <String>("gsc_optioncode"), filterResult.GetAttributeValue <String>("gsc_optioncode"));
            #endregion
        }