|
| 1 | +using Amazon.Lambda.Annotations.SourceGenerator; |
| 2 | +using Amazon.Lambda.Annotations.SourceGenerator.Models; |
| 3 | +using Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes; |
| 4 | +using Amazon.Lambda.Annotations.SourceGenerator.Writers; |
| 5 | +using Amazon.Lambda.Annotations.S3; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Amazon.Lambda.Annotations.SourceGenerators.Tests.WriterTests |
| 11 | +{ |
| 12 | + public partial class CloudFormationWriterTests |
| 13 | + { |
| 14 | + [Theory] |
| 15 | + [ClassData(typeof(S3EventsTestData))] |
| 16 | + public void VerifyS3EventAttributes_AreCorrectlyApplied(CloudFormationTemplateFormat templateFormat, S3EventAttribute s3EventAttribute) |
| 17 | + { |
| 18 | + // ARRANGE |
| 19 | + var mockFileManager = GetMockFileManager(string.Empty); |
| 20 | + var lambdaFunctionModel = GetLambdaFunctionModel(); |
| 21 | + lambdaFunctionModel.PackageType = LambdaPackageType.Zip; |
| 22 | + lambdaFunctionModel.Attributes.Add(new AttributeModel<S3EventAttribute> { Data = s3EventAttribute }); |
| 23 | + var cloudFormationWriter = GetCloudFormationWriter(mockFileManager, _directoryManager, templateFormat, _diagnosticReporter); |
| 24 | + var report = GetAnnotationReport([lambdaFunctionModel]); |
| 25 | + |
| 26 | + // ACT |
| 27 | + cloudFormationWriter.ApplyReport(report); |
| 28 | + |
| 29 | + // ASSERT |
| 30 | + ITemplateWriter templateWriter = templateFormat == CloudFormationTemplateFormat.Json ? new JsonWriter() : new YamlWriter(); |
| 31 | + templateWriter.Parse(mockFileManager.ReadAllText(ServerlessTemplateFilePath)); |
| 32 | + |
| 33 | + var eventName = s3EventAttribute.ResourceName; |
| 34 | + var eventPath = $"Resources.{lambdaFunctionModel.ResourceName}.Properties.Events.{eventName}"; |
| 35 | + var eventPropertiesPath = $"{eventPath}.Properties"; |
| 36 | + |
| 37 | + Assert.True(templateWriter.Exists(eventPath)); |
| 38 | + Assert.Equal("S3", templateWriter.GetToken<string>($"{eventPath}.Type")); |
| 39 | + |
| 40 | + // Bucket is always a Ref |
| 41 | + var bucketName = s3EventAttribute.Bucket.StartsWith("@") ? s3EventAttribute.Bucket.Substring(1) : s3EventAttribute.Bucket; |
| 42 | + Assert.Equal(bucketName, templateWriter.GetToken<string>($"{eventPropertiesPath}.Bucket.Ref")); |
| 43 | + |
| 44 | + // Events |
| 45 | + if (s3EventAttribute.IsEventsSet) |
| 46 | + { |
| 47 | + var expectedEvents = s3EventAttribute.Events.Split(';').Select(x => x.Trim()).ToList(); |
| 48 | + Assert.Equal(expectedEvents, templateWriter.GetToken<List<string>>($"{eventPropertiesPath}.Events")); |
| 49 | + } |
| 50 | + |
| 51 | + // Filter |
| 52 | + if (s3EventAttribute.IsFilterPrefixSet || s3EventAttribute.IsFilterSuffixSet) |
| 53 | + { |
| 54 | + Assert.True(templateWriter.Exists($"{eventPropertiesPath}.Filter.S3Key.Rules")); |
| 55 | + var rules = templateWriter.GetToken<List<Dictionary<string, string>>>($"{eventPropertiesPath}.Filter.S3Key.Rules"); |
| 56 | + if (s3EventAttribute.IsFilterPrefixSet) |
| 57 | + { |
| 58 | + Assert.Contains(rules, r => r["Name"] == "prefix" && r["Value"] == s3EventAttribute.FilterPrefix); |
| 59 | + } |
| 60 | + if (s3EventAttribute.IsFilterSuffixSet) |
| 61 | + { |
| 62 | + Assert.Contains(rules, r => r["Name"] == "suffix" && r["Value"] == s3EventAttribute.FilterSuffix); |
| 63 | + } |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + Assert.False(templateWriter.Exists($"{eventPropertiesPath}.Filter")); |
| 68 | + } |
| 69 | + |
| 70 | + // Enabled |
| 71 | + Assert.Equal(s3EventAttribute.IsEnabledSet, templateWriter.Exists($"{eventPropertiesPath}.Enabled")); |
| 72 | + if (s3EventAttribute.IsEnabledSet) |
| 73 | + { |
| 74 | + Assert.Equal(s3EventAttribute.Enabled, templateWriter.GetToken<bool>($"{eventPropertiesPath}.Enabled")); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + [Theory] |
| 79 | + [InlineData(CloudFormationTemplateFormat.Json)] |
| 80 | + [InlineData(CloudFormationTemplateFormat.Yaml)] |
| 81 | + public void VerifyS3EventProperties_AreSyncedCorrectly(CloudFormationTemplateFormat templateFormat) |
| 82 | + { |
| 83 | + // ARRANGE |
| 84 | + var mockFileManager = GetMockFileManager(string.Empty); |
| 85 | + var lambdaFunctionModel = GetLambdaFunctionModel(); |
| 86 | + lambdaFunctionModel.PackageType = LambdaPackageType.Zip; |
| 87 | + var eventResourceName = "MyBucket"; |
| 88 | + var eventPropertiesPath = $"Resources.{lambdaFunctionModel.ResourceName}.Properties.Events.{eventResourceName}.Properties"; |
| 89 | + var syncedEventPropertiesPath = $"Resources.{lambdaFunctionModel.ResourceName}.Metadata.SyncedEventProperties"; |
| 90 | + |
| 91 | + // Initial attribute with filters |
| 92 | + var initialAttribute = new S3EventAttribute("@MyBucket") |
| 93 | + { |
| 94 | + FilterPrefix = "uploads/", |
| 95 | + FilterSuffix = ".jpg" |
| 96 | + }; |
| 97 | + lambdaFunctionModel.Attributes = [new AttributeModel<S3EventAttribute> { Data = initialAttribute }]; |
| 98 | + var cloudFormationWriter = GetCloudFormationWriter(mockFileManager, _directoryManager, templateFormat, _diagnosticReporter); |
| 99 | + var report = GetAnnotationReport([lambdaFunctionModel]); |
| 100 | + |
| 101 | + // ACT |
| 102 | + cloudFormationWriter.ApplyReport(report); |
| 103 | + |
| 104 | + // Assert initial properties |
| 105 | + ITemplateWriter templateWriter = templateFormat == CloudFormationTemplateFormat.Json ? new JsonWriter() : new YamlWriter(); |
| 106 | + templateWriter.Parse(mockFileManager.ReadAllText(ServerlessTemplateFilePath)); |
| 107 | + |
| 108 | + Assert.Equal("MyBucket", templateWriter.GetToken<string>($"{eventPropertiesPath}.Bucket.Ref")); |
| 109 | + Assert.True(templateWriter.Exists($"{eventPropertiesPath}.Filter.S3Key.Rules")); |
| 110 | + Assert.True(templateWriter.Exists($"{eventPropertiesPath}.Events")); |
| 111 | + |
| 112 | + // Update attribute - remove filters, add Enabled=false |
| 113 | + var updatedAttribute = new S3EventAttribute("@MyBucket") |
| 114 | + { |
| 115 | + Enabled = false |
| 116 | + }; |
| 117 | + lambdaFunctionModel.Attributes = [new AttributeModel<S3EventAttribute> { Data = updatedAttribute }]; |
| 118 | + report = GetAnnotationReport([lambdaFunctionModel]); |
| 119 | + |
| 120 | + cloudFormationWriter.ApplyReport(report); |
| 121 | + |
| 122 | + // Assert updated properties |
| 123 | + templateWriter.Parse(mockFileManager.ReadAllText(ServerlessTemplateFilePath)); |
| 124 | + Assert.Equal("MyBucket", templateWriter.GetToken<string>($"{eventPropertiesPath}.Bucket.Ref")); |
| 125 | + Assert.False(templateWriter.Exists($"{eventPropertiesPath}.Filter")); |
| 126 | + Assert.False(templateWriter.GetToken<bool>($"{eventPropertiesPath}.Enabled")); |
| 127 | + } |
| 128 | + |
| 129 | + public class S3EventsTestData : TheoryData<CloudFormationTemplateFormat, S3EventAttribute> |
| 130 | + { |
| 131 | + public S3EventsTestData() |
| 132 | + { |
| 133 | + foreach (var templateFormat in new List<CloudFormationTemplateFormat> { CloudFormationTemplateFormat.Json, CloudFormationTemplateFormat.Yaml }) |
| 134 | + { |
| 135 | + // Simple - default events |
| 136 | + Add(templateFormat, new S3EventAttribute("@MyBucket")); |
| 137 | + |
| 138 | + // With custom events |
| 139 | + Add(templateFormat, new S3EventAttribute("@MyBucket") { Events = "s3:ObjectCreated:*;s3:ObjectRemoved:*" }); |
| 140 | + |
| 141 | + // With filters |
| 142 | + Add(templateFormat, new S3EventAttribute("@MyBucket") { FilterPrefix = "uploads/", FilterSuffix = ".jpg" }); |
| 143 | + |
| 144 | + // With prefix only |
| 145 | + Add(templateFormat, new S3EventAttribute("@MyBucket") { FilterPrefix = "logs/" }); |
| 146 | + |
| 147 | + // With suffix only |
| 148 | + Add(templateFormat, new S3EventAttribute("@MyBucket") { FilterSuffix = ".png" }); |
| 149 | + |
| 150 | + // With custom resource name and disabled |
| 151 | + Add(templateFormat, new S3EventAttribute("@ImageBucket") { ResourceName = "ImageBucketEvent", Enabled = false }); |
| 152 | + |
| 153 | + // All properties |
| 154 | + Add(templateFormat, new S3EventAttribute("@MyBucket") |
| 155 | + { |
| 156 | + ResourceName = "FullS3Event", |
| 157 | + Events = "s3:ObjectCreated:Put", |
| 158 | + FilterPrefix = "data/", |
| 159 | + FilterSuffix = ".csv", |
| 160 | + Enabled = true |
| 161 | + }); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments