|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | 4 | using Amazon.Lambda.Annotations.SourceGenerator; |
| 5 | +using Amazon.Lambda.Annotations.SourceGenerator.FileIO; |
5 | 6 | using Amazon.Lambda.Annotations.SourceGenerator.Models; |
6 | 7 | using Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes; |
7 | 8 | using Amazon.Lambda.Annotations.SourceGenerator.Writers; |
8 | 9 | using Amazon.Lambda.Annotations.Schedule; |
9 | 10 | using System.Collections.Generic; |
| 11 | +using System.IO; |
10 | 12 | using Xunit; |
11 | 13 |
|
12 | 14 | namespace Amazon.Lambda.Annotations.SourceGenerators.Tests.WriterTests |
@@ -140,5 +142,136 @@ public void VerifyScheduleEvent_MinimalAttributes(CloudFormationTemplateFormat t |
140 | 142 | Assert.False(templateWriter.Exists($"{eventPropertiesPath}.Input")); |
141 | 143 | Assert.False(templateWriter.Exists($"{eventPropertiesPath}.Enabled")); |
142 | 144 | } |
| 145 | + |
| 146 | + [Theory] |
| 147 | + [InlineData(CloudFormationTemplateFormat.Json)] |
| 148 | + [InlineData(CloudFormationTemplateFormat.Yaml)] |
| 149 | + public void VerifyScheduleEventInput_RelativeFilePath_ReadsFileContents(CloudFormationTemplateFormat templateFormat) |
| 150 | + { |
| 151 | + // ARRANGE - Set up a mock file manager with a JSON file at a relative path |
| 152 | + var mockFileManager = GetMockFileManager(string.Empty); |
| 153 | + var expectedJson = "{\"action\": \"cleanup\", \"target\": \"logs\"}"; |
| 154 | + var inputFilePath = Path.Combine(ProjectRootDirectory, "schedule-input.json"); |
| 155 | + mockFileManager.WriteAllText(inputFilePath, expectedJson); |
| 156 | + |
| 157 | + var lambdaFunctionModel = GetLambdaFunctionModel(); |
| 158 | + lambdaFunctionModel.PackageType = LambdaPackageType.Zip; |
| 159 | + |
| 160 | + var att = new ScheduleEventAttribute("rate(1 hour)") |
| 161 | + { |
| 162 | + ResourceName = "HourlyCleanup", |
| 163 | + Input = "schedule-input.json" |
| 164 | + }; |
| 165 | + lambdaFunctionModel.Attributes.Add(new AttributeModel<ScheduleEventAttribute> { Data = att }); |
| 166 | + var cloudFormationWriter = GetCloudFormationWriter(mockFileManager, _directoryManager, templateFormat, _diagnosticReporter); |
| 167 | + var report = GetAnnotationReport([lambdaFunctionModel]); |
| 168 | + |
| 169 | + // ACT |
| 170 | + cloudFormationWriter.ApplyReport(report); |
| 171 | + |
| 172 | + // ASSERT - The file contents should be used instead of the file path |
| 173 | + ITemplateWriter templateWriter = templateFormat == CloudFormationTemplateFormat.Json ? new JsonWriter() : new YamlWriter(); |
| 174 | + templateWriter.Parse(mockFileManager.ReadAllText(ServerlessTemplateFilePath)); |
| 175 | + |
| 176 | + var eventPropertiesPath = $"Resources.{lambdaFunctionModel.ResourceName}.Properties.Events.HourlyCleanup.Properties"; |
| 177 | + Assert.Equal(expectedJson, templateWriter.GetToken<string>($"{eventPropertiesPath}.Input")); |
| 178 | + } |
| 179 | + |
| 180 | + [Theory] |
| 181 | + [InlineData(CloudFormationTemplateFormat.Json)] |
| 182 | + [InlineData(CloudFormationTemplateFormat.Yaml)] |
| 183 | + public void VerifyScheduleEventInput_AbsoluteFilePath_ReadsFileContents(CloudFormationTemplateFormat templateFormat) |
| 184 | + { |
| 185 | + // ARRANGE - Set up a mock file manager with a JSON file at an absolute path |
| 186 | + // Use Path.GetTempPath() to ensure the path is rooted on both Windows and Linux |
| 187 | + var mockFileManager = GetMockFileManager(string.Empty); |
| 188 | + var expectedJson = "{\"environment\": \"production\"}"; |
| 189 | + var absoluteInputPath = Path.Combine(Path.GetTempPath(), "config", "schedule-input.json"); |
| 190 | + mockFileManager.WriteAllText(absoluteInputPath, expectedJson); |
| 191 | + |
| 192 | + var lambdaFunctionModel = GetLambdaFunctionModel(); |
| 193 | + lambdaFunctionModel.PackageType = LambdaPackageType.Zip; |
| 194 | + |
| 195 | + var att = new ScheduleEventAttribute("rate(5 minutes)") |
| 196 | + { |
| 197 | + ResourceName = "FrequentCheck", |
| 198 | + Input = absoluteInputPath |
| 199 | + }; |
| 200 | + lambdaFunctionModel.Attributes.Add(new AttributeModel<ScheduleEventAttribute> { Data = att }); |
| 201 | + var cloudFormationWriter = GetCloudFormationWriter(mockFileManager, _directoryManager, templateFormat, _diagnosticReporter); |
| 202 | + var report = GetAnnotationReport([lambdaFunctionModel]); |
| 203 | + |
| 204 | + // ACT |
| 205 | + cloudFormationWriter.ApplyReport(report); |
| 206 | + |
| 207 | + // ASSERT - The file contents should be used instead of the file path |
| 208 | + ITemplateWriter templateWriter = templateFormat == CloudFormationTemplateFormat.Json ? new JsonWriter() : new YamlWriter(); |
| 209 | + templateWriter.Parse(mockFileManager.ReadAllText(ServerlessTemplateFilePath)); |
| 210 | + |
| 211 | + var eventPropertiesPath = $"Resources.{lambdaFunctionModel.ResourceName}.Properties.Events.FrequentCheck.Properties"; |
| 212 | + Assert.Equal(expectedJson, templateWriter.GetToken<string>($"{eventPropertiesPath}.Input")); |
| 213 | + } |
| 214 | + |
| 215 | + [Theory] |
| 216 | + [InlineData(CloudFormationTemplateFormat.Json)] |
| 217 | + [InlineData(CloudFormationTemplateFormat.Yaml)] |
| 218 | + public void VerifyScheduleEventInput_LiteralJson_UsedAsIs(CloudFormationTemplateFormat templateFormat) |
| 219 | + { |
| 220 | + // ARRANGE - Input is a literal JSON string, not a file path |
| 221 | + var mockFileManager = GetMockFileManager(string.Empty); |
| 222 | + var lambdaFunctionModel = GetLambdaFunctionModel(); |
| 223 | + lambdaFunctionModel.PackageType = LambdaPackageType.Zip; |
| 224 | + |
| 225 | + var literalJson = "{\"key\": \"value\"}"; |
| 226 | + var att = new ScheduleEventAttribute("rate(5 minutes)") |
| 227 | + { |
| 228 | + ResourceName = "LiteralInputSchedule", |
| 229 | + Input = literalJson |
| 230 | + }; |
| 231 | + lambdaFunctionModel.Attributes.Add(new AttributeModel<ScheduleEventAttribute> { Data = att }); |
| 232 | + var cloudFormationWriter = GetCloudFormationWriter(mockFileManager, _directoryManager, templateFormat, _diagnosticReporter); |
| 233 | + var report = GetAnnotationReport([lambdaFunctionModel]); |
| 234 | + |
| 235 | + // ACT |
| 236 | + cloudFormationWriter.ApplyReport(report); |
| 237 | + |
| 238 | + // ASSERT - The literal JSON should be used as-is since it doesn't resolve to a file |
| 239 | + ITemplateWriter templateWriter = templateFormat == CloudFormationTemplateFormat.Json ? new JsonWriter() : new YamlWriter(); |
| 240 | + templateWriter.Parse(mockFileManager.ReadAllText(ServerlessTemplateFilePath)); |
| 241 | + |
| 242 | + var eventPropertiesPath = $"Resources.{lambdaFunctionModel.ResourceName}.Properties.Events.LiteralInputSchedule.Properties"; |
| 243 | + Assert.Equal(literalJson, templateWriter.GetToken<string>($"{eventPropertiesPath}.Input")); |
| 244 | + } |
| 245 | + |
| 246 | + [Theory] |
| 247 | + [InlineData(CloudFormationTemplateFormat.Json)] |
| 248 | + [InlineData(CloudFormationTemplateFormat.Yaml)] |
| 249 | + public void VerifyScheduleEventInput_NonExistentFilePath_UsedAsIs(CloudFormationTemplateFormat templateFormat) |
| 250 | + { |
| 251 | + // ARRANGE - Input looks like a file path but the file doesn't exist |
| 252 | + var mockFileManager = GetMockFileManager(string.Empty); |
| 253 | + var lambdaFunctionModel = GetLambdaFunctionModel(); |
| 254 | + lambdaFunctionModel.PackageType = LambdaPackageType.Zip; |
| 255 | + |
| 256 | + var nonExistentPath = "does-not-exist.json"; |
| 257 | + var att = new ScheduleEventAttribute("rate(5 minutes)") |
| 258 | + { |
| 259 | + ResourceName = "MissingFileSchedule", |
| 260 | + Input = nonExistentPath |
| 261 | + }; |
| 262 | + lambdaFunctionModel.Attributes.Add(new AttributeModel<ScheduleEventAttribute> { Data = att }); |
| 263 | + var cloudFormationWriter = GetCloudFormationWriter(mockFileManager, _directoryManager, templateFormat, _diagnosticReporter); |
| 264 | + var report = GetAnnotationReport([lambdaFunctionModel]); |
| 265 | + |
| 266 | + // ACT |
| 267 | + cloudFormationWriter.ApplyReport(report); |
| 268 | + |
| 269 | + // ASSERT - The path string should be used as-is since the file doesn't exist |
| 270 | + ITemplateWriter templateWriter = templateFormat == CloudFormationTemplateFormat.Json ? new JsonWriter() : new YamlWriter(); |
| 271 | + templateWriter.Parse(mockFileManager.ReadAllText(ServerlessTemplateFilePath)); |
| 272 | + |
| 273 | + var eventPropertiesPath = $"Resources.{lambdaFunctionModel.ResourceName}.Properties.Events.MissingFileSchedule.Properties"; |
| 274 | + Assert.Equal(nonExistentPath, templateWriter.GetToken<string>($"{eventPropertiesPath}.Input")); |
| 275 | + } |
143 | 276 | } |
144 | 277 | } |
0 commit comments