public async Task ShouldCallLambdaWithEachMessage( SnsRecord <TestLambdaMessage> record, ServiceCollection serviceCollection, JsonSerializer serializer, ILogger logger, [Substitute] TestSnsLambda lambda ) { var request = new SnsEvent <TestLambdaMessage>(new[] { record }); serviceCollection.AddSingleton <ISerializer>(serializer); var serviceProvider = serviceCollection.BuildServiceProvider(); var cancellationToken = new CancellationToken(false); var host = new TestSnsLambdaHost(lambdaHost => { lambdaHost.Lambda = lambda; lambdaHost.Scope = serviceProvider.CreateScope(); lambdaHost.Serializer = serializer; lambdaHost.Logger = logger; }); using var inputStream = await StreamUtils.CreateJsonStream(request); await host.InvokeLambda(inputStream, cancellationToken); await lambda.Received().Handle(Matches(record.Sns), Is(cancellationToken)); }
public async Task ShouldCallCreate_IfRequestTypeIsCreate( ServiceCollection serviceCollection, CustomResourceRequest <object> request, JsonSerializer serializer, ILogger logger, [Substitute] TestCustomResourceLambda lambda, [Substitute] IHttpClient httpClient ) { serviceCollection.AddSingleton <ISerializer>(serializer); serviceCollection.AddSingleton(httpClient); var serviceProvider = serviceCollection.BuildServiceProvider(); var cancellationToken = new CancellationToken(false); var host = new TestCustomResourceLambdaHost(lambdaHost => { lambdaHost.Lambda = lambda; lambdaHost.Scope = serviceProvider.CreateScope(); lambdaHost.Serializer = serializer; lambdaHost.Logger = logger; }); request.RequestType = CustomResourceRequestType.Create; using var inputStream = await StreamUtils.CreateJsonStream(request); await host.InvokeLambda(inputStream, cancellationToken); await lambda.DidNotReceiveWithAnyArgs().Update(default !, default);
public async Task ShouldThrowIfEventDeserializesToNull( ServiceCollection serviceCollection, JsonSerializer serializer, ILogger logger, [Substitute] TestSnsLambda lambda ) { serviceCollection.AddSingleton <ISerializer>(serializer); var serviceProvider = serviceCollection.BuildServiceProvider(); var cancellationToken = new CancellationToken(false); var host = new TestSnsLambdaHost(lambdaHost => { lambdaHost.Lambda = lambda; lambdaHost.Scope = serviceProvider.CreateScope(); lambdaHost.Serializer = serializer; lambdaHost.Logger = logger; }); using var inputStream = await StreamUtils.CreateJsonStream <object?>(null); Func <Task> func = () => host.InvokeLambda(inputStream, cancellationToken); await func.Should().ThrowAsync <InvalidLambdaParameterException>(); }
public async Task Run_PerformsCreateIfUpdateRequiresReplacement_ForNestedTypes( string name, string requestId, string stackId, string logicalResourceId, [Substitute] ILambdaContext context ) { using var generation = await project.GenerateAssembly(); using var server = new InMemoryServer(); var requestType = CustomResourceRequestType.Update; var(assembly, _) = generation; var handler = new HandlerWrapper <object>(assembly, handlerTypeName); var request = CreateRequest(assembly, name); request.RequestType = requestType; request.ResponseURL = new Uri(server.Address); request.StackId = stackId; request.RequestId = requestId; request.LogicalResourceId = logicalResourceId; request.ResourceProperties.Metadata.CreatedBy = "Joe New"; request.OldResourceProperties.Metadata.CreatedBy = "Joe Old"; using var inputStream = await StreamUtils.CreateJsonStream(request); await handler.Run(inputStream, context); var httpRequest = server.Requests .Should() .Contain(request => request.HttpMethod == "PUT" ) .Which; // Tests to make sure status is serialized to all caps httpRequest.Body.Should().MatchRegex("\"Status\":[ ]?\"SUCCESS\""); var body = JsonSerializer.Deserialize <CustomResourceResponse <ResponseData> >(httpRequest.Body); body.Should().Match <CustomResourceResponse <ResponseData> >(response => response.PhysicalResourceId == name && response.StackId == stackId && response.RequestId == requestId && response.LogicalResourceId == logicalResourceId && response.Status == CustomResourceResponseStatus.Success && response.Data !.MethodCalled == "Create" ); }
public async Task TracingShouldBeEnabled() { using var generation = await project.GenerateAssembly(); var(assembly, _) = generation; var handler = new HandlerWrapper <bool>(assembly, "Lambdajection.CompilationTests.Tracing.Handler"); using var inputStream = await StreamUtils.CreateJsonStream(string.Empty); bool result = await handler.Run(inputStream, null !); result.Should().BeTrue(); }
public async Task Handler_RunsWithoutError() { using var generation = await project.GenerateAssembly(); var(assembly, _) = generation; var handlerType = assembly.GetType("Lambdajection.CompilationTests.AmazonFactories.Handler") !; var runMethod = handlerType.GetMethod("Run") !; using var inputStream = await StreamUtils.CreateJsonStream("foo"); var resultStream = await(Task <Stream>) runMethod.Invoke(null, new[] { inputStream, null }) !; var result = await JsonSerializer.DeserializeAsync <string>(resultStream); result.Should().Be("ok"); }
public async Task Run_ShouldNotSendFailure_IfOldNameIsLessThan3Chars( string requestId, string stackId, string logicalResourceId, CustomResourceRequestType requestType, [Substitute] ILambdaContext context ) { var name = "abcde"; var oldName = "a"; using var generation = await project.GenerateAssembly(); using var server = new InMemoryServer(); var(assembly, _) = generation; var handler = new HandlerWrapper <object>(assembly, handlerTypeName); var request = CreateRequest(assembly, name); request.RequestType = requestType; request.ResponseURL = new Uri(server.Address); request.StackId = stackId; request.RequestId = requestId; request.LogicalResourceId = logicalResourceId; request.PhysicalResourceId = null; request.OldResourceProperties.Name = oldName; var inputStream = await StreamUtils.CreateJsonStream(request); await handler.Run(inputStream, context); var httpRequest = server.Requests .Should() .Contain(request => request.HttpMethod == "PUT" ) .Which; var body = JsonSerializer.Deserialize <CustomResourceResponse <ResponseData> >(httpRequest.Body); body.Should().Match <CustomResourceResponse <ResponseData> >(response => response.StackId == stackId && response.RequestId == requestId && response.LogicalResourceId == logicalResourceId && response.Status == CustomResourceResponseStatus.Success ); }
public async Task Run_ReturnsContext( ILambdaContext context ) { using var generation = await project.GenerateAssembly(); var(assembly, _) = generation; var handlerType = assembly.GetType("Lambdajection.CompilationTests.LambdaContext.Handler") !; var runMethod = handlerType.GetMethod("Run") !; using var inputStream = await StreamUtils.CreateJsonStream(string.Empty); var resultStream = await(Task <Stream>) runMethod.Invoke(null, new object[] { inputStream, context }) !; var result = await JsonSerializer.DeserializeAsync <string>(resultStream); result.Should().Be(context.AwsRequestId); }
public async Task EncryptedConfigValueShouldBeReadFromEnvironmentAndDecrypted() { using var generation = await project.GenerateAssembly(); var(assembly, _) = generation; var handlerType = assembly.GetType("Lambdajection.CompilationTests.Configuration.Handler") !; var runMethod = handlerType.GetMethod("Run") !; using var inputStream = await StreamUtils.CreateJsonStream(string.Empty); var resultStream = await(Task <Stream>) runMethod.Invoke(null, new[] { inputStream, null }) !; var result = await JsonSerializer.DeserializeAsync <JsonElement>(resultStream); result.TryGetProperty("ExampleEncryptedValue", out var value); value.ToString().Should().BeEquivalentTo("[decrypted] " + exampleEncryptedValue); }
public async Task Handle_ReturnsConfiguration() { using var generation = await project.GenerateAssembly(); var(assembly, context) = generation; var handlerType = assembly.GetType("Lambdajection.CompilationTests.Configuration.Handler") !; var runMethod = handlerType.GetMethod("Run") !; using var inputStream = await StreamUtils.CreateJsonStream(string.Empty); var resultStream = await(Task <Stream>) runMethod.Invoke(null, new[] { inputStream, null }) !; var result = await JsonSerializer.DeserializeAsync <JsonElement>(resultStream); result.TryGetProperty("ExampleConfigValue", out var value); value.ToString().Should().Be(exampleConfigValue); }
public async Task Run_ShouldReturnIdIfUsingCamelCase( string id ) { using var generation = await project.GenerateAssembly(); var(assembly, _) = generation; var requestType = assembly.GetType("Lambdajection.CompilationTests.Serialization.Request") !; var handler = new HandlerWrapper <string>(assembly, "Lambdajection.CompilationTests.Serialization.Handler"); var request = (dynamic)Activator.CreateInstance(requestType) !; request.Id = id; using var inputStream = await StreamUtils.CreateJsonStream(request, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); string result = await handler.Run(inputStream, null); result.Should().Be(id); }
public async Task Run_DisposesLambdaAsynchronously() { using var generation = await project.GenerateAssembly(); var(assembly, _) = generation; var handlerType = assembly.GetType("Lambdajection.CompilationTests.Disposables.AsyncDisposableHandler") !; var runMethod = handlerType.GetMethod("Run") !; var disposeWasCalledProperty = handlerType.GetProperty("DisposeAsyncWasCalled", BindingFlags.Static | BindingFlags.Public); var disposeWasCalledGetter = disposeWasCalledProperty !.GetGetMethod(); var disposeWasCalledBefore = (bool)disposeWasCalledGetter !.Invoke(null, Array.Empty <object>()) !; disposeWasCalledBefore.Should().BeFalse(); using var inputStream = await StreamUtils.CreateJsonStream(string.Empty); await(Task <Stream>) runMethod.Invoke(null, new[] { inputStream, null }) !; var disposeWasCalledAfter = (bool)disposeWasCalledGetter !.Invoke(null, Array.Empty <object>()) !; disposeWasCalledAfter.Should().BeTrue(); }