Skip to content

Commit

Permalink
Prototype YAML role serialization
Browse files Browse the repository at this point in the history
* Serialized in 'reverse' of standard Sitecore serialization, i.e. a role declares which roles it is a member of, instead of declaring which roles are in its role.
* Reversing the order makes a lot more sense for real usage (e.g. "mysite\foo" is a member of "sitecore\authors" as opposed to having to serialize sitecore\authors to get that link
* Thanks to OlegJytnik for an initial implementation using a hacked Sitecore format.
  • Loading branch information
kamsar committed Apr 10, 2016
1 parent b248579 commit f7831d5
Show file tree
Hide file tree
Showing 34 changed files with 710 additions and 552 deletions.
8 changes: 7 additions & 1 deletion Unicorn.sln
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
VisualStudioVersion = 14.0.25123.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicorn", "src\Unicorn\Unicorn.csproj", "{82C4441B-E11E-4601-B6B7-C5AD71B2AB70}"
EndProject
Expand All @@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicorn.Roles", "src\Unicor
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicorn.Users", "src\Unicorn.Users\Unicorn.Users.csproj", "{9522A1A0-9633-470B-8E3F-2CD884B3FB3C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicorn.Roles.Tests", "src\Unicorn.Roles.Tests\Unicorn.Roles.Tests.csproj", "{7731968E-8A3E-4F36-860D-CE614C3D9114}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,6 +53,10 @@ Global
{9522A1A0-9633-470B-8E3F-2CD884B3FB3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9522A1A0-9633-470B-8E3F-2CD884B3FB3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9522A1A0-9633-470B-8E3F-2CD884B3FB3C}.Release|Any CPU.Build.0 = Release|Any CPU
{7731968E-8A3E-4F36-860D-CE614C3D9114}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7731968E-8A3E-4F36-860D-CE614C3D9114}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7731968E-8A3E-4F36-860D-CE614C3D9114}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7731968E-8A3E-4F36-860D-CE614C3D9114}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
@@ -0,0 +1,58 @@
using System.IO;
using FluentAssertions;
using Unicorn.Roles.Formatting;
using Unicorn.Roles.Model;
using Xunit;

namespace Unicorn.Roles.Tests.Formatting
{
public class YamlRoleSerializationFormatterTests
{
[Fact]
public void ShouldFormatSingleRole_AsExpected()
{
var sut = new YamlRoleSerializationFormatter();

using (var ms = new MemoryStream())
{
sut.WriteSerializedRole(new SerializedRoleData("Test", new string[0], "Foo"), ms);

ms.Seek(0, SeekOrigin.Begin);

using (var reader = new StreamReader(ms))
{
var yml = reader.ReadToEnd();

yml.Should().Be(@"---
Role: Test
");
}
}
}

[Fact]
public void ShouldFormatRoleWithParents_AsExpected()
{
var sut = new YamlRoleSerializationFormatter();

using (var ms = new MemoryStream())
{
sut.WriteSerializedRole(new SerializedRoleData("Test", new [] { "Foo", "Foo-Bar" }, "Foo"), ms);

ms.Seek(0, SeekOrigin.Begin);

using (var reader = new StreamReader(ms))
{
var yml = reader.ReadToEnd();

yml.Should().Be(@"---
Role: Test
MemberOf:
Role: Foo
Role: ""Foo-Bar""
");
}
}
}
}
}
36 changes: 36 additions & 0 deletions src/Unicorn.Roles.Tests/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Unicorn.Roles.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Unicorn.Roles.Tests")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7731968e-8a3e-4f36-860d-ce614c3d9114")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -0,0 +1,52 @@
using System.IO;
using System.Reflection;
using System.Xml;
using FluentAssertions;
using Unicorn.Roles.Model;
using Unicorn.Roles.RolePredicates;
using Xunit;

namespace Unicorn.Roles.Tests.RolePredicates
{
public class ConfigurationRolePredicateTests
{
[Theory]
// Domain tests
[InlineData(@"allfather\Foo", true)]
[InlineData(@"AllFather\ZeusDogg", true)]
// Excluded tests
[InlineData(@"NOTME\haha", false)]
[InlineData(@"haha", false)]
// Pattern tests
[InlineData(@"some\gonk droid", true)]
[InlineData(@"some\fake", false)]
public void Includes_MatchesExpectedPathResult(string testPath, bool expectedResult)
{
var predicate = CreateTestPredicate(CreateTestConfiguration());

predicate.Includes(new SerializedRoleData(testPath, new string[0], @"C:\fake.yml")).IsIncluded.Should().Be(expectedResult);
}

private ConfigurationRolePredicate CreateTestPredicate(XmlNode configNode)
{
return new ConfigurationRolePredicate(configNode);
}

private XmlNode CreateTestConfiguration()
{
var assembly = Assembly.GetExecutingAssembly();
string text;
// ReSharper disable AssignNullToNotNullAttribute
using (var textStreamReader = new StreamReader(assembly.GetManifestResourceStream("Unicorn.Roles.Tests.RolePredicates.TestConfiguration.xml")))
// ReSharper restore AssignNullToNotNullAttribute
{
text = textStreamReader.ReadToEnd();
}

var doc = new XmlDocument();
doc.LoadXml(text);

return doc.DocumentElement;
}
}
}
6 changes: 6 additions & 0 deletions src/Unicorn.Roles.Tests/RolePredicates/TestConfiguration.xml
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>

<predicate>
<include domain="allfather" />
<include domain="some" pattern="^gonk" />
</predicate>
102 changes: 102 additions & 0 deletions src/Unicorn.Roles.Tests/Unicorn.Roles.Tests.csproj
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{7731968E-8A3E-4F36-860D-CE614C3D9114}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Unicorn.Roles.Tests</RootNamespace>
<AssemblyName>Unicorn.Roles.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="FluentAssertions, Version=4.3.2.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
<HintPath>..\..\packages\FluentAssertions.4.3.2\lib\net45\FluentAssertions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FluentAssertions.Core, Version=4.3.2.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
<HintPath>..\..\packages\FluentAssertions.4.3.2\lib\net45\FluentAssertions.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Sitecore.Kernel">
<HintPath>..\..\lib\Sitecore\v7\Sitecore.Kernel.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.assert, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.assert.2.1.0\lib\dotnet\xunit.assert.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.core, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.extensibility.core.2.1.0\lib\dotnet\xunit.core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.execution.desktop, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Formatting\YamlRoleSerializationFormatterTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RolePredicates\ConfigurationRolePredicateTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Rainbow\src\Rainbow\Rainbow.csproj">
<Project>{8ff75c8b-2a9e-4d95-91ad-333071813199}</Project>
<Name>Rainbow</Name>
</ProjectReference>
<ProjectReference Include="..\Unicorn.Roles\Unicorn.Roles.csproj">
<Project>{cef534b3-ab68-45ce-afea-810d0e4a6613}</Project>
<Name>Unicorn.Roles</Name>
</ProjectReference>
<ProjectReference Include="..\Unicorn\Unicorn.csproj">
<Project>{82c4441b-e11e-4601-b6b7-c5ad71b2ab70}</Project>
<Name>Unicorn</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="RolePredicates\TestConfiguration.xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
10 changes: 10 additions & 0 deletions src/Unicorn.Roles.Tests/packages.config
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FluentAssertions" version="4.3.2" targetFramework="net45" />
<package id="xunit" version="2.1.0" targetFramework="net45" />
<package id="xunit.abstractions" version="2.0.0" targetFramework="net45" />
<package id="xunit.assert" version="2.1.0" targetFramework="net45" />
<package id="xunit.core" version="2.1.0" targetFramework="net45" />
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="net45" />
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net45" />
</packages>
16 changes: 0 additions & 16 deletions src/Unicorn.Roles/Data/DefaultSyncRole.cs

This file was deleted.

0 comments on commit f7831d5

Please sign in to comment.