Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address form component #1

Closed
wants to merge 15 commits into from
Closed
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ csharp_style_var_when_type_is_apparent = true
dotnet_diagnostic.IDE0007.severity = error
dotnet_diagnostic.IDE0008.severity = error

csharp_style_expression_bodied_constructors = true : error
csharp_style_expression_bodied_constructors = false : error
dotnet_diagnostic.IDE0021.severity = error

csharp_style_expression_bodied_methods = true : error
csharp_style_expression_bodied_methods = false : error
dotnet_diagnostic.IDE0022.severity = error

csharp_style_expression_bodied_operators = true : error
Expand Down
36 changes: 21 additions & 15 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<CentralPackageVersionOverrideEnabled>false</CentralPackageVersionOverrideEnabled>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Kentico.Xperience.Admin" Version="28.1.0" />
<PackageVersion Include="Kentico.Xperience.WebApp" Version="28.1.0" />
<PackageVersion Include="kentico.xperience.azurestorage" Version="28.1.0" />
<PackageVersion Include="kentico.xperience.imageprocessing" Version="28.1.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="SonarAnalyzer.CSharp" Version="9.6.0.74858" />
</ItemGroup>
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<CentralPackageVersionOverrideEnabled>false</CentralPackageVersionOverrideEnabled>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Kentico.Xperience.Admin" Version="28.4.3" />
<PackageVersion Include="Kentico.Xperience.Core.Tests" Version="28.4.3" />
<PackageVersion Include="Kentico.Xperience.WebApp" Version="28.1.0" />
<PackageVersion Include="kentico.xperience.azurestorage" Version="28.1.0" />
<PackageVersion Include="kentico.xperience.imageprocessing" Version="28.1.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0-release-24177-07" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="NUnit" Version="4.1.0" />
<PackageVersion Include="NUnit.ConsoleRunner" Version="3.17.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageVersion Include="SonarAnalyzer.CSharp" Version="9.23.1.88495" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Net.Http.Headers;
using CMS.Tests;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

using NUnit.Framework;

namespace Kentico.Xperience.GoogleMaps.Tests
{
/// <summary>
/// Tests for <see cref="StartupExtensions"/> class.
/// </summary>
public class StartupExtensionsTests
{
[TestFixture]
public class AddGoogleMapsTests : UnitTests
{
private const string APIKEY = "APIKey";

private IServiceCollection services;
private IConfiguration configuration;


[SetUp]
public void Setup()
{
services = new ServiceCollection();
}


[Test]
public void AddGoogleMaps_ValidOptions_SetupOptionsAddHttpClient()
{
configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ $"{GoogleMapsConstants.SECTION_KEY}:{nameof(GoogleMapsOptions.APIKey)}", APIKEY }
})
.Build();

services.AddGoogleMaps(configuration);

var serviceProvider = services.BuildServiceProvider();
var googleMapsOptions = serviceProvider.GetRequiredService<IOptions<GoogleMapsOptions>>();
var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient(GoogleMapsConstants.CLIENT_NAME);

Assert.Multiple(() =>
{
Assert.That(googleMapsOptions, Is.Not.Null);
Assert.That(googleMapsOptions.Value.APIKey, Is.EqualTo(APIKEY));
Assert.That(httpClient.DefaultRequestHeaders.Accept, Is.InstanceOf<HttpHeaderValueCollection<MediaTypeWithQualityHeaderValue>>());
Assert.That(httpClient.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/json")));
});
}


[TestCase(null, TestName = "AddGoogleMaps_APIKeyNull_ThrowsException")]
[TestCase("", TestName = "AddGoogleMaps_APIKeyEmpty_ThrowsException")]
public void AddGoogleMaps(string apiKey)
{
configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ $"{GoogleMapsConstants.SECTION_KEY}:{nameof(GoogleMapsOptions.APIKey)}", apiKey },
})
.Build();

Assert.That(() => services.AddGoogleMaps(configuration), Throws.InvalidOperationException);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Kentico.Xperience.Core.Tests" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit.ConsoleRunner" />
<PackageReference Include="NUnit3TestAdapter" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Kentico.Xperience.GoogleMaps\Kentico.Xperience.GoogleMaps.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="SonarAnalyzer.CSharp">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
229 changes: 229 additions & 0 deletions Kentico.Xperience.GoogleMaps.Tests/Services/AddressValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
using NSubstitute;
using NUnit.Framework;

namespace Kentico.Xperience.GoogleMaps.Tests
{
public class AddressValidatorTests
{
[TestFixture]
public class ValidateTests : AddressValidatorTestsBase
{
[Test]
public async Task Validate_ValidAddress_ReturnsExpectedResult()
{
const string VALID_ADDRESS = "1600 Amphitheatre Parkway, Mountain View, CA";

MockHttpClient(new List<HttpResponseMessage>
{
GetMessage(new GeocodeResponse
{
Status = "OK",
Results = new List<GeocodeResult>
{
new()
{
FormattedAddress = VALID_ADDRESS,
},
},
}),
GetMessage(new AddressValidationResponse
{
Result = new AddressValidationResult
{
Verdict = new AddressValidationVerdict
{
AddressComplete = true,
},
Address = new AddressValidationAddress
{
FormattedAddress = VALID_ADDRESS,
},
},
})
});

var result = await addressValidator.Validate(VALID_ADDRESS);

Assert.Multiple(() =>
{
Assert.That(result.IsValid, Is.True);
Assert.That(result.FormattedAddress, Is.EqualTo(VALID_ADDRESS));
Assert.That(NumberOfRequests, Is.EqualTo(2));

httpClientFactory.Received(2).CreateClient(GoogleMapsConstants.CLIENT_NAME);
});
}


[Test]
public async Task Validate_InvalidAddress_ReturnsExpectedResult()
{
const string INVALID_ADDRESS = "This is invalid address.";

MockHttpClient(new List<HttpResponseMessage>
{
GetMessage(new GeocodeResponse
{
Status = "ZERO_RESULTS",
Results = new List<GeocodeResult>(),
})
});

var result = await addressValidator.Validate(INVALID_ADDRESS);

Assert.Multiple(() =>
{
Assert.That(result.IsValid, Is.False);
Assert.That(NumberOfRequests, Is.EqualTo(1));

httpClientFactory.Received(1).CreateClient(GoogleMapsConstants.CLIENT_NAME);
});
}


[Test]
public async Task Validate_CompanyNamesEnabled_ReturnsExpectedResult()
{
const string COMPANY_NAME = "Rockstar Games";
const string COMPANY_ADDRESS = "622 Broadway, New York, NY 10012, USA";

MockHttpClient(new List<HttpResponseMessage>
{
GetMessage(new GeocodeResponse
{
Status = "OK",
Results = new List<GeocodeResult>
{
new()
{
FormattedAddress = COMPANY_ADDRESS,
},
},
}),
GetMessage(new AddressValidationResponse
{
Result = new AddressValidationResult
{
Verdict = new AddressValidationVerdict
{
AddressComplete = true,
},
Address = new AddressValidationAddress
{
FormattedAddress = COMPANY_ADDRESS,
},
},
})
});

var result = await addressValidator.Validate(COMPANY_NAME);

Assert.Multiple(() =>
{
Assert.That(result.IsValid, Is.True);
Assert.That(result.FormattedAddress, Is.EqualTo(COMPANY_ADDRESS));
Assert.That(NumberOfRequests, Is.EqualTo(2));

httpClientFactory.Received(2).CreateClient(GoogleMapsConstants.CLIENT_NAME);
});
}


[Test]
public async Task Validate_CompanyNamesDisabled_ReturnsExpectedResult()
{
const string COMPANY_NAME = "Rockstar Games";

MockHttpClient(new List<HttpResponseMessage>
{
GetMessage(new AddressValidationResponse
{
Result = new AddressValidationResult
{
Verdict = new AddressValidationVerdict
{
AddressComplete = null,
},
Address = new AddressValidationAddress
{
FormattedAddress = COMPANY_NAME,
},
},
})
});

var result = await addressValidator.Validate(COMPANY_NAME, "US", false);

Assert.Multiple(() =>
{
Assert.That(result.IsValid, Is.False);
Assert.That(NumberOfRequests, Is.EqualTo(1));

httpClientFactory.Received(1).CreateClient(GoogleMapsConstants.CLIENT_NAME);
});
}


[Test]
public async Task Validate_NotCompleteAddress_ReturnsExpectedResult()
{
const string NOT_COMPLETE_ADDRESS = "Baltimore, MD";

MockHttpClient(new List<HttpResponseMessage>
{
GetMessage(new GeocodeResponse
{
Status = "OK",
Results = new List<GeocodeResult>
{
new()
{
FormattedAddress = NOT_COMPLETE_ADDRESS,
},
},
}),
GetMessage(new AddressValidationResponse
{
Result = new AddressValidationResult
{
Verdict = new AddressValidationVerdict
{
AddressComplete = null,
},
Address = new AddressValidationAddress
{
FormattedAddress = NOT_COMPLETE_ADDRESS,
},
},
})
});

var result = await addressValidator.Validate(NOT_COMPLETE_ADDRESS);

Assert.Multiple(() =>
{
Assert.That(result.IsValid, Is.False);
Assert.That(NumberOfRequests, Is.EqualTo(2));

httpClientFactory.Received(2).CreateClient(GoogleMapsConstants.CLIENT_NAME);
});
}


[TestCase(null, TestName = "Validate_NullValue_ThrowsException")]
[TestCase("", TestName = "Validate_EmptyValue_ThrowsException")]
public async Task Validate(string value)
{
var result = await addressValidator.Validate(value);

Assert.Multiple(() =>
{
Assert.That(result.IsValid, Is.False);
Assert.That(NumberOfRequests, Is.EqualTo(0));

httpClientFactory.Received(0).CreateClient(GoogleMapsConstants.CLIENT_NAME);
});
}
}
}
}
Loading
Loading