/// <summary>
        /// Creates a copy of an activity (Virtual Resource metadata)
        /// </summary>
        /// <param name="source">The source activity to copy from</param>
        /// <param name="srcParent"></param>
        /// <param name="newParent"></param>
        /// <param name="Mappings"></param>
        /// <param name="isPartOfResourceCopy"></param>
        /// <returns>Guid of the Activity (metadata) node that was created</returns>
        public Guid CopyActivity(Guid source, Guid srcParent, Guid newParent, Dictionary <Guid?, Guid?> Mappings, bool isPartOfResourceCopy)
        {
            using (EnterpriseTestContext ctx = new EnterpriseTestContext())
            {
                VirtualResourceMetadata vrmdSource = VirtualResourceMetadata.Select(ctx, source);
                VirtualResourceMetadata vrmdCopy   = vrmdSource.Copy(isPartOfResourceCopy);

                vrmdCopy.Name = vrmdSource.Name;
                VirtualResource dstVR;
                HandleObjectChange(ctx, vrmdCopy);
                ctx.SaveChanges();
                if (vrmdSource.VirtualResourceId == srcParent)
                {
                    dstVR = VirtualResource.Select(ctx, newParent);
                }
                else
                {
                    try
                    {
                        vrmdCopy.FolderId = (Guid)Mappings[srcParent];
                        dstVR             = VirtualResource.Select(ctx, (Guid)Mappings[vrmdSource.VirtualResourceId]);
                    }
                    catch (Exception)
                    {
                        throw new Exception("Cannot find New Target VR / MetaData Folder for " + source.ToString());
                    }
                }
                dstVR.VirtualResourceMetadataSet.Add(vrmdCopy);
                HandleObjectChange(ctx, vrmdCopy);
                HandleObjectChange(ctx, dstVR);
                ctx.SaveChanges();
                return(vrmdCopy.VirtualResourceMetadataId);
            }
        }
        /// <summary>
        /// Gets the entity object.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public EntityObject GetEntityObject(Guid id)
        {
            // Discard our old entity context and create a new one.  This seems wasteful,
            // but if we maintain the same context it will cache all the data that is ever
            // loaded from it.  This can cause huge memory growth over time, as well as
            // concurrency issues.  Creating a new context is cheap and avoids these issues.
            if (_context != null)
            {
                _context.Dispose();
            }
            _context = new EnterpriseTestContext();

            EnterpriseTestMapNode node = _databaseMap[id];

            switch (node.NodeType)
            {
            case ConfigurationObjectType.EnterpriseScenario:
                return(EnterpriseScenario.Select(_context, id));

            case ConfigurationObjectType.VirtualResource:
                return(VirtualResource.Select(_context, id));

            case ConfigurationObjectType.ResourceMetadata:
                return(VirtualResourceMetadata.Select(_context, id));

            case ConfigurationObjectType.ScenarioFolder:
            case ConfigurationObjectType.ResourceFolder:
            case ConfigurationObjectType.MetadataFolder:
                return(ConfigurationTreeFolder.Select(_context, id));

            default:
                return(null);
            }
        }
 /// <summary>
 /// Creates a copy of a Resource.
 /// </summary>
 /// <param name="source">The Source Resource to copy from</param>
 /// <param name="srcParent"></param>
 /// <param name="newParent"></param>
 /// <param name="Mappings"></param>
 /// <returns>Guid of the Resource node that was created</returns>
 public Guid CopyResource(Guid source, Guid srcParent, Guid newParent, Dictionary <Guid?, Guid?> Mappings)
 {
     using (EnterpriseTestContext ctx = new EnterpriseTestContext())
     {
         VirtualResource vrSource = VirtualResource.Select(ctx, source);
         VirtualResource vrCopy   = vrSource.ShallowCopy();
         HandleObjectChange(ctx, vrCopy);
         ctx.SaveChanges();
         EnterpriseScenario dstScenario;
         if (vrSource.EnterpriseScenarioId == srcParent)
         {
             dstScenario = EnterpriseScenario.Select(ctx, newParent);
         }
         else
         {
             try
             {
                 vrCopy.FolderId = (Guid)Mappings[srcParent];
                 dstScenario     = EnterpriseScenario.Select(ctx, (Guid)Mappings[vrSource.EnterpriseScenarioId]);
             }
             catch (Exception)
             {
                 throw new Exception("Cannot find New Target Scenario /Resource Folder for " + source.ToString());
             }
         }
         dstScenario.VirtualResources.Add(vrCopy);
         HandleObjectChange(ctx, vrCopy);
         HandleObjectChange(ctx, dstScenario);
         ctx.SaveChanges();
         return(vrCopy.VirtualResourceId);
     }
 }
예제 #4
0
        private void UpdateTestResult()
        {
            Collection <int> testcaseIds = new Collection <int>();

            Dictionary <int, bool> testIdResult = new Dictionary <int, bool>();

            //using (SqlAdapter datalogsqlAdapter = new SqlAdapter(DataLogSqlConnection.ConnectionString))
            //{
            //    //string sqlText = "select (select count(*) from ActivityExecution where SessionId = '{0}' and UpdateType = 'Completed') as PassedCount, (select count(*) from ActivityExecution where SessionId = '{0}') as TotalCount".FormatWith(_ticket.SessionId);
            //    string sqlText = "select VirtualResourceMetadataId, MIN(case when UpdateType = 'Completed' then 1 else 0 end) as TestResult from ActivityExecution where SessionId = {0}  group by VirtualResourceMetadataId".FormatWith(_ticket.SessionId);
            //    var reader = datalogsqlAdapter.ExecuteReader(sqlText);
            //    if (reader != null)
            //    {
            //        while (reader.Read())
            //        {
            //            TestIdResult.Add(Guid.Parse(reader["VirtualResourceId"].ToString()), Convert.ToBoolean(reader.GetInt32(1))) ;
            //        }

            //    }

            //    reader.Close();
            //}

            DataLogContext traceContext = DbConnect.DataLogContext();

            {
                var activityExecutions = traceContext.SessionData(_ticket.SessionId).Activities;

                var failedActivities = activityExecutions.Where(x => x.Status == "Failed");

                using (EnterpriseTestContext context = new EnterpriseTestContext())
                {
                    foreach (var metadataId in activityExecutions.Select(x => x.ResourceMetadataId.Value).Distinct())
                    {
                        testcaseIds.Add(VirtualResource.Select(context, VirtualResourceMetadata.Select(context, metadataId).VirtualResourceId).TestCaseId);
                    }

                    foreach (var testcaseId in testcaseIds.Distinct())
                    {
                        testIdResult.Add(testcaseId, true);
                    }
                    foreach (var failedActivity in failedActivities)
                    {
                        testIdResult.Remove(VirtualResource.Select(context, VirtualResourceMetadata.Select(context, failedActivity.ResourceMetadataId.Value).VirtualResourceId).TestCaseId);

                        testIdResult.Add(VirtualResource.Select(context, VirtualResourceMetadata.Select(context, failedActivity.ResourceMetadataId.Value).VirtualResourceId).TestCaseId, false);
                    }
                }
            }
            _scenarioList.ElementAt(_currentScenarioIndex).TestResult = testIdResult;
        }
        /// <summary>
        /// Enables/disable the specified object.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="enabled">Whether the object should be enabled.</param>
        public void EnableDisable(Guid id, bool enabled)
        {
            using (EnterpriseTestContext context = new EnterpriseTestContext())
            {
                EnterpriseTestMapNode node = _databaseMap[id];
                switch (node.NodeType)
                {
                case ConfigurationObjectType.VirtualResource:
                    var resource = VirtualResource.Select(context, id);
                    resource.Enabled = enabled;
                    HandleObjectChange(context, resource);
                    break;

                case ConfigurationObjectType.ResourceMetadata:
                    var metadata = VirtualResourceMetadata.Select(context, id);
                    metadata.Enabled = enabled;
                    HandleObjectChange(context, metadata);
                    break;
                }
                context.SaveChanges();
            }
        }
        private void MoveHandler(EnterpriseTestContext context, IEnumerable <Guid> affected)
        {
            foreach (Guid affectedId in affected)
            {
                EnterpriseTestMapNode moved = _databaseMap[affectedId];
                switch (moved.NodeType)
                {
                case ConfigurationObjectType.EnterpriseScenario:
                    var scenario = EnterpriseScenario.Select(context, affectedId);
                    scenario.FolderId = moved.FolderId;
                    HandleObjectChange(context, scenario);
                    break;

                case ConfigurationObjectType.VirtualResource:
                    var resource = VirtualResource.Select(context, affectedId);
                    resource.EnterpriseScenarioId = (Guid)moved.ParentId;
                    resource.FolderId             = moved.FolderId;
                    HandleObjectChange(context, resource);
                    break;

                case ConfigurationObjectType.ResourceMetadata:
                    var metadata = VirtualResourceMetadata.Select(context, affectedId);
                    metadata.VirtualResourceId = (Guid)moved.ParentId;
                    metadata.FolderId          = moved.FolderId;
                    HandleObjectChange(context, metadata);
                    break;

                case ConfigurationObjectType.ScenarioFolder:
                case ConfigurationObjectType.ResourceFolder:
                case ConfigurationObjectType.MetadataFolder:
                    var folder = ConfigurationTreeFolder.Select(context, affectedId);
                    folder.ParentId = moved.ParentId;
                    HandleObjectChange(context, folder);
                    break;
                }
            }
            context.SaveChanges();
        }
        /// <summary>
        /// Renames the object with the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="name">The name.</param>
        public void Rename(Guid id, string name)
        {
            using (EnterpriseTestContext context = new EnterpriseTestContext())
            {
                EnterpriseTestMapNode node = _databaseMap[id];
                switch (node.NodeType)
                {
                case ConfigurationObjectType.EnterpriseScenario:
                    var scenario = EnterpriseScenario.Select(context, id);
                    scenario.Name = name;
                    HandleObjectChange(context, scenario);
                    break;

                case ConfigurationObjectType.VirtualResource:
                    var resource = VirtualResource.Select(context, id);
                    resource.Name = name;
                    HandleObjectChange(context, resource);
                    break;

                case ConfigurationObjectType.ResourceMetadata:
                    var metadata = VirtualResourceMetadata.Select(context, id);
                    metadata.Name = name;
                    HandleObjectChange(context, metadata);
                    break;

                case ConfigurationObjectType.ScenarioFolder:
                case ConfigurationObjectType.ResourceFolder:
                case ConfigurationObjectType.MetadataFolder:
                    var folder = ConfigurationTreeFolder.Select(context, id);
                    folder.Name = name;
                    HandleObjectChange(context, folder);
                    break;
                }
                context.SaveChanges();
            }
        }
        private static void DatabaseDelete(IEnumerable <EnterpriseTestMapNode> nodes)
        {
            //--------------------------------------------------------------------------------------------------
            // ADD: CR #1894.  Cascading deletes were throwing an unhandled exception because of a DB Foreign
            // Key Constraint on the folderId field.  What that means is that the ORDER in which things are
            // deleted, now becomes very important.  If we attempt to remove a 'container' before we remove
            // the contents, the exception will be thrown when the contents are removed.  So - in order to make
            // this work, we group the objects to be deleted - and then we have to order those groups (since the
            // order of the GroupBy could be non-deterministic) and make sure that we delete things in the
            // correct sequence.  There is probably a more elegant way of doing this - but there was no time.
            using (EnterpriseTestContext context = new EnterpriseTestContext())
            {
                int[] ordered = new int[6] {
                    -1, -1, -1, -1, -1, -1
                };

                var colls = nodes.GroupBy(n => n.NodeType, n => n.Id);
                int ndx   = 0;
                foreach (var grp in colls)
                {
                    switch (grp.Key)
                    {
                    case ConfigurationObjectType.ResourceMetadata:
                        ordered[0] = ndx++;
                        break;

                    case ConfigurationObjectType.MetadataFolder:
                        ordered[1] = ndx++;
                        break;

                    case ConfigurationObjectType.VirtualResource:
                        ordered[2] = ndx++;
                        break;

                    case ConfigurationObjectType.ResourceFolder:
                        ordered[3] = ndx++;
                        break;

                    case ConfigurationObjectType.EnterpriseScenario:
                        ordered[4] = ndx++;
                        break;

                    case ConfigurationObjectType.ScenarioFolder:
                        ordered[5] = ndx++;
                        break;
                    }
                }

                for (ndx = 0; ndx < 6; ndx++)
                {
                    if (ordered[ndx] >= 0)
                    {
                        switch (colls.ElementAt(ordered[ndx]).Key)
                        {
                        case ConfigurationObjectType.ResourceMetadata:
                            foreach (var vrmd in VirtualResourceMetadata.Select(context, colls.ElementAt(ordered[ndx])))
                            {
                                context.DeleteObject(vrmd);
                            }
                            break;

                        case ConfigurationObjectType.MetadataFolder:
                            foreach (var mdf in ConfigurationTreeFolder.Select(context, colls.ElementAt(ordered[ndx])))
                            {
                                context.DeleteObject(mdf);
                            }
                            break;

                        case ConfigurationObjectType.VirtualResource:
                            foreach (var vr in VirtualResource.Select(context, colls.ElementAt(ordered[ndx])))
                            {
                                context.DeleteObject(vr);
                            }
                            break;

                        case ConfigurationObjectType.ResourceFolder:
                            foreach (var rf in ConfigurationTreeFolder.Select(context, colls.ElementAt(ordered[ndx])))
                            {
                                context.DeleteObject(rf);
                            }
                            break;

                        case ConfigurationObjectType.EnterpriseScenario:
                            foreach (var scenario in EnterpriseScenario.Select(context, colls.ElementAt(ordered[ndx])))
                            {
                                context.DeleteObject(scenario);
                            }
                            break;

                        case ConfigurationObjectType.ScenarioFolder:
                            foreach (var sf in ConfigurationTreeFolder.Select(context, colls.ElementAt(ordered[ndx])))
                            {
                                var deletingScenarioIds = nodes.Where(n => n.NodeType == ConfigurationObjectType.EnterpriseScenario).Select(n => n.Id);
                                foreach (var folder in ConfigurationTreeFolder.Select(context, colls.ElementAt(ordered[ndx])))
                                {
                                    var containedScenarios = ConfigurationTreeFolder.ContainedScenarioIds(context, folder.ConfigurationTreeFolderId);
                                    if (containedScenarios.All(n => deletingScenarioIds.Contains(n)))
                                    {
                                        // All of the sceanrios under this folder are in the list of scenarios to delete,
                                        // so we can delete this folder as well.
                                        context.DeleteObject(folder);
                                    }
                                }
                            }
                            break;
                        }
                        context.SaveChanges();
                    }
                }
            }
        }