From 8d59c6e85a888ae7f89f99d6d71114fb6a5667bd Mon Sep 17 00:00:00 2001 From: Fabricio Date: Mon, 5 Apr 2021 20:52:58 -0300 Subject: [PATCH] Initial commit --- .gitignore | 362 ++++++++++++++++++ .../InterestValidatorTests.cs | 127 ++++++ .../MyBusinessLogic.Tests.csproj | 20 + MyBusinessLogic/Interest.cs | 10 + MyBusinessLogic/InterestArgs.cs | 16 + MyBusinessLogic/InterestValidator.cs | 56 +++ MyBusinessLogic/InterestValidatorResult.cs | 23 ++ MyBusinessLogic/MyBusinessLogic.csproj | 8 + SafeGateway.sln | 31 ++ 9 files changed, 653 insertions(+) create mode 100644 .gitignore create mode 100644 MyBusinessLogic.Tests/InterestValidatorTests.cs create mode 100644 MyBusinessLogic.Tests/MyBusinessLogic.Tests.csproj create mode 100644 MyBusinessLogic/Interest.cs create mode 100644 MyBusinessLogic/InterestArgs.cs create mode 100644 MyBusinessLogic/InterestValidator.cs create mode 100644 MyBusinessLogic/InterestValidatorResult.cs create mode 100644 MyBusinessLogic/MyBusinessLogic.csproj create mode 100644 SafeGateway.sln diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a8542d --- /dev/null +++ b/.gitignore @@ -0,0 +1,362 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd \ No newline at end of file diff --git a/MyBusinessLogic.Tests/InterestValidatorTests.cs b/MyBusinessLogic.Tests/InterestValidatorTests.cs new file mode 100644 index 0000000..7cb91f0 --- /dev/null +++ b/MyBusinessLogic.Tests/InterestValidatorTests.cs @@ -0,0 +1,127 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace MyBusinessLogic.Tests +{ + [TestClass] + public class InterestValidatorTests + { + [TestMethod] + public void ValidRawArgs_Validate_ReturnsNoErrorAndValidArgs() + { + // Arrange + var validator = new InterestValidator(); + var args = new InterestArgs() + { + Principal = 15m, + Rate = .5m , + PeriodInYears = 3, + }; + + // Act + var result = validator.Validate(args); + + // Assert + Assert.AreEqual(15m, result.ValidArgs.Principal); + Assert.AreEqual(.5m, result.ValidArgs.Rate); + Assert.AreEqual(3, result.ValidArgs.PeriodInYears); + Assert.AreEqual(0, result.Errors.Count); + } + + [TestMethod] + public void NullValues_Validate_ReturnsError() + { + // Arrange + var validator = new InterestValidator(); + var args = new InterestArgs() + { + Principal = null, + Rate = null, + PeriodInYears = null, + }; + + // Act + var result = validator.Validate(args); + + // Assert + Assert.IsNull(result.ValidArgs); + Assert.AreEqual(3, result.Errors.Count); + + var errorMsg1 = result.Errors[0].ToLower(); + Assert.IsTrue(errorMsg1.Contains("principal")); + Assert.IsTrue(errorMsg1.Contains("null")); + + var errorMsg2 = result.Errors[1].ToLower(); + Assert.IsTrue(errorMsg2.Contains("rate")); + Assert.IsTrue(errorMsg2.Contains("null")); + + var errorMsg3 = result.Errors[2].ToLower(); + Assert.IsTrue(errorMsg3.Contains("period")); + Assert.IsTrue(errorMsg3.Contains("null")); + } + + [TestMethod] + public void NegativeValues_Validate_ReturnsError() + { + // Arrange + var validator = new InterestValidator(); + var args = new InterestArgs() + { + Principal = -1, + Rate = -2, + PeriodInYears = -3, + }; + + // Act + var result = validator.Validate(args); + + // Assert + Assert.IsNull(result.ValidArgs); + Assert.AreEqual(3, result.Errors.Count); + + var errorMsg1 = result.Errors[0].ToLower(); + Assert.IsTrue(errorMsg1.Contains("principal")); + Assert.IsTrue(errorMsg1.Contains("zero")); + + var errorMsg2 = result.Errors[1].ToLower(); + Assert.IsTrue(errorMsg2.Contains("rate")); + Assert.IsTrue(errorMsg2.Contains("zero")); + + var errorMsg3 = result.Errors[2].ToLower(); + Assert.IsTrue(errorMsg3.Contains("period")); + Assert.IsTrue(errorMsg3.Contains("zero")); + } + + [TestMethod] + public void ZeroValues_Validate_ReturnsError() + { + // Arrange + var validator = new InterestValidator(); + var args = new InterestArgs() + { + Principal = 0, + Rate = 0, + PeriodInYears = 0, + }; + + // Act + var result = validator.Validate(args); + + // Assert + Assert.IsNull(result.ValidArgs); + Assert.AreEqual(3, result.Errors.Count); + + var errorMsg1 = result.Errors[0].ToLower(); + Assert.IsTrue(errorMsg1.Contains("principal")); + Assert.IsTrue(errorMsg1.Contains("zero")); + + var errorMsg2 = result.Errors[1].ToLower(); + Assert.IsTrue(errorMsg2.Contains("rate")); + Assert.IsTrue(errorMsg2.Contains("zero")); + + var errorMsg3 = result.Errors[2].ToLower(); + Assert.IsTrue(errorMsg3.Contains("period")); + Assert.IsTrue(errorMsg3.Contains("zero")); + } + } +} diff --git a/MyBusinessLogic.Tests/MyBusinessLogic.Tests.csproj b/MyBusinessLogic.Tests/MyBusinessLogic.Tests.csproj new file mode 100644 index 0000000..c91c700 --- /dev/null +++ b/MyBusinessLogic.Tests/MyBusinessLogic.Tests.csproj @@ -0,0 +1,20 @@ + + + + net5.0 + + false + + + + + + + + + + + + + + diff --git a/MyBusinessLogic/Interest.cs b/MyBusinessLogic/Interest.cs new file mode 100644 index 0000000..5a4ea95 --- /dev/null +++ b/MyBusinessLogic/Interest.cs @@ -0,0 +1,10 @@ +namespace MyBusinessLogic +{ + public class Interest + { + public decimal Calculate(ValidInterestArgs args) + { + return args.Principal * args.Rate * args.PeriodInYears / 100; + } + } +} diff --git a/MyBusinessLogic/InterestArgs.cs b/MyBusinessLogic/InterestArgs.cs new file mode 100644 index 0000000..e089f31 --- /dev/null +++ b/MyBusinessLogic/InterestArgs.cs @@ -0,0 +1,16 @@ +namespace MyBusinessLogic +{ + public record InterestArgs + { + public decimal? Principal { get; init; } + public decimal? Rate { get; init; } + public int? PeriodInYears { get; init; } + } + + public record ValidInterestArgs + { + public decimal Principal { get; init; } + public decimal Rate { get; init; } + public int PeriodInYears { get; init; } + } +} diff --git a/MyBusinessLogic/InterestValidator.cs b/MyBusinessLogic/InterestValidator.cs new file mode 100644 index 0000000..49f8f45 --- /dev/null +++ b/MyBusinessLogic/InterestValidator.cs @@ -0,0 +1,56 @@ +using System.Collections.Generic; +using System.Linq; + +namespace MyBusinessLogic +{ + public class InterestValidator + { + public InterestValidatorResult Validate(InterestArgs rawArgs) + { + var errors = GetErrors(rawArgs); + if (errors.Any()) + { + return new InterestValidatorResult(errors); + } + + var validArgs = new ValidInterestArgs() + { + Principal = rawArgs.Principal!.Value, + Rate = rawArgs.Rate!.Value, + PeriodInYears = rawArgs.PeriodInYears!.Value, + }; + + return new InterestValidatorResult(validArgs); + } + + private static IEnumerable GetErrors(InterestArgs rawArgs) + { + if (rawArgs.Principal == null) + { + yield return "Principal cannot be null"; + } + else if (rawArgs.Principal <= 0) + { + yield return "Principal must be greater than zero."; + } + + if (rawArgs.Rate == null) + { + yield return "Rate cannot be null"; + } + else if (rawArgs.Rate <= 0) + { + yield return "Rate must be greater than zero."; + } + + if (rawArgs.PeriodInYears == null) + { + yield return "PeriodInYears cannot be null"; + } + else if (rawArgs.PeriodInYears <= 0) + { + yield return "PeriodInYears must be greater than zero."; + } + } + } +} diff --git a/MyBusinessLogic/InterestValidatorResult.cs b/MyBusinessLogic/InterestValidatorResult.cs new file mode 100644 index 0000000..54d1a40 --- /dev/null +++ b/MyBusinessLogic/InterestValidatorResult.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using System.Linq; + +namespace MyBusinessLogic +{ + public record InterestValidatorResult + { + public ValidInterestArgs? ValidArgs { get; } + public List Errors { get; } + + public InterestValidatorResult(ValidInterestArgs validArgs) + { + this.ValidArgs = validArgs; + this.Errors = Enumerable.Empty().ToList(); + } + + public InterestValidatorResult(IEnumerable errors) + { + this.ValidArgs = null; + this.Errors = errors.ToList(); + } + } +} diff --git a/MyBusinessLogic/MyBusinessLogic.csproj b/MyBusinessLogic/MyBusinessLogic.csproj new file mode 100644 index 0000000..2991887 --- /dev/null +++ b/MyBusinessLogic/MyBusinessLogic.csproj @@ -0,0 +1,8 @@ + + + + net5.0 + enable + + + diff --git a/SafeGateway.sln b/SafeGateway.sln new file mode 100644 index 0000000..ea333e2 --- /dev/null +++ b/SafeGateway.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31105.61 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyBusinessLogic", "MyBusinessLogic\MyBusinessLogic.csproj", "{1430F1F9-FB04-4972-81A8-AD146AD8E210}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyBusinessLogic.Tests", "MyBusinessLogic.Tests\MyBusinessLogic.Tests.csproj", "{CA47838A-1AB5-45E2-BD54-56B1994968F8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1430F1F9-FB04-4972-81A8-AD146AD8E210}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1430F1F9-FB04-4972-81A8-AD146AD8E210}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1430F1F9-FB04-4972-81A8-AD146AD8E210}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1430F1F9-FB04-4972-81A8-AD146AD8E210}.Release|Any CPU.Build.0 = Release|Any CPU + {CA47838A-1AB5-45E2-BD54-56B1994968F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CA47838A-1AB5-45E2-BD54-56B1994968F8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CA47838A-1AB5-45E2-BD54-56B1994968F8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CA47838A-1AB5-45E2-BD54-56B1994968F8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C574D6AE-0C8A-4C8D-AB61-56AA79D0E333} + EndGlobalSection +EndGlobal