Merged from master to resolve conflicts

This commit is contained in:
Philip Dimitratos 2017-10-18 14:18:58 -07:00
Родитель ec1c44e6e2 eb914c4f6e
Коммит 1bce945fb1
15 изменённых файлов: 307 добавлений и 22 удалений

Просмотреть файл

@ -1,7 +1,7 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15 # Visual Studio 15
VisualStudioVersion = 15.0.26730.12 VisualStudioVersion = 15.0.26730.16
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sia.Shared", "src\Sia.Shared\Sia.Shared.csproj", "{C4AA9F6D-DFB1-4044-A7CD-FA3B6D5BA6AE}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sia.Shared", "src\Sia.Shared\Sia.Shared.csproj", "{C4AA9F6D-DFB1-4044-A7CD-FA3B6D5BA6AE}"
EndProject EndProject
@ -21,6 +21,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{931F26AB-5
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sia.Gateway.Tests", "test\Sia.Gateway.Tests\Sia.Gateway.Tests.csproj", "{7FED8930-1B06-4954-8337-C2DEC6697D6F}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sia.Gateway.Tests", "test\Sia.Gateway.Tests\Sia.Gateway.Tests.csproj", "{7FED8930-1B06-4954-8337-C2DEC6697D6F}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sia.Shared.Tests", "Sia.Shared.Tests\Sia.Shared.Tests.csproj", "{BF2EE536-9EB2-4E97-8F6A-BDF6571BAA67}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -55,6 +57,10 @@ Global
{7FED8930-1B06-4954-8337-C2DEC6697D6F}.Debug|Any CPU.Build.0 = Debug|Any CPU {7FED8930-1B06-4954-8337-C2DEC6697D6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FED8930-1B06-4954-8337-C2DEC6697D6F}.Release|Any CPU.ActiveCfg = Release|Any CPU {7FED8930-1B06-4954-8337-C2DEC6697D6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FED8930-1B06-4954-8337-C2DEC6697D6F}.Release|Any CPU.Build.0 = Release|Any CPU {7FED8930-1B06-4954-8337-C2DEC6697D6F}.Release|Any CPU.Build.0 = Release|Any CPU
{BF2EE536-9EB2-4E97-8F6A-BDF6571BAA67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BF2EE536-9EB2-4E97-8F6A-BDF6571BAA67}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF2EE536-9EB2-4E97-8F6A-BDF6571BAA67}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF2EE536-9EB2-4E97-8F6A-BDF6571BAA67}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -67,6 +73,7 @@ Global
{B92790C5-C4F1-4052-ABA7-68448F28DDB2} = {09E441A6-06F8-457A-981A-394473FB8CD5} {B92790C5-C4F1-4052-ABA7-68448F28DDB2} = {09E441A6-06F8-457A-981A-394473FB8CD5}
{8D465104-1385-4EE4-A1ED-AEE7708A537F} = {09E441A6-06F8-457A-981A-394473FB8CD5} {8D465104-1385-4EE4-A1ED-AEE7708A537F} = {09E441A6-06F8-457A-981A-394473FB8CD5}
{7FED8930-1B06-4954-8337-C2DEC6697D6F} = {931F26AB-58CD-4667-A281-EEC186F2FB59} {7FED8930-1B06-4954-8337-C2DEC6697D6F} = {931F26AB-58CD-4667-A281-EEC186F2FB59}
{BF2EE536-9EB2-4E97-8F6A-BDF6571BAA67} = {931F26AB-58CD-4667-A281-EEC186F2FB59}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {05069EBB-9F94-4DAA-A619-C2F496B37BA4} SolutionGuid = {05069EBB-9F94-4DAA-A619-C2F496B37BA4}

Просмотреть файл

@ -0,0 +1,72 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using Sia.Shared.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sia.Shared.Tests.Data
{
[TestClass]
public class PartialJsonResolverTests
{
[TestMethod]
public void ResolveJsonToString_Method_Resolve_SerializesSourceArgumentObjectToString()
{
var input = new TestHasJsonDataObject
{
Data = new JsonSerializationTestObject()
};
var expectedResultDataValue = JsonSerializationTestObject.ExpectedSerialization();
var objectUnderTest = new ResolveJsonToString<TestHasJsonDataObject, TestHasJsonDataString>();
var result = objectUnderTest.Resolve(input, null, null, null);
Assert.AreEqual(expectedResultDataValue, result, false);
}
[TestMethod]
public void ResolveStringToJson_Method_Resolve_SerializesSourceArgumentStringToObject()
{
var input = new TestHasJsonDataString()
{
Data = JsonSerializationTestObject.ExpectedSerialization()
};
var expectedResult = new JsonSerializationTestObject();
var objectUnderTest = new ResolveStringToJson<TestHasJsonDataString, TestHasJsonDataObject>();
var result = objectUnderTest.Resolve(input, null, null, null);
Assert.AreEqual(expectedResult.a, ExtractPropertyFromResult(result, "a"));
Assert.AreEqual(expectedResult.b, ExtractPropertyFromResult(result, "b"));
}
private static JToken ExtractPropertyFromResult(object result, string propName) => ((JObject)result).Property(propName).Value;
}
internal class JsonSerializationTestObject :IEquatable<JsonSerializationTestObject>
{
public static string ExpectedSerialization()
=> "{\"a\":\"ValueOfA\",\"b\":1}";
public bool Equals(JsonSerializationTestObject other)
=> a == other.a && b == other.b;
public string a { get; set; } = "ValueOfA";
public int b { get; set; } = 1;
}
internal class TestHasJsonDataString : IJsonDataString
{
public string Data { get; set; }
}
internal class TestHasJsonDataObject : IJsonDataObject
{
public object Data { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20171012-09" />
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\Sia.Shared\Sia.Shared.csproj" />
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,65 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sia.Shared.Validation;
using System;
using System.Collections.Generic;
using System.Text;
namespace Sia.Shared.Tests.Validation
{
[TestClass]
public class ThrowIfTests
{
[TestMethod]
public void Null_StaticMethod_WhenObjectIsNotNull_ReturnsObject()
{
var input = new Object();
var result = ThrowIf.Null(input, nameof(input));
Assert.AreSame(input, result);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Null_StaticMethod_WhenObjectIsNull_ThrowsArgumentNullException()
{
object input = null;
var result = ThrowIf.Null(input, nameof(input));
//expect exception
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void NullOrWhiteSpace_StaticMethod_WhenInputIsNull_ThrowsArgumentException()
{
string input = null;
var result = ThrowIf.NullOrWhiteSpace(input, nameof(input));
//Expect exception
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void NullOrWhiteSpace_StaticMethod_WhenInputIsOnlyWhitespace_ThrowsArgumentException()
{
string input = " ";
var result = ThrowIf.NullOrWhiteSpace(input, nameof(input));
//Expect exception
}
[TestMethod]
public void NullOrWhiteSpace_StaticMethod_WhenInputStringWithAnyNonWhitespace_ReturnsString()
{
string input = " . ";
var result = ThrowIf.NullOrWhiteSpace(input, nameof(input));
Assert.AreSame(input, result);
}
}
}

Просмотреть файл

@ -1,14 +1,18 @@
using Sia.Shared.Data; using Microsoft.EntityFrameworkCore;
using Sia.Shared.Data;
using System; using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace Sia.Data.Incidents.Models namespace Sia.Data.Incidents.Models
{ {
public class Event : IEntity public class Event : IEntity, IJsonDataString
{ {
public long Id { get; set; } public long Id { get; set; }
public long? IncidentId { get; set; } public long? IncidentId { get; set; }
public long EventTypeId { get; set; } public long EventTypeId { get; set; }
public DateTime Occurred { get; set; } public DateTime Occurred { get; set; }
public DateTime EventFired { get; set; } public DateTime EventFired { get; set; }
[Column(TypeName = "varchar(max)")]
public string Data { get; set; }
} }
} }

Просмотреть файл

@ -16,10 +16,13 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
<PackageReference Include="Microsoft.Sia.Shared" Version="1.0.7-alpha" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" /> <PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
<PackageReference Include="System.Globalization.Extensions" Version="4.3.0" /> <PackageReference Include="System.Globalization.Extensions" Version="4.3.0" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Sia.Shared\Sia.Shared.csproj" />
</ItemGroup>
</Project> </Project>

Просмотреть файл

@ -1,9 +1,12 @@
using System; using Sia.Shared.Data;
using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Dynamic;
namespace Sia.Domain.ApiModels namespace Sia.Domain.ApiModels
{ {
public class NewEvent public class NewEvent
:IJsonDataObject
{ {
[Required] [Required]
public long? EventTypeId { get; set; } public long? EventTypeId { get; set; }
@ -11,5 +14,6 @@ namespace Sia.Domain.ApiModels
public DateTime? Occurred { get; set; } public DateTime? Occurred { get; set; }
[Required] [Required]
public DateTime? EventFired { get; set; } public DateTime? EventFired { get; set; }
public object Data { get; set; }
} }
} }

Просмотреть файл

@ -2,16 +2,18 @@
using Sia.Shared.Data; using Sia.Shared.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Dynamic;
using System.Text; using System.Text;
namespace Sia.Domain namespace Sia.Domain
{ {
public class Event : IEntity public class Event : IEntity, IJsonDataObject
{ {
public long Id { get; set; } public long Id { get; set; }
public long? IncidentId { get; set; } public long? IncidentId { get; set; }
public long EventTypeId { get; set; } public long EventTypeId { get; set; }
public DateTime Occurred { get; set; } public DateTime Occurred { get; set; }
public DateTime EventFired { get; set; } public DateTime EventFired { get; set; }
public object Data { get; set; }
} }
} }

Просмотреть файл

@ -15,7 +15,10 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.DataAnnotations" Version="2.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.DataAnnotations" Version="2.0.0" />
<PackageReference Include="Microsoft.Sia.Shared" Version="1.0.7-alpha" /> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Sia.Shared\Sia.Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

Просмотреть файл

@ -2,6 +2,7 @@
using AutoMapper.EquivalencyExpression; using AutoMapper.EquivalencyExpression;
using Sia.Domain; using Sia.Domain;
using Sia.Domain.ApiModels; using Sia.Domain.ApiModels;
using Sia.Gateway.Protocol;
using Sia.Shared.Data; using Sia.Shared.Data;
namespace Sia.Gateway.Initialization namespace Sia.Gateway.Initialization
@ -32,25 +33,35 @@ namespace Sia.Gateway.Initialization
configuration.CreateMap<Participant, Data.Incidents.Models.Participant>(); configuration.CreateMap<Participant, Data.Incidents.Models.Participant>();
configuration.CreateMap<Data.Incidents.Models.Participant, Participant>(); configuration.CreateMap<Data.Incidents.Models.Participant, Participant>();
configuration.CreateMap<NewEvent, Data.Incidents.Models.Event>().EqualityInsertOnly()
configuration.CreateMap<NewEvent, Data.Incidents.Models.Event>().EqualityInsertOnly(); .UseResolveJsonToString();
configuration.CreateMap<Event, Data.Incidents.Models.Event>().EqualityById(); configuration.CreateMap<Event, Data.Incidents.Models.Event>().EqualityById()
configuration.CreateMap<Data.Incidents.Models.Event, Event>().EqualityById(); .UseResolveJsonToString();
configuration.CreateMap<Data.Incidents.Models.Event, Event>().EqualityById()
.UseResolveStringToJson();
}); });
} }
private static IMappingExpression<TSource, TDestination> UseResolveJsonToString<TSource, TDestination>(this IMappingExpression<TSource, TDestination> mapping)
where TSource: IJsonDataObject
where TDestination: IJsonDataString
=> mapping.ForMember((ev) => ev.Data, (config) => config.ResolveUsing<ResolveJsonToString<TSource, TDestination>>());
private static IMappingExpression<TSource, TDestination> UseResolveStringToJson<TSource, TDestination>(this IMappingExpression<TSource, TDestination> mapping)
where TSource : IJsonDataString
where TDestination : IJsonDataObject
=> mapping.ForMember((ev) => ev.Data, (config) => config.ResolveUsing<ResolveStringToJson<TSource, TDestination>>());
public static IMappingExpression<T1, T2> EqualityInsertOnly<T1, T2>(this IMappingExpression<T1, T2> mappingExpression) public static IMappingExpression<T1, T2> EqualityInsertOnly<T1, T2>(this IMappingExpression<T1, T2> mappingExpression)
where T1 : class where T1 : class
where T2 : class where T2 : class
{ => mappingExpression.EqualityComparison((one, two) => false);
return mappingExpression.EqualityComparison((one, two) => false);
}
public static IMappingExpression<T1, T2> EqualityById<T1, T2>(this IMappingExpression<T1, T2> mappingExpression) public static IMappingExpression<T1, T2> EqualityById<T1, T2>(this IMappingExpression<T1, T2> mappingExpression)
where T1 : class, IEntity where T1 : class, IEntity
where T2 : class, IEntity where T2 : class, IEntity
{ => mappingExpression.EqualityComparison((one, two) => one.Id == two.Id);
return mappingExpression.EqualityComparison((one, two) => one.Id == two.Id);
}
} }
} }

Просмотреть файл

@ -18,6 +18,8 @@ using Sia.Shared.Validation;
using System; using System;
using System.Reflection; using System.Reflection;
using System.Runtime.Loader; using System.Runtime.Loader;
using Sia.Gateway.Protocol;
using System.Buffers;
using Sia.Domain; using Sia.Domain;
namespace Sia.Gateway.Initialization namespace Sia.Gateway.Initialization
@ -121,7 +123,12 @@ namespace Sia.Gateway.Initialization
=> new UrlHelper(iFactory.GetService<IActionContextAccessor>().ActionContext) => new UrlHelper(iFactory.GetService<IActionContextAccessor>().ActionContext)
); );
services.AddMvc(); services.AddMvc(options =>
{
options.OutputFormatters.Insert(0, new PartialSerializedJsonOutputFormatter(
new MvcJsonOptions().SerializerSettings,
ArrayPool<char>.Shared));
});
services services
.AddAuthentication(authOptions => .AddAuthentication(authOptions =>
{ {

Просмотреть файл

@ -0,0 +1,51 @@
using Microsoft.AspNetCore.Mvc.Formatters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Buffers;
using System.Text;
using Sia.Shared.Data;
namespace Sia.Gateway.Protocol
{
public class PartialSerializedJsonOutputFormatter : JsonOutputFormatter
{
public PartialSerializedJsonOutputFormatter(JsonSerializerSettings serializerSettings, ArrayPool<char> charPool)
: base(serializerSettings, charPool)
{
}
public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
{
var dataStream = (IEnumerable<IJsonDataObject>)context.Object;
foreach (var objectToWrite in dataStream)
{
var jsonData = objectToWrite.Data;
if (jsonData is string) objectToWrite.Data = Deserialize((string)jsonData);
}
return base.WriteResponseBodyAsync(context, selectedEncoding);
}
private const int NumberOfCharactersInGenericTypeNotUsedByGetInterfaceMethod = 3;
protected override bool CanWriteType(Type type)
{
if (!type.IsGenericType) return false;
if (type.GetGenericArguments().Count() != 1) return false;
var enumIntName = typeof(IEnumerable<>).ToString();
var enumerableInterface = type.GetInterface(enumIntName
.Substring(0, enumIntName.Length - NumberOfCharactersInGenericTypeNotUsedByGetInterfaceMethod));
if (enumerableInterface is null) return false;
return !(type.GetGenericArguments()[0].GetInterface(nameof(IJsonDataObject)) is null);
}
private object Deserialize(string serializedData) => JsonConvert.DeserializeObject(serializedData);
}
}

Просмотреть файл

@ -0,0 +1,38 @@
using AutoMapper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;
namespace Sia.Shared.Data
{
public interface IJsonDataString
{
string Data { get; set; }
}
public interface IJsonDataObject
{
object Data { get; set; }
}
public class ResolveJsonToString<TSource, TDestination>
: IValueResolver<TSource, TDestination, string>
where TSource: IJsonDataObject
where TDestination: IJsonDataString
{
public string Resolve(TSource source, TDestination destination, string destMember, ResolutionContext context)
=> JsonConvert.SerializeObject(source.Data);
}
public class ResolveStringToJson<TSource, TDestination>
: IValueResolver<TSource, TDestination, object>
where TSource : IJsonDataString
where TDestination : IJsonDataObject
{
public object Resolve(TSource source, TDestination destination, object destMember, ResolutionContext context)
=> JsonConvert.DeserializeObject(source.Data);
}
}

Просмотреть файл

@ -14,6 +14,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper" Version="6.1.1" />
<PackageReference Include="Microsoft.Azure.KeyVault" Version="2.3.2" /> <PackageReference Include="Microsoft.Azure.KeyVault" Version="2.3.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="3.16.1" /> <PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="3.16.1" />

Просмотреть файл

@ -7,8 +7,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper" Version="6.1.1" /> <PackageReference Include="AutoMapper" Version="6.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.1.18" /> <PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.1.18" /> <PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup> </ItemGroup>