Skip to content

Commit

Permalink
Refactor Amazon services and enhance testing setup
Browse files Browse the repository at this point in the history
- Simplified return value in `AmazonApiAuthenticatorService` to only return the access token.
- Updated sender details in `AmazonShippingRateProvider` for clarity.
- Replaced `labelOptions` with `shippingDetails` in `AmazonShippingShipmentProvider` for consistency.
- Enhanced `FedExServiceCollectionExtensions` to include logging and register `IFedexApiAuthenticatorService`.
- Streamlined service setup by removing unnecessary logging in various service extension classes.
- Updated `EasyKeys.Shipping.FuncTest` project file to include references to Amazon Rates and Shipment projects.
- Improved `ServiceProviderInstance` for easier Amazon service configuration in tests.
- Adjusted shipment date in `TestShipments` for testing scenarios.
- Added unit tests for `AmazonShippingRateProvider` and `AmazonShippingShipmentProvider` to validate functionality.
  • Loading branch information
Brandon Moffett committed Dec 16, 2024
1 parent c000fbc commit c1a295b
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task<string> GetTokenAsync(CancellationToken cancellationToken = de

_expirationClock.AddOrUpdate(nameof(_expirationClock), (x) => DateTimeOffset.Now.AddSeconds(token.expires_in - 5), (x, y) => y.AddMilliseconds(token.expires_in - 5));

return $"{token.token_type} {token.access_token}";
return token.access_token;
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public async Task<Shipment> GetRatesAsync(Shipment shipment, CancellationToken c
City = shipment.DestinationAddress.City,
CountryCode = shipment.DestinationAddress.CountryCode,
PostalCode = shipment.DestinationAddress.PostalCode,
Email = "unknown name",
PhoneNumber = "unknown phone number"
},
ShipFrom = new Abstractions.OpenApis.V2.Shipping.Address()
Expand All @@ -54,6 +53,7 @@ public async Task<Shipment> GetRatesAsync(Shipment shipment, CancellationToken c
City = shipment.OriginAddress.City,
CountryCode = shipment.OriginAddress.CountryCode,
PostalCode = shipment.OriginAddress.PostalCode,
Name = "Easykeys fullfuilment team",
Email = "[email protected]",
CompanyName = "EasyKeys",
PhoneNumber = "unknown phone number"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public AmazonShippingShipmentProvider(
public Task<ShipmentLabel> CreateShipmentAsync(
string RateId,
Shipping.Abstractions.Models.Shipment shipment,
ShippingDetails labelOptions,
ShippingDetails shippingDetails,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public async Task<ShipmentLabel> CreateSmartShipmentAsync(
Shipping.Abstractions.Models.Shipment shipment,
ShippingDetails labelOptions,
ShippingDetails shippingDetails,
CancellationToken cancellationToken = default)
{
var label = new ShipmentLabel();
Expand All @@ -52,16 +52,16 @@ public async Task<ShipmentLabel> CreateSmartShipmentAsync(
ShipDate = shipment.Options.ShippingDate.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'"),
ShipTo = new Abstractions.OpenApis.V2.Shipping.Address()
{
Name = labelOptions.Recipient.FullName,
CompanyName = labelOptions.Recipient.Company,
Name = shippingDetails.Recipient.FullName,
CompanyName = shippingDetails.Recipient.Company,
AddressLine1 = shipment.DestinationAddress.StreetLine,
AddressLine2 = shipment.DestinationAddress.StreetLine2,
StateOrRegion = shipment.DestinationAddress.StateOrProvince,
City = shipment.DestinationAddress.City,
CountryCode = shipment.DestinationAddress.CountryCode,
PostalCode = shipment.DestinationAddress.PostalCode,
Email = labelOptions.Recipient.Email,
PhoneNumber = labelOptions.Recipient.PhoneNumber
Email = shippingDetails.Recipient.Email,
PhoneNumber = shippingDetails.Recipient.PhoneNumber
},
ShipFrom = new Abstractions.OpenApis.V2.Shipping.Address()
{
Expand All @@ -71,10 +71,10 @@ public async Task<ShipmentLabel> CreateSmartShipmentAsync(
City = shipment.OriginAddress.City,
CountryCode = shipment.OriginAddress.CountryCode,
PostalCode = shipment.OriginAddress.PostalCode,
Email = labelOptions.Sender.Email,
CompanyName = labelOptions.Sender.Company,
PhoneNumber = labelOptions.Sender.PhoneNumber,
Name = labelOptions.Sender.FullName
Email = shippingDetails.Sender.Email,
CompanyName = shippingDetails.Sender.Company,
PhoneNumber = shippingDetails.Sender.PhoneNumber,
Name = shippingDetails.Sender.FullName
},
Packages = new ()
{
Expand Down Expand Up @@ -122,28 +122,28 @@ public async Task<ShipmentLabel> CreateSmartShipmentAsync(
},
ServiceSelection = new ()
{
ServiceId = new () { labelOptions.ServiceId }
ServiceId = new () { shippingDetails.ServiceId }
},
LabelSpecifications = new ()
{
Format = labelOptions.LabelFormat switch
Format = shippingDetails.LabelFormat switch
{
"PNG" => DocumentFormat.PNG,
"PDF" => DocumentFormat.PDF,
_ => DocumentFormat.PNG
},
Size = new ()
{
Length = (double)labelOptions.LabelDimensions.Length,
Width = (double)labelOptions.LabelDimensions.Width,
Unit = labelOptions.LabelUnit switch
Length = (double)shippingDetails.LabelDimensions.Length,
Width = (double)shippingDetails.LabelDimensions.Width,
Unit = shippingDetails.LabelUnit switch
{
"INCH" => DocumentSizeUnit.INCH,
"CM" => DocumentSizeUnit.CENTIMETER,
_ => DocumentSizeUnit.INCH
}
},
Dpi = labelOptions.LabelDpi,
Dpi = shippingDetails.LabelDpi,
PageLayout = "DEFAULT",
NeedFileJoining = false,
RequestedDocumentTypes = new Collection<DocumentType> { DocumentType.LABEL }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public interface IAmazonShippingShipmentProvider
{
Task<ShipmentLabel> CreateSmartShipmentAsync(
Shipping.Abstractions.Models.Shipment shipment,
ShippingDetails labelOptions,
ShippingDetails shippingDetails,
CancellationToken cancellationToken = default);

Task<ShipmentLabel> CreateShipmentAsync(
string RateId,
Shipping.Abstractions.Models.Shipment shipment,
ShippingDetails labelOptions,
ShippingDetails shippingDetails,
CancellationToken cancellationToken = default);

Task<ShipmentCancelledResult> CancelShipmentAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public static IServiceCollection AddFedExApiClients(
string sectionName = nameof(FedExApiOptions),
Action<FedExApiOptions, IServiceProvider>? configure = null)
{
services.AddLogging();

services.AddChangeTokenOptions<FedExApiOptions>(sectionName, null, (options, config) => configure?.Invoke(options, config));
services.AddSingleton<IFedexApiAuthenticatorService, FedexApiAuthenticatorService>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ public static IServiceCollection AddRestApiFedExAddressValidationProvider(
string sectionName = nameof(FedExApiOptions),
Action<FedExApiOptions, IServiceProvider>? configOptions = null)
{
services.AddChangeTokenOptions<FedExApiOptions>(
sectionName: sectionName,
configureAction: (options, sp) => configOptions?.Invoke(options, sp));

services.AddLogging();

services.AddFedExApiClients();

services.AddTransient<IFedExAddressValidationProvider, EasyKeys.Shipping.FedEx.AddressValidation.RestApi.Impl.FedExAddressValidationProvider>();

services.AddTransient<IAddressValidationProvider, EasyKeys.Shipping.FedEx.AddressValidation.RestApi.Impl.FedExAddressValidationProvider>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public static IServiceCollection AddRestApiFedExRateProvider(
string sectionName = nameof(FedExApiOptions),
Action<FedExApiOptions, IServiceProvider>? configOptions = null)
{

Check warning on line 41 in src/EasyKeys.Shipping.FedEx.Rates/DependencyInjection/FedExRatesServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / build

services.AddLogging();

services.AddFedExApiClients();

services.AddTransient<IFedExRateProvider, EasyKeys.Shipping.FedEx.Rates.RestApi.Impl.FedexRateProvider>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public static IServiceCollection AddRestApiFedExShipmentProvider(
string sectionName = nameof(FedExApiOptions),
Action<FedExApiOptions, IServiceProvider>? configOptions = null)
{

Check warning on line 41 in src/EasyKeys.Shipping.FedEx.Shipment/DependencyInjection/FedExShippingServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / build

services.AddLogging();

services.AddFedExApiClients();

services.AddTransient<IFedExShipmentProvider, EasyKeys.Shipping.FedEx.Shipment.RestApi.Impl.FedExShipmentProvider>();

Expand Down
40 changes: 40 additions & 0 deletions test/EasyKeys.Shipping.FuncTest/Amazon/AmazonRateProviderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using EasyKeys.Shipping.Amazon.Rates;
using EasyKeys.Shipping.Stamps.Shipment;

using EasyKeysShipping.FuncTest.TestHelpers;

using Microsoft.Extensions.DependencyInjection;

namespace EasyKeys.Shipping.FuncTest.Amazon;

public class AmazonShippingRateProviderTests
{
private readonly IAmazonShippingRateProvider _rateProvider;
private readonly ITestOutputHelper _output;

public AmazonShippingRateProviderTests(ITestOutputHelper output)
{
_output = output;
_rateProvider = ServiceProviderInstance.GetAmazonServices(output)
.GetRequiredService<IAmazonShippingRateProvider>();
}

[Fact]
public async Task Return_Shipment_With_Rates_Successfully()
{
var shipment = TestShipments.CreateDomesticShipment();

var result = await _rateProvider.GetRatesAsync(shipment, CancellationToken.None);

Assert.NotNull(result);

Assert.NotNull(result.Rates);

Assert.Empty(result.InternalErrors);

foreach (var rate in result.Rates)
{
_output.WriteLine($"{rate.ServiceName} - {rate.TotalCharges} / {rate.TotalCharges2}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using EasyKeys.Shipping.Amazon.Shipment;
using EasyKeys.Shipping.Amazon.Shipment.Models;
using EasyKeys.Shipping.Stamps.Abstractions.Models;
using EasyKeys.Shipping.Stamps.Rates.Models;

using EasyKeysShipping.FuncTest.TestHelpers;

using Microsoft.Extensions.DependencyInjection;

namespace EasyKeys.Shipping.FuncTest.Amazon;

public class AmazonShippingShipmentProviderTests
{
private readonly ITestOutputHelper _output;
private readonly IAmazonShippingShipmentProvider _shipmentProvider;

public AmazonShippingShipmentProviderTests(ITestOutputHelper output)
{
_output = output;
_shipmentProvider = ServiceProviderInstance.GetAmazonServices(output).GetRequiredService<IAmazonShippingShipmentProvider>();
}

[Fact]
public async Task Process_Domestic_Shipment_Successfully()
{
var (sender, recipient) = TestShipments.CreateContactInfo();

var shipmentDetails = new ShippingDetails();

shipmentDetails.Sender = sender;
shipmentDetails.Recipient = recipient;

var rateOptions = new RateOptions()
{
Sender = sender,
Recipient = recipient,
ServiceType = StampsServiceType.Priority
};

var labels = await _shipmentProvider.CreateSmartShipmentAsync(
TestShipments.CreateDomesticShipment(),
shipmentDetails,
CancellationToken.None);

Assert.NotNull(labels);
Assert.NotNull(labels.Labels[0].Bytes[0]);
}

}

Check failure on line 49 in test/EasyKeys.Shipping.FuncTest/Amazon/AmazonShippingShipmentProviderTests.cs

View workflow job for this annotation

GitHub Actions / build

Check failure on line 49 in test/EasyKeys.Shipping.FuncTest/Amazon/AmazonShippingShipmentProviderTests.cs

View workflow job for this annotation

GitHub Actions / build

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<Using Include="Xunit.Abstractions" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\EasyKeys.Shipping.Amazon.Rates\EasyKeys.Shipping.Amazon.Rates.csproj" />
<ProjectReference Include="..\..\src\EasyKeys.Shipping.Amazon.Shipment\EasyKeys.Shipping.Amazon.Shipment.csproj" />
<ProjectReference Include="..\..\src\EasyKeys.Shipping.FedEx.AddressValidation\EasyKeys.Shipping.FedEx.AddressValidation.csproj" />
<ProjectReference Include="..\..\src\EasyKeys.Shipping.FedEx.Rates\EasyKeys.Shipping.FedEx.Rates.csproj" />
<ProjectReference Include="..\..\src\EasyKeys.Shipping.FedEx.Shipment\EasyKeys.Shipping.FedEx.Shipment.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Bet.Extensions.Testing.Logging;

using EasyKeys.Shipping.Amazon.Rates.DependencyInjection;
using EasyKeys.Shipping.Amazon.Shipment.DependencyInjection;
using EasyKeys.Shipping.Stamps.Shipment.DependencyInjection;
using EasyKeys.Shipping.Stamps.Tracking.DependencyInjection;

Expand All @@ -10,6 +12,28 @@ namespace EasyKeysShipping.FuncTest.TestHelpers;

public static class ServiceProviderInstance
{
public static IServiceProvider GetAmazonServices(ITestOutputHelper output)
{
var services = new ServiceCollection();
var dic = new Dictionary<string, string>
{
{ "AzureVault:BaseUrl", "https://easykeysshipping.vault.azure.net/" },
{ "AmazonShippingApiOptions:IsDevelopment", "true" }
};

var configBuilder = new ConfigurationBuilder().AddInMemoryCollection(dic);
configBuilder.AddAzureKeyVault(hostingEnviromentName: "Development", usePrefix: true);
Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", "Development");
services.AddLogging(x => x.AddXunit(output));

services.AddSingleton<IConfiguration>(configBuilder.Build());
services.AddAmazonShippingClient();
services.AddRestApiAmazonRateProvider();
services.AddRestApiAmazonShipmentProvider();

return services.BuildServiceProvider();
}

public static IServiceProvider GetFedExServices(ITestOutputHelper output)
{
var services = new ServiceCollection();
Expand All @@ -34,9 +58,11 @@ public static IServiceProvider GetFedExServices(ITestOutputHelper output)
services.AddFedExTrackingProvider();

// adress validation apis
services.AddFedExApiClients();
services.AddRestApiFedExAddressValidationProvider();
services.AddRestApiFedExRateProvider();
services.AddRestApiFedExShipmentProvider();

return services.BuildServiceProvider();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static Shipment CreateDomesticShipment()
};

var validatedAddress = new ValidateAddress(Guid.NewGuid().ToString(), destinationAddress);
var shipmentOptions = new ShipmentOptions(StampsPackageType.Package.Name, DateTime.Now);
var shipmentOptions = new ShipmentOptions(StampsPackageType.Package.Name, DateTime.Now.AddDays(1));
return new Shipment(
originAddress,
validatedAddress.ProposedAddress ?? validatedAddress.OriginalAddress,
Expand Down

0 comments on commit c1a295b

Please sign in to comment.