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

Myfinal #4

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 8 additions & 5 deletions ArdalisRating.Tests/ArdalisRating.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>

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

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
57 changes: 57 additions & 0 deletions ArdalisRating.Tests/JsonPolicySerializerGetPolicyFromJsonString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Xunit;


namespace ArdalisRating.Tests
{
public class JsonPolicySerializerGetPolicyFromJsonString
{
[Fact]
public void ReturnsDefaultPolicyFromEmptyJsonString()
{
var inputJson = "{}";
var serializer = new JsonPolicySerializer();

var result = serializer.GetPolicyFromJsonString(inputJson);

var policy = new Policy();
AssertPoliciesEqual(result, policy);
}

[Fact]
public void ReturnsSimpleAutoPolicyFromValidJsonString()
{
var inputJson = @"{
""type"": ""Auto"",
""make"": ""BMW""
}
";
var serializer = new JsonPolicySerializer();

var result = serializer.GetPolicyFromJsonString(inputJson);

var policy = new Policy
{
Type = "Auto",
Make = "BMW"
};
AssertPoliciesEqual(result, policy);
}

private static void AssertPoliciesEqual(Policy result, Policy policy)
{
Assert.Equal(policy.Address, result.Address);
Assert.Equal(policy.Amount, result.Amount);
Assert.Equal(policy.BondAmount, result.BondAmount);
Assert.Equal(policy.DateOfBirth, result.DateOfBirth);
Assert.Equal(policy.Deductible, result.Deductible);
Assert.Equal(policy.FullName, result.FullName);
Assert.Equal(policy.IsSmoker, result.IsSmoker);
Assert.Equal(policy.Make, result.Make);
Assert.Equal(policy.Miles, result.Miles);
Assert.Equal(policy.Model, result.Model);
Assert.Equal(policy.Type, result.Type);
Assert.Equal(policy.Valuation, result.Valuation);
Assert.Equal(policy.Year, result.Year);
}
}
}
108 changes: 84 additions & 24 deletions ArdalisRating.Tests/RatingEngineRate.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,108 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using Xunit;


namespace ArdalisRating.Tests
{
public class RatingEngineRate
// only for testing purpose
public class FakeLogger : ILogger
{
[Fact]
public void ReturnsRatingOf10000For200000LandPolicy()
{
var policy = new Policy
public List<string> LoggedMessages { get; } = new List<string>();

public void Log(string message)
{
LoggedMessages.Add(message);
}
}

// Only for testing purpose
public class FakePolicySource : IPolicySource
{
Type = PolicyType.Land,
BondAmount = 200000,
Valuation = 200000
};
string json = JsonConvert.SerializeObject(policy);
File.WriteAllText("policy.json", json);

var engine = new RatingEngine();
engine.Rate();
var result = engine.Rating;

Assert.Equal(10000, result);
}
public string PolicyString { get; set; } = "";

public string GetPolicyFromSource()
{
return PolicyString;
}
}

public class RatingEngineRate
{
private readonly RatingEngine _engine;
private readonly ILogger _logger;
private readonly IPolicySource _policySource;
private IRatingContext Context { get; set; }

public RatingEngineRate()
{
_logger = new FakeLogger();
_policySource = new FakePolicySource();
_engine = new RatingEngine(_logger, _policySource);
this.Context = new DefaultRatingContext(_policySource);
}

[Fact]
public void ReturnsRatingOf10000For200000LandPolicy()
{
var policy = new Policy
{
Type = "Land",
BondAmount = 200000,
Valuation = 200000
};
string json = JsonConvert.SerializeObject(policy);
//File.WriteAllText("policy.json", json);
_policySource.PolicyString = json;

//var engine = new RatingEngine();
_engine.Rate();
var result = _engine.Rating;

Assert.Equal(10000, result);
}

[Fact]
public void ReturnsRatingOf0For200000BondOn260000LandPolicy()
{
var policy = new Policy
{
Type = PolicyType.Land,
Type = "Land",
BondAmount = 200000,
Valuation = 260000
};
string json = JsonConvert.SerializeObject(policy);
File.WriteAllText("policy.json", json);

var engine = new RatingEngine();
engine.Rate();
var result = engine.Rating;
// var _engine = new RatingEngine();
_engine.Rate();
var result = _engine.Rating;

Assert.Equal(0, result);
}

[Fact]
public void LogsStartingLoadingAndCompleting()
{
var policy = new Policy()
{
Type = "Land",
BondAmount = 2000000,
Valuation = 260000
};

string json = JsonConvert.SerializeObject(policy);
File.WriteAllText("policy.json", json);

_engine.Rate();
var result = _engine.Rating;


Assert.Contains(_logger.LoggedMessages, m => m == "Starting rate.");
Assert.Contains(_logger.LoggedMessages, m => m == "Loading policy.");
Assert.Contains(_logger.LoggedMessages, m => m == "Rating completed.");

}
}
}
2 changes: 1 addition & 1 deletion ArdalisRating/ArdalisRating.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions ArdalisRating/Core/Interfaces/ILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ArdalisRating
{
public interface ILogger
{
//public List<string> LoggedMessages { get; }
void Log(string message);
}
}
7 changes: 7 additions & 0 deletions ArdalisRating/Core/Interfaces/IPolicySerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ArdalisRating
{
public interface IPolicySerializer
{
public Policy GetPolicyFromJsonString(string jsonString);
}
}
7 changes: 7 additions & 0 deletions ArdalisRating/Core/Interfaces/IPolicySource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ArdalisRating
{
public interface IPolicySource
{
string GetPolicyFromSource();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace ArdalisRating

public class Policy
{
public PolicyType Type { get; set; }
public string Type { get; set; }

#region Life Insurance
public string FullName { get; set; }
public DateTime DateOfBirth { get; set; }
Expand All @@ -20,13 +21,16 @@ public class Policy
public decimal BondAmount { get; set; }
#endregion

#region Flood In Addition To Land
public int ElevationAboveSeaLevelFeet { get; set; }
#endregion

#region Auto
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public int Miles { get; set; }
public decimal Deductible { get; set; }
#endregion

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum PolicyType
{
Life = 0,
Land = 1,
Auto = 2
Auto = 2,
Flood = 3
}
}
43 changes: 43 additions & 0 deletions ArdalisRating/Core/Rater/AutoPolicyRater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;

namespace ArdalisRating
{
public class AutoPolicyRater : Rater
{
//public AutoPolicyRater(IRatingContext context)
/*
public AutoPolicyRater(IRatingUpdater ratingUpdater, ILogger logger)
: base(ratingUpdater, logger)
{

}
*/
public AutoPolicyRater(ILogger logger)
: base(logger) { }


//public override void Rate(Policy policy)
public override decimal Rate(Policy policy)
{
Logger.Log("Rating AUTO policy...");
Logger.Log("Validating policy.");
if (String.IsNullOrEmpty(policy.Make))
{
Logger.Log("Auto policy must specify Make");
return 0m;
}
if (policy.Make == "BMW")
{
if (policy.Deductible < 500)
{
//_ratingUpdater.UpdateRating(1000m);
return 1000m;
}
//_ratingUpdater.UpdateRating(900m);
return 900m;
}

return 0m;
}
}
}
54 changes: 54 additions & 0 deletions ArdalisRating/Core/Rater/FloodPolicyRater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace ArdalisRating
{
public class FloodPolicyRater : Rater
{
/*
public FloodPolicyRater(IRatingUpdater ratingUpdater)
: base(ratingUpdater)
{
}
*/
public FloodPolicyRater(ILogger logger)
: base(logger)
{
}

public override decimal Rate(Policy policy)
{
// The _logger is removed and the Logger used across
//_logger.Log("Rating FLOOD policy...");
Logger.Log("Rating FLOOD policy...");
Logger.Log("Validating policy.");
if (policy.BondAmount == 0 || policy.Valuation == 0)
{
Logger.Log("Flood policy must specify Bond Amount and Valuation.");
return 0m;
}
if (policy.ElevationAboveSeaLevelFeet <= 0)
{
Logger.Log("Flood policy is not available for elevations at or below sea level.");
return 0m;
}
if (policy.BondAmount < 0.8m * policy.Valuation)
{
Logger.Log("Insufficient bond amount.");
return 0m;
}
decimal multiple = 1.0m;
if (policy.ElevationAboveSeaLevelFeet < 100)
{
multiple = 2.0m;
}
else if (policy.ElevationAboveSeaLevelFeet < 500)
{
multiple = 1.5m;
}
else if (policy.ElevationAboveSeaLevelFeet < 1000)
{
multiple = 1.1m;
}
//_ratingUpdater.UpdateRating(policy.BondAmount * 0.05m * multiple);
return (policy.BondAmount * 0.05m * multiple);
}
}
}
Loading