Skip to content
Draft
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
39 changes: 39 additions & 0 deletions RefactorThis.Application.UnitTest/FakeInvoiceRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using RefactorThis.Application.Interfaces;
using RefactorThis.Domain.Entities;
using System.Collections.Generic;
using System.Linq;

namespace RefactorThis.UnitTests
{
public class FakeInvoiceRepository : IInvoiceRepository
{
private readonly List<Invoice> _invoices = new List<Invoice>();

public void Add(Invoice invoice)
{
_invoices.Add(invoice);
}

public Invoice GetInvoiceByPayment(Payment payment)
{
// For simplicity, return first invoice (or null if none)
return _invoices.FirstOrDefault();
}

public IEnumerable<Invoice> GetAllInvoices()
{
return _invoices;
}

public Invoice GetInvoice(string reference)
{
// Return invoice by reference if needed
return _invoices.FirstOrDefault();
}

public void SaveInvoice(Invoice invoice)
{
// In-memory, nothing needed. Could replace the invoice if needed.
}
}
}
146 changes: 146 additions & 0 deletions RefactorThis.Application.UnitTest/InvoicePaymentProcessorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
using NUnit.Framework;
using RefactorThis.Application.Common;
using RefactorThis.Application.Interfaces;
using RefactorThis.Domain;
using RefactorThis.Domain.Entities;
using RefactorThis.Domain.Enums;
using RefactorThis.Domain.PaymentStrategies;
using RefactorThis.UnitTests;
using System;
using System.Collections.Generic;

namespace RefactorThis.Application.UnitTests
{
[TestFixture]
public class InvoicePaymentProcessorTests
{
private IInvoiceRepository _repo;
private IInvoicePaymentStrategyFactory _strategyFactory;
private InvoiceService _paymentProcessor;


[SetUp]
public void Setup()
{
_repo = new FakeInvoiceRepository();
_strategyFactory = new InvoicePaymentStrategyFactory();
_paymentProcessor = new InvoiceService(_repo, _strategyFactory);
}

[Test]
public void ProcessPayment_Should_ThrowException_When_NoInvoiceFoundForPaymentReference()
{
var payment = new Payment();
var ex = Assert.Throws<InvalidOperationException>(() => _paymentProcessor.ProcessPayment(payment));
Assert.AreEqual("There is no invoice matching this payment", ex.Message);
}

[Test]
public void ProcessPayment_Should_ReturnFailureMessage_When_NoPaymentNeeded()
{
var invoice = CreateInvoice(0, 0, 0);
var payment = new Payment();

var result = ProcessInvoicePayment(invoice, payment);

Assert.AreEqual(PaymentResultMessages.ToMessage(PaymentResultCode.NoPaymentNeeded, invoice), result);
}

[Test]
public void ProcessPayment_Should_ReturnFailureMessage_When_InvoiceAlreadyFullyPaid()
{

var invoice = CreateInvoice(10, 10, 10);
var payment = new Payment();

var result = ProcessInvoicePayment(invoice, payment);
Assert.AreEqual(PaymentResultMessages.ToMessage(PaymentResultCode.InvoiceAlreadyPaid, invoice), result);
}

[Test]
public void ProcessPayment_Should_ReturnFailureMessage_When_PartialPaymentExistsAndAmountPaidExceedsAmountDue()
{
var invoice = CreateInvoice(10, 5, 5);
var payment = new Payment() { Amount = 6 };

var result = ProcessInvoicePayment(invoice, payment);
Assert.AreEqual(PaymentResultMessages.ToMessage(PaymentResultCode.PaymentGreaterThanRemaining, invoice), result);
}

[Test]
public void ProcessPayment_Should_ReturnFailureMessage_When_NoPartialPaymentExistsAndAmountPaidExceedsInvoiceAmount()
{
var invoice = CreateInvoice(5, 0, 0);
var payment = new Payment() { Amount = 6 };

var result = ProcessInvoicePayment(invoice, payment);

Assert.AreEqual(PaymentResultMessages.ToMessage(PaymentResultCode.PaymentGreaterThanInvoiceAmount, invoice), result);
}

[Test]
public void ProcessPayment_Should_ReturnFullyPaidMessage_When_PartialPaymentExistsAndAmountPaidEqualsAmountDue()
{
var invoice = CreateInvoice(10, 5, 5);
var payment = new Payment() { Amount = 5 };

var result = ProcessInvoicePayment(invoice, payment);

Assert.AreEqual(PaymentResultMessages.ToMessage(PaymentResultCode.FullyPaid, invoice), result);
}

[Test]
public void ProcessPayment_Should_ReturnFullyPaidMessage_When_NoPartialPaymentExistsAndAmountPaidEqualsInvoiceAmount()
{
var invoice = CreateInvoice(10, 0, 10);
var payment = new Payment() { Amount = 10 };

var result = ProcessInvoicePayment(invoice, payment);

Assert.AreEqual(PaymentResultMessages.ToMessage(PaymentResultCode.InvoiceAlreadyPaid, invoice), result);
}

[Test]
public void ProcessPayment_Should_ReturnPartiallyPaidMessage_When_PartialPaymentExistsAndAmountPaidIsLessThanAmountDue()
{
var invoice = CreateInvoice(10, 5, 5);
var payment = new Payment() { Amount = 1 };

var result = ProcessInvoicePayment(invoice, payment);
Assert.AreEqual(PaymentResultMessages.ToMessage(PaymentResultCode.PartiallyPaid, invoice), result);
}

[Test]
public void ProcessPayment_Should_ReturnPartiallyPaidMessage_WhenFirstPartialPayment()
{
var invoice = CreateInvoice(10, 0, 0);
var payment = new Payment() { Amount = 1 };

var result = ProcessInvoicePayment(invoice, payment);
Assert.AreEqual(PaymentResultMessages.ToMessage(PaymentResultCode.PartiallyPaid, invoice), result);
}

private Invoice CreateInvoice(decimal amount, decimal amountPaid = 0, params decimal[] payments)
{
var invoice = new Invoice
{
Amount = amount,
AmountPaid = amountPaid,
Payments = new List<Payment>()
};

foreach (var p in payments)
{
invoice.Payments.Add(new Payment { Amount = p });
}

return invoice;
}

private string ProcessInvoicePayment(Invoice invoice, Payment payment)
{
_repo.Add(invoice);
return _paymentProcessor.ProcessPayment(payment);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>

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

<ItemGroup>
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\RefactorThis.Application\RefactorThis.Application.csproj" />
<ProjectReference Include="..\RefactorThis.Domain\RefactorThis.Domain.csproj" />
<ProjectReference Include="..\RefactorThis.Infrastructure\RefactorThis.Infrastructure.csproj" />
</ItemGroup>

</Project>
44 changes: 44 additions & 0 deletions RefactorThis.Application/Common/PaymentResultMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using RefactorThis.Domain.Entities;
using RefactorThis.Domain.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RefactorThis.Application.Common
{
public static class PaymentResultMessages
{
public static string ToMessage(PaymentResultCode code, Invoice invoice)
{
switch (code)
{
case PaymentResultCode.NoPaymentNeeded:
return "No payment needed";

case PaymentResultCode.InvoiceAlreadyPaid:
return "Invoice was already fully paid";

case PaymentResultCode.PaymentGreaterThanRemaining:
return "The payment is greater than the partial amount remaining";

case PaymentResultCode.PaymentGreaterThanInvoiceAmount:
return "The payment is greater than the invoice amount";

case PaymentResultCode.FullyPaid:
return invoice.Payments != null && invoice.Payments.Count > 1
? "Final partial payment received, invoice is now fully paid"
: "Invoice is now fully paid";

case PaymentResultCode.PartiallyPaid:
return invoice.Payments != null && invoice.Payments.Count > 1
? "Another partial payment received, still not fully paid"
: "Invoice is now partially paid";

default:
return "Unknown payment status";
}
}
}
}
16 changes: 16 additions & 0 deletions RefactorThis.Application/Interfaces/IInvoiceRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using RefactorThis.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RefactorThis.Application.Interfaces
{
public interface IInvoiceRepository
{
Invoice GetInvoice(string reference);
void Add(Invoice invoice);
void SaveInvoice(Invoice invoice);
}
}
33 changes: 33 additions & 0 deletions RefactorThis.Application/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("RefactorThis.Application")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RefactorThis.Application")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("666b1ab5-1d20-40f8-a5f5-59bbcd655a58")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
62 changes: 62 additions & 0 deletions RefactorThis.Application/RefactorThis.Application.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{666B1AB5-1D20-40F8-A5F5-59BBCD655A58}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>RefactorThis.Application</RootNamespace>
<AssemblyName>RefactorThis.Application</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Common\PaymentResultMessages.cs" />
<Compile Include="Interfaces\IInvoiceRepository.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\InvoiceService.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RefactorThis.Domain\RefactorThis.Domain.csproj">
<Project>{5310b2fe-e26d-414e-b656-1f74c5a70368}</Project>
<Name>RefactorThis.Domain</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Loading