added resource mapping validation tests

This commit is contained in:
kay.one 2013-04-21 14:04:09 -07:00
commit c3214a2e88
17 changed files with 297 additions and 76 deletions

View file

@ -0,0 +1,60 @@
using System;
using System.Linq;
using System.Reflection;
using NzbDrone.Api.REST;
using NzbDrone.Common.Reflection;
using NzbDrone.Core.Datastore;
namespace NzbDrone.Api.Mapping
{
public static class MappingValidation
{
public static void ValidateMapping(Type modelType, Type resourceType)
{
var errors = modelType.GetSimpleProperties().Select(p => GetError(resourceType, p)).Where(c => c != null).ToList();
if (errors.Any())
{
throw new ResourceMappingException(errors);
}
PrintExtraProperties(modelType, resourceType);
}
private static void PrintExtraProperties(Type modelType, Type resourceType)
{
var resourceBaseProperties = typeof(RestResource).GetProperties().Select(c => c.Name);
var resourceProperties = resourceType.GetProperties().Select(c => c.Name).Except(resourceBaseProperties);
var modelProperties = modelType.GetProperties().Select(c => c.Name);
var extra = resourceProperties.Except(modelProperties);
foreach (var extraProp in extra)
{
Console.WriteLine("Extra: [{0}]", extraProp);
}
}
private static string GetError(Type resourceType, PropertyInfo modelProperty)
{
var resourceProperty = resourceType.GetProperties().FirstOrDefault(c => c.Name == modelProperty.Name);
if (resourceProperty == null)
{
return string.Format("public {0} {1} {{ get; set; }}", modelProperty.PropertyType.Name, modelProperty.Name);
}
if (resourceProperty.PropertyType != modelProperty.PropertyType)
{
return string.Format("Excpected {0}.{1} to have type of {2} but found {3}", resourceType.Name, resourceProperty.Name, modelProperty.PropertyType, resourceProperty.PropertyType);
}
return null;
}
}
}

View file

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace NzbDrone.Api.Mapping
{
public class ResourceMappingException : ApplicationException
{
public ResourceMappingException(IEnumerable<string> error)
: base(Environment.NewLine + String.Join(Environment.NewLine, error.OrderBy(c => c)))
{
}
}
}

View file

@ -0,0 +1,41 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Omu.ValueInjecter;
namespace NzbDrone.Api.Mapping
{
public static class ValueInjectorExtensions
{
public static TTarget InjectTo<TTarget>(this object source) where TTarget : new()
{
var targetType = typeof(TTarget);
if (targetType.IsGenericType &&
targetType.GetGenericTypeDefinition() != null &&
targetType.GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable)) &&
source.GetType().IsGenericType &&
source.GetType().GetGenericTypeDefinition() != null &&
source.GetType().GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable)))
{
var result = new TTarget();
var listSubType = targetType.GetGenericArguments()[0];
var listType = typeof(List<>).MakeGenericType(listSubType);
var addMethod = listType.GetMethod("Add");
foreach (var sourceItem in (IEnumerable)source)
{
var e = Activator.CreateInstance(listSubType).InjectFrom(sourceItem);
addMethod.Invoke(result, new[] { e });
}
return result;
}
return (TTarget)new TTarget().InjectFrom(source);
}
}
}