diff --git a/src/StubGenerator.Test/CacheManagerTests.cs b/src/StubGenerator.Test/CacheManagerTests.cs index 3b68b2d..9509a31 100644 --- a/src/StubGenerator.Test/CacheManagerTests.cs +++ b/src/StubGenerator.Test/CacheManagerTests.cs @@ -1,6 +1,5 @@ using System.Threading.Tasks; using StubGenerator.Caching; -using StubGenerator.Core; using StubGenerator.Test.Models; using Xunit; @@ -17,13 +16,11 @@ public CacheManagerTests() _stubTypeMemoryCache = new MemoryStubTypeCache(_cacheKeyGenerator); } - - [Fact(DisplayName = "Cache_Key_Generator_Test")] public void CacheKeyGeneratorTest() { var cacheKey = _cacheKeyGenerator.GenerateKey(); - + Assert.NotEmpty(cacheKey); } [Fact(DisplayName = "Should add PropertyInfo Cache Successfully")] diff --git a/src/StubGenerator.Test/StubManagerTests.cs b/src/StubGenerator.Test/StubManagerTests.cs index b756f17..f2e96d0 100644 --- a/src/StubGenerator.Test/StubManagerTests.cs +++ b/src/StubGenerator.Test/StubManagerTests.cs @@ -1,11 +1,9 @@ +using System; using System.Globalization; using System.Linq; -using StubGenerator.Caching; using StubGenerator.Core; -using StubGenerator.Defaults; using StubGenerator.Test.Models; using Xunit; -using System; namespace StubGenerator.Test { @@ -136,7 +134,7 @@ public void Should_Set_Given_Values_While_Generating_Data() var givenDataTime = new DateTime(2017, 1, 1); var givenEnum = EnTestEnum.Option3; var givenInteger = 31; - var generatedStubData = _stubManager.CreateNew(x => { x.DateTimeProperty = givenDataTime; x.EnumProperty = givenEnum; x.IntegerProperty = givenInteger; }); + var generatedStubData = _stubManager.CreateNew(setDefaults: x => { x.DateTimeProperty = givenDataTime; x.EnumProperty = givenEnum; x.IntegerProperty = givenInteger; }); Assert.Equal(givenDataTime, generatedStubData.DateTimeProperty); Assert.Equal(givenEnum, generatedStubData.EnumProperty); Assert.Equal(givenInteger, generatedStubData.IntegerProperty); diff --git a/src/StubMiddleware.Core/Caching/MemoryStubTypeCache.cs b/src/StubMiddleware.Core/Caching/MemoryStubTypeCache.cs index 540f493..8fe94cf 100644 --- a/src/StubMiddleware.Core/Caching/MemoryStubTypeCache.cs +++ b/src/StubMiddleware.Core/Caching/MemoryStubTypeCache.cs @@ -1,49 +1,45 @@ -using System.Collections.Concurrent; -using StubGenerator.Core; +using System; +using System.Collections.Concurrent; using System.Reflection; namespace StubGenerator.Caching { public class MemoryStubTypeCache : IStubTypeCache { - private readonly ConcurrentDictionary Cache; + private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); private readonly IStubTypeCacheKeyGenerator _cacheKeyGenerator; public MemoryStubTypeCache() - :this(new DefaultStubTypeCacheKeyGenerator()) + : this(new DefaultStubTypeCacheKeyGenerator()) { } public MemoryStubTypeCache(IStubTypeCacheKeyGenerator cacheKeyGenerator) { - Cache = new ConcurrentDictionary(); - _cacheKeyGenerator = cacheKeyGenerator; + _cacheKeyGenerator = cacheKeyGenerator ?? throw new ArgumentNullException(nameof(cacheKeyGenerator)); } public PropertyInfo[] Get(T instance) where T : class { string cacheKey = _cacheKeyGenerator.GenerateKey(); - Cache.TryGetValue(cacheKey, out PropertyInfo[] result); + _cache.TryGetValue(cacheKey, out PropertyInfo[] result); return result; } public PropertyInfo[] GetOrAdd(T instance, PropertyInfo[] propertyInfos) where T : class { var cacheKey = _cacheKeyGenerator.GenerateKey(); - return Cache.GetOrAdd(cacheKey, i => { return propertyInfos; }); + return _cache.GetOrAdd(cacheKey, i => { return propertyInfos; }); } public bool Set(T instance, PropertyInfo[] stubTypeItem) where T : class { string cacheKey = _cacheKeyGenerator.GenerateKey(); - return Cache.TryAdd(cacheKey, stubTypeItem); + return _cache.TryAdd(cacheKey, stubTypeItem); } - public void Clear() => Cache.Clear(); + public void Clear() => _cache.Clear(); - public bool IsEmpty() - { - return Cache.Count == 0; - } + public bool IsEmpty() => _cache.Count == 0; } } diff --git a/src/StubMiddleware.Core/Core/FakeDataFactory.cs b/src/StubMiddleware.Core/Core/FakeDataFactory.cs index 34158c7..f07b690 100644 --- a/src/StubMiddleware.Core/Core/FakeDataFactory.cs +++ b/src/StubMiddleware.Core/Core/FakeDataFactory.cs @@ -13,13 +13,13 @@ public class FakeDataFactory : IFakeDataFactory private readonly IConventionMappingProfile _stubDataMappingProfile; public FakeDataFactory() - :this(new DefaultConventionMappingProfile()) + : this(new DefaultConventionMappingProfile()) { } public FakeDataFactory(IConventionMappingProfile stubDataMappingProfile) { - _stubDataMappingProfile = stubDataMappingProfile; + _stubDataMappingProfile = stubDataMappingProfile ?? throw new ArgumentNullException(nameof(stubDataMappingProfile)); } public object ProvideValue(PropertyInfo propertyInfo) @@ -27,7 +27,7 @@ public object ProvideValue(PropertyInfo propertyInfo) if (!propertyInfo.PropertyType.IsSimple()) return null; - var matchingConvetion = _stubDataMappingProfile.Conventions.Where(c => c.Condition(propertyInfo)).FirstOrDefault(); + var matchingConvetion = _stubDataMappingProfile.Conventions.FirstOrDefault(c => c.Condition(propertyInfo)); if (matchingConvetion != null) return matchingConvetion.Generator.Generate(); @@ -35,7 +35,7 @@ public object ProvideValue(PropertyInfo propertyInfo) if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) propertyType = propertyInfo.PropertyType.GetGenericArguments()[0]; - + return GenerateValueByType(propertyType); } @@ -53,6 +53,8 @@ private static object GenerateValueByType(Type propertyType) return new EnumValueGenerator(propertyType).Generate(); else if (propertyType == typeof(Guid)) return new GuidValueGenerator().Generate(); + else if (propertyType == typeof(bool)) + return new BoolValueGenerator().Generate(); else return null; } diff --git a/src/StubMiddleware.Core/Core/FakeDataGenerators/BoolValueGenerator.cs b/src/StubMiddleware.Core/Core/FakeDataGenerators/BoolValueGenerator.cs new file mode 100644 index 0000000..186f782 --- /dev/null +++ b/src/StubMiddleware.Core/Core/FakeDataGenerators/BoolValueGenerator.cs @@ -0,0 +1,17 @@ +using System; + +namespace StubGenerator.Core.FakeDataGenerators +{ + public class BoolValueGenerator : IValueGenerator + { + private readonly Random _random; + public BoolValueGenerator() + { + _random = new Random(); + } + public object Generate() + { + return _random.NextDouble() >= 0.5; + } + } +} diff --git a/src/StubMiddleware.Core/Core/Interfaces/IStubManager.cs b/src/StubMiddleware.Core/Core/Interfaces/IStubManager.cs index 2ac3120..016c5ca 100644 --- a/src/StubMiddleware.Core/Core/Interfaces/IStubManager.cs +++ b/src/StubMiddleware.Core/Core/Interfaces/IStubManager.cs @@ -5,7 +5,7 @@ namespace StubGenerator.Core { public interface IStubManager { - IList CreateListOfSize(int size, Action setDefaults = null) where T : class, new(); - T CreateNew(Action setDefaults = null) where T : class, new(); + IList CreateListOfSize(int size, int subItemSize = 3, Action setDefaults = null) where T : class, new(); + T CreateNew(int subItemSize = 3, Action setDefaults = null) where T : class, new(); } } \ No newline at end of file diff --git a/src/StubMiddleware.Core/Core/StubManager.cs b/src/StubMiddleware.Core/Core/StubManager.cs index cf4b69f..0926ea2 100644 --- a/src/StubMiddleware.Core/Core/StubManager.cs +++ b/src/StubMiddleware.Core/Core/StubManager.cs @@ -3,10 +3,9 @@ using System.Linq; using System.Reflection; using StubGenerator.Caching; -using StubGenerator.Extensions; -using System.Collections; -using StubGenerator.Core.Interfaces; using StubGenerator.Core.FakeDataProvider; +using StubGenerator.Core.Interfaces; +using StubGenerator.Extensions; namespace StubGenerator.Core { @@ -16,9 +15,8 @@ public class StubManager : IStubManager private readonly IStubTypeCache _stubTypeCache; public StubManager(StubManagerOptions stubManagerOptions) - :this(stubManagerOptions, new MemoryStubTypeCache(), new FakeDataFactory()) + : this(stubManagerOptions, new MemoryStubTypeCache(), new FakeDataFactory()) { - } public StubManager(StubManagerOptions stubManagerOptions, IStubTypeCache stubTypeCache, IFakeDataFactory fakeDataFactory) @@ -30,7 +28,7 @@ public StubManager(StubManagerOptions stubManagerOptions, IStubTypeCache stubTyp public StubManagerOptions StubManagerOptions { get; private set; } - public T CreateNew(Action setDefaults = null) where T : class, new() + public T CreateNew(int subItemSize = 3, Action setDefaults = null) where T : class, new() { var instance = new T(); var cachedPropertyInfo = _stubTypeCache.Get(instance); @@ -40,35 +38,35 @@ public StubManager(StubManagerOptions stubManagerOptions, IStubTypeCache stubTyp _stubTypeCache.Set(instance, cachedPropertyInfo); } - FillPropertiesWithFakeData(instance, cachedPropertyInfo); + FillPropertiesWithFakeData(instance, cachedPropertyInfo, subItemSize); setDefaults?.Invoke(instance); return instance; } - public IList CreateListOfSize(int size, Action setDefaults = null) where T : class, new() + public IList CreateListOfSize(int size, int subItemSize = 3, Action setDefaults = null) where T : class, new() { var result = new List(); for (int i = 0; i < size; i++) { - result.Add(CreateNew(setDefaults)); + result.Add(CreateNew(subItemSize, setDefaults)); } return result; } - private void FillPropertiesWithFakeData(TObject obj, PropertyInfo[] propertyInfos) + private void FillPropertiesWithFakeData(TObject obj, PropertyInfo[] propertyInfos, int listItemSize = 3) { foreach (PropertyInfo property in propertyInfos) { - if(property.PropertyType.IsCollectionType()) + if (property.PropertyType.IsCollectionType()) { var collectionTypeInstance = Activator.CreateInstance(property.PropertyType); var complexType = property.PropertyType.GetGenericArguments()[0]; property.SetValue(obj, collectionTypeInstance); - for(var i = 0; i < 3; i++) + for (var i = 0; i < listItemSize; i++) { - var item = Activator.CreateInstance(complexType); + var item = Activator.CreateInstance(complexType); FillPropertiesWithFakeData(item, _stubTypeCache.GetOrAdd(item, property.PropertyType.GetGenericArguments()[0].GetProperties())); collectionTypeInstance.GetType().GetMethod("Add").Invoke(collectionTypeInstance, new[] { item }); } @@ -89,6 +87,5 @@ private void FillPropertiesWithFakeData(TObject obj, PropertyInfo[] pro } } } - } } diff --git a/src/StubMiddleware.Core/Extensions/StubManagerExtensions.cs b/src/StubMiddleware.Core/Extensions/StubManagerExtensions.cs index e00a5e0..574a483 100644 --- a/src/StubMiddleware.Core/Extensions/StubManagerExtensions.cs +++ b/src/StubMiddleware.Core/Extensions/StubManagerExtensions.cs @@ -17,7 +17,7 @@ private static Type LoadType(string typeName) return result; } - public static object InvokeCreateNew(this IStubManager stubManager, string typeName) + public static object InvokeCreateNew(this IStubManager stubManager, string typeName, int subItemSize = 3) { if (stubManager == null) { @@ -29,15 +29,20 @@ public static object InvokeCreateNew(this IStubManager stubManager, string typeN throw new ArgumentException("message", nameof(typeName)); } + if (subItemSize <= 0) + { + throw new ArgumentOutOfRangeException(nameof(subItemSize), "Subitem Size must be positive number!"); + } + var type = LoadType(typeName); MethodInfo method = typeof(IStubManager).GetMethod("CreateNew"); MethodInfo genericMethod = method.MakeGenericMethod(type); - return genericMethod.Invoke(stubManager, null); + return genericMethod.Invoke(stubManager, new object[] { subItemSize, null }); } - public static object InvokeCreateListOfSize(this IStubManager stubManager, string typeName, int size) + public static object InvokeCreateListOfSize(this IStubManager stubManager, string typeName, int size, int subItemSize) { if (stubManager == null) { @@ -49,10 +54,15 @@ public static object InvokeCreateListOfSize(this IStubManager stubManager, strin throw new ArgumentOutOfRangeException(nameof(size), "List Size must be positive number!"); } + if (subItemSize <= 0) + { + throw new ArgumentOutOfRangeException(nameof(subItemSize), "Subitem 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 }); + return genericMethod.Invoke(stubManager, parameters: new object[] { size, subItemSize, null }); } } } diff --git a/src/StubMiddleware.Web.Test/Controllers/ValuesController.cs b/src/StubMiddleware.Web.Test/Controllers/ValuesController.cs index ac13b29..5331f4b 100644 --- a/src/StubMiddleware.Web.Test/Controllers/ValuesController.cs +++ b/src/StubMiddleware.Web.Test/Controllers/ValuesController.cs @@ -12,10 +12,10 @@ 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" }); + var url = Url.Action("get", "stubmiddleware", new { className = $"{testModelNs}, {testModelAsmName}", subItemlistSize = 3, 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 }); + var urlList = Url.Action("list", "stubmiddleware", new { className = $"{testModelNs}, {testModelAsmName}", subItemlistSize = 3, culture = "en-us", listSize = 10 }); result.Links.Add(new LinkInfo() { Href = urlList, Method = "GET", Rel = "self" }); return Ok(result); diff --git a/src/StubMiddleware/StubMiddlewareController.cs b/src/StubMiddleware/StubMiddlewareController.cs index 8b2dc9c..104d70b 100644 --- a/src/StubMiddleware/StubMiddlewareController.cs +++ b/src/StubMiddleware/StubMiddlewareController.cs @@ -16,7 +16,7 @@ public StubMiddlewareController(IStubManager stubManager) => [HttpGet, Route("list")] - public async Task list([FromQuery]string className, [FromQuery]int listSize, [FromQuery]string culture) + public async Task list([FromQuery]string className, [FromQuery]int listSize, [FromQuery]int subItemlistSize, [FromQuery]string culture) { if (!string.IsNullOrWhiteSpace(culture)) { @@ -24,7 +24,7 @@ public async Task list([FromQuery]string className, [FromQuery]in CultureInfo.CurrentCulture = cultureInfo; CultureInfo.CurrentUICulture = cultureInfo; } - var instance = _stubManager.InvokeCreateListOfSize(className, listSize); + var instance = _stubManager.InvokeCreateListOfSize(className, listSize, subItemlistSize == 0 ? 3 : subItemlistSize); if (instance == null) { return NotFound(className); @@ -34,7 +34,7 @@ public async Task list([FromQuery]string className, [FromQuery]in [HttpGet, Route("get")] - public async Task get([FromQuery]string className, [FromQuery]string culture) + public async Task get([FromQuery]string className, [FromQuery]int subItemlistSize, [FromQuery]string culture) { if (!string.IsNullOrWhiteSpace(culture)) { @@ -43,7 +43,7 @@ public async Task get([FromQuery]string className, [FromQuery]str CultureInfo.CurrentUICulture = cultureInfo; } - var instance = _stubManager.InvokeCreateNew(className); + var instance = _stubManager.InvokeCreateNew(className, subItemlistSize == 0 ? 3 : subItemlistSize); if (instance == null) { return NotFound(className);