< Summary

Class:MUNity.Troubleshooting.Resa.ResolutionTroubleshooting
Assembly:MUNity.Schema
File(s):C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNitySchema\Troubleshooting\Resa\ResolutionTroubleshooting.cs
Covered lines:0
Uncovered lines:131
Coverable lines:131
Total lines:276
Line coverage:0% (0 of 131)
Covered branches:0
Total branches:98
Branch coverage:0% (0 of 98)
Covered methods:0
Total methods:19
Method coverage:0% (0 of 19)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Bugfinder()100%10%
.ctor()100%10%
get_Description()100%10%
Detect(...)0%180%
Fix(...)0%160%
.ctor()100%10%
get_Description()100%10%
Detect(...)0%60%
Fix(...)0%60%
.ctor()100%10%
get_Description()100%10%
Detect(...)0%140%
Fix(...)0%140%
.ctor()100%10%
get_Description()100%10%
Detect(...)0%80%
Fix(...)0%80%
IsResolutionCorrupted(...)0%40%
FixResolution(...)0%40%

File(s)

C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNitySchema\Troubleshooting\Resa\ResolutionTroubleshooting.cs

#LineLine coverage
 1using MUNity.Extensions.ResolutionExtensions;
 2using MUNity.Models.Resolution;
 3using System;
 4using System.Collections.Generic;
 5using System.Collections.ObjectModel;
 6using System.Linq;
 7using System.Text;
 8
 9namespace MUNity.Troubleshooting.Resa
 10{
 11    /// <summary>
 12    /// This component searches for errors inside a given Resolution and returns a bug report and will fix some bugs ins
 13    /// document.
 14    /// </summary>
 15    public class ResolutionTroubleshooting
 16    {
 17        private static IEnumerable<IResolutionBug> Bugfinder
 18        {
 19            get
 020            {
 021                yield return new InvalidResolutionHeader();
 022                yield return new InvalidPreamble();
 023                yield return new InvalidOperativeSection();
 024                yield return new InvalidAmendments();
 025            }
 26        }
 27
 28        /// <summary>
 29        /// Something in the Header is invalid. For example a string is null, but strings should always be string.Empty 
 30        /// </summary>
 31        public class InvalidResolutionHeader : IResolutionBug
 32        {
 033            private string detectedError = "";
 34
 35            /// <summary>
 36            /// Gives back a string containing information about the error.
 37            /// </summary>
 038            public string Description => detectedError;
 39
 40            /// <summary>
 41            /// Detects bugs inside the Header of a resolution and returns true if bugs are found.
 42            /// </summary>
 43            /// <param name="resolution"></param>
 44            /// <returns></returns>
 45            public bool Detect(Resolution resolution)
 046            {
 047                Dictionary<string, object> notNullProps = new Dictionary<string, object>()
 048                {
 049                    { "Header", resolution.Header },
 050                    { "AgendaItem", resolution.Header?.AgendaItem },
 051                    { "CommitteeName", resolution.Header?.CommitteeName },
 052                    { "FullName", resolution.Header?.FullName },
 053                    { "Name", resolution.Header?.Name },
 054                    { "Session", resolution.Header?.Session },
 055                    { "SubmitterName", resolution.Header?.SubmitterName },
 056                    { "Topic", resolution.Header?.Topic }
 057                };
 58
 059                foreach (var element in notNullProps)
 060                {
 061                    if (element.Value == null) detectedError += $"{element.Key} is not allowed to be null.\n";
 062                }
 063                return notNullProps.ContainsValue(null);
 064            }
 65
 66            /// <summary>
 67            /// Fixes the bugs that where found before.
 68            /// </summary>
 69            /// <param name="resolution"></param>
 70            /// <returns></returns>
 71            public bool Fix(Resolution resolution)
 072            {
 073                if (resolution.Header == null) resolution.Header = new ResolutionHeader();
 074                if (resolution.Header.AgendaItem == null) resolution.Header.AgendaItem = string.Empty;
 075                if (resolution.Header.CommitteeName == null) resolution.Header.CommitteeName = string.Empty;
 076                if (resolution.Header.FullName == null) resolution.Header.FullName = string.Empty;
 077                if (resolution.Header.Name == null) resolution.Header.Name = string.Empty;
 078                if (resolution.Header.Session == null) resolution.Header.Session = string.Empty;
 079                if (resolution.Header.SubmitterName == null) resolution.Header.SubmitterName = string.Empty;
 080                if (resolution.Header.Topic == null) resolution.Header.Topic = string.Empty;
 081                return true;
 082            }
 83        }
 84
 85        /// <summary>
 86        /// A component to check the preamble for errors.
 87        /// </summary>
 88        public class InvalidPreamble : IResolutionBug
 89        {
 090            private string output = "";
 91
 92            /// <summary>
 93            /// Description of bugs that are found inside the Preamble
 94            /// </summary>
 095            public string Description => output;
 96
 97            /// <summary>
 98            /// Detect bugs inside the preamble
 99            /// </summary>
 100            /// <param name="resolution"></param>
 101            /// <returns></returns>
 102            public bool Detect(Resolution resolution)
 0103            {
 0104                if (resolution.Preamble == null)
 0105                {
 0106                    output = "Preamble cannot be null!";
 0107                    return true;
 108                }
 0109                if (resolution.Preamble.Paragraphs == null)
 0110                {
 0111                    output = "Preamble Paragraphs cannot be null";
 0112                    return true;
 113                }
 0114                if (string.IsNullOrEmpty(resolution.Preamble.PreambleId))
 0115                {
 0116                    output = "Preamble needs to have an Id";
 0117                    return true;
 118                }
 0119                return false;
 0120            }
 121
 122            /// <summary>
 123            /// Fixes known bugs inside the preamble.
 124            /// </summary>
 125            /// <param name="resolution"></param>
 126            /// <returns></returns>
 127            public bool Fix(Resolution resolution)
 0128            {
 0129                if (resolution.Preamble == null) resolution.Preamble = new ResolutionPreamble();
 0130                if (resolution.Preamble.Paragraphs == null) resolution.Preamble.Paragraphs = new List<PreambleParagraph>
 0131                if (string.IsNullOrEmpty(resolution.Preamble.PreambleId)) resolution.Preamble.PreambleId = Guid.NewGuid(
 0132                return true;
 0133            }
 134        }
 135
 136        /// <summary>
 137        /// Resolution Bug Handler for Invalid Operative Sections
 138        /// </summary>
 139        public class InvalidOperativeSection : IResolutionBug
 140        {
 141
 0142            private string output = "";
 143            /// <summary>
 144            /// A Description of bugs that may be found inside the reoslution
 145            /// </summary>
 0146            public string Description => output;
 147
 148            /// <summary>
 149            /// Try to find know bugs inside the resolution.
 150            /// </summary>
 151            /// <param name="resolution"></param>
 152            /// <returns>Returns true if bugs are found or false if no known bugs are found.</returns>
 153            public bool Detect(Resolution resolution)
 0154            {
 0155                if (resolution.OperativeSection == null) return true;
 0156                if (resolution.OperativeSection.AddAmendments == null) return true;
 0157                if (resolution.OperativeSection.ChangeAmendments == null) return true;
 0158                if (resolution.OperativeSection.DeleteAmendments == null) return true;
 0159                if (resolution.OperativeSection.MoveAmendments == null) return true;
 0160                if (string.IsNullOrEmpty(resolution.OperativeSection.OperativeSectionId)) return true;
 0161                if (resolution.OperativeSection.Paragraphs == null) return true;
 0162                return false;
 0163            }
 164
 165            /// <summary>
 166            /// Attemps to fix the known bugs. Will rerun the Detection at the end and return true if the
 167            /// detection cant find any more bugs. Will return false if there are still bugs. This may cant be fixed.
 168            /// </summary>
 169            /// <param name="resolution"></param>
 170            /// <returns>Returns true when the bugs are fixed.</returns>
 171            public bool Fix(Resolution resolution)
 0172            {
 0173                if (resolution.OperativeSection == null) resolution.OperativeSection = new OperativeSection();
 0174                if (resolution.OperativeSection.AddAmendments == null) resolution.OperativeSection.AddAmendments = new L
 0175                if (resolution.OperativeSection.ChangeAmendments == null) resolution.OperativeSection.ChangeAmendments =
 0176                if (resolution.OperativeSection.DeleteAmendments == null) resolution.OperativeSection.DeleteAmendments =
 0177                if (resolution.OperativeSection.MoveAmendments == null) resolution.OperativeSection.MoveAmendments = new
 0178                if (string.IsNullOrEmpty(resolution.OperativeSection.OperativeSectionId)) resolution.OperativeSection.Op
 0179                if (resolution.OperativeSection.Paragraphs == null) resolution.OperativeSection.Paragraphs = new List<Op
 0180                return Detect(resolution) == false;
 0181            }
 182        }
 183
 184        /// <summary>
 185        /// Worker to find invalid amendments in the resolution
 186        /// </summary>
 187        public class InvalidAmendments : IResolutionBug
 188        {
 0189            private string bugs = "";
 190
 191            /// <summary>
 192            /// Description of bugs that may be found inside the resolution.
 193            /// </summary>
 0194            public string Description => bugs;
 195
 196            /// <summary>
 197            /// Detect errors inside the resolutions amendments
 198            /// </summary>
 199            /// <param name="resolution"></param>
 200            /// <returns></returns>
 201            public bool Detect(Resolution resolution)
 0202            {
 0203                var ghosts = resolution.OperativeSection.WhereParagraph(n => n.IsVirtual && (
 0204                    resolution.OperativeSection.MoveAmendments.All(a => a.NewTargetSectionId != n.OperativeParagraphId) 
 0205                    resolution.OperativeSection.AddAmendments.All(a => a.TargetSectionId != n.OperativeParagraphId))
 0206                );
 207
 0208                if (ghosts.Any())
 0209                {
 0210                    foreach (var ghost in ghosts)
 0211                    {
 0212                        bugs += $"{ghost.OperativeParagraphId} (Virutal: {ghost.IsVirtual}) is a ghost. There is no amen
 0213                    }
 0214                }
 215
 0216                return bugs != "";
 0217            }
 218
 219            /// <summary>
 220            /// Fixes the known amendment bugs
 221            /// </summary>
 222            /// <param name="resolution"></param>
 223            /// <returns>Will always return true.</returns>
 224            public bool Fix(Resolution resolution)
 0225            {
 0226                var ghosts = resolution.OperativeSection.WhereParagraph(n => n.IsVirtual && (resolution.OperativeSection
 0227                    resolution.OperativeSection.AddAmendments.All(a => a.TargetSectionId != n.OperativeParagraphId)));
 228
 0229                if (ghosts.Any())
 0230                {
 0231                    foreach (var ghost in ghosts)
 0232                    {
 0233                        resolution.OperativeSection.RemoveOperativeParagraph(ghost);
 0234                    }
 0235                }
 236
 0237                return true;
 0238            }
 239        }
 240
 241        /// <summary>
 242        /// Check if a given Resolution is corrupted, has any kind of errors.
 243        /// </summary>
 244        /// <param name="resolution"></param>
 245        /// <returns></returns>
 246        public static (bool isCorrupted, string log) IsResolutionCorrupted(Resolution resolution)
 0247        {
 0248            var result = false;
 0249            string output = "";
 0250            foreach (var finder in Bugfinder)
 0251            {
 0252                if (finder.Detect(resolution))
 0253                {
 0254                    result = true;
 0255                    output += finder.Description;
 0256                }
 0257            }
 0258            return (result, output);
 0259        }
 260
 261        /// <summary>
 262        /// Fix the Resolution with the registered Bugfinders.
 263        /// </summary>
 264        /// <param name="resolution"></param>
 265        /// <returns></returns>
 266        public static bool FixResolution(Resolution resolution)
 0267        {
 0268            var result = true;
 0269            foreach (var finder in Bugfinder)
 0270            {
 0271                if (!finder.Fix(resolution)) result = false;
 0272            }
 0273            return result;
 0274        }
 275    }
 276}