예제 #1
0
        public string MessageCompare()
        {
            Dictionary <string, Dictionary <string, dynamic> > description = new Dictionary <string, Dictionary <string, dynamic> >();
            BaseMessage bundle          = TestMessage.GetMessage();
            DeathRecord record          = ReferenceRecord.GetRecord();
            BaseMessage referenceBundle = new Message(ReferenceRecord, bundle.MessageType).GetMessage();
            // On the frontend this shares the same view as the RecordCompare below. This heading
            // is shown above the results in the app.
            string heading = "Message Validation Results";

            description.Add(heading, new Dictionary <string, dynamic>());
            Dictionary <string, dynamic> category = description[heading];

            foreach (PropertyInfo property in bundle.GetType().GetProperties())
            {
                Total += 1;
                // Add the new property to the category
                category[property.Name]                = new Dictionary <string, dynamic>();
                category[property.Name]["Name"]        = property.Name;
                category[property.Name]["Type"]        = (Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType).Name;
                category[property.Name]["Description"] = Message.GetDescriptionFor(property.Name);
                category[property.Name]["Value"]       = property.GetValue(referenceBundle);
                category[property.Name]["FoundValue"]  = property.GetValue(bundle);
                // The record should be valid since we check its validity elsewhere.
                // Here we just check to make sure the record is properly embedded
                // into the message bundle.
                if (property.PropertyType == typeof(DeathRecord))
                {
                    // Do not count DeathRecord base as part of the Total (since the Total is increased per-element in RecordCompare)
                    Total -= 1;
                    DeathRecord extracted = (DeathRecord)property.GetValue(bundle);
                    TestRecord = new Record(extracted);
                    int previousIncorrect = Incorrect;
                    category[property.Name]["SnippetJSON"] = RecordCompare();
                    // See if the value of Incorrect changed in 'RecordCompare' and use that to determine if the
                    // Record matches or not.
                    category[property.Name]["Match"] = previousIncorrect.Equals(Incorrect) ? "true" : "false";
                }
                else if (Message.validatePresenceOnly(property.Name))
                {
                    // Basic message validation ensures that these fields are present if they are required
                    // These values do not have to match our reference record and are just therefore
                    // set to valid here.
                    Correct += 1;
                    category[property.Name]["Match"] = "true";
                    // Override the displayed value to be equal to what the user provided on the UI
                    category[property.Name]["Value"] = category[property.Name]["FoundValue"];
                }
                else
                {
                    // Using == here seems to be checking ReferenceEquals and not Equals, causing the equality to return false.
                    // Calling Prop1.Equals(Prop2) here raises an error if the value is null in the ReferenceBundle.
                    // The best option here is to just use the object.Equals operator.
                    if (Equals(property.GetValue(referenceBundle), property.GetValue(bundle)))
                    {
                        Correct += 1;
                        category[property.Name]["Match"] = "true";
                    }
                    else
                    {
                        Incorrect += 1;
                        category[property.Name]["Match"] = "false";
                    }
                }
            }
            return(JsonConvert.SerializeObject(description));
        }