Exemple #1
0
        protected virtual TestApplication CreateAndLoadTestApplication <TModule>(ApplicationControllerBase applicationController = null, ISettingsManager settingsManager = null, bool loadXrmConnection = true, bool addSavedConnectionAppConnectionModule = true)
            where TModule : ModuleBase, new()
        {
            var testApplication = TestApplication.CreateTestApplication(applicationController, settingsManager);

            testApplication.AddModule <TModule>();
            if (addSavedConnectionAppConnectionModule)
            {
                testApplication.AddModule <SavedConnectionAppConnectionModule>();
            }
            if (loadXrmConnection)
            {
                SavedXrmConnectionsModule.RefreshXrmServices(GetXrmRecordConfiguration(), testApplication.Controller);
                testApplication.Controller.RegisterInstance <ISavedXrmConnections>(new SavedXrmConnections.SavedXrmConnections
                {
                    Connections = new[] { GetSavedXrmRecordConfiguration() }
                });
            }
            return(testApplication);
        }
        private void LoadActiveXrmConnection(Dictionary <string, string> arguments)
        {
            //if we have an active connection in the command line then we need to load it
            if (arguments.ContainsKey("ActiveXrmConnection"))
            {
                var savedConnection = arguments["ActiveXrmConnection"];
                if (string.IsNullOrWhiteSpace(savedConnection))
                {
                    throw new NullReferenceException("Command Argument ActiveXrmConnection Is Empty");
                }

                ConsoleApplicationController.WriteToConsole($"Loading ActiveXrmConnection: '{savedConnection}'");

                var savedConnections = Controller.ResolveType(typeof(ISavedXrmConnections)) as ISavedXrmConnections;
                if (savedConnections == null)
                {
                    throw new NullReferenceException(nameof(savedConnections));
                }

                var matchingConnections = savedConnections
                                          .Connections
                                          .Where(c => c.Name == savedConnection)
                                          .ToArray();
                if (!matchingConnections.Any())
                {
                    throw new NullReferenceException("No saved Connection Found With Name Of " + savedConnection);
                }

                var setConnectionActive = matchingConnections.First();
                var validate            = setConnectionActive.Validate();
                if (!validate.IsValid)
                {
                    throw new Exception($"Error Loading ActiveXrmConnection '{savedConnection}': {validate.GetErrorString()}");
                }

                SavedXrmConnectionsModule.RefreshXrmServices(matchingConnections.First(), Controller);
            }
        }
Exemple #3
0
        protected override void CompleteDialogExtention()
        {
            if (SaveSettings)
            {
                var isMovingFolder = VisualStudioService.GetSolutionFolder(VisualStudioService.ItemFolderName) == null &&
                                     VisualStudioService.GetItemText("solution.xrmconnection", "SolutionItems") != null;
                base.CompleteDialogExtention();
                //set the active connection to the connection selected as active
                if (SettingsObject.Connections != null)
                {
                    var activeConnections = SettingsObject.Connections.Where(c => c.Active);
                    if (activeConnections.Any())
                    {
                        var activeConnection = activeConnections.First();
                        var settingsManager  = ApplicationController.ResolveType(typeof(ISettingsManager)) as ISettingsManager;
                        if (settingsManager == null)
                        {
                            throw new NullReferenceException("settingsManager");
                        }
                        settingsManager.SaveSettingsObject(activeConnection);

                        SavedXrmConnectionsModule.RefreshXrmServices(activeConnection, ApplicationController, xrmRecordService: (RefreshActiveServiceConnection ? XrmRecordService : null));
                        LookupService = (XrmRecordService)ApplicationController.ResolveType(typeof(XrmRecordService));
                    }
                }
                if (isMovingFolder)
                {
                    var openIt = ApplicationController.UserConfirmation("This Visual Studio extention is changing the way saved settings are stored. Click yes to open a window outlining the changes, and detailing code changes required if you use instances of the Xrm Solution Template");
                    if (openIt)
                    {
                        var blah             = new SettingsFolderMoving();
                        var displaySomething = new ObjectDisplayViewModel(blah, FormController.CreateForObject(blah, ApplicationController, null));
                        ApplicationController.NavigateTo(displaySomething);
                    }
                }
            }
        }
        public void CheckRefreshActiveSettings()
        {
            //okay so I could not subscribe to when a solution is opened
            //at which point I would need to load these settings into the unity container
            //so each time a button is clicked I will just ensure the objects in unity
            //are consistent with the active solutions settings

            var settingsManager     = (ISettingsManager)Controller.ResolveType(typeof(ISettingsManager));
            var settingsConnection  = settingsManager.Resolve <XrmRecordConfiguration>();
            var containerConnection = (IXrmRecordConfiguration)Controller.ResolveType(typeof(IXrmRecordConfiguration));

            if (settingsConnection.AuthenticationProviderType != containerConnection.AuthenticationProviderType ||
                settingsConnection.DiscoveryServiceAddress != containerConnection.DiscoveryServiceAddress ||
                settingsConnection.OrganizationUniqueName != containerConnection.OrganizationUniqueName ||
                settingsConnection.Domain != containerConnection.Domain ||
                settingsConnection.Username != containerConnection.Username ||
                settingsConnection.Password?.GetRawPassword() != containerConnection.Password?.GetRawPassword())
            {
                SavedXrmConnectionsModule.RefreshXrmServices(settingsConnection, Controller);
            }
            var packageSettings = settingsManager.Resolve <XrmPackageSettings>();

            Controller.RegisterInstance(typeof(XrmPackageSettings), packageSettings);
        }
Exemple #5
0
        public void DeploymentImportSqlTest()
        {
            //this script create some data in a database
            //then runs a synch twice into dynamics with a reference change in between

            //it also fakes the process running in a console app

            //also verifies email sent with summary

            var queueSendFrom = XrmRecordService.ToIRecord(TestQueue);
            var queueSendTo   = XrmRecordService.ToIRecord(TestQueue1);

            //script of basic database import
            PrepareTests();
            DeleteAll(Entities.email);

            var sqlServer    = @"LT5CG0110PZT";
            var databaseName = "TestScriptDatabaseImport";

            //request with mappings from sql to dynamics
            var request = new ImportSqlRequest()
            {
                ConnectionString             = $"Provider=sqloledb;Data Source={sqlServer};Initial Catalog={databaseName};Integrated Security=SSPI;",
                SendNotificationAtCompletion = true,
                SendNotificationFromQueue    = XrmRecordService.ToLookup(queueSendFrom),
                SendNotificationToQueue      = XrmRecordService.ToLookup(queueSendTo),
                Mappings = new[]
                {
                    new ImportSqlRequest.SqlImportTableMapping
                    {
                        SourceTable = new RecordType("Accounts", "Accounts"),
                        TargetType  = new RecordType(Entities.account, Entities.account),
                        Mappings    = new []
                        {
                            new ImportSqlRequest.SqlImportTableMapping.SqlImportFieldMapping()
                            {
                                SourceColumn = new RecordField("Name", "Name"),
                                TargetField  = new RecordField(Fields.account_.name, Fields.account_.name)
                            }
                        }
                    },
                    new ImportSqlRequest.SqlImportTableMapping
                    {
                        SourceTable = new RecordType("TestRecords", "TestRecords"),
                        TargetType  = new RecordType(Entities.jmcg_testentity, Entities.jmcg_testentity),
                        Mappings    = new []
                        {
                            new ImportSqlRequest.SqlImportTableMapping.SqlImportFieldMapping()
                            {
                                SourceColumn = new RecordField("Name", "Name"),
                                TargetField  = new RecordField(Fields.jmcg_testentity_.jmcg_name, Fields.jmcg_testentity_.jmcg_name)
                            },
                            new ImportSqlRequest.SqlImportTableMapping.SqlImportFieldMapping()
                            {
                                SourceColumn = new RecordField("Account", "Account"),
                                TargetField  = new RecordField(Fields.jmcg_testentity_.jmcg_account, Fields.jmcg_testentity_.jmcg_account)
                            },
                            new ImportSqlRequest.SqlImportTableMapping.SqlImportFieldMapping()
                            {
                                SourceColumn = new RecordField("Integer", "Integer"),
                                TargetField  = new RecordField(Fields.jmcg_testentity_.jmcg_integer, Fields.jmcg_testentity_.jmcg_integer)
                            }
                        }
                    }
                }
            };

            //create record and field metadata for the source mappings
            //which will be used to create the source database
            var recordMetadatas = new List <RecordMetadata>();

            foreach (var item in request.Mappings)
            {
                var recordMetadata = new RecordMetadata()
                {
                    SchemaName  = item.SourceTable.Key,
                    DisplayName = item.SourceTable.Key
                };
                var fields = new List <FieldMetadata>();
                foreach (var column in item.Mappings)
                {
                    fields.Add(new StringFieldMetadata(column.SourceColumn.Key, column.SourceColumn.Key));
                }
                recordMetadata.Fields = fields;
                recordMetadatas.Add(recordMetadata);
            }

            //if the database doesnt exist create it
            if (!SqlProvider.DatabaseExists(sqlServer, databaseName))
            {
                SqlProvider.CreateDatabase(sqlServer, databaseName);
            }
            //ensure the source database contains tables/columns for our source mappings metadata
            var recordMetadataService = new SqlRecordMetadataService(new SqlServerAndDbSettings(sqlServer, databaseName), recordMetadatas);

            recordMetadataService.RefreshSource();

            //configure synch data
            var recordsToCreate = 3;

            foreach (var tableMapping in request.Mappings)
            {
                //delete all in both
                var truncate = $"truncate table {tableMapping.SourceTable.Key}";
                recordMetadataService.ExecuteSql(truncate);
                DeleteAll(tableMapping.TargetType.Key);

                //create data in the db
                for (var i = 1; i <= recordsToCreate; i++)
                {
                    var newRecord = recordMetadataService.NewRecord(tableMapping.SourceTable.Key);
                    foreach (var fieldMapping in tableMapping.Mappings)
                    {
                        newRecord.SetField(fieldMapping.SourceColumn.Key, i.ToString(), recordMetadataService);
                    }
                    recordMetadataService.Create(newRecord);
                }
            }

            //run the synch
            var app = CreateAndLoadTestApplication <ImportSqlModule>();

            app.AddModule <ConsoleApplicationModule>();

            //navigate to the dialog
            var dialog         = app.NavigateToDialog <ImportSqlModule, ImportSqlDialog>();
            var entryViewmodel = app.GetSubObjectEntryViewModel(dialog);

            app.EnterObject(request, entryViewmodel);
            entryViewmodel.SaveButtonViewModel.Invoke();

            var completionScreen = app.GetCompletionViewModel(dialog);
            var importResponse   = completionScreen.GetObject() as ImportSqlResponse;

            Assert.IsNotNull(importResponse);
            Assert.IsFalse(importResponse.ResponseItems.Any());

            foreach (var tableMapping in request.Mappings)
            {
                var records = XrmRecordService.RetrieveAll(tableMapping.TargetType.Key, null);
                Assert.AreEqual(recordsToCreate, records.Count());
            }

            //point the account field in the db to 3
            var linkAllToAccount3 = $"update TestRecords set Account = 3";

            recordMetadataService.ExecuteSql(linkAllToAccount3);

            dialog         = app.NavigateToDialog <ImportSqlModule, ImportSqlDialog>();
            entryViewmodel = app.GetSubObjectEntryViewModel(dialog);
            app.EnterObject(request, entryViewmodel);

            //okay here we generate a saved request and get the command line for it
            //to also run a console app after this second synch
            ClearSavedRequests(app, entryViewmodel);
            //trigger save request
            var saveRequestButton = entryViewmodel.GetButton("SAVEREQUEST");

            saveRequestButton.Invoke();

            //enter and save details including autoload
            var saveRequestForm = app.GetSubObjectEntryViewModel(entryViewmodel);
            var detailsEntered  = new SaveAndLoadFields()
            {
                Name = "TestName"
            };

            app.EnterAndSaveObject(detailsEntered, saveRequestForm);
            Assert.IsFalse(entryViewmodel.ChildForms.Any());
            Assert.IsFalse(entryViewmodel.LoadingViewModel.IsLoading);

            //invoke load request dialog
            var loadRequestButton = entryViewmodel.GetButton("LOADREQUEST");

            loadRequestButton.Invoke();
            var loadRequestForm = app.GetSubObjectEntryViewModel(entryViewmodel);

            //verify there is a saved request and trigger the generate bat button
            var subGrid = loadRequestForm.GetEnumerableFieldViewModel(nameof(SavedSettings.SavedRequests));

            Assert.IsTrue(subGrid.GridRecords.Count() == 1);
            subGrid.GridRecords.First().IsSelected = true;

            var generateBatButton = subGrid.DynamicGridViewModel.GetButton("GENERATEBAT");

            generateBatButton.Invoke();

            var testFiles = FileUtility.GetFiles(TestingFolder);

            Assert.AreEqual(1, testFiles.Count());
            Assert.IsTrue(testFiles.First().EndsWith(".bat"));
            var batContent = File.ReadAllText(testFiles.First());

            loadRequestForm.CancelButtonViewModel.Invoke();
            Assert.IsFalse(entryViewmodel.ChildForms.Any());
            Assert.IsFalse(entryViewmodel.LoadingViewModel.IsLoading);

            //okay we now have the bat args for later so lets run the update synch in the app
            entryViewmodel.SaveButtonViewModel.Invoke();
            completionScreen = app.GetCompletionViewModel(dialog);
            importResponse   = completionScreen.GetObject() as ImportSqlResponse;
            Assert.IsNotNull(importResponse);
            Assert.IsFalse(importResponse.ResponseItems.Any());

            //verify all point to account 3
            var updatedRecords = XrmRecordService.RetrieveAll(Entities.jmcg_testentity, null);

            Assert.AreEqual(3, updatedRecords.Count());
            Assert.IsTrue(updatedRecords.All(r => r.GetLookupName(Fields.jmcg_testentity_.jmcg_account) == "3"));

            //okay lets fake run it for the console app args generated
            var args = ConsoleTestUtility.CommandLineToArgs(batContent)
                       .Skip(1)
                       .ToArray();

            var arguments       = ConsoleApplication.ParseCommandLineArguments(args);
            var applicationName = arguments.ContainsKey("SettingsFolderName") ? arguments["SettingsFolderName"] : "Unknown Console Context";

            //okay need to create app
            var dependencyResolver = new DependencyContainer();
            var controller         = new ConsoleApplicationController(applicationName, dependencyResolver);
            var settingsManager    = new DesktopSettingsManager(controller);
            var applicationOptions = new ApplicationOptionsViewModel(controller);
            var consoleApp         = new ConsoleApplication(controller, applicationOptions, settingsManager);

            //load modules in folder path
            consoleApp.LoadModulesInExecutionFolder();

            var connection = GetSavedXrmRecordConfiguration();

            SavedXrmConnectionsModule.RefreshXrmServices(GetXrmRecordConfiguration(), consoleApp.Controller);
            consoleApp.Controller.RegisterInstance <ISavedXrmConnections>(new SavedXrmConnections
            {
                Connections = new[] { connection }
            });

            //run app
            consoleApp.Run(args);

            //email created for each synch
            var emails = XrmRecordService.RetrieveAll(Entities.email, null);

            Assert.AreEqual(3, emails.Count());
            Assert.IsTrue(emails.All(e => e.GetOptionKey(Fields.email_.statecode) == OptionSets.Email.ActivityStatus.Completed.ToString()));
        }
Exemple #6
0
        public void ConsoleApplicationModuleWithXrmConnectionGenerateBatAndExecuteTest()
        {
            //this is done in the load.edit saved request form
            //a button is added the the saved request grid whiuch generates the .bat

            //so we need to save a request
            //then navigate to the save requests and trigger the generate bat button

            var testApplication = CreateThisApp();

            //set to no previously saved ones
            var savedRequests   = new SavedSettings();
            var settingsManager = testApplication.Controller.ResolveType <ISettingsManager>();

            settingsManager.SaveSettingsObject(savedRequests, typeof(TestSavedRequestWithXrmConnectionDialogRequest));

            //navigate to the request and populate the string field
            var entryForm = testApplication.NavigateToDialogModuleEntryForm <TestSavedRequestWithXrmConnectionModule, TestSavedRequestWithXrmConnectionDialog>();
            var request   = new TestSavedRequestWithXrmConnectionDialogRequest()
            {
                SomeArbitraryString = nameof(TestSavedRequestWithXrmConnectionDialogRequest.SomeArbitraryString)
            };

            testApplication.EnterObject(request, entryForm);

            //trigger save request
            var saveRequestButton = entryForm.GetButton("SAVEREQUEST");

            saveRequestButton.Invoke();

            //enter and save details including autoload
            var saveRequestForm = testApplication.GetSubObjectEntryViewModel(entryForm);
            var detailsEntered  = new SaveAndLoadFields()
            {
                Name = "TestName"
            };

            testApplication.EnterAndSaveObject(detailsEntered, saveRequestForm);
            Assert.IsFalse(entryForm.ChildForms.Any());
            Assert.IsFalse(entryForm.LoadingViewModel.IsLoading);

            //reopen app/dialog
            testApplication = CreateThisApp();
            entryForm       = testApplication.NavigateToDialogModuleEntryForm <TestSavedRequestWithXrmConnectionModule, TestSavedRequestWithXrmConnectionDialog>();

            //invoke load request dialog
            var loadRequestButton = entryForm.GetButton("LOADREQUEST");

            loadRequestButton.Invoke();
            var loadRequestForm = testApplication.GetSubObjectEntryViewModel(entryForm);

            //verify there is a saved request and trigger the generate bat button
            var subGrid = loadRequestForm.GetEnumerableFieldViewModel(nameof(SavedSettings.SavedRequests));

            Assert.IsTrue(subGrid.GridRecords.Count() == 1);
            subGrid.GridRecords.First().IsSelected = true;

            var generateBatButton = subGrid.DynamicGridViewModel.GetButton("GENERATEBAT");

            generateBatButton.Invoke();

            var testFiles = FileUtility.GetFiles(TestingFolder);

            Assert.AreEqual(1, testFiles.Count());
            Assert.IsTrue(testFiles.First().EndsWith(".bat"));

            var batContent = File.ReadAllText(testFiles.First());

            var args = ConsoleTestUtility.CommandLineToArgs(batContent)
                       .Skip(1)
                       .ToArray();

            var arguments       = ConsoleApplication.ParseCommandLineArguments(args);
            var applicationName = arguments.ContainsKey("SettingsFolderName") ? arguments["SettingsFolderName"] : "Unknown Console Context";

            //okay need to create app
            var dependencyResolver = new DependencyContainer();
            var controller         = new ConsoleApplicationController(applicationName, dependencyResolver);

            settingsManager = new DesktopSettingsManager(controller);
            var applicationOptions = new ApplicationOptionsViewModel(controller);
            var app = new ConsoleApplication(controller, applicationOptions, settingsManager);

            //load modules in folder path
            app.LoadModulesInExecutionFolder();

            //for this we will register saved connections in the console
            //in reality they would have been created on disk by the app and loaded by the module\

            //this was just debugging an invalid connection
            //var c1 = GetXrmRecordConfiguration();
            //var c2 = GetSavedXrmRecordConfiguration();
            //c1.Password = new Password("Nope", false, true);
            //c2.Password = new Password("Nope", false, true);
            //XrmConnectionModule.RefreshXrmServices(c1, app.Controller);
            //app.Controller.RegisterInstance<ISavedXrmConnections>(new SavedXrmConnections
            //{
            //    Connections = new[] { c2 }
            //});

            SavedXrmConnectionsModule.RefreshXrmServices(GetXrmRecordConfiguration(), app.Controller);
            app.Controller.RegisterInstance <ISavedXrmConnections>(new SavedXrmConnections
            {
                Connections = new[] { GetSavedXrmRecordConfiguration() }
            });

            //run app
            app.Run(args);
        }