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

fix(ODataClientFactory) #259

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Liquid.Repository.OData/Liquid.Repository.OData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Copyright>Avanade 2019</Copyright>
<PackageProjectUrl>https://github.com/Avanade/Liquid-Application-Framework</PackageProjectUrl>
<PackageIcon>logo.png</PackageIcon>
<Version>8.0.0-beta-03</Version>
<Version>8.0.0-beta-04</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsPackable>true</IsPackable>
<DebugType>Full</DebugType>
Expand All @@ -31,8 +31,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Liquid.Core" Version="8.0.0-beta-06" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Liquid.Core" Version="8.0.0-beta-07" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Simple.OData.Client" Version="6.0.1" />
</ItemGroup>

Expand Down
9 changes: 5 additions & 4 deletions src/Liquid.Repository.OData/ODataClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
///<inheritdoc/>
public IODataClient CreateODataClientAsync(string entityName)
{
var token = _context.Get("OdataToken").ToString();
var hasToken = _context.current.ContainsKey("OdataToken");
var token = _context.Get("OdataToken")?.ToString();

if (string.IsNullOrEmpty(token))
if (!hasToken || string.IsNullOrEmpty(token))
{
throw new InvalidOperationException("Token is required to perform this operation. The 'OdataToken' variable" +
" must be declared in the LiquidContext service.");
throw new KeyNotFoundException("Token is required to perform this operation. The 'OdataToken' " +
"key was not found in the context.");
}

var settings = _options?.Value?.Settings.FirstOrDefault(x => x.EntityName == entityName);

Check warning on line 36 in src/Liquid.Repository.OData/ODataClientFactory.cs

View workflow job for this annotation

GitHub Actions / call-reusable-build-workflow / build

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

if (settings == null)
throw new ArgumentOutOfRangeException(nameof(entityName));
Expand All @@ -50,7 +51,7 @@
/// <returns></returns>
private static ODataClientSettings GetODataSettings(ODataSettings? settings, string token)
{
var odataSettings = new ODataClientSettings(new Uri(settings.BaseUrl));

Check warning on line 54 in src/Liquid.Repository.OData/ODataClientFactory.cs

View workflow job for this annotation

GitHub Actions / call-reusable-build-workflow / build

Dereference of a possibly null reference.

Check warning on line 54 in src/Liquid.Repository.OData/ODataClientFactory.cs

View workflow job for this annotation

GitHub Actions / call-reusable-build-workflow / build

Dereference of a possibly null reference.

odataSettings.BeforeRequest = (message) =>
{
Expand All @@ -59,11 +60,11 @@

if (!settings.ValidateCert)
{
var handler = new HttpClientHandler();

Check warning on line 63 in src/Liquid.Repository.OData/ODataClientFactory.cs

View workflow job for this annotation

GitHub Actions / call-reusable-build-workflow / build

Remove the unused local variable 'handler'. (https://rules.sonarsource.com/csharp/RSPEC-1481)

odataSettings.OnApplyClientHandler = (handler) =>
{
handler.ServerCertificateCustomValidationCallback +=

Check warning on line 67 in src/Liquid.Repository.OData/ODataClientFactory.cs

View workflow job for this annotation

GitHub Actions / call-reusable-build-workflow / build

Enable server certificate validation on this SSL/TLS connection (https://rules.sonarsource.com/csharp/RSPEC-4830)

Check warning on line 67 in src/Liquid.Repository.OData/ODataClientFactory.cs

View workflow job for this annotation

GitHub Actions / call-reusable-build-workflow / build

Enable server certificate validation on this SSL/TLS connection (https://rules.sonarsource.com/csharp/RSPEC-4830)
(sender, certificate, chain, errors) =>
{
return true;
Expand Down
20 changes: 15 additions & 5 deletions test/Liquid.Repository.OData.Tests/ODataClientFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public ODataClientFactoryTests()

_context = Substitute.For<ILiquidContext>();
_context.Get("OdataToken").Returns("token");
_context.current.ContainsKey("OdataToken").Returns(true);

_sut = new ODataClientFactory(_options, _context);
}
Expand Down Expand Up @@ -80,18 +81,27 @@ public void OdataClientFactory_WhenValidateCertIsTrue_ReturnClient()
[Fact]
public void OdataClientFactory_WhenTokenIsNotSet_ThrowException()
{
_context.Get("OdataToken").Returns("");
var context = Substitute.For<ILiquidContext>();

Assert.Throws<InvalidOperationException>(() => _sut.CreateODataClientAsync("TestEntities"));
context.Get("OdataToken").Returns("");
context.current.ContainsKey("OdataToken").Returns(true);

var sut = new ODataClientFactory(_options, context);

Assert.Throws<KeyNotFoundException>(() => sut.CreateODataClientAsync("TestEntities"));
}

[Fact]
public void OdataClientFactory_WhenTokenIsNull_ThrowException()
public void OdataClientFactory_WhenTokenIsNotSetInContext_ThrowException()
{
var context = Substitute.For<ILiquidContext>();
var context = Substitute.For<ILiquidContext>();

context.Get("OdataToken").Returns(null);
context.current.ContainsKey("OdataToken").Returns(false);

var sut = new ODataClientFactory(_options, context);

Assert.Throws<NullReferenceException>(() => sut.CreateODataClientAsync("TestEntities"));
Assert.Throws<KeyNotFoundException>(() => sut.CreateODataClientAsync("TestEntities"));
}

[Fact]
Expand Down
Loading