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

TECH-30764 Refactor the solution. Added Unit Test and fixed issues #14

Merged
merged 2 commits into from
Sep 17, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MyCRM.Lodgement.Core\MyCRM.Lodgement.Common.csproj" />
<ProjectReference Include="..\MyCRM.Lodgement.Sample\MyCRM.Lodgement.Sample.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@

using LMGTech.DotNetLixi;
using LMGTech.DotNetLixi.Models;
using MyCRM.Lodgement.Common.Utilities;

namespace MyCRM.Lodgement.Common.Tests;

using Newtonsoft.Json.Linq;
using System;
using System.IO;
using Xunit;

public class LixiPackageSerializerTests
{


[Fact]
public void Serialize_ShouldSerializeToJson_WhenMediaTypeIsJson_AndCountryIsAustralia()
{
// Arrange
var package = CreateTestPackage();

// Act
var result = LixiPackageSerializer.Serialize(package, LixiCountry.Australia, "application/json");

// Assert
Assert.NotNull(result);
Assert.Contains("\"@CompanyName\":\"Test Company\"", result);
}

[Fact]
public void Serialize_ShouldSerializeToXml_WhenMediaTypeIsXml_AndCountryIsAustralia()
{
// Arrange
var package = CreateTestPackage();

// Act
var result = LixiPackageSerializer.Serialize(package, LixiCountry.Australia, "application/xml");

// Assert
Assert.NotNull(result);
Assert.Contains("CompanyName=\"Test Company\"", result);
}



[Fact]
public void Serialize_ShouldSerializeToXml_WhenMediaTypeIsXml_AndCountryIsNewZealand()
{
// Arrange
var package = CreateTestPackage();

// Act
var result = LixiPackageSerializer.Serialize(package, LixiCountry.NewZealand, "application/xml");

// Assert
Assert.NotNull(result);
Assert.Contains("CompanyName=\"Test Company\"", result);
}

[Fact]
public void Serialize_ShouldThrowNotImplementedException_WhenMediaTypeIsUnsupported()
{
// Arrange
var package = CreateTestPackage();

// Act & Assert
var ex = Assert.Throws<NotImplementedException>(() =>
LixiPackageSerializer.Serialize(package, LixiCountry.Australia, "application/unsupported"));
Assert.Equal("Media Type application/unsupported not supported.", ex.Message);
}

[Fact]
public void Serialize_ShouldThrowArgumentNullException_WhenPackageIsNull()
{
// Act & Assert
Assert.Throws<ArgumentNullException>(() =>
LixiPackageSerializer.Serialize(null, LixiCountry.Australia, "application/json"));
}

[Fact]
public void ObfuscateJson_ShouldObfuscateAllProperties_WhenNoPropertiesToObfuscateSpecified()
{
// Arrange
var package = JObject.FromObject(CreateTestPackage());

// Act
var result = LixiPackageSerializer.ObfuscateJson(package);

// Assert
Assert.NotNull(result);
Assert.DoesNotContain("123456789", result); // BrokerApplicationReferenceNumber should be obfuscated
Assert.DoesNotContain("Test Company", result); // CompanyName should be obfuscated
Assert.DoesNotContain("test@gmail.com", result); // ABN should be obfuscated
Assert.DoesNotContain("0450000111", result); // Phone number should be obfuscated
}

[Fact]
public void ObfuscateJson_ShouldObfuscateOnlySpecifiedProperties()
{
// Arrange
var package = JObject.FromObject(CreateTestPackage());
string[] propertiesToObfuscate = { "CompanyName", "UniqueID" };

// Act
var result = LixiPackageSerializer.ObfuscateJson(package, propertiesToObfuscate);

// Assert
Assert.NotNull(result);
Assert.DoesNotContain("LoanScenario-12345", result); // UniqueID should be obfuscated
Assert.DoesNotContain("Test Company", result); // CompanyName should be obfuscated
}


private Package CreateTestPackage()
{
return new Package
{
// Populate with test data
Content = new PackageContent
{
Application = new Application
{
SalesChannel = new SalesChannel()
{
Company = new SalesChannelCompany()
{
CompanyName = "Test Company",
BusinessNumber = "123456789"
},
LoanWriter = new SalesChannelLoanWriter()
{
Contact = new SalesChannelLoanWriterContact()
{
Email = "test@gmail.com",
Mobile = new PhoneType()
{
Number = "0450000111"
}
}
}
},
UniqueID = "LoanScenario-12345"
}
}
};
}
}
Loading
Loading