Skip to content

Commit c2430ff

Browse files
authored
Merge pull request #18 from sliedig/develop
chore: maintenance tasks
2 parents 1b04c3f + 7dd3c7e commit c2430ff

19 files changed

+224
-200
lines changed

Unicorn.Contracts/ContractsService.Test/ContractsService.Tests.csproj

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
1111
<PackageReference Include="Amazon.Lambda.DynamoDBEvents" Version="2.1.1" />
1212
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
13-
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.103" />
14-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
13+
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.200.9" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
1515
<PackageReference Include="Moq" Version="4.18.4" />
16-
<PackageReference Include="xunit" Version="2.4.2" />
16+
<PackageReference Include="xunit" Version="2.5.0" />
1717
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
1818
<PrivateAssets>all</PrivateAssets>
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -26,28 +26,28 @@
2626

2727
<ItemGroup>
2828
<None Update="events\create_valid_event.json">
29-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
29+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3030
</None>
3131
<None Update="events\create_empty_dict_body_event.json">
32-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
32+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3333
</None>
3434
<None Update="events\create_missing_body_event.json">
35-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
35+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3636
</None>
3737
<None Update="events\create_wrong_event.json">
38-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
38+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3939
</None>
4040
<None Update="events\update_empty_dict_body_event.json">
41-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
41+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4242
</None>
4343
<None Update="events\update_missing_body_event.json">
44-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
44+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4545
</None>
4646
<None Update="events\update_valid_event.json">
47-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
47+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4848
</None>
4949
<None Update="events\update_wrong_event.json">
50-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
50+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
5151
</None>
5252
</ItemGroup>
5353

Unicorn.Contracts/ContractsService.Test/CreateContractFunctionTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Amazon.DynamoDBv2.DataModel;
99
using Amazon.Lambda.APIGatewayEvents;
1010
using Moq;
11-
using Unicorn.Contracts.Tests;
1211
using Xunit;
1312
using Xunit.Abstractions;
1413

@@ -30,6 +29,7 @@ public CreateContractFunctionTest(ITestOutputHelper testOutputHelper)
3029
[Fact]
3130
public async Task CreateValidContractPublishesDraftContractStatusChangedEvent()
3231
{
32+
// Arrange
3333
var request = TestHelpers.LoadApiGatewayProxyRequest("./events/create_valid_event.json");
3434

3535
var mockDynamoDbContext = new Mock<IDynamoDBContext>();

Unicorn.Contracts/ContractsService.Test/TestHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using Amazon.Lambda.Serialization.SystemTextJson;
1010
using Amazon.Lambda.TestUtilities;
1111

12-
namespace Unicorn.Contracts.Tests;
12+
namespace Unicorn.Contracts.ContractService.Tests;
1313

1414
public static class TestHelpers
1515
{

Unicorn.Contracts/ContractsService.Test/UpdateContractFunctionTest.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Net;
7+
using System.Threading;
78
using System.Threading.Tasks;
89
using Amazon.DynamoDBv2.DataModel;
910
using Amazon.Lambda.APIGatewayEvents;
1011
using Moq;
11-
using Unicorn.Contracts.Tests;
1212
using Xunit;
1313
using Xunit.Abstractions;
1414

@@ -22,34 +22,50 @@ public class UpdateContractFunctionTest
2222
public UpdateContractFunctionTest(ITestOutputHelper testOutputHelper)
2323
{
2424
_testOutputHelper = testOutputHelper;
25+
2526
// Set env variable for Powertools Metrics
26-
Environment.SetEnvironmentVariable("POWERTOOLS_METRICS_NAMESPACE","ContractService");
27+
Environment.SetEnvironmentVariable("POWERTOOLS_METRICS_NAMESPACE", "ContractService");
2728
}
2829

29-
[Trait("Category", "MetricsImplementation")]
3030
[Fact]
3131
public async Task UpdateContractPublishesApprovedContractStatusChangedEvent()
3232
{
3333
var request = TestHelpers.LoadApiGatewayProxyRequest("./events/update_valid_event.json");
3434

3535
var mockDynamoDbContext = new Mock<IDynamoDBContext>();
36-
var mockPublisher = new Mock<IPublisher>();
36+
var retContract = new Contract()
37+
{
38+
PropertyId = "usa/anytown/main-street/123",
39+
ContractId = Guid.NewGuid(),
40+
Address = new Address()
41+
{
42+
City = "anytown",
43+
Number = 123,
44+
Street = "main-street"
45+
}
46+
};
3747

48+
mockDynamoDbContext
49+
.Setup(x => x.LoadAsync<Contract>(It.IsAny<string>(), CancellationToken.None).Result)
50+
.Returns(retContract);
51+
52+
var mockPublisher = new Mock<IPublisher>();
53+
3854
var context = TestHelpers.NewLambdaContext();
3955

4056
var expectedResponse = new APIGatewayProxyResponse
4157
{
4258
StatusCode = (int)HttpStatusCode.OK,
4359
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
4460
};
45-
46-
var function = new CreateContractFunction(mockDynamoDbContext.Object, mockPublisher.Object);
61+
62+
var function = new UpdateContractFunction(mockDynamoDbContext.Object, mockPublisher.Object);
4763
var response = await function.FunctionHandler(request, context);
48-
64+
4965
mockPublisher.Verify(
50-
client => client.PublishEvent(It.IsAny<Contract>()), Times.Once);
51-
//TODO: Verify with contract status = DRAFT
52-
66+
client => client.PublishEvent(It.IsAny<Contract>()), Times.Once);
67+
//TODO: Verify with contract status = DRAFT
68+
5369
_testOutputHelper.WriteLine("Lambda Response: \n" + response.Body);
5470
_testOutputHelper.WriteLine("Expected Response: \n" + expectedResponse.Body);
5571

Unicorn.Contracts/ContractsService/ContractsService.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
1414
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
1515
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
16-
<PackageReference Include="AWS.Lambda.Powertools.Common" Version="1.1.0" />
17-
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.0" />
18-
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.2.0" />
19-
<PackageReference Include="AWS.Lambda.Powertools.Tracing" Version="1.1.0" />
20-
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.103" />
21-
<PackageReference Include="AWSSDK.EventBridge" Version="3.7.102.15" />
16+
<PackageReference Include="AWS.Lambda.Powertools.Common" Version="1.1.1" />
17+
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.1" />
18+
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.3.2" />
19+
<PackageReference Include="AWS.Lambda.Powertools.Tracing" Version="1.1.1" />
20+
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.200.9" />
21+
<PackageReference Include="AWSSDK.EventBridge" Version="3.7.200.10" />
2222
<PackageReference Include="AWSXRayRecorder.Handlers.AwsSdk" Version="2.12.0" />
2323
</ItemGroup>
2424
</Project>

Unicorn.Contracts/ContractsService/CreateContractFunction.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ public CreateContractFunction()
5454
/// <summary>
5555
/// Testing constructor for CreateContractFunction
5656
/// </summary>
57-
/// <param name="dynamoDbContext"></param>
58-
/// <param name="publisher"></param>
57+
/// <param name="dynamoDbContext">Instance of IDynamoDbContext</param>
58+
/// <param name="publisher">Instance of IPublisher</param>
5959
public CreateContractFunction(IDynamoDBContext dynamoDbContext, IPublisher publisher)
6060
{
61-
_publisher = publisher;
6261
_dynamoDbContext = dynamoDbContext;
62+
_publisher = publisher;
6363
}
6464

6565
/// <summary>
@@ -106,16 +106,16 @@ public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyReques
106106
Address = contractRequest.Address,
107107
SellerName = contractRequest.SellerName
108108
};
109-
109+
110110
Logger.AppendKey("Contract", contract);
111111
Logger.LogInformation("Creating new contract");
112112

113113
// Create entry in DDB for new contract
114114
await CreateContract(contract).ConfigureAwait(false);
115-
115+
116116
// Publish ContractStatusChanged event
117117
await _publisher.PublishEvent(contract).ConfigureAwait(false);
118-
118+
119119
// return generated contract ID back to user:
120120
return new APIGatewayProxyResponse
121121
{
@@ -133,14 +133,14 @@ public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyReques
133133
private CreateContractRequest ValidateRequestBody(APIGatewayProxyRequest apigProxyEvent)
134134
{
135135
Logger.LogInformation("Parsing API Gateway request body to CreateContractRequest type.");
136-
137-
CreateContractRequest? request;
138-
139-
try
136+
137+
CreateContractRequest? request;
138+
139+
try
140140
{
141141
if (apigProxyEvent.Body == null)
142142
throw new EventValidationException("API Gateway.");
143-
143+
144144
request = JsonSerializer.Deserialize<CreateContractRequest>(apigProxyEvent.Body, new JsonSerializerOptions()
145145
{
146146
PropertyNameCaseInsensitive = true
@@ -196,6 +196,9 @@ private async Task CreateContract(Contract contract)
196196
{
197197
Logger.LogInformation($"Saving contract for Property ID: {contract.PropertyId}");
198198
await _dynamoDbContext.SaveAsync(contract);
199+
200+
// Add custom metric for "New Contracts"
201+
Metrics.AddMetric("NewContracts", 1, MetricUnit.Count, MetricResolution.Standard);
199202
}
200203
catch (Exception e)
201204
{

Unicorn.Contracts/ContractsService/UpdateContractFunction.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,31 @@
88
using Amazon;
99
using Amazon.DynamoDBv2;
1010
using Amazon.DynamoDBv2.DataModel;
11-
using Amazon.EventBridge;
1211
using Amazon.Lambda.APIGatewayEvents;
1312
using Amazon.Lambda.Core;
1413
using Amazon.Util;
14+
using Amazon.XRay.Recorder.Handlers.AwsSdk;
1515
using AWS.Lambda.Powertools.Logging;
1616
using AWS.Lambda.Powertools.Metrics;
1717
using AWS.Lambda.Powertools.Tracing;
1818
using DynamoDBContextConfig = Amazon.DynamoDBv2.DataModel.DynamoDBContextConfig;
1919

20-
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
21-
// [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
22-
2320
namespace Unicorn.Contracts.ContractService
2421
{
2522
public class UpdateContractFunction
2623
{
2724
private readonly IDynamoDBContext _dynamoDbContext;
25+
private readonly IPublisher _publisher;
2826

2927
/// <summary>
3028
/// Default constructor for CreateContractFunction
3129
/// </summary>
3230
public UpdateContractFunction()
3331
{
32+
AWSSDKHandler.RegisterXRayForAllServices();
33+
34+
_publisher = new Publisher();
35+
3436
var dynamodbTable = Environment.GetEnvironmentVariable("DYNAMODB_TABLE");
3537
if (string.IsNullOrEmpty(dynamodbTable))
3638
{
@@ -45,15 +47,14 @@ public UpdateContractFunction()
4547
}
4648

4749
/// <summary>
48-
///
50+
/// Testing constructor for UpdateContractFunction
4951
/// </summary>
50-
/// <param name="dynamoDbContext"></param>
51-
/// <param name="eventBridgeClient"></param>
52-
/// <param name="serviceNamespace"></param>
53-
public UpdateContractFunction(IDynamoDBContext dynamoDbContext, AmazonEventBridgeClient eventBridgeClient,
54-
string serviceNamespace)
52+
/// <param name="dynamoDbContext">Instance of IDynamoDbContext</param>
53+
/// <param name="publisher">Instance of IPublisher</param>
54+
public UpdateContractFunction(IDynamoDBContext dynamoDbContext, IPublisher publisher)
5555
{
5656
_dynamoDbContext = dynamoDbContext;
57+
_publisher = publisher;
5758
}
5859

5960
/// <summary>
@@ -82,10 +83,9 @@ public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyReques
8283

8384
// Create entry in DDB for new contract
8485
await UpdateContract(existingContract).ConfigureAwait(false);
85-
86+
8687
// Publish ContractStatusChanged event
87-
var publisher = new Publisher();
88-
await publisher.PublishEvent(existingContract);
88+
await _publisher.PublishEvent(existingContract).ConfigureAwait(false);
8989

9090
// return generated contract ID back to user:
9191
return new APIGatewayProxyResponse
@@ -134,14 +134,14 @@ private UpdateContractRequest ValidateEvent(APIGatewayProxyRequest apigProxyEven
134134
{
135135
try
136136
{
137-
Console.WriteLine($"Getting contract {propertyId}");
137+
Logger.LogInformation($"Getting contract {propertyId}");
138138
var contract = await _dynamoDbContext.LoadAsync<Contract>(propertyId).ConfigureAwait(false);
139-
Console.WriteLine($"Found contact: {contract != null}");
139+
Logger.LogInformation($"Found contact: {contract != null}");
140140
return contract;
141141
}
142142
catch (Exception e)
143143
{
144-
Console.WriteLine($"Error loading contract {propertyId}: {e.Message}");
144+
Logger.LogError($"Error loading contract {propertyId}: {e.Message}");
145145
return null;
146146
}
147147
}
@@ -156,12 +156,12 @@ private async Task UpdateContract(Contract contract)
156156
{
157157
try
158158
{
159-
Console.WriteLine($"Saving contract with id {contract.PropertyId}");
159+
Logger.LogInformation($"Saving contract with id {contract.PropertyId}");
160160
await _dynamoDbContext.SaveAsync(contract).ConfigureAwait(false);
161161
}
162162
catch (AmazonDynamoDBException e)
163163
{
164-
Console.WriteLine(e);
164+
Logger.LogError(e);
165165
throw;
166166
}
167167
}

Unicorn.Properties/PropertiesService.Tests/PropertiesService.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<ItemGroup>
88
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
99
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
1111
<PackageReference Include="Moq" Version="4.18.4" />
12-
<PackageReference Include="xunit" Version="2.4.2" />
12+
<PackageReference Include="xunit" Version="2.5.0" />
1313
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
1414
<PrivateAssets>all</PrivateAssets>
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Unicorn.Properties/PropertiesService/ContentIntegrityValidatorFunction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public object FunctionHandler(object input, ILambdaContext context)
5959
}
6060

6161
var jsonObject = JsonObject.Create(document.RootElement);
62-
jsonObject.Add("ValidationResult", status);
62+
jsonObject.Add("ValidationResult", status); //TODO: review this implementation
6363
return jsonObject;
6464
}
6565
}

Unicorn.Properties/PropertiesService/ContractStatusChangedEventHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public ContractStatusChangedEventHandler()
5555
public async Task FunctionHandler(CloudWatchEvent<ContractStatusChangedEvent> contractStatusChangedEvent,
5656
ILambdaContext context)
5757
{
58-
context.Logger.LogInformation(JsonSerializer.Serialize(contractStatusChangedEvent));
58+
Logger.LogInformation(JsonSerializer.Serialize(contractStatusChangedEvent));
5959
try
6060
{
6161
await SaveContractStatus(contractStatusChangedEvent.Detail).ConfigureAwait(false);

0 commit comments

Comments
 (0)