ImportExportService::Import(recipe) implementation. Currently supports metadata and settings.

Unit tests.

--HG--
branch : dev
This commit is contained in:
Suha Can
2011-03-10 14:23:46 -08:00
parent f2ce9c0329
commit 11e0cce6db
4 changed files with 120 additions and 4 deletions

View File

@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using Autofac;
using NUnit.Framework;
using Orchard.Caching;
using Orchard.Environment.Descriptor;
using Orchard.Environment.Descriptor.Models;
using Orchard.Environment.Extensions;
using Orchard.Environment.Extensions.Loaders;
using Orchard.FileSystems.AppData;
using Orchard.FileSystems.WebSite;
using Orchard.ImportExport.Services;
using Orchard.Recipes.Services;
using Orchard.Services;
using Orchard.Tests.Modules.Recipes.Services;
using Orchard.Tests.Stubs;
namespace Orchard.Tests.Modules.ImportExport.Services {
[TestFixture]
public class ImportExportManagerTests {
private IContainer _container;
private IImportExportService _importExportService;
[SetUp]
public void Init() {
var builder = new ContainerBuilder();
builder.RegisterType<ImportExportService>().As<IImportExportService>();
builder.RegisterType<StubShellDescriptorManager>().As<IShellDescriptorManager>();
builder.RegisterType<RecipeManager>().As<IRecipeManager>();
builder.RegisterType<RecipeHarvester>().As<IRecipeHarvester>();
builder.RegisterType<RecipeStepExecutor>().As<IRecipeStepExecutor>();
builder.RegisterType<StubStepQueue>().As<IRecipeStepQueue>().InstancePerLifetimeScope();
builder.RegisterType<StubRecipeJournal>().As<IRecipeJournal>();
builder.RegisterType<StubRecipeScheduler>().As<IRecipeScheduler>();
builder.RegisterType<ExtensionManager>().As<IExtensionManager>();
builder.RegisterType<StubAppDataFolder>().As<IAppDataFolder>();
builder.RegisterType<StubClock>().As<IClock>();
builder.RegisterType<StubCacheManager>().As<ICacheManager>();
builder.RegisterType<Environment.Extensions.ExtensionManagerTests.StubLoaders>().As<IExtensionLoader>();
builder.RegisterType<RecipeParser>().As<IRecipeParser>();
builder.RegisterType<StubWebSiteFolder>().As<IWebSiteFolder>();
builder.RegisterType<CustomRecipeHandler>().As<IRecipeHandler>();
_container = builder.Build();
_importExportService = _container.Resolve<IImportExportService>();
}
[Test]
public void ImportSucceedsWhenRecipeContainsImportSteps() {
Assert.DoesNotThrow(() => _importExportService.Import(
@"<Orchard>
<Recipe>
<Name>MyModuleInstaller</Name>
</Recipe>
<Settings />
</Orchard>"));
}
[Test]
public void ImportFailsWhenRecipeContainsNonImportSteps() {
Assert.Throws(typeof(InvalidOperationException), () => _importExportService.Import(
@"<Orchard>
<Recipe>
<Name>MyModuleInstaller</Name>
</Recipe>
<Module name=""MyModule"" />
</Orchard>"));
}
}
public class StubShellDescriptorManager : IShellDescriptorManager {
public ShellDescriptor GetShellDescriptor() {
return new ShellDescriptor();
}
public void UpdateShellDescriptor(int priorSerialNumber, IEnumerable<ShellFeature> enabledFeatures, IEnumerable<ShellParameter> parameters) {
}
}
}

View File

@@ -140,6 +140,7 @@
<ItemGroup>
<Compile Include="CodeGeneration\Commands\CodeGenerationCommandsTests.cs" />
<Compile Include="Comments\Services\CommentServiceTests.cs" />
<Compile Include="ImportExport\Services\ImportExportServiceTests.cs" />
<Compile Include="Indexing\IndexingTaskExecutorTests.cs" />
<Compile Include="Indexing\LuceneIndexProviderTests.cs" />
<Compile Include="Indexing\LuceneSearchBuilderTests.cs" />
@@ -197,6 +198,10 @@
<Project>{14C049FD-B35B-415A-A824-87F26B26E7FD}</Project>
<Name>Orchard.Comments</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Web\Modules\Orchard.ImportExport\Orchard.ImportExport.csproj">
<Project>{FE5C5947-D2D5-42C5-992A-13D672946135}</Project>
<Name>Orchard.ImportExport</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Web\Modules\Orchard.Indexing\Orchard.Indexing.csproj">
<Project>{EA2B9121-EF54-40A6-A53E-6593C86EE696}</Project>
<Name>Orchard.Indexing</Name>

View File

@@ -1,6 +1,6 @@
namespace Orchard.ImportExport.Services {
public interface IImportExportService : IDependency {
void Import(string recipe);
void Import(string recipeText);
}
}

View File

@@ -1,12 +1,22 @@
using JetBrains.Annotations;
using System;
using JetBrains.Annotations;
using Orchard.Environment.Descriptor;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Recipes.Models;
using Orchard.Recipes.Services;
namespace Orchard.ImportExport.Services {
[UsedImplicitly]
public class ImportExportService : IImportExportService {
public ImportExportService(IRecipeParser recipeParser, IRecipeManager recipeManager) {
private readonly IRecipeParser _recipeParser;
private readonly IRecipeManager _recipeManager;
private readonly IShellDescriptorManager _shellDescriptorManager;
public ImportExportService(IRecipeParser recipeParser, IRecipeManager recipeManager, IShellDescriptorManager shellDescriptorManager) {
_recipeParser = recipeParser;
_recipeManager = recipeManager;
_shellDescriptorManager = shellDescriptorManager;
Logger = NullLogger.Instance;
T = NullLocalizer.Instance;
}
@@ -14,7 +24,29 @@ namespace Orchard.ImportExport.Services {
public Localizer T { get; set; }
public ILogger Logger { get; set; }
public void Import(string recipe) {
public void Import(string recipeText) {
var recipe = _recipeParser.ParseRecipe(recipeText);
CheckRecipeSteps(recipe);
_recipeManager.Execute(recipe);
UpdateShell();
}
private void CheckRecipeSteps(Recipe recipe) {
foreach (var step in recipe.RecipeSteps) {
switch (step.Name) {
case "Metadata":
case "Settings":
case "Data":
break;
default:
throw new InvalidOperationException(T("Step {0} is not a supported import step.", step.Name).Text);
}
}
}
private void UpdateShell() {
var descriptor = _shellDescriptorManager.GetShellDescriptor();
_shellDescriptorManager.UpdateShellDescriptor(descriptor.SerialNumber, descriptor.Features, descriptor.Parameters);
}
}
}