Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
MCKanpolat committed Feb 26, 2018
1 parent 4e31537 commit 234541e
Show file tree
Hide file tree
Showing 36 changed files with 417 additions and 117 deletions.
16 changes: 14 additions & 2 deletions StubMiddleware.sln
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2027
VisualStudioVersion = 15.0.27130.2036
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubMiddleware", "src\StubMiddleware\StubMiddleware.csproj", "{4253CF0A-AEFA-4614-8744-4A0022C88A02}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubGenerator", "src\StubMiddleware.Core\StubGenerator.csproj", "{62328D05-B62C-43BB-8A18-BD4C40ECEA02}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubGenerator.Test", "src\StubGenerator.Test\StubGenerator.Test.csproj", "{2407E03B-1076-46CE-B4F1-02724D730933}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubGenerator.Test", "src\StubGenerator.Test\StubGenerator.Test.csproj", "{2407E03B-1076-46CE-B4F1-02724D730933}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubMiddleware.Web.Test", "src\StubMiddleware.Web.Test\StubMiddleware.Web.Test.csproj", "{317167C4-04C9-484B-BE73-299C69DEDB2D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubGenerator.Test.Models", "src\StubGenerator.Test.Models\StubGenerator.Test.Models.csproj", "{8E7F96E9-CB7C-4E2A-BD35-A664910DD0B1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -27,6 +31,14 @@ Global
{2407E03B-1076-46CE-B4F1-02724D730933}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2407E03B-1076-46CE-B4F1-02724D730933}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2407E03B-1076-46CE-B4F1-02724D730933}.Release|Any CPU.Build.0 = Release|Any CPU
{317167C4-04C9-484B-BE73-299C69DEDB2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{317167C4-04C9-484B-BE73-299C69DEDB2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{317167C4-04C9-484B-BE73-299C69DEDB2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{317167C4-04C9-484B-BE73-299C69DEDB2D}.Release|Any CPU.Build.0 = Release|Any CPU
{8E7F96E9-CB7C-4E2A-BD35-A664910DD0B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E7F96E9-CB7C-4E2A-BD35-A664910DD0B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E7F96E9-CB7C-4E2A-BD35-A664910DD0B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E7F96E9-CB7C-4E2A-BD35-A664910DD0B1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace StubGenerator.Test.Dto
namespace StubGenerator.Test.Models
{
public class PersonDto
{
Expand Down
21 changes: 21 additions & 0 deletions src/StubGenerator.Test.Models/RestApiResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;

namespace StubGenerator.Test.Models
{
public class RestApiResult
{
public RestApiResult()
{
Links = new List<LinkInfo>();
}
public List<LinkInfo> Links { get; set; }
}


public class LinkInfo
{
public string Href { get; set; }
public string Rel { get; set; }
public string Method { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

</Project>
10 changes: 5 additions & 5 deletions src/StubGenerator.Test/CacheManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
using StubGenerator.Caching;
using StubGenerator.Core;
using StubGenerator.Defaults;
using StubGenerator.Test.Dto;
using StubGenerator.Test.Models;
using Xunit;

namespace StubGenerator.Test
{
public class CacheManagerTests
{
private readonly CacheKeyGeneratorBase _cacheKeyGenerator;
private readonly IStubTypeCacheKeyGenerator _cacheKeyGenerator;
private readonly StubTypeCacheManager _stubTypeCacheManager;
private readonly IStubTypeCache _stubTypeMemoryCache;
private readonly StubManager _stubManager;
private readonly IStubManager _stubManager;

public CacheManagerTests()
{
_cacheKeyGenerator = new DefaultCacheKeyGenerator();
_cacheKeyGenerator = new DefaultStubTypeCacheKeyGenerator();
_stubTypeMemoryCache = new StubTypeMemoryCache(_cacheKeyGenerator);
_stubTypeCacheManager = new StubTypeCacheManager(_stubTypeMemoryCache);
var stubManagerOptions = new StubManagerOptions() { AutoGenerateUnknown = true, AutoResolveByNaming = true };
_stubManager = new StubManager(stubManagerOptions, _stubTypeCacheManager, new DefaultFakeDataMappingProfile());
_stubManager = new StubManager(stubManagerOptions, _stubTypeCacheManager, new DefaultStubDataMappingProfile());
}

[Fact(DisplayName = "Cache_Key_Generator_Test")]
Expand Down
1 change: 1 addition & 0 deletions src/StubGenerator.Test/StubGenerator.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\StubGenerator.Test.Models\StubGenerator.Test.Models.csproj" />
<ProjectReference Include="..\StubMiddleware.Core\StubGenerator.csproj" />
</ItemGroup>

Expand Down
6 changes: 3 additions & 3 deletions src/StubGenerator.Test/StubManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using StubGenerator.Caching;
using StubGenerator.Core;
using StubGenerator.Defaults;
using StubGenerator.Test.Dto;
using StubGenerator.Test.Models;
using Xunit;

namespace StubGenerator.Test
Expand All @@ -15,8 +15,8 @@ public StubManagerTests()
{
var stubManagerOptions = new StubManagerOptions() { AutoGenerateUnknown = true, AutoResolveByNaming = true };
_stubManager = new StubManager(stubManagerOptions,
new StubTypeCacheManager(new StubTypeMemoryCache(new DefaultCacheKeyGenerator())),
new DefaultFakeDataMappingProfile());
new StubTypeCacheManager(new StubTypeMemoryCache(new DefaultStubTypeCacheKeyGenerator())),
new DefaultStubDataMappingProfile());
}

[Fact(DisplayName = "Mapping_Check_By_Naming_Default_Conventions")]
Expand Down
7 changes: 0 additions & 7 deletions src/StubMiddleware.Core/Caching/CacheKeyGeneratorBase.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace StubGenerator.Caching
{
public sealed class DefaultCacheKeyGenerator : CacheKeyGeneratorBase
public sealed class DefaultStubTypeCacheKeyGenerator : IStubTypeCacheKeyGenerator
{
public override string GenerateKey<T>()
public string GenerateKey<T>()
{
var refType = typeof(T);
return $"{refType.Assembly.GetName().Name}_{refType.FullName}";
Expand Down
7 changes: 7 additions & 0 deletions src/StubMiddleware.Core/Caching/IStubTypeCacheKeyGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace StubGenerator.Caching
{
public interface IStubTypeCacheKeyGenerator
{
string GenerateKey<T>();
}
}
4 changes: 2 additions & 2 deletions src/StubMiddleware.Core/Caching/StubTypeMemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace StubGenerator.Caching
public class StubTypeMemoryCache : IStubTypeCache
{
private readonly ConcurrentDictionary<string, StubTypeItem> Cache;
private readonly CacheKeyGeneratorBase _cacheKeyGenerator;
public StubTypeMemoryCache(CacheKeyGeneratorBase cacheKeyGenerator)
private readonly IStubTypeCacheKeyGenerator _cacheKeyGenerator;
public StubTypeMemoryCache(IStubTypeCacheKeyGenerator cacheKeyGenerator)
{
Cache = new ConcurrentDictionary<string, StubTypeItem>();
_cacheKeyGenerator = cacheKeyGenerator;
Expand Down
20 changes: 10 additions & 10 deletions src/StubMiddleware.Core/Core/FakerMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ namespace StubGenerator.Core
{
internal class FakerMapping
{
private readonly ConcurrentDictionary<FakeDataType, Func<object>> _fakerMappings;
private readonly ConcurrentDictionary<StubDataType, Func<object>> _fakerMappings;
public FakerMapping()
{
_fakerMappings = new ConcurrentDictionary<FakeDataType, Func<object>>();
_fakerMappings.AddOrUpdate(FakeDataType.Email,
_fakerMappings = new ConcurrentDictionary<StubDataType, Func<object>>();
_fakerMappings.AddOrUpdate(StubDataType.Email,
() => { return Faker.Internet.Email(); },
ArgumentExistingEx());

_fakerMappings.AddOrUpdate(FakeDataType.FirstName,
_fakerMappings.AddOrUpdate(StubDataType.FirstName,
() => { return Faker.Name.First(); },
ArgumentExistingEx());

_fakerMappings.AddOrUpdate(FakeDataType.LastName,
_fakerMappings.AddOrUpdate(StubDataType.LastName,
() => { return Faker.Name.Last(); },
ArgumentExistingEx());

_fakerMappings.AddOrUpdate(FakeDataType.City,
_fakerMappings.AddOrUpdate(StubDataType.City,
() => { return Faker.Address.City(); },
ArgumentExistingEx());
}

private static Func<FakeDataType, Func<object>, Func<object>> ArgumentExistingEx()
private static Func<StubDataType, Func<object>, Func<object>> ArgumentExistingEx()
{
return (key, eval) =>
{
Expand All @@ -41,15 +41,15 @@ static FakerMapping()
internal static FakerMapping Instance { get; private set; }


public object GenerateData(FakeDataType fakeDataType)
public object GenerateData(StubDataType stubDataType)
{
if (_fakerMappings.TryGetValue(fakeDataType, out Func<object> func))
if (_fakerMappings.TryGetValue(stubDataType, out Func<object> func))
{
return func.Invoke();
}
else
{
throw new NotSupportedException($"The {fakeDataType} type not found!");
throw new NotSupportedException($"The {stubDataType} type not found!");
}
}
}
Expand Down
9 changes: 0 additions & 9 deletions src/StubMiddleware.Core/Core/IFakeDataMappingProfile.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace StubGenerator.Core
{
public interface IFakeDataMap
public interface IStubDataMap
{
Predicate<PropertyInfo> Condition { get; }
FakeDataType FakeDataType { get; }
StubDataType StubDataType { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
///
/// </summary>
/// <typeparam name="T">Property Type</typeparam>
public interface IFakeDataMap<T> : IFakeDataMap
public interface IStubDataMap<T> : IStubDataMap
{
}
}
9 changes: 9 additions & 0 deletions src/StubMiddleware.Core/Core/IStubDataMappingProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace StubGenerator.Core
{
public interface IStubDataMappingProfile
{
IEnumerable<IStubDataMap> Conventions { get; }
}
}
10 changes: 10 additions & 0 deletions src/StubMiddleware.Core/Core/IStubManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace StubGenerator.Core
{
public interface IStubManager
{
IList<T> CreateListOfSize<T>(int size) where T : class, new();
T CreateNew<T>() where T : class, new();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace StubGenerator.Core
{
public enum FakeDataType
public enum StubDataType
{
DomainName,
DomainSuffix,
Expand Down
20 changes: 10 additions & 10 deletions src/StubMiddleware.Core/Core/StubManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@

namespace StubGenerator.Core
{
public class StubManager
public class StubManager : IStubManager
{
public StubManager(StubManagerOptions stubManagerOptions, IStubTypeCacheManager stubTypeCacheManager, IFakeDataMappingProfile fakeDataMappingProfile)
public StubManager(StubManagerOptions stubManagerOptions, IStubTypeCacheManager stubTypeCacheManager, IStubDataMappingProfile stubDataMappingProfile)
{
StubManagerOptions = stubManagerOptions ?? throw new ArgumentNullException(nameof(stubManagerOptions));
StubTypeCacheManager = stubTypeCacheManager ?? throw new ArgumentNullException(nameof(stubTypeCacheManager));
FakeDataMappingProfile = fakeDataMappingProfile ?? throw new ArgumentNullException(nameof(fakeDataMappingProfile));
StubDataMappingProfile = stubDataMappingProfile ?? throw new ArgumentNullException(nameof(stubDataMappingProfile));
}

public IStubTypeCacheManager StubTypeCacheManager { get; private set; }
public IFakeDataMappingProfile FakeDataMappingProfile { get; private set; }
public IStubDataMappingProfile StubDataMappingProfile { get; private set; }
public StubManagerOptions StubManagerOptions { get; private set; }

internal void GenerateData<T>(T instance, PropertyInfo propertyInfo)
{
var convention = FakeDataMappingProfile.Conventions.FirstOrDefault(w => w.Condition.Invoke(propertyInfo));
object generatedData = convention != null ? HandleKnownType(instance, convention.FakeDataType) : HandleUnknownType(propertyInfo);
var convention = StubDataMappingProfile.Conventions.FirstOrDefault(w => w.Condition.Invoke(propertyInfo));
object generatedData = convention != null ? HandleKnownType(instance, convention.StubDataType) : HandleUnknownType(propertyInfo);
propertyInfo.SetValue(instance, generatedData, null);
}

internal object HandleKnownType<T>(T instance, FakeDataType fakeDataType)
internal object HandleKnownType<T>(T instance, StubDataType fakeDataType)
{
return FakerMapping.Instance.GenerateData(fakeDataType);
}
Expand All @@ -48,12 +48,12 @@ internal object HandleUnknownType(PropertyInfo propertyInfo)
if (cachedStub == null)
{
var avaliableProps = instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(w => w.CanWrite);
var mapping = new List<KeyValuePair<string, FakeDataType>>();
var mapping = new List<KeyValuePair<string, StubDataType>>();
foreach (var pinfo in avaliableProps)
{
var convention = FakeDataMappingProfile.Conventions.FirstOrDefault(w => w.Condition.Invoke(pinfo));
var convention = StubDataMappingProfile.Conventions.FirstOrDefault(w => w.Condition.Invoke(pinfo));
if (convention != null)
mapping.Add(new KeyValuePair<string, FakeDataType>(pinfo.Name, convention.FakeDataType));
mapping.Add(new KeyValuePair<string, StubDataType>(pinfo.Name, convention.StubDataType));
}
var typeItem = new StubTypeItem();
typeItem.SetMapping(mapping);
Expand Down
58 changes: 58 additions & 0 deletions src/StubMiddleware.Core/Core/StubManagerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Reflection;

namespace StubGenerator.Core
{
public static class StubManagerExtensions
{
private static Type LoadType(string typeName)
{
if (string.IsNullOrWhiteSpace(typeName))
{
throw new ArgumentException("message", nameof(typeName));
}
var result = Type.GetType(typeName);
if (result == null)
throw new TypeLoadException($"The type '{typeName}' couldn't be loaded");
return result;
}

public static object InvokeCreateNew(this IStubManager stubManager, string typeName)
{
if (stubManager == null)
{
throw new ArgumentNullException(nameof(stubManager));
}

if (string.IsNullOrWhiteSpace(typeName))
{
throw new ArgumentException("message", nameof(typeName));
}

var type = LoadType(typeName);


MethodInfo method = typeof(IStubManager).GetMethod("CreateNew");
MethodInfo genericMethod = method.MakeGenericMethod(type);
return genericMethod.Invoke(stubManager, null);
}

public static object InvokeCreateListOfSize(this IStubManager stubManager, string typeName, int size)
{
if (stubManager == null)
{
throw new ArgumentNullException(nameof(stubManager));
}

if (size <= 0)
{
throw new ArgumentOutOfRangeException(nameof(size), "List Size must be positive number!");
}

var type = LoadType(typeName);
MethodInfo method = typeof(IStubManager).GetMethod("CreateListOfSize");
MethodInfo genericMethod = method.MakeGenericMethod(type);
return genericMethod.Invoke(stubManager, parameters: new object[] { size });
}
}
}
Loading

0 comments on commit 234541e

Please sign in to comment.