Skip to content

Commit

Permalink
TECH-30764 Refactor the solution. Added Unit Test and fixed issues
Browse files Browse the repository at this point in the history
  • Loading branch information
AliArmanLMG committed Sep 16, 2024
1 parent 9ac4f81 commit 593cf4f
Show file tree
Hide file tree
Showing 22 changed files with 3,616 additions and 1,664 deletions.
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

0 comments on commit 593cf4f

Please sign in to comment.