public void EmailTargets_CreatedTopicCorrectly()
        {
            // arrange
            var resource = new AwsResource <FakeResourceType>("name", new FakeResourceType());
            var targets  = new List <AlertTarget>()
            {
                new AlertEmail("*****@*****.**"),
                new AlertEmail("*****@*****.**")
            };
            var alarms = new List <Alarm>();

            alarms.Add(CreateExampleAlarm(resource));

            // act
            var template = new CloudWatchCloudFormationTemplate(AlertingGroupName, targets);

            template.AddAlarms(alarms);
            var result = template.WriteJson();

            // assert
            var parsed     = JObject.Parse(result);
            var emailTopic = parsed["Resources"]["EmailTopic"];

            Assert.That(emailTopic, Is.Not.Null);
            Assert.That((string)emailTopic["Properties"]["TopicName"], Is.EqualTo($"AwsWatchman_Email_{AlertingGroupName}"));

            var emailsInTopic = emailTopic["Properties"]["Subscription"].ToList();

            Assert.That(emailsInTopic.All(j => (string)j["Protocol"] == "email"));
            Assert.That(emailsInTopic.Count, Is.EqualTo(2));

            Assert.That(emailsInTopic.Exists(j => (string)j["Endpoint"] == "*****@*****.**"));
            Assert.That(emailsInTopic.Exists(j => (string)j["Endpoint"] == "*****@*****.**"));
        }
        public void EmailAndUrlTargets_CreatesBothTopics()
        {
            // arrange
            var resource = new AwsResource <FakeResourceType>("name", new FakeResourceType());

            var alarms = new List <Alarm>();

            alarms.Add(CreateExampleAlarm(new List <AlertTarget>()
            {
                new AlertEmail("*****@*****.**"),
                new AlertUrl("*****@*****.**")
            }, resource));

            // act
            var template = new CloudWatchCloudFormationTemplate();

            template.AddAlarms(alarms);
            var result = template.WriteJson();

            // assert
            var parsed     = JObject.Parse(result);
            var emailTopic = parsed["Resources"]["EmailTopic"];
            var urlTopic   = parsed["Resources"]["UrlTopic"];

            Assert.That(emailTopic, Is.Not.Null);
            Assert.That(urlTopic, Is.Not.Null);
        }
        public void UrlTargets_CreatedTopicCorrectly()
        {
            // arrange
            var resource = new AwsResource <FakeResourceType>("name", new FakeResourceType());

            var alarms = new List <Alarm>();

            alarms.Add(CreateExampleAlarm(new List <AlertTarget>()
            {
                new AlertUrl()
                {
                    Url = "http://banana"
                },
                new AlertUrl()
                {
                    Url = "https://banana2"
                },
            }, resource));

            // act
            var template = new CloudWatchCloudFormationTemplate();

            template.AddAlarms(alarms);
            var result = template.WriteJson();

            // assert
            var parsed     = JObject.Parse(result);
            var emailTopic = parsed["Resources"]["UrlTopic"];

            Assert.That(emailTopic, Is.Not.Null);
            Assert.That((string)emailTopic["Properties"]["TopicName"], Is.EqualTo($"AwsWatchman_Url_{AlertingGroupName}"));

            var emailsInTopic = emailTopic["Properties"]["Subscription"].ToList();

            Assert.That(emailsInTopic.Count, Is.EqualTo(2));

            Assert.That(emailsInTopic.Exists(j =>

                                             (string)j["Endpoint"] == "http://banana" && (string)j["Protocol"] == "http"
                                             ));

            Assert.That(emailsInTopic.Exists(j =>

                                             (string)j["Endpoint"] == "https://banana2" && (string)j["Protocol"] == "https"
                                             ));
        }
        public void TargetMappingIsCorrect()
        {
            // arrange
            var resource = new AwsResource <FakeResourceType>("name", new FakeResourceType());

            var alarms = new List <Alarm>();

            alarms.Add(CreateExampleAlarm(new List <AlertTarget>()
            {
                new AlertUrl()
                {
                    Url = "http://banana"
                },
                new AlertEmail()
                {
                    Email = "*****@*****.**"
                },
            }, resource));

            // act
            var template = new CloudWatchCloudFormationTemplate();

            template.AddAlarms(alarms);
            var result = template.WriteJson();

            // assert
            var parsed = JObject.Parse(result);

            var alarm = ((JObject)parsed["Resources"])
                        .Properties()
                        .Single(j => j.Value["Type"].Value <string>() == "AWS::CloudWatch::Alarm")
                        .Value;

            var okTargets           = (JArray)alarm["Properties"]["OKActions"];
            var insufficientTargets = (JArray)alarm["Properties"]["InsufficientDataActions"];
            var alarmTargets        = (JArray)alarm["Properties"]["AlarmActions"];

            // ok alarms only go to urls, everything else goes to email also
            Assert.That((string)okTargets.Single()["Ref"], Is.EqualTo("UrlTopic"));

            Assert.That(insufficientTargets.Any(j => (string)j["Ref"] == "UrlTopic"));
            Assert.That(insufficientTargets.Any(j => (string)j["Ref"] == "EmailTopic"));
            Assert.That(alarmTargets.Any(j => (string)j["Ref"] == "UrlTopic"));
            Assert.That(alarmTargets.Any(j => (string)j["Ref"] == "EmailTopic"));
        }
        public void NoTargets_CreatesNoTopics()
        {
            // arrange
            var resource = new AwsResource <FakeResourceType>("name", new FakeResourceType());

            var alarms = new List <Alarm>();

            alarms.Add(CreateExampleAlarm(resource));

            // act
            var template = new CloudWatchCloudFormationTemplate("group-name", new List <AlertTarget>());

            template.AddAlarms(alarms);
            var result = template.WriteJson();

            // assert
            var parsed     = JObject.Parse(result);
            var emailTopic = parsed["Resources"]["EmailTopic"];
            var urlTopic   = parsed["Resources"]["UrlTopic"];

            Assert.That(emailTopic, Is.Null);
            Assert.That(urlTopic, Is.Null);
        }