Skip to content

Commit 2acfb66

Browse files
committed
add e2e tests for idempotency method
1 parent fe4ef8b commit 2acfb66

File tree

3 files changed

+141
-4
lines changed

3 files changed

+141
-4
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.IO;
19+
using System.Net.Http;
20+
using System.Text.Json;
21+
using System.Threading.Tasks;
22+
using Amazon.DynamoDBv2;
23+
using Amazon.Lambda.APIGatewayEvents;
24+
using Amazon.Lambda.Core;
25+
26+
namespace AWS.Lambda.Powertools.Idempotency.Tests.Handlers;
27+
28+
public class IdempotencyFunctionMethodDecorated
29+
{
30+
public bool MethodCalled;
31+
32+
public IdempotencyFunctionMethodDecorated(AmazonDynamoDBClient client)
33+
{
34+
Idempotency.Configure(builder =>
35+
builder
36+
.UseDynamoDb(storeBuilder =>
37+
storeBuilder
38+
.WithTableName("idempotency_table")
39+
.WithDynamoDBClient(client)
40+
));
41+
}
42+
43+
44+
public async Task<APIGatewayProxyResponse> Handle(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
45+
{
46+
Idempotency.RegisterLambdaContext(context);
47+
var result= await InternalFunctionHandler(apigProxyEvent);
48+
49+
return result;
50+
}
51+
52+
private async Task<APIGatewayProxyResponse> InternalFunctionHandler(APIGatewayProxyRequest apigProxyEvent)
53+
{
54+
Dictionary<string, string> headers = new()
55+
{
56+
{"Content-Type", "application/json"},
57+
{"Access-Control-Allow-Origin", "*"},
58+
{"Access-Control-Allow-Methods", "GET, OPTIONS"},
59+
{"Access-Control-Allow-Headers", "*"}
60+
};
61+
62+
try
63+
{
64+
var address = JsonDocument.Parse(apigProxyEvent.Body).RootElement.GetProperty("address").GetString();
65+
var pageContents = await GetPageContents(address);
66+
var output = $"{{ \"message\": \"hello world\", \"location\": \"{pageContents}\" }}";
67+
68+
return new APIGatewayProxyResponse
69+
{
70+
Body = output,
71+
StatusCode = 200,
72+
Headers = headers
73+
};
74+
75+
}
76+
catch (IOException)
77+
{
78+
return new APIGatewayProxyResponse
79+
{
80+
Body = "{}",
81+
StatusCode = 500,
82+
Headers = headers
83+
};
84+
}
85+
}
86+
87+
[Idempotent]
88+
private async Task<string> GetPageContents(string address)
89+
{
90+
MethodCalled = true;
91+
92+
var client = new HttpClient();
93+
using var response = await client.GetAsync(address);
94+
using var content = response.Content;
95+
var pageContent = await content.ReadAsStringAsync();
96+
97+
return pageContent;
98+
}
99+
}

libraries/tests/AWS.Lambda.Powertools.Idempotency.Tests/IdempotencyTest.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
/*
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
66
* A copy of the License is located at
7-
*
7+
*
88
* http://aws.amazon.com/apache2.0
9-
*
9+
*
1010
* or in the "license" file accompanying this file. This file is distributed
1111
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
1212
* express or implied. See the License for the specific language governing
1313
* permissions and limitations under the License.
1414
*/
1515

16+
using System;
1617
using System.IO;
1718
using System.Text.Json;
1819
using System.Threading.Tasks;
1920
using Amazon.DynamoDBv2;
2021
using Amazon.DynamoDBv2.Model;
2122
using Amazon.Lambda.APIGatewayEvents;
23+
using Amazon.Lambda.TestUtilities;
2224
using AWS.Lambda.Powertools.Idempotency.Tests.Handlers;
2325
using AWS.Lambda.Powertools.Idempotency.Tests.Persistence;
2426
using FluentAssertions;
@@ -67,4 +69,40 @@ public async Task EndToEndTest()
6769
});
6870
scanResponse.Count.Should().Be(1);
6971
}
72+
73+
[Fact]
74+
[Trait("Category", "Integration")]
75+
public async Task EndToEndTestMethod()
76+
{
77+
var function = new IdempotencyFunctionMethodDecorated(_client);
78+
79+
var options = new JsonSerializerOptions
80+
{
81+
PropertyNameCaseInsensitive = true
82+
};
83+
84+
var context = new TestLambdaContext
85+
{
86+
RemainingTime = TimeSpan.FromSeconds(30)
87+
};
88+
89+
var request = JsonSerializer.Deserialize<APIGatewayProxyRequest>(await File.ReadAllTextAsync("./resources/apigw_event2.json"),options);
90+
91+
var response = await function.Handle(request, context);
92+
function.MethodCalled.Should().BeTrue();
93+
94+
function.MethodCalled = false;
95+
96+
var response2 = await function.Handle(request, context);
97+
function.MethodCalled.Should().BeFalse();
98+
99+
JsonSerializer.Serialize(response).Should().Be(JsonSerializer.Serialize(response));
100+
response2.Body.Should().Contain("hello world");
101+
102+
var scanResponse = await _client.ScanAsync(new ScanRequest
103+
{
104+
TableName = _tableName
105+
});
106+
scanResponse.Count.Should().Be(1);
107+
}
70108
}

libraries/tests/AWS.Lambda.Powertools.Idempotency.Tests/Internal/IdempotentAspectTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public void Handle_WhenIdempotencyOnSubMethodAnnotated_AndSecondCall_AndNotExpir
310310
{
311311
// Arrange
312312
var store = Substitute.For<BasePersistenceStore>();
313-
store.SaveInProgress(Arg.Any<JsonDocument>(), Arg.Any<DateTimeOffset>(), Arg.Any<double>())
313+
store.SaveInProgress(Arg.Any<JsonDocument>(), Arg.Any<DateTimeOffset>(), null)
314314
.Throws(new IdempotencyItemAlreadyExistsException());
315315

316316
Idempotency.Configure(builder => builder.WithPersistenceStore(store));

0 commit comments

Comments
 (0)