public void UpdateAnnotationTest()
        {
            var          result     = instance.GetAnnotations(webId, null);
            PIAnnotation annotation = result.Items.Where(m => m.Name == Constants.AF_EVENT_FRAME_ANNOTATION_NAME).FirstOrDefault();

            annotation.Value = "odosdsaaCSWCCSA";
            string id = annotation.Id.ToString();

            annotation.Id           = null;
            annotation.CreationDate = null;
            annotation.Creator      = null;
            annotation.Modifier     = null;
            annotation.ModifyDate   = null;
            annotation.Links        = null;
            instance.UpdateAnnotation(webId: webId, id: id, annotation: annotation);

            string       path       = Constants.AF_EVENT_FRAME_PATH;
            AFEventFrame eventFrame = AFObject.FindObject(path) as AFEventFrame;

            eventFrame.Refresh();
            IList <AFAnnotation> annotations = eventFrame.GetAnnotations();
            string afAnnValue = annotations.Where(m => m.Name == annotation.Name).First().Value.ToString();

            Assert.IsTrue(afAnnValue == annotation.Value.ToString());
        }
        public void CreateAnnotationTest()
        {
            PIAnnotation annotation = new PIAnnotation()
            {
                Name  = Constants.AF_EVENT_FRAME_ANNOTATION_NAME + "2",
                Value = "MainValue"
            };

            instance.CreateAnnotation(webId, annotation);

            string     path = Constants.AF_EVENT_FRAME_PATH;
            AFDatabase db   = StandardPISystem.Databases[Constants.AF_DATABASE_NAME];

            db.Refresh();
            AFEventFrame         eventFrame  = AFObject.FindObject(path) as AFEventFrame;
            IList <AFAnnotation> annotations = eventFrame.GetAnnotations();

            Assert.IsNotNull(annotations.Where(m => m.Name == annotation.Name).FirstOrDefault());
        }
        public void DeleteAnnotationTest()
        {
            var          result     = instance.GetAnnotations(webId, null);
            PIAnnotation annotation = result.Items.Where(m => m.Name == Constants.AF_EVENT_FRAME_ANNOTATION_NAME).FirstOrDefault();

            annotation.Value = "odosdsaaCSWCCSA";
            string id = annotation.Id.ToString();

            instance.DeleteAnnotation(webId: webId, id: id);
            string     path = Constants.AF_EVENT_FRAME_PATH;
            AFDatabase db   = StandardPISystem.Databases[Constants.AF_DATABASE_NAME];

            db.Refresh();
            AFEventFrame eventFrame = AFObject.FindObject(path) as AFEventFrame;
            AFAnnotation ann        = eventFrame.GetAnnotations().Where(an => an.Name == Constants.AF_EVENT_FRAME_ANNOTATION_NAME).FirstOrDefault();

            Assert.IsNull(ann);
            DeleteSampleDatabaseForTests();
            CreateSampleDatabaseForTests();
        }
        public void NotificationRuleSendEmailAnnotationTest()
        {
            AFDatabase db                   = AFFixture.AFDatabase;
            var        elementName          = $"{NotificationsFixture.TestPrefix}_{NotificationsFixture.TestInfix}_{nameof(NotificationRuleSendEmailAnnotationTest)}";
            var        notificationRuleName = $"{NotificationsFixture.TestPrefix}_{NotificationsFixture.TestInfix}_NotificationRule1";
            var        eventFrameName       = $"{NotificationsFixture.TestPrefix}_{NotificationsFixture.TestInfix}_EventFrame1";

            AFFixture.RemoveElementIfExists(elementName, Output);
            Guid?eventFrameId = null;
            AFNotificationContactTemplate emailEndpoint = null;

            try
            {
                emailEndpoint = CreateEmailEndpoint(nameof(NotificationRuleSendEmailAnnotationTest));
                Output.WriteLine($"Create email notification contact template [{emailEndpoint.Name}].");
                PISystem.CheckIn();

                Output.WriteLine($"Create element [{elementName}] with notification rule [{notificationRuleName}]");
                var element          = db.Elements.Add(elementName);
                var notificationRule = element.NotificationRules.Add(notificationRuleName);
                notificationRule.Criteria = $"Name:{NotificationsFixture.TestPrefix}*";
                var format = notificationRule.DeliveryFormats.Add("testFormat", EmailPlugIn);
                NotificationsFixture.SetFormatProperties(format.Properties, nameof(NotificationRuleSendEmailAnnotationTest));

                var subscriber = notificationRule.Subscribers.Add(emailEndpoint);
                subscriber.DeliveryFormat = format;

                notificationRule.SetStatus(AFStatus.Enabled);
                db.CheckIn();

                Output.WriteLine("Waiting for notification to startup.");
                Thread.Sleep(TimeSpan.FromSeconds(10));

                var eventFrame = new AFEventFrame(db, eventFrameName)
                {
                    PrimaryReferencedElement = element,
                };

                eventFrame.SetStartTime(AFTime.Now);
                eventFrame.CheckIn();
                eventFrameId = eventFrame.ID;
                Output.WriteLine($"Created event frame [{eventFrameName}].");

                // Verify annotations are gotten correctly
                var annotations = eventFrame.GetAnnotations();
                if (annotations.Count == 0)
                {
                    AssertEventually.True(
                        () => eventFrame.GetAnnotations().Count != 0,
                        TimeSpan.FromSeconds(60),
                        TimeSpan.FromSeconds(5),
                        "Did not find any annotations.");
                    annotations = eventFrame.GetAnnotations();
                }

                Output.WriteLine("Verify the notification is sent for the event frame and the annotation is set properly.");
                Assert.True(annotations.Count == 1, $"Expected to get only one annotation, but got {annotations.Count}.");
                Assert.True(annotations[0].Owner.ID == eventFrameId, "The owner of the annotation is not set properly.");
                Assert.True(annotations[0].Name == NotificationsFixture.AnnotationName, "The name of the annotation is not set properly.");
                Assert.False(string.IsNullOrWhiteSpace(annotations[0].Description), "The description of the annotation is not set properly.");
                Assert.True((string)annotations[0].Value == string.Format(CultureInfo.InvariantCulture, NotificationsFixture.SentAnnotation, 1),
                            "The value of the annotation is not set properly.");

                Output.WriteLine("Verify the content of the annotation is set properly.");
                Assert.False(string.IsNullOrWhiteSpace(annotations[0].Description), "The description of the annotation is not set properly.");
                var description = NotificationsFixture.DeserializeAnnotationDescription(annotations[0].Description);
                var subscribers = description.Subscribers;
                Assert.True(description.Notification == notificationRuleName, "The notification rule name is not set properly.");
                Assert.True(subscribers.Count == 1, $"Expected to get only one subscriber, but got {description.Subscribers.Count}.");
                Assert.True(subscribers[0].Name == emailEndpoint.Name, "The name of the subscriber is not set properly.");
                Assert.True(subscribers[0].Configuration == Settings.PINotificationsRecipientEmailAddress, "The email address of the subscriber is not set properly.");
                Assert.True(subscribers[0].Type == "Email", "The type of the subscriber is not set properly.");
            }
            finally
            {
                AFFixture.RemoveElementIfExists(elementName, Output);
                if (eventFrameId != null)
                {
                    AFFixture.RemoveEventFrameIfExists(eventFrameId.GetValueOrDefault(), Output);
                }

                if (emailEndpoint != null)
                {
                    PISystem.NotificationContactTemplates.Remove(emailEndpoint.ID);
                    PISystem.CheckIn();
                }
            }
        }
        public void NotificationRuleSendToEscalationContactTest()
        {
            AFDatabase db                   = AFFixture.AFDatabase;
            var        elementName          = $"{NotificationsFixture.TestPrefix}_{NotificationsFixture.TestInfix}_{nameof(NotificationRuleSendToEscalationContactTest)}";
            var        notificationRuleName = $"{NotificationsFixture.TestPrefix}_{NotificationsFixture.TestInfix}_NotificationRule1";
            var        escalationNotificationContactTemplateName = $"{NotificationsFixture.TestPrefix}_{NotificationsFixture.TestInfix}_Escalation1";
            var        eventFrameName = $"{NotificationsFixture.TestPrefix}_{NotificationsFixture.TestInfix}_EventFrame1";
            var        emailContactsCountInEscalation = 2;
            var        escalationPeriod = TimeSpan.FromSeconds(20);

            AFFixture.RemoveElementIfExists(elementName, Output);
            Guid?eventFrameId   = null;
            var  emailEndpoints = new List <AFNotificationContactTemplate>();
            AFNotificationContactTemplate escalation = null;

            try
            {
                Output.WriteLine($"Created group notification contact template [{escalationNotificationContactTemplateName}]" +
                                 $" with [{emailContactsCountInEscalation}] email notification contact added.");
                escalation = new AFNotificationContactTemplate(PISystem, escalationNotificationContactTemplateName)
                {
                    ContactType       = AFNotificationContactType.Escalation,
                    EscalationTimeout = escalationPeriod,
                };
                escalation.CheckIn();

                for (var i = 0; i < emailContactsCountInEscalation; i++)
                {
                    var emailEndpoint = CreateEmailEndpoint($"{nameof(NotificationRuleSendToEscalationContactTest)}_{i}");
                    escalation.NotificationContactTemplates.Add(emailEndpoint);
                    emailEndpoints.Add(emailEndpoint);
                }

                PISystem.CheckIn();

                Output.WriteLine($"Created element [{elementName}] with notification rule [{notificationRuleName}].");
                var element          = db.Elements.Add(elementName);
                var notificationRule = element.NotificationRules.Add(notificationRuleName);
                notificationRule.Criteria = $"Name:{NotificationsFixture.TestPrefix}*";
                var subscriber = notificationRule.Subscribers.Add(escalation);
                notificationRule.SetStatus(AFStatus.Enabled);
                db.CheckIn();

                Output.WriteLine("Waiting for notification to startup.");
                Thread.Sleep(TimeSpan.FromSeconds(10));

                var eventFrame = new AFEventFrame(db, eventFrameName)
                {
                    PrimaryReferencedElement = element,
                };

                Output.WriteLine($"Create event frame [{eventFrameName}].");
                eventFrame.SetStartTime(AFTime.Now);
                eventFrame.CheckIn();
                eventFrameId = eventFrame.ID;

                Output.WriteLine("Waiting for escalation period.");
                Thread.Sleep(TimeSpan.FromSeconds(30));

                // Verify annotations are gotten correctly
                var annotations = eventFrame.GetAnnotations();
                if (annotations.Count == 0)
                {
                    AssertEventually.True(
                        () => eventFrame.GetAnnotations().Count != 0,
                        TimeSpan.FromSeconds(60),
                        TimeSpan.FromSeconds(5),
                        "Did not find any annotations.");
                    annotations = eventFrame.GetAnnotations();
                }

                Output.WriteLine("Verify the notification is sent for the event frame and the annotation is set properly.");
                Assert.True(annotations.Count == emailContactsCountInEscalation, $"Expected to get [{emailContactsCountInEscalation}] annotations, but got [{annotations.Count}].");
                for (var i = 0; i < emailContactsCountInEscalation; i++)
                {
                    Assert.True(annotations[i].Name == NotificationsFixture.AnnotationName, "The name of the annotation is not set properly.");

                    Assert.False(string.IsNullOrWhiteSpace(annotations[i].Description), "The description of the annotation is not set properly.");
                    var description = NotificationsFixture.DeserializeAnnotationDescription(annotations[i].Description);
                    var subscribers = description.Subscribers;
                    Assert.True(description.Notification == notificationRuleName, "The notification rule name is not set properly.");
                    Assert.True(subscribers.Count == 1, $"Expected to get only one subscriber, but got {description.Subscribers.Count}.");
                    Assert.True(subscribers[0].Name == emailEndpoints[i].Name, $"The name of the [{i}] subscriber is not set properly.");
                    Assert.True(subscribers[0].Configuration == Settings.PINotificationsRecipientEmailAddress, $"The configuration of the [{i}] subscriber is not displayed in the annotation.");
                    Assert.True(subscribers[0].Type == "Email", $"The type of the [{i}] subscriber is not set properly.");

                    if (i == 0)
                    {
                        Assert.True((string)annotations[i].Value == string.Format(CultureInfo.InvariantCulture, NotificationsFixture.SentAnnotation, 1),
                                    "The value of the annotation is not set properly.");
                    }
                    else
                    {
                        Assert.True((string)annotations[i].Value == string.Format(CultureInfo.InvariantCulture, NotificationsFixture.EscalatedAnnotation, 1),
                                    "The value of the annotation is not set properly.");
                    }
                }

                for (var i = emailContactsCountInEscalation - 1; i > 0; i--)
                {
                    Assert.True(annotations[i].CreationDate - annotations[i - 1].CreationDate >= escalationPeriod, $"The escalation period is not performed properly.");
                }
            }
            finally
            {
                AFFixture.RemoveElementIfExists(elementName, Output);
                if (eventFrameId != null)
                {
                    AFFixture.RemoveEventFrameIfExists(eventFrameId.GetValueOrDefault(), Output);
                }

                if (escalation != null)
                {
                    PISystem.NotificationContactTemplates.Remove(escalation.ID);
                }

                foreach (var emailEndpoint in emailEndpoints)
                {
                    PISystem.NotificationContactTemplates.Remove(emailEndpoint.ID);
                }

                PISystem.CheckIn();
            }
        }