diff --git a/StubMiddleware.sln b/StubMiddleware.sln index bf16551..c95a4f3 100644 --- a/StubMiddleware.sln +++ b/StubMiddleware.sln @@ -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 @@ -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 diff --git a/src/StubGenerator.Test/Dto/PersonDto.cs b/src/StubGenerator.Test.Models/PersonDto.cs similarity index 81% rename from src/StubGenerator.Test/Dto/PersonDto.cs rename to src/StubGenerator.Test.Models/PersonDto.cs index 0992665..192cf75 100644 --- a/src/StubGenerator.Test/Dto/PersonDto.cs +++ b/src/StubGenerator.Test.Models/PersonDto.cs @@ -1,4 +1,4 @@ -namespace StubGenerator.Test.Dto +namespace StubGenerator.Test.Models { public class PersonDto { diff --git a/src/StubGenerator.Test.Models/RestApiResult.cs b/src/StubGenerator.Test.Models/RestApiResult.cs new file mode 100644 index 0000000..fcd1bc3 --- /dev/null +++ b/src/StubGenerator.Test.Models/RestApiResult.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; + +namespace StubGenerator.Test.Models +{ + public class RestApiResult + { + public RestApiResult() + { + Links = new List(); + } + public List Links { get; set; } + } + + + public class LinkInfo + { + public string Href { get; set; } + public string Rel { get; set; } + public string Method { get; set; } + } +} diff --git a/src/StubGenerator.Test.Models/StubGenerator.Test.Models.csproj b/src/StubGenerator.Test.Models/StubGenerator.Test.Models.csproj new file mode 100644 index 0000000..5766db6 --- /dev/null +++ b/src/StubGenerator.Test.Models/StubGenerator.Test.Models.csproj @@ -0,0 +1,7 @@ + + + + netcoreapp2.0 + + + diff --git a/src/StubGenerator.Test/CacheManagerTests.cs b/src/StubGenerator.Test/CacheManagerTests.cs index a84974e..b9722db 100644 --- a/src/StubGenerator.Test/CacheManagerTests.cs +++ b/src/StubGenerator.Test/CacheManagerTests.cs @@ -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")] diff --git a/src/StubGenerator.Test/StubGenerator.Test.csproj b/src/StubGenerator.Test/StubGenerator.Test.csproj index 0f79e0d..7c49270 100644 --- a/src/StubGenerator.Test/StubGenerator.Test.csproj +++ b/src/StubGenerator.Test/StubGenerator.Test.csproj @@ -14,6 +14,7 @@ + diff --git a/src/StubGenerator.Test/StubManagerTests.cs b/src/StubGenerator.Test/StubManagerTests.cs index 0a64c2e..a37d16b 100644 --- a/src/StubGenerator.Test/StubManagerTests.cs +++ b/src/StubGenerator.Test/StubManagerTests.cs @@ -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 @@ -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")] diff --git a/src/StubMiddleware.Core/Caching/CacheKeyGeneratorBase.cs b/src/StubMiddleware.Core/Caching/CacheKeyGeneratorBase.cs deleted file mode 100644 index 222cd1b..0000000 --- a/src/StubMiddleware.Core/Caching/CacheKeyGeneratorBase.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace StubGenerator.Caching -{ - public abstract class CacheKeyGeneratorBase - { - public abstract string GenerateKey(); - } -} \ No newline at end of file diff --git a/src/StubMiddleware.Core/Caching/DefaultCacheKeyGenerator.cs b/src/StubMiddleware.Core/Caching/DefaultStubTypeCacheKeyGenerator.cs similarity index 59% rename from src/StubMiddleware.Core/Caching/DefaultCacheKeyGenerator.cs rename to src/StubMiddleware.Core/Caching/DefaultStubTypeCacheKeyGenerator.cs index b7f30a3..1a35212 100644 --- a/src/StubMiddleware.Core/Caching/DefaultCacheKeyGenerator.cs +++ b/src/StubMiddleware.Core/Caching/DefaultStubTypeCacheKeyGenerator.cs @@ -1,8 +1,8 @@ namespace StubGenerator.Caching { - public sealed class DefaultCacheKeyGenerator : CacheKeyGeneratorBase + public sealed class DefaultStubTypeCacheKeyGenerator : IStubTypeCacheKeyGenerator { - public override string GenerateKey() + public string GenerateKey() { var refType = typeof(T); return $"{refType.Assembly.GetName().Name}_{refType.FullName}"; diff --git a/src/StubMiddleware.Core/Caching/IStubTypeCacheKeyGenerator.cs b/src/StubMiddleware.Core/Caching/IStubTypeCacheKeyGenerator.cs new file mode 100644 index 0000000..7c6cfea --- /dev/null +++ b/src/StubMiddleware.Core/Caching/IStubTypeCacheKeyGenerator.cs @@ -0,0 +1,7 @@ +namespace StubGenerator.Caching +{ + public interface IStubTypeCacheKeyGenerator + { + string GenerateKey(); + } +} \ No newline at end of file diff --git a/src/StubMiddleware.Core/Caching/StubTypeMemoryCache.cs b/src/StubMiddleware.Core/Caching/StubTypeMemoryCache.cs index 6375fdc..57b43e4 100644 --- a/src/StubMiddleware.Core/Caching/StubTypeMemoryCache.cs +++ b/src/StubMiddleware.Core/Caching/StubTypeMemoryCache.cs @@ -6,8 +6,8 @@ namespace StubGenerator.Caching public class StubTypeMemoryCache : IStubTypeCache { private readonly ConcurrentDictionary Cache; - private readonly CacheKeyGeneratorBase _cacheKeyGenerator; - public StubTypeMemoryCache(CacheKeyGeneratorBase cacheKeyGenerator) + private readonly IStubTypeCacheKeyGenerator _cacheKeyGenerator; + public StubTypeMemoryCache(IStubTypeCacheKeyGenerator cacheKeyGenerator) { Cache = new ConcurrentDictionary(); _cacheKeyGenerator = cacheKeyGenerator; diff --git a/src/StubMiddleware.Core/Core/FakerMapping.cs b/src/StubMiddleware.Core/Core/FakerMapping.cs index 9518b00..3e257b7 100644 --- a/src/StubMiddleware.Core/Core/FakerMapping.cs +++ b/src/StubMiddleware.Core/Core/FakerMapping.cs @@ -5,28 +5,28 @@ namespace StubGenerator.Core { internal class FakerMapping { - private readonly ConcurrentDictionary> _fakerMappings; + private readonly ConcurrentDictionary> _fakerMappings; public FakerMapping() { - _fakerMappings = new ConcurrentDictionary>(); - _fakerMappings.AddOrUpdate(FakeDataType.Email, + _fakerMappings = new ConcurrentDictionary>(); + _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, Func> ArgumentExistingEx() + private static Func, Func> ArgumentExistingEx() { return (key, eval) => { @@ -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 func)) + if (_fakerMappings.TryGetValue(stubDataType, out Func func)) { return func.Invoke(); } else { - throw new NotSupportedException($"The {fakeDataType} type not found!"); + throw new NotSupportedException($"The {stubDataType} type not found!"); } } } diff --git a/src/StubMiddleware.Core/Core/IFakeDataMappingProfile.cs b/src/StubMiddleware.Core/Core/IFakeDataMappingProfile.cs deleted file mode 100644 index 1f96152..0000000 --- a/src/StubMiddleware.Core/Core/IFakeDataMappingProfile.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Collections.Generic; - -namespace StubGenerator.Core -{ - public interface IFakeDataMappingProfile - { - IEnumerable Conventions { get; } - } -} diff --git a/src/StubMiddleware.Core/Core/IFakeDataMap.cs b/src/StubMiddleware.Core/Core/IStubDataMap.cs similarity index 64% rename from src/StubMiddleware.Core/Core/IFakeDataMap.cs rename to src/StubMiddleware.Core/Core/IStubDataMap.cs index 8f2118e..33b2cd2 100644 --- a/src/StubMiddleware.Core/Core/IFakeDataMap.cs +++ b/src/StubMiddleware.Core/Core/IStubDataMap.cs @@ -3,9 +3,9 @@ namespace StubGenerator.Core { - public interface IFakeDataMap + public interface IStubDataMap { Predicate Condition { get; } - FakeDataType FakeDataType { get; } + StubDataType StubDataType { get; } } } diff --git a/src/StubMiddleware.Core/Core/IFakeDataMapOfType.cs b/src/StubMiddleware.Core/Core/IStubDataMapOfType.cs similarity index 74% rename from src/StubMiddleware.Core/Core/IFakeDataMapOfType.cs rename to src/StubMiddleware.Core/Core/IStubDataMapOfType.cs index c640c66..7e0a3cb 100644 --- a/src/StubMiddleware.Core/Core/IFakeDataMapOfType.cs +++ b/src/StubMiddleware.Core/Core/IStubDataMapOfType.cs @@ -4,7 +4,7 @@ /// /// /// Property Type - public interface IFakeDataMap : IFakeDataMap + public interface IStubDataMap : IStubDataMap { } } diff --git a/src/StubMiddleware.Core/Core/IStubDataMappingProfile.cs b/src/StubMiddleware.Core/Core/IStubDataMappingProfile.cs new file mode 100644 index 0000000..515c0ce --- /dev/null +++ b/src/StubMiddleware.Core/Core/IStubDataMappingProfile.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace StubGenerator.Core +{ + public interface IStubDataMappingProfile + { + IEnumerable Conventions { get; } + } +} diff --git a/src/StubMiddleware.Core/Core/IStubManager.cs b/src/StubMiddleware.Core/Core/IStubManager.cs new file mode 100644 index 0000000..e57eabe --- /dev/null +++ b/src/StubMiddleware.Core/Core/IStubManager.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; + +namespace StubGenerator.Core +{ + public interface IStubManager + { + IList CreateListOfSize(int size) where T : class, new(); + T CreateNew() where T : class, new(); + } +} \ No newline at end of file diff --git a/src/StubMiddleware.Core/Core/FakeDataType.cs b/src/StubMiddleware.Core/Core/StubDataType.cs similarity index 96% rename from src/StubMiddleware.Core/Core/FakeDataType.cs rename to src/StubMiddleware.Core/Core/StubDataType.cs index e38a678..e4eae12 100644 --- a/src/StubMiddleware.Core/Core/FakeDataType.cs +++ b/src/StubMiddleware.Core/Core/StubDataType.cs @@ -1,6 +1,6 @@ namespace StubGenerator.Core { - public enum FakeDataType + public enum StubDataType { DomainName, DomainSuffix, diff --git a/src/StubMiddleware.Core/Core/StubManager.cs b/src/StubMiddleware.Core/Core/StubManager.cs index cbc7294..386dc1b 100644 --- a/src/StubMiddleware.Core/Core/StubManager.cs +++ b/src/StubMiddleware.Core/Core/StubManager.cs @@ -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 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 instance, FakeDataType fakeDataType) + internal object HandleKnownType(T instance, StubDataType fakeDataType) { return FakerMapping.Instance.GenerateData(fakeDataType); } @@ -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>(); + var mapping = new List>(); 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(pinfo.Name, convention.FakeDataType)); + mapping.Add(new KeyValuePair(pinfo.Name, convention.StubDataType)); } var typeItem = new StubTypeItem(); typeItem.SetMapping(mapping); diff --git a/src/StubMiddleware.Core/Core/StubManagerExtensions.cs b/src/StubMiddleware.Core/Core/StubManagerExtensions.cs new file mode 100644 index 0000000..e00a5e0 --- /dev/null +++ b/src/StubMiddleware.Core/Core/StubManagerExtensions.cs @@ -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 }); + } + } +} diff --git a/src/StubMiddleware.Core/Core/StubTypeItem.cs b/src/StubMiddleware.Core/Core/StubTypeItem.cs index 92c1b3c..360c152 100644 --- a/src/StubMiddleware.Core/Core/StubTypeItem.cs +++ b/src/StubMiddleware.Core/Core/StubTypeItem.cs @@ -5,10 +5,10 @@ namespace StubGenerator.Core { public sealed class StubTypeItem { - private List> _propertyMapping; - public StubTypeItem() => _propertyMapping = new List>(); + private List> _propertyMapping; + public StubTypeItem() => _propertyMapping = new List>(); - public void SetMapping(List> mapping) + public void SetMapping(List> mapping) { _propertyMapping = mapping ?? throw new ArgumentNullException(nameof(mapping)); } diff --git a/src/StubMiddleware.Core/Defaults/DefaultFakeDataMappingProfile.cs b/src/StubMiddleware.Core/Defaults/DefaultFakeDataMappingProfile.cs deleted file mode 100644 index c62f45e..0000000 --- a/src/StubMiddleware.Core/Defaults/DefaultFakeDataMappingProfile.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections.Generic; -using StubGenerator.Core; - -namespace StubGenerator.Defaults -{ - public class DefaultFakeDataMappingProfile : IFakeDataMappingProfile - { - private readonly List _conventions; - public DefaultFakeDataMappingProfile() - { - _conventions = new List(); - _conventions.Add(new FirstNameDataNamingMap()); - _conventions.Add(new LastNameDataNamingMap()); - _conventions.Add(new EmailFakeDataNamingMap()); - } - - public IEnumerable Conventions => _conventions; - } -} diff --git a/src/StubMiddleware.Core/Defaults/DefaultStubDataMappingProfile.cs b/src/StubMiddleware.Core/Defaults/DefaultStubDataMappingProfile.cs new file mode 100644 index 0000000..376b66b --- /dev/null +++ b/src/StubMiddleware.Core/Defaults/DefaultStubDataMappingProfile.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; +using StubGenerator.Core; + +namespace StubGenerator.Defaults +{ + public class DefaultStubDataMappingProfile : IStubDataMappingProfile + { + private readonly List _conventions; + public DefaultStubDataMappingProfile() + { + _conventions = new List(); + _conventions.Add(new FirstNameStubDataMap()); + _conventions.Add(new LastNameStubDataMap()); + _conventions.Add(new EmailStubDataMap()); + } + + public IEnumerable Conventions => _conventions; + } +} diff --git a/src/StubMiddleware.Core/Defaults/EmailFakeDataNamingMap.cs b/src/StubMiddleware.Core/Defaults/EmailStubDataMap.cs similarity index 62% rename from src/StubMiddleware.Core/Defaults/EmailFakeDataNamingMap.cs rename to src/StubMiddleware.Core/Defaults/EmailStubDataMap.cs index a019496..1484991 100644 --- a/src/StubMiddleware.Core/Defaults/EmailFakeDataNamingMap.cs +++ b/src/StubMiddleware.Core/Defaults/EmailStubDataMap.cs @@ -4,10 +4,10 @@ namespace StubGenerator.Defaults { - public class EmailFakeDataNamingMap : IFakeDataMap + public class EmailStubDataMap : IStubDataMap { public Predicate Condition => w => w.Name.Contains("mail"); - public FakeDataType FakeDataType { get => FakeDataType.Email; } + public StubDataType StubDataType { get => StubDataType.Email; } } } diff --git a/src/StubMiddleware.Core/Defaults/FirstNameDataNamingMap.cs b/src/StubMiddleware.Core/Defaults/FirstNameStubDataMap.cs similarity index 68% rename from src/StubMiddleware.Core/Defaults/FirstNameDataNamingMap.cs rename to src/StubMiddleware.Core/Defaults/FirstNameStubDataMap.cs index d510bd8..a46130e 100644 --- a/src/StubMiddleware.Core/Defaults/FirstNameDataNamingMap.cs +++ b/src/StubMiddleware.Core/Defaults/FirstNameStubDataMap.cs @@ -4,10 +4,10 @@ namespace StubGenerator.Defaults { - public class FirstNameDataNamingMap : IFakeDataMap + public class FirstNameStubDataMap : IStubDataMap { public Predicate Condition => w => w.Name.ToLowerInvariant().Contains("name") && w.Name.ToLowerInvariant().Contains("first"); - public FakeDataType FakeDataType { get => FakeDataType.FirstName; } + public StubDataType StubDataType { get => StubDataType.FirstName; } } } diff --git a/src/StubMiddleware.Core/Defaults/LastNameDataNamingMap.cs b/src/StubMiddleware.Core/Defaults/LastNameStubDataMap.cs similarity index 68% rename from src/StubMiddleware.Core/Defaults/LastNameDataNamingMap.cs rename to src/StubMiddleware.Core/Defaults/LastNameStubDataMap.cs index 09c8e7e..67cc783 100644 --- a/src/StubMiddleware.Core/Defaults/LastNameDataNamingMap.cs +++ b/src/StubMiddleware.Core/Defaults/LastNameStubDataMap.cs @@ -4,10 +4,10 @@ namespace StubGenerator.Defaults { - public class LastNameDataNamingMap : IFakeDataMap + public class LastNameStubDataMap : IStubDataMap { public Predicate Condition => w => w.Name.ToLowerInvariant().Contains("name") && w.Name.ToLowerInvariant().Contains("last"); - public FakeDataType FakeDataType { get => FakeDataType.LastName; } + public StubDataType StubDataType { get => StubDataType.LastName; } } } diff --git a/src/StubMiddleware.Web.Test/Controllers/ValuesController.cs b/src/StubMiddleware.Web.Test/Controllers/ValuesController.cs new file mode 100644 index 0000000..ac13b29 --- /dev/null +++ b/src/StubMiddleware.Web.Test/Controllers/ValuesController.cs @@ -0,0 +1,24 @@ +using Microsoft.AspNetCore.Mvc; +using StubGenerator.Test.Models; + +namespace StubMiddleware.Web.Test.Controllers +{ + [Route("api/[controller]")] + public class ValuesController : Controller + { + [HttpGet] + public IActionResult Get() + { + var testModelNs = typeof(PersonDto).FullName; + var testModelAsmName = typeof(PersonDto).Assembly.GetName().Name; + var result = new RestApiResult(); + var url = Url.Action("get", "stubmiddleware", new { className = $"{testModelNs}, {testModelAsmName}", culture = "en-us" }); + result.Links.Add(new LinkInfo() { Href = url, Method = "GET", Rel = "self" }); + + var urlList = Url.Action("list", "stubmiddleware", new { className = $"{testModelNs}, {testModelAsmName}", culture = "en-us", listSize = 10 }); + result.Links.Add(new LinkInfo() { Href = urlList, Method = "GET", Rel = "self" }); + + return Ok(result); + } + } +} diff --git a/src/StubMiddleware.Web.Test/Program.cs b/src/StubMiddleware.Web.Test/Program.cs new file mode 100644 index 0000000..5c0bc84 --- /dev/null +++ b/src/StubMiddleware.Web.Test/Program.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; + +namespace StubMiddleware.Web.Test +{ + public class Program + { + public static void Main(string[] args) + { + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseKestrel() + .UseStartup() + .Build(); + } +} diff --git a/src/StubMiddleware.Web.Test/Startup.cs b/src/StubMiddleware.Web.Test/Startup.cs new file mode 100644 index 0000000..df26ad6 --- /dev/null +++ b/src/StubMiddleware.Web.Test/Startup.cs @@ -0,0 +1,34 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace StubMiddleware.Web.Test +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddMvc(); + services.AddStubMiddlewareWithDefaults(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + app.UseMvc(); + } + } +} diff --git a/src/StubMiddleware.Web.Test/StubMiddleware.Web.Test.csproj b/src/StubMiddleware.Web.Test/StubMiddleware.Web.Test.csproj new file mode 100644 index 0000000..dbd4f19 --- /dev/null +++ b/src/StubMiddleware.Web.Test/StubMiddleware.Web.Test.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp2.0 + + + + + + + + + + + + + + + + diff --git a/src/StubMiddleware.Web.Test/appsettings.Development.json b/src/StubMiddleware.Web.Test/appsettings.Development.json new file mode 100644 index 0000000..fa8ce71 --- /dev/null +++ b/src/StubMiddleware.Web.Test/appsettings.Development.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "IncludeScopes": false, + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/src/StubMiddleware.Web.Test/appsettings.json b/src/StubMiddleware.Web.Test/appsettings.json new file mode 100644 index 0000000..26bb0ac --- /dev/null +++ b/src/StubMiddleware.Web.Test/appsettings.json @@ -0,0 +1,15 @@ +{ + "Logging": { + "IncludeScopes": false, + "Debug": { + "LogLevel": { + "Default": "Warning" + } + }, + "Console": { + "LogLevel": { + "Default": "Warning" + } + } + } +} diff --git a/src/StubMiddleware/StubMiddleware.cs b/src/StubMiddleware/StubMiddleware.cs deleted file mode 100644 index 34485d8..0000000 --- a/src/StubMiddleware/StubMiddleware.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; - -namespace StubMiddleware -{ - public class StubMiddleware - { - private readonly RequestDelegate _next; - - public StubMiddleware(RequestDelegate next) - { - _next = next; - } - - public Task Invoke(HttpContext httpContext) - { - return _next(httpContext); - } - } -} diff --git a/src/StubMiddleware/StubMiddlewareController.cs b/src/StubMiddleware/StubMiddlewareController.cs new file mode 100644 index 0000000..8b2dc9c --- /dev/null +++ b/src/StubMiddleware/StubMiddlewareController.cs @@ -0,0 +1,54 @@ +using System; +using System.Globalization; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using StubGenerator.Core; + +namespace StubMiddleware +{ + [Route("StubMiddleware")] + public class StubMiddlewareController : Controller + { + private readonly IStubManager _stubManager; + + public StubMiddlewareController(IStubManager stubManager) => + _stubManager = stubManager ?? throw new ArgumentNullException(nameof(stubManager)); + + + [HttpGet, Route("list")] + public async Task list([FromQuery]string className, [FromQuery]int listSize, [FromQuery]string culture) + { + if (!string.IsNullOrWhiteSpace(culture)) + { + var cultureInfo = new CultureInfo(culture); + CultureInfo.CurrentCulture = cultureInfo; + CultureInfo.CurrentUICulture = cultureInfo; + } + var instance = _stubManager.InvokeCreateListOfSize(className, listSize); + if (instance == null) + { + return NotFound(className); + } + return Ok(instance); + } + + + [HttpGet, Route("get")] + public async Task get([FromQuery]string className, [FromQuery]string culture) + { + if (!string.IsNullOrWhiteSpace(culture)) + { + var cultureInfo = new CultureInfo(culture); + CultureInfo.CurrentCulture = cultureInfo; + CultureInfo.CurrentUICulture = cultureInfo; + } + + var instance = _stubManager.InvokeCreateNew(className); + if (instance == null) + { + return NotFound(className); + } + return Ok(instance); + } + } +} diff --git a/src/StubMiddleware/StubMiddlewareExtensions.cs b/src/StubMiddleware/StubMiddlewareExtensions.cs index 92ccd7d..01beec2 100644 --- a/src/StubMiddleware/StubMiddlewareExtensions.cs +++ b/src/StubMiddleware/StubMiddlewareExtensions.cs @@ -1,19 +1,57 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using StubGenerator.Caching; +using StubGenerator.Core; +using StubGenerator.Defaults; namespace StubMiddleware { public static class StubMiddlewareExtensions { - public static IApplicationBuilder UseStubMiddleware(this IApplicationBuilder app, StubMiddlewareOptions options) + public static void AddStubMiddlewareWithDefaults(this IServiceCollection services) { - return app.Map(options.Route, mapApp => - { - mapApp.Run(async context => - { - await context.Response.WriteAsync("Hello World!"); - }); - }); + var stubMiddlewareOptions = new StubMiddlewareOptions(); + services.AddSingleton(stubMiddlewareOptions); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + } + + public static void AddStubMiddleware(this IServiceCollection services, StubMiddlewareOptions stubMiddlewareOptions) + where TStubDataMappingProfile : class, IStubDataMappingProfile + where TCacheKeyGenerator : class, IStubTypeCacheKeyGenerator + where TStubTypeCache : class, IStubTypeCache + { + services.AddSingleton(stubMiddlewareOptions); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + } + + public static void AddStubMiddleware(this IServiceCollection services, StubMiddlewareOptions stubMiddlewareOptions) + where TStubDataMappingProfile : class, IStubDataMappingProfile + { + services.AddSingleton(stubMiddlewareOptions); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + } + + public static void AddStubMiddleware(this IServiceCollection services, StubMiddlewareOptions stubMiddlewareOptions) + where TStubDataMappingProfile : class, IStubDataMappingProfile + where TStubTypeCacheKeyGenerator : class, IStubTypeCacheKeyGenerator + { + services.AddSingleton(stubMiddlewareOptions); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); } } } diff --git a/src/StubMiddleware/StubMiddlewareOptions.cs b/src/StubMiddleware/StubMiddlewareOptions.cs index 1def919..ca1b3de 100644 --- a/src/StubMiddleware/StubMiddlewareOptions.cs +++ b/src/StubMiddleware/StubMiddlewareOptions.cs @@ -4,6 +4,6 @@ namespace StubMiddleware { public sealed class StubMiddlewareOptions : StubManagerOptions { - public string Route { get; set; } = "/stubgenerator"; + public string Route { get; set; } = ""; } }