Пример #1
0
        public void PublicMethodNullArguments()
        {
            ObjectHelper <Plugin> .ApplyCode(new Type[] { }, null, (plugin, type, sb) =>
            {
                var hasError      = false;
                var declaredSteps = plugin.Steps;

                foreach (var step in declaredSteps)
                {
                    var listParamValues = new List <object>();

                    var parameters = step.Method.GetParameters();

                    for (var i = 0; i < parameters.Length; i++)
                    {
                        listParamValues.Clear();
                        var nullParameterName = parameters.ElementAt(i).Name;
                        for (var j = 0; j < parameters.Length; j++)
                        {
                            var param = parameters.ElementAt(j);
                            if (i == j)
                            {
                                listParamValues.Add(null);
                            }
                            else
                            {
                                if (typeof(IPluginContext).IsAssignableFrom(param.ParameterType))
                                {
                                    listParamValues.Add(new MockPluginContext(Messages.Create, Stages.PostOperation, Modes.Asynchronous, "test"));
                                }
                                else if (typeof(IService).IsAssignableFrom(param.ParameterType))
                                {
                                    var mockServiceType = typeof(Mock <>).MakeGenericType(param.ParameterType);

                                    dynamic obj = mockServiceType.GetConstructor(new Type[] { }).Invoke(new object[] { });
                                    listParamValues.Add((object)obj.Object);
                                }
                            }
                        }
                        var message = string.Format("Method {0}.{1} does not raise ArgumentNullException for parameter {2}", plugin.GetType().Name, step.Method.Name, nullParameterName);
                        AssertException <ArgumentNullException>(() =>
                        {
                            step.Method.Invoke(plugin, listParamValues.ToArray());
                        }, (e) => { StringAssert.Contains(e.Message, nullParameterName, message); }, message);
                    }
                }

                return(hasError);
            }, true);
        }
Пример #2
0
        public void ServiceMethodTestCoverage()
        {
            var mockOrganizationService = new Mock <IOrganizationService>();

            ObjectHelper <DefaultService> .ApplyCode(new Type[] { typeof(IServiceContext) }, new object[] { new MockServiceContext(mockOrganizationService.Object) }, (service, serviceType, sb) =>
            {
                if (serviceType == null)
                {
                    serviceType = typeof(IService);
                }

                var hasError        = false;
                var declaredMethods = serviceType.GetMethods().Select(m => m.Name).ToList();

                var testedMethods = GetTestedMethods(service);

                var notTestedMethods   = declaredMethods.Except(testedMethods);
                var notDeclaredMethods = testedMethods.Except(declaredMethods);

                if (notTestedMethods.Any() || notDeclaredMethods.Any())
                {
                    hasError = true;
                    sb.AppendFormat("\r\nService {0} :\r\n", service.GetType().Name);
                    if (notTestedMethods.Any())
                    {
                        sb.AppendLine("\tNot tested methods :");
                        foreach (var methodName in notTestedMethods)
                        {
                            sb.AppendFormat("\t\t- {0}\r\n", methodName);
                        }
                    }
                    if (notDeclaredMethods.Any())
                    {
                        sb.AppendLine("\tNot declared methods :");
                        foreach (var methodName in notDeclaredMethods)
                        {
                            sb.AppendFormat("\t\t- {0}\r\n", methodName);
                        }
                    }
                }
                return(hasError);
            }, false);
        }
Пример #3
0
        public void StepCoverage()
        {
            ObjectHelper <Plugin> .ApplyCode(new Type[] { }, null, (plugin, type, sb) =>
            {
                var hasError      = false;
                var declaredSteps = plugin.Steps;

                var testedSteps = GetTestedSteps(plugin);
                var comparer    = new StepComparer();

                var notTestedSteps   = declaredSteps.Except(testedSteps, comparer);
                var notDeclaredSteps = testedSteps.Except(declaredSteps, comparer);

                if (notTestedSteps.Any() || notDeclaredSteps.Any())
                {
                    hasError = true;
                    sb.AppendFormat("\r\nPlugin {0} :\r\n", plugin.GetType().Name);
                    if (notTestedSteps.Any())
                    {
                        sb.AppendLine("\tNot tested steps :");
                        foreach (var step in notTestedSteps)
                        {
                            sb.AppendFormat("\t\t- {0} {1} {2} {3}\r\n", step.EntityName, step.Message, step.Mode, step.Stage);
                        }
                    }
                    if (notDeclaredSteps.Any())
                    {
                        sb.AppendLine("\tNot declared steps :");
                        foreach (var step in notDeclaredSteps)
                        {
                            sb.AppendFormat("\t\t- {0} {1} {2} {3}\r\n", step.EntityName, step.Message, step.Mode, step.Stage);
                        }
                    }
                }
                return(hasError);
            }, false);
        }
Пример #4
0
        public void InstantiateAllServices()
        {
            var mockOrganizationService = new Mock <IOrganizationService>();

            ObjectHelper <DefaultService> .ApplyCode(new Type[] { typeof(IServiceContext) }, new object[] { new MockServiceContext(mockOrganizationService.Object) }, null);
        }
Пример #5
0
 public void InstantiateAllPlugins()
 {
     ObjectHelper <Plugin> .ApplyCode(new Type[] { }, null, null);
 }
Пример #6
0
        public static void Register <T, U>(string projectName, string assemblyPath, Func <T, Plugin> PluginConverter, Func <U, Plugin> WorkflowConverter)
        {
            Assembly pluginAssembly = typeof(T).Assembly;

            var pluginList = new List <Deploy.Plugin>();

            ObjectHelper <T> .ApplyCode(new Type[] { }, null, (plugin, type, sb) => { pluginList.Add(PluginConverter(plugin)); return(false); });

            ObjectHelper <U> .ApplyCode(new Type[] { }, null, (wf, type, sb) => { pluginList.Add(WorkflowConverter(wf)); return(false); });

            if (!(ConfigurationManager.GetSection("xrmFramework") is XrmFrameworkSection deploySection))
            {
                return;
            }

            var pluginSolutionUniqueName = deploySection.Projects.OfType <ProjectElement>().Single(p => p.Name == projectName).TargetSolution;

            EnsureAppConfigLink();

            var organizationName = ConfigurationManager.ConnectionStrings[deploySection.SelectedConnection].ConnectionString;

            Console.WriteLine($"You are about to deploy on {organizationName} organization. If ok press any key.");
            Console.ReadKey();
            Console.WriteLine("Connecting to CRM...");

            var cs = ConnectionStringParser.Parse(organizationName);

            var service = new OrganizationServiceProxy(new Uri(new Uri(cs.Url), "/XRMServices/2011/Organization.svc"), null, new ClientCredentials {
                UserName = { UserName = cs.Username, Password = cs.Password }
            }, null);

            service.EnableProxyTypes();

            var assembly = GetAssemblyByName(service, pluginAssembly.GetName().Name);

            var profilerAssembly = GetProfilerAssembly(service);

            InitMetadata(service, pluginSolutionUniqueName);

            var registeredPluginTypes = new List <PluginType>();
            var profiledSteps         = new List <SdkMessageProcessingStep>();
            ICollection <SdkMessageProcessingStep> registeredSteps = Enumerable.Empty <SdkMessageProcessingStep>().ToList();

            if (assembly == null)
            {
                Console.WriteLine("Registering assembly");

                assembly = GetAssemblyToRegister(pluginAssembly, assemblyPath);

                assembly.Id = service.Create(assembly);
            }
            else
            {
                Console.WriteLine("Updating plugin assembly");

                var updatedAssembly = new Entity("pluginassembly");
                updatedAssembly.Id         = assembly.Id;
                updatedAssembly["content"] = Convert.ToBase64String(File.ReadAllBytes(assemblyPath));

                registeredPluginTypes = GetRegisteredPluginTypes(service, assembly.Id).ToList();
                registeredSteps       = GetRegisteredSteps(service, assembly.Id);

                if (profilerAssembly != null)
                {
                    profiledSteps = GetRegisteredSteps(service, profilerAssembly.Id).ToList();
                }

                Console.WriteLine("{0} Steps profiled", profiledSteps.Count());

                foreach (var registeredType in registeredPluginTypes)
                {
                    if (pluginList.All(p => p.FullName != registeredType.Name) && pluginList.Where(p => p.IsWorkflow).All(c => c.FullName != registeredType.FriendlyName))
                    {
                        var registeredStepsForPluginType = registeredSteps.Where(s => s.EventHandler.Id == registeredType.Id).ToList();
                        foreach (var step in registeredStepsForPluginType)
                        {
                            service.Delete(SdkMessageProcessingStep.EntityLogicalName, step.Id);
                            registeredSteps.Remove(step);
                        }
                        service.Delete(PluginType.EntityLogicalName, registeredType.Id);
                    }
                }
                service.Update(updatedAssembly);
            }

            AddSolutionComponentToSolution(service, pluginSolutionUniqueName, assembly.ToEntityReference());

            var registeredImages = GetRegisteredImages(service, assembly.Id);

            foreach (var plugin in pluginList.Where(p => !p.IsWorkflow))
            {
                Console.WriteLine("Registering pluginType {0}", plugin.FullName);

                var registeredPluginType = registeredPluginTypes.FirstOrDefault(p => p.Name == plugin.FullName);

                if (registeredPluginType == null)
                {
                    registeredPluginType    = GetPluginTypeToRegister(assembly.Id, plugin.FullName);
                    registeredPluginType.Id = service.Create(registeredPluginType);
                }

                var registeredStepsForPluginType = registeredSteps.Where(s => s.EventHandler.Id == registeredPluginType.Id).ToList();

                var comparer = new SdkMessageStepComparer();

                foreach (var convertedStep in plugin.Steps)
                {
                    var stepToRegister = GetStepToRegister(registeredPluginType.Id, convertedStep);

                    var registeredStep = registeredStepsForPluginType.FirstOrDefault(s => comparer.Equals(s, stepToRegister));

                    if (registeredStep == null)
                    {
                        stepToRegister.Id = service.Create(stepToRegister);
                    }
                    else
                    {
                        registeredSteps.Remove(registeredStep);
                        stepToRegister.Id = registeredStep.Id;
                        if (comparer.NeedsUpdate(stepToRegister, registeredStep))
                        {
                            var profiledStep = profiledSteps.FirstOrDefault(p => p.Name.StartsWith(registeredStep.Name));

                            if (profiledStep != null)
                            {
                                service.Delete(profiledStep.LogicalName, profiledStep.Id);

                                service.Execute(new SetStateRequest
                                {
                                    EntityMoniker = registeredStep.ToEntityReference(),
                                    State         = new OptionSetValue((int)SdkMessageProcessingStepState.Enabled),
                                    Status        = new OptionSetValue(-1)
                                });
                            }

                            service.Update(stepToRegister);
                        }
                    }

                    AddSolutionComponentToSolution(service, pluginSolutionUniqueName, stepToRegister.ToEntityReference());

                    if (convertedStep.Message != Messages.Associate.ToString() && convertedStep.Message != Messages.Lose.ToString() && convertedStep.Message != Messages.Win.ToString())
                    {
                        var registeredPostImage = registeredImages.FirstOrDefault(i => i.Name == "PostImage" && i.SdkMessageProcessingStepId.Id == stepToRegister.Id);

                        if (convertedStep.PostImageUsed && convertedStep.Message != Messages.Delete.ToString())
                        {
                            if (registeredPostImage == null)
                            {
                                registeredPostImage    = GetImageToRegister(service, stepToRegister.Id, convertedStep, false);
                                registeredPostImage.Id = service.Create(registeredPostImage);
                            }
                            else if (registeredPostImage.Attributes1 != convertedStep.PostImageAttributes)
                            {
                                registeredPostImage.Attributes1 = convertedStep.PostImageAttributes;
                                service.Update(registeredPostImage);
                            }
                        }
                        else if (registeredPostImage != null)
                        {
                            service.Delete(registeredPostImage.LogicalName, registeredPostImage.Id);
                        }

                        var registeredPreImage = registeredImages.FirstOrDefault(i => i.Name == "PreImage" && i.SdkMessageProcessingStepId.Id == stepToRegister.Id);

                        if (convertedStep.PreImageUsed)
                        {
                            if (registeredPreImage == null)
                            {
                                registeredPreImage    = GetImageToRegister(service, stepToRegister.Id, convertedStep, true);
                                registeredPreImage.Id = service.Create(registeredPreImage);
                            }
                            else if (registeredPreImage.Attributes1 != convertedStep.PreImageAttributes)
                            {
                                registeredPreImage.Attributes1 = convertedStep.PreImageAttributes;
                                service.Update(registeredPreImage);
                            }
                        }
                        else if (registeredPreImage != null)
                        {
                            service.Delete(registeredPreImage.LogicalName, registeredPreImage.Id);
                        }
                    }
                }
            }

            foreach (var customWf in pluginList.Where(p => p.IsWorkflow))
            {
                var registeredPluginType = registeredPluginTypes.FirstOrDefault(p => p.FriendlyName == customWf.FullName);

                if (registeredPluginType == null)
                {
                    registeredPluginType    = GetCustomWorkflowTypeToRegister(assembly.Id, customWf.FullName, customWf.DisplayName);
                    registeredPluginType.Id = service.Create(registeredPluginType);
                }
                if (registeredPluginType.Name != customWf.DisplayName)
                {
                    registeredPluginType.Name = customWf.DisplayName;
                    service.Update(registeredPluginType);
                }
            }

            foreach (var step in registeredSteps)
            {
                service.Delete(SdkMessageProcessingStep.EntityLogicalName, step.Id);
            }
        }