Skip to content

Commit

Permalink
basic unit test structure for SimplePolicyPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
igormcoelho committed Nov 3, 2018
1 parent 39f0bde commit d87b47e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 4 deletions.
26 changes: 26 additions & 0 deletions SimplePolicy.UnitTests/SimplePolicy.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>SimplePolicy.UnitTests</AssemblyName>
<RootNamespace>SimplePolicy.UnitTests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="4.19.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SimplePolicy\SimplePolicy.csproj" />
</ItemGroup>

<!--
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
-->
</Project>
34 changes: 34 additions & 0 deletions SimplePolicy.UnitTests/UT_SimplePolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.Extensions.Configuration;
using System.IO;
using System.Reflection;
using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Plugins;


namespace SimplePolicy.UnitTests
{
[TestClass]
public class UT_SimplePolicy
{
SimplePolicyPlugin uut;

[TestInitialize]
public void TestSetup()
{
string path = Path.Combine("Plugins", Assembly.GetExecutingAssembly().GetName().Name);
string relativepath = "/../../../../../config.json";
string fullpath = Path.GetFullPath(path + relativepath);
IConfigurationSection config = new ConfigurationBuilder().AddJsonFile(fullpath).Build().GetSection("PluginConfiguration");
SimplePolicyPlugin.SetConfiguration(config);
uut = new SimplePolicyPlugin();
}

[TestMethod]
public void TestMaxTransactionsPerBlock()
{
uut.GetMaxTransactionsPerBlock().Should().Be(500);
}
}
}
10 changes: 10 additions & 0 deletions SimplePolicy.UnitTests/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"PluginConfiguration": {
"MaxTransactionsPerBlock": 500,
"MaxFreeTransactionsPerBlock": 20,
"BlockedAccounts": {
"Type": "AllowAll",
"List": []
}
}
}
17 changes: 13 additions & 4 deletions SimplePolicy/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ internal class Settings
public int MaxFreeTransactionsPerBlock { get; }
public BlockedAccounts BlockedAccounts { get; }

public static Settings Default { get; }

static Settings()
public static IConfigurationSection config = null;
private static Settings _default = null;
public static Settings Default
{
Default = new Settings(Assembly.GetExecutingAssembly().GetConfiguration());
get
{
if(_default == null)
{
if(config == null)
config = Assembly.GetExecutingAssembly().GetConfiguration();
_default = new Settings(config);
}
return _default;
}
}

public Settings(IConfigurationSection section)
Expand Down
11 changes: 11 additions & 0 deletions SimplePolicy/SimplePolicyPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Configuration;

namespace Neo.Plugins
{
public class SimplePolicyPlugin : Plugin, ILogPlugin, IPolicyPlugin
{
private static string log_dictionary = Path.Combine(AppContext.BaseDirectory, "Logs");

public static void SetConfiguration(IConfigurationSection config)
{
Settings.config = config;
}

public int GetMaxTransactionsPerBlock()
{
return Settings.Default.MaxTransactionsPerBlock;
}

public bool FilterForMemoryPool(Transaction tx)
{
switch (Settings.Default.BlockedAccounts.Type)
Expand Down

0 comments on commit d87b47e

Please sign in to comment.