"use strict";
const assert_1 = require("@aws-cdk/assert");
const s3n = require("@aws-cdk/aws-s3-notifications");
const cdk = require("@aws-cdk/cdk");
const s3 = require("../lib");
const notification_dests_1 = require("./notification-dests");
module.exports = {
    'bucket without notifications'(test) {
        const stack = new cdk.Stack();
        new s3.Bucket(stack, 'MyBucket');
        assert_1.expect(stack).toMatch({
            "Resources": {
                "MyBucketF68F3FF0": {
                    "Type": "AWS::S3::Bucket"
                }
            }
        });
        test.done();
    },
    'when notification are added, a custom resource is provisioned + a lambda handler for it'(test) {
        const stack = new cdk.Stack();
        const bucket = new s3.Bucket(stack, 'MyBucket');
        const topic = new notification_dests_1.Topic(stack, 'MyTopic');
        bucket.onEvent(s3.EventType.ObjectCreated, topic);
        assert_1.expect(stack).to(assert_1.haveResource('AWS::S3::Bucket'));
        assert_1.expect(stack).to(assert_1.haveResource('AWS::Lambda::Function', { Description: 'AWS CloudFormation handler for "Custom::S3BucketNotifications" resources (@aws-cdk/aws-s3)' }));
        assert_1.expect(stack).to(assert_1.haveResource('Custom::S3BucketNotifications'));
        test.done();
    },
    'bucketNotificationTarget is not called during synthesis'(test) {
        const stack = new cdk.Stack();
        // notice the order here - topic is defined before bucket
        // but this shouldn't impact the fact that the topic policy includes
        // the bucket information
        const topic = new notification_dests_1.Topic(stack, 'Topic');
        const bucket = new s3.Bucket(stack, 'MyBucket');
        bucket.onObjectCreated(topic);
        assert_1.expect(stack).to(assert_1.haveResource('AWS::SNS::TopicPolicy', {
            "Topics": [
                {
                    "Ref": "TopicBFC7AF6E"
                }
            ],
            "PolicyDocument": {
                "Statement": [
                    {
                        "Action": "sns:Publish",
                        "Condition": {
                            "ArnLike": {
                                "aws:SourceArn": {
                                    "Fn::GetAtt": [
                                        "MyBucketF68F3FF0",
                                        "Arn"
                                    ]
                                }
                            }
                        },
                        "Effect": "Allow",
                        "Principal": {
                            "Service": "s3.amazonaws.com"
                        },
                        "Resource": {
                            "Ref": "TopicBFC7AF6E"
                        },
                        "Sid": "sid0"
                    }
                ],
                "Version": "2012-10-17"
            }
        }));
        test.done();
    },
    'subscription types'(test) {
        const stack = new cdk.Stack();
        const bucket = new s3.Bucket(stack, 'TestBucket');
        const queueTarget = {
            asBucketNotificationDestination: _ => ({
                type: s3n.BucketNotificationDestinationType.Queue,
                arn: new cdk.Arn('arn:aws:sqs:...')
            })
        };
        const lambdaTarget = {
            asBucketNotificationDestination: _ => ({
                type: s3n.BucketNotificationDestinationType.Lambda,
                arn: new cdk.Arn('arn:aws:lambda:...')
            })
        };
        const topicTarget = {
            asBucketNotificationDestination: _ => ({
                type: s3n.BucketNotificationDestinationType.Topic,
                arn: new cdk.Arn('arn:aws:sns:...')
            })
        };
        bucket.onEvent(s3.EventType.ObjectCreated, queueTarget);
        bucket.onEvent(s3.EventType.ObjectCreated, lambdaTarget);
        bucket.onObjectRemoved(topicTarget, { prefix: 'prefix' });
        assert_1.expect(stack).to(assert_1.haveResource('Custom::S3BucketNotifications', {
            "ServiceToken": {
                "Fn::GetAtt": [
                    "BucketNotificationsHandler050a0587b7544547bf325f094a3db8347ECC3691",
                    "Arn"
                ]
            },
            "BucketName": {
                "Ref": "TestBucket560B80BC"
            },
            "NotificationConfiguration": {
                "LambdaFunctionConfigurations": [
                    {
                        "Events": [
                            "s3:ObjectCreated:*"
                        ],
                        "LambdaFunctionArn": "arn:aws:lambda:..."
                    }
                ],
                "QueueConfigurations": [
                    {
                        "Events": [
                            "s3:ObjectCreated:*"
                        ],
                        "QueueArn": "arn:aws:sqs:..."
                    }
                ],
                "TopicConfigurations": [
                    {
                        "Events": [
                            "s3:ObjectRemoved:*"
                        ],
                        "TopicArn": "arn:aws:sns:...",
                        "Filter": {
                            "Key": {
                                "FilterRules": [
                                    {
                                        "Name": "prefix",
                                        "Value": "prefix"
                                    }
                                ]
                            }
                        }
                    }
                ]
            }
        }));
        test.done();
    },
    'multiple subscriptions of the same type'(test) {
        const stack = new cdk.Stack();
        const bucket = new s3.Bucket(stack, 'TestBucket');
        bucket.onEvent(s3.EventType.ObjectRemovedDelete, {
            asBucketNotificationDestination: _ => ({
                type: s3n.BucketNotificationDestinationType.Queue,
                arn: new cdk.Arn('arn:aws:sqs:...:queue1')
            })
        });
        bucket.onEvent(s3.EventType.ObjectRemovedDelete, {
            asBucketNotificationDestination: _ => ({
                type: s3n.BucketNotificationDestinationType.Queue,
                arn: new cdk.Arn('arn:aws:sqs:...:queue2')
            })
        });
        assert_1.expect(stack).to(assert_1.haveResource('Custom::S3BucketNotifications', {
            "ServiceToken": {
                "Fn::GetAtt": [
                    "BucketNotificationsHandler050a0587b7544547bf325f094a3db8347ECC3691",
                    "Arn"
                ]
            },
            "BucketName": {
                "Ref": "TestBucket560B80BC"
            },
            "NotificationConfiguration": {
                "QueueConfigurations": [
                    {
                        "Events": [
                            "s3:ObjectRemoved:Delete"
                        ],
                        "QueueArn": "arn:aws:sqs:...:queue1"
                    },
                    {
                        "Events": [
                            "s3:ObjectRemoved:Delete"
                        ],
                        "QueueArn": "arn:aws:sqs:...:queue2"
                    }
                ]
            }
        }));
        test.done();
    },
    'prefix/suffix filters'(test) {
        const stack = new cdk.Stack();
        const bucket = new s3.Bucket(stack, 'TestBucket');
        const bucketNotificationTarget = {
            type: s3n.BucketNotificationDestinationType.Queue,
            arn: new cdk.Arn('arn:aws:sqs:...')
        };
        bucket.onEvent(s3.EventType.ObjectRemovedDelete, { asBucketNotificationDestination: _ => bucketNotificationTarget }, { prefix: 'images/', suffix: '.jpg' });
        assert_1.expect(stack).to(assert_1.haveResource('Custom::S3BucketNotifications', {
            "ServiceToken": {
                "Fn::GetAtt": [
                    "BucketNotificationsHandler050a0587b7544547bf325f094a3db8347ECC3691",
                    "Arn"
                ]
            },
            "BucketName": {
                "Ref": "TestBucket560B80BC"
            },
            "NotificationConfiguration": {
                "QueueConfigurations": [
                    {
                        "Events": [
                            "s3:ObjectRemoved:Delete"
                        ],
                        "Filter": {
                            "Key": {
                                "FilterRules": [
                                    {
                                        "Name": "suffix",
                                        "Value": ".jpg"
                                    },
                                    {
                                        "Name": "prefix",
                                        "Value": "images/"
                                    }
                                ]
                            }
                        },
                        "QueueArn": "arn:aws:sqs:..."
                    }
                ]
            }
        }));
        test.done();
    }
};
//# sourceMappingURL=data:application/json;base64, 