private void DeleteEntities(IEnumerable <CustomisationImportRequest> requests)
        {
            var response = new CustomisationImportResponse();

            foreach (var request in requests)
            {
                var optionMetadata =
                    CustomisationImportService.ExtractOptionSetsFromExcel(request.ExcelFile.FileName, Controller, response);
                var fieldMetadata =
                    CustomisationImportService.ExtractFieldMetadataFromExcel(request.ExcelFile.FileName, Controller,
                                                                             optionMetadata, response).Values;
                var recordMetadata =
                    CustomisationImportService.ExtractRecordMetadataFromExcel(request.ExcelFile.FileName, Controller,
                                                                              fieldMetadata, response).Values;
                foreach (
                    var metadata in recordMetadata)
                {
                    if (XrmRecordService.RecordTypeExists(metadata.SchemaName))
                    {
                        XrmRecordService.DeleteRecordType(metadata.SchemaName);
                    }
                    Assert.IsFalse(XrmRecordService.RecordTypeExists(metadata.SchemaName));
                }
            }
        }
        public void CustomisationImportTestIgnore()
        {
            var file     = "TestCustomisationsIgnore.xlsx";
            var response = new CustomisationImportResponse();

            var optionMetadata =
                CustomisationImportService.ExtractOptionSetsFromExcel(file, Controller, response);

            Assert.AreEqual(1, optionMetadata.Count());

            var fieldMetadata =
                CustomisationImportService.ExtractFieldMetadataFromExcel(file, Controller,
                                                                         optionMetadata, response).Values;

            Assert.AreEqual(1, fieldMetadata.Count());

            var recordMetadata =
                CustomisationImportService.ExtractRecordMetadataFromExcel(file, Controller,
                                                                          fieldMetadata, response);

            Assert.AreEqual(1, recordMetadata.Count());

            var relationshipMetadata =
                CustomisationImportService.ExtractRelationshipMetadataFromExcel(file, Controller, response);

            Assert.AreEqual(1, relationshipMetadata.Count());
        }
        private void VerifyRecordTypes(CustomisationImportRequest request)
        {
            var response = new CustomisationImportResponse();

            var optionMetadata =
                CustomisationImportService.ExtractOptionSetsFromExcel(request.ExcelFile.FileName, Controller, response);
            var fieldMetadata =
                CustomisationImportService.ExtractFieldMetadataFromExcel(request.ExcelFile.FileName, Controller,
                                                                         optionMetadata, response).Values;
            var recordMetadata =
                CustomisationImportService.ExtractRecordMetadataFromExcel(request.ExcelFile.FileName, Controller,
                                                                          fieldMetadata, response).Values;

            foreach (var metadata in recordMetadata)
            {
                Assert.IsTrue(XrmRecordService.RecordTypeExists(metadata.SchemaName));
                Assert.IsTrue(metadata.DisplayName == XrmRecordService.GetRecordTypeMetadata(metadata.SchemaName).DisplayName);
                Assert.IsTrue(metadata.CollectionName ==
                              XrmRecordService.GetRecordTypeMetadata(metadata.SchemaName).CollectionName);
                Assert.IsTrue(metadata.Audit == XrmRecordService.GetRecordTypeMetadata(metadata.SchemaName).Audit);
            }
        }
        private void VerifyViews(CustomisationImportRequest request)
        {
            var response = new CustomisationImportResponse();

            var optionMetadata =
                CustomisationImportService.ExtractOptionSetsFromExcel(request.ExcelFile.FileName, Controller, response);
            var fieldMetadata =
                CustomisationImportService.ExtractFieldMetadataFromExcel(request.ExcelFile.FileName, Controller,
                                                                         optionMetadata, response).Values;
            var recordMetadata =
                CustomisationImportService.ExtractRecordMetadataFromExcel(request.ExcelFile.FileName, Controller,
                                                                          fieldMetadata, response).Values;

            foreach (var metadata in recordMetadata)
            {
                var views = XrmRecordService.GetViewsToUpdate(metadata);
                Assert.IsTrue(views.Any());
                foreach (var query in views)
                {
                    foreach (var viewField in metadata.Views.First().Fields)
                    {
                        if (query.Contains("fetchxml"))
                        {
                            Assert.IsTrue(
                                query.GetStringField("fetchxml")
                                .Contains("<attribute name=\"" + viewField.FieldName + "\" />"));
                        }
                        if (query.Contains("layoutxml"))
                        {
                            Assert.IsTrue(
                                query.GetStringField("layoutxml")
                                .Contains("<cell name=\"" + viewField.FieldName + "\" width=\"" + viewField.Width +
                                          "\" />"));
                        }
                    }
                }
            }
        }
        public void CustomisationImportTestImportServiceAddToSolution()
        {
            //verifies that entities or shared option sets
            //are added to solution for customisation import where selected
            PrepareTests();

            var testSolution = ReCreateTestSolution();

            //initial verifies created entity, field, shared option set and n2n relationship
            var request = new CustomisationImportRequest
            {
                ExcelFile        = new FileReference("TestCustomisationsAddToSolution.xlsx"),
                Entities         = true,
                Fields           = true,
                Relationships    = true,
                Views            = false,
                FieldOptionSets  = true,
                SharedOptionSets = true,
                AddToSolution    = true,
                Solution         = testSolution.ToLookup()
            };

            var importService =
                new XrmCustomisationImportService(XrmRecordService);

            var response = importService.Execute(request, CreateServiceRequestController());

            if (response.HasError)
            {
                Assert.Fail(response.GetResponseItemsWithError().First().Exception.DisplayString());
            }

            Assert.IsFalse(response.ExcelReadErrors);
            Assert.IsNull(response.Exception);

            var currentComponentIds = XrmRecordService.RetrieveAllAndClauses(Entities.solutioncomponent, new[]
            {
                new Condition(Fields.solutioncomponent_.solutionid, ConditionType.Equal, testSolution.Id)
            }, null).Select(c => c.GetIdField(Fields.solutioncomponent_.objectid));

            var dummyResponse        = new CustomisationImportResponse();
            var relationShipmetadata =
                CustomisationImportService.ExtractRelationshipMetadataFromExcel(request.ExcelFile.FileName, Controller, response).Values;
            var optionMetadata =
                CustomisationImportService.ExtractOptionSetsFromExcel(request.ExcelFile.FileName, Controller, response)
                .Where(o => o.IsSharedOptionSet);

            var fieldMetadata =
                CustomisationImportService.ExtractFieldMetadataFromExcel(request.ExcelFile.FileName, Controller, optionMetadata, response).Values;

            var typeMetadata =
                CustomisationImportService.ExtractRecordMetadataFromExcel(request.ExcelFile.FileName, Controller, fieldMetadata, response).Values;

            Assert.IsTrue(relationShipmetadata.All(r => currentComponentIds.Contains(XrmRecordService.GetRecordTypeMetadata(r.RecordType1).MetadataId)));
            Assert.IsTrue(relationShipmetadata.All(r => currentComponentIds.Contains(XrmRecordService.GetRecordTypeMetadata(r.RecordType2).MetadataId)));
            Assert.IsTrue(optionMetadata.All(r => currentComponentIds.Contains(XrmRecordService.GetSharedPicklist(r.SchemaName).MetadataId)));
            Assert.IsTrue(fieldMetadata.All(r => currentComponentIds.Contains(XrmRecordService.GetRecordTypeMetadata(r.RecordType).MetadataId)));
            Assert.IsTrue(typeMetadata.All(r => currentComponentIds.Contains(XrmRecordService.GetRecordTypeMetadata(r.SchemaName).MetadataId)));

            //this one verifies where just fields picklist options and entity views
            testSolution = ReCreateTestSolution();

            request = new CustomisationImportRequest
            {
                ExcelFile        = new FileReference("TestCustomisationsAddToSolution.xlsx"),
                Entities         = false,
                Fields           = false,
                Relationships    = false,
                Views            = true,
                FieldOptionSets  = true,
                SharedOptionSets = true,
                AddToSolution    = true,
                Solution         = testSolution.ToLookup()
            };

            importService =
                new XrmCustomisationImportService(XrmRecordService);

            response = importService.Execute(request, CreateServiceRequestController());
            if (response.HasError)
            {
                Assert.Fail(response.GetResponseItemsWithError().First().Exception.DisplayString());
            }

            Assert.IsFalse(response.ExcelReadErrors);
            Assert.IsNull(response.Exception);

            currentComponentIds = XrmRecordService.RetrieveAllAndClauses(Entities.solutioncomponent, new[]
            {
                new Condition(Fields.solutioncomponent_.solutionid, ConditionType.Equal, testSolution.Id)
            }, null).Select(c => c.GetIdField(Fields.solutioncomponent_.objectid));
            //addded for field picklist change
            Assert.IsTrue(currentComponentIds.Contains(XrmRecordService.GetRecordTypeMetadata(Entities.account).MetadataId));
            //addded for field change
            Assert.IsTrue(currentComponentIds.Contains(XrmRecordService.GetRecordTypeMetadata("new_testentitysolutionadd").MetadataId));
        }