Skip to content

Commit

Permalink
FakeDataProvider is implemented, project structure is updated and new…
Browse files Browse the repository at this point in the history
… conventions are added.
  • Loading branch information
GnktYnk authored and MCKanpolat committed Feb 27, 2018
1 parent 444284d commit 03fad72
Show file tree
Hide file tree
Showing 49 changed files with 665 additions and 379 deletions.
6 changes: 3 additions & 3 deletions StubMiddleware.sln
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2036
VisualStudioVersion = 15.0.27004.2008
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("{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}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubMiddleware.Web.Samples", "src\StubMiddleware.Web.Test\StubMiddleware.Web.Samples.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}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubGenerator.Test.Models", "src\StubGenerator.Test.Models\StubGenerator.Test.Models.csproj", "{8E7F96E9-CB7C-4E2A-BD35-A664910DD0B1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
50 changes: 50 additions & 0 deletions src/StubGenerator.Test.Models/ModelWithComplexTypeProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;

namespace StubGenerator.Test.Models
{
public class ModelWithComplexTypeProperty
{
public string ParentString { get; set; }

public decimal ParentDecimal { get; set; }

public string HomePhone { get; set; }

public string MobilePhone { get; set; }

public int? NullableInteger { get; set; }

public InnerComplexType ComplexType { get; set; }

public List<InnerComplexType> CollectionTypeComplex { get; set; }
}

public class InnerComplexType
{
public int IntegerProperty { get; set; }

public string StringProperty { get; set; }

public DateTime DateTimeProperty { get; set; }

public EnTestEnum EnumProperty { get; set; }

public Guid GuidProperty { get; set; }

public decimal DecimalProperty { get; set; }

public double DoubleProperty { get; set; }

public int? NullableIntegerProperty { get; set; }

public Nullable<int> NullableIntegerProperty2 { get; set; }
}

public enum EnTestEnum
{
Option1,
Option2,
Option3
}
}
39 changes: 20 additions & 19 deletions src/StubGenerator.Test/CacheManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Threading.Tasks;
using StubGenerator.Caching;
using StubGenerator.Core;
using StubGenerator.Defaults;
using StubGenerator.Test.Models;
using Xunit;

Expand All @@ -10,49 +9,51 @@ namespace StubGenerator.Test
public class CacheManagerTests
{
private readonly IStubTypeCacheKeyGenerator _cacheKeyGenerator;
private readonly StubTypeCacheManager _stubTypeCacheManager;
private readonly IStubTypeCache _stubTypeMemoryCache;
private readonly IStubManager _stubManager;

public CacheManagerTests()
{
_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 DefaultStubDataMappingProfile());
_stubTypeMemoryCache = new MemoryStubTypeCache(_cacheKeyGenerator);
}



[Fact(DisplayName = "Cache_Key_Generator_Test")]
public void CacheKeyGeneratorTest()
{
var cacheKey = _cacheKeyGenerator.GenerateKey<PersonDto>();
Assert.NotEmpty(cacheKey);

}

[Fact(DisplayName = "Should add PropertyInfo Cache Successfully")]
public void Should_Add_PropertyInfos_To_Cache_Successfully()
{
var personData = new PersonDto();
_stubTypeMemoryCache.Set(personData, personData.GetType().GetProperties());
var cachedPropertyInfos = _stubTypeMemoryCache.Get(personData);
Assert.Equal(cachedPropertyInfos.Length, personData.GetType().GetProperties().Length);
}


[Fact(DisplayName = "Memory_Cache_Manager_Empty_Test")]
public void CacheManagerEmptyTest()
{
var stubDto = _stubManager.CreateNew<PersonDto>();
var stubTypeItem = new StubTypeItem();
_stubTypeCacheManager.Set(stubDto, stubTypeItem);
_stubTypeCacheManager.Clear();
Assert.Null(_stubTypeCacheManager.Get(stubDto));
Assert.True(_stubTypeCacheManager.IsEmpty());
var personData = new PersonDto();
_stubTypeMemoryCache.Set(personData, personData.GetType().GetProperties());
_stubTypeMemoryCache.Clear();
Assert.True(_stubTypeMemoryCache.IsEmpty());
}


[Fact(DisplayName = "Memory_Cache_Manager_Concurrency_Test")]
public void CacheManagerConcurrencyTest()
{
var stubDto = _stubManager.CreateNew<PersonDto>();
var stubTypeItem = new StubTypeItem();

var personData = new PersonDto();
var mainTask = Task.Run(async () =>
{
await Task.Run(() => _stubTypeMemoryCache.Set(stubDto, stubTypeItem));
await Task.Run(() => _stubTypeMemoryCache.Get(stubDto));
await Task.Run(() => _stubTypeMemoryCache.Set(personData, personData.GetType().GetProperties()));
await Task.Run(() => _stubTypeMemoryCache.Get(personData));
});

Assert.Null(mainTask.Exception);
Expand Down
84 changes: 81 additions & 3 deletions src/StubGenerator.Test/StubManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using StubGenerator.Defaults;
using StubGenerator.Test.Models;
using Xunit;
using System;

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

[Fact(DisplayName = "Mapping_Check_By_Naming_Default_Conventions")]
Expand Down Expand Up @@ -63,5 +62,84 @@ public void Naming_Mapping_Check_Culture()
Assert.NotNull(stubDto.LastName);
Assert.NotNull(stubDto.Email);
}

[Fact(DisplayName = "Should Generate Data For Guid Type")]
public void Should_Generate_Data_For_Guid_Type()
{
var generatedStubData = _stubManager.CreateNew<InnerComplexType>();
Assert.NotEqual(Guid.Empty, generatedStubData.GuidProperty);
Assert.True(Guid.TryParse(generatedStubData.GuidProperty.ToString(), out _));
}

[Fact(DisplayName = "Should Generate Data For DateTime Type")]
public void Should_Generate_Data_For_DateTime_Type()
{
var generatedStubData = _stubManager.CreateNew<InnerComplexType>();
Assert.IsType<DateTime>(generatedStubData.DateTimeProperty);
Assert.NotEqual(default(DateTime), generatedStubData.DateTimeProperty);
}

[Fact(DisplayName = "Should Generate Data For Decimal Type")]
public void Should_Generate_Data_For_Decimal_Type()
{
var generatedStubData = _stubManager.CreateNew<InnerComplexType>();
Assert.IsType<decimal>(generatedStubData.DecimalProperty);
Assert.NotEqual(default(decimal), generatedStubData.DecimalProperty);
}

[Fact(DisplayName = "Should Generate Data For Double Type")]
public void Should_Generate_Data_For_Double_Type()
{
var generatedStubData = _stubManager.CreateNew<InnerComplexType>();
Assert.IsType<double>(generatedStubData.DoubleProperty);
Assert.NotEqual(default(double), generatedStubData.DoubleProperty);
}

[Fact(DisplayName = "Should Generate Data For Enum Type")]
public void Should_Generate_Data_For_Enum_Type()
{
var generatedStubData = _stubManager.CreateNew<InnerComplexType>();
var enumValues = Enum.GetValues(typeof(EnTestEnum)).Cast<EnTestEnum>().ToList();
Assert.Contains(generatedStubData.EnumProperty, enumValues);
}

[Fact(DisplayName = "Should Generate Data for Nullable Type")]
public void Should_Generate_Data_For_Nullable_Type()
{
var generatedStubData = _stubManager.CreateNew<InnerComplexType>();
Assert.NotNull(generatedStubData.NullableIntegerProperty);
Assert.NotNull(generatedStubData.NullableIntegerProperty2);
}

[Fact(DisplayName = "Should Generate Data For Complex Type Properties")]
public void Should_Generate_Data_For_Complex_Type_Properties()
{
var generatedStubData = _stubManager.CreateNew<ModelWithComplexTypeProperty>();
Assert.NotNull(generatedStubData.ComplexType);
Assert.True(Guid.NewGuid() != generatedStubData.ComplexType.GuidProperty);
Assert.True(default(decimal) != generatedStubData.ComplexType.DecimalProperty);
Assert.True(default(double) != generatedStubData.ComplexType.DoubleProperty);
Assert.True(default(int) != generatedStubData.ComplexType.IntegerProperty);
}

[Fact(DisplayName = "Should Generate Data For Collection Types")]
public void Should_Generate_Data_For_Collection_Types()
{
var generatedStubData = _stubManager.CreateNew<ModelWithComplexTypeProperty>();
Assert.NotEmpty(generatedStubData.CollectionTypeComplex);
Assert.True(generatedStubData.CollectionTypeComplex[0].IntegerProperty != default(int));
}

[Fact(DisplayName = "Should Set Given values while generating data")]
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<InnerComplexType>(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);
}
}
}
6 changes: 4 additions & 2 deletions src/StubMiddleware.Core/Caching/IStubTypeCache.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using StubGenerator.Core;
using System.Reflection;

namespace StubGenerator.Caching
{
public interface IStubTypeCache
{
void Clear();
StubTypeItem Get<T>(T instance) where T : class;
bool Set<T>(T instance, StubTypeItem stubTypeItem) where T : class;
PropertyInfo[] Get<T>(T instance) where T : class;
PropertyInfo[] GetOrAdd<T>(T instance, PropertyInfo[] propertyInfos) where T : class;
bool Set<T>(T instance, PropertyInfo[] stubTypeItem) where T : class;
bool IsEmpty();
}
}
12 changes: 0 additions & 12 deletions src/StubMiddleware.Core/Caching/IStubTypeCacheManager.cs

This file was deleted.

49 changes: 49 additions & 0 deletions src/StubMiddleware.Core/Caching/MemoryStubTypeCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Collections.Concurrent;
using StubGenerator.Core;
using System.Reflection;

namespace StubGenerator.Caching
{
public class MemoryStubTypeCache : IStubTypeCache
{
private readonly ConcurrentDictionary<string, PropertyInfo[]> Cache;
private readonly IStubTypeCacheKeyGenerator _cacheKeyGenerator;

public MemoryStubTypeCache()
:this(new DefaultStubTypeCacheKeyGenerator())
{
}

public MemoryStubTypeCache(IStubTypeCacheKeyGenerator cacheKeyGenerator)
{
Cache = new ConcurrentDictionary<string, PropertyInfo[]>();
_cacheKeyGenerator = cacheKeyGenerator;
}

public PropertyInfo[] Get<T>(T instance) where T : class
{
string cacheKey = _cacheKeyGenerator.GenerateKey<T>();
Cache.TryGetValue(cacheKey, out PropertyInfo[] result);
return result;
}

public PropertyInfo[] GetOrAdd<T>(T instance, PropertyInfo[] propertyInfos) where T : class
{
var cacheKey = _cacheKeyGenerator.GenerateKey<T>();
return Cache.GetOrAdd(cacheKey, i => { return propertyInfos; });
}

public bool Set<T>(T instance, PropertyInfo[] stubTypeItem) where T : class
{
string cacheKey = _cacheKeyGenerator.GenerateKey<T>();
return Cache.TryAdd(cacheKey, stubTypeItem);
}

public void Clear() => Cache.Clear();

public bool IsEmpty()
{
return Cache.Count == 0;
}
}
}
27 changes: 0 additions & 27 deletions src/StubMiddleware.Core/Caching/StubTypeCacheManager.cs

This file was deleted.

Loading

0 comments on commit 03fad72

Please sign in to comment.