This commit is contained in:
Chris Cheetham 2021-06-21 08:41:31 -04:00
Родитель 9050c76390
Коммит 9cb2e570bb
3 изменённых файлов: 107 добавлений и 93 удалений

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

@ -117,16 +117,22 @@ namespace Steeltoe.InitializrApi.Controllers
}
Logger.LogDebug("Project specification: {ProjectSpec}", normalizedSpec);
var projectArchive = await _projectGenerator.GenerateProjectArchive(normalizedSpec);
if (projectArchive is null)
try
{
return NotFound($"No project template for spec: {normalizedSpec}");
var projectPackage = await _projectGenerator.GenerateProjectArchive(normalizedSpec);
return File(
projectPackage,
$"application/zip",
$"{normalizedSpec.Name}.zip");
}
catch (NoProjectForSpecException e)
{
return NotFound(e.Message);
}
catch (InvalidSpecException e)
{
return BadRequest(e.Message);
}
return File(
projectArchive,
$"application/zip",
$"{normalizedSpec.Name}.zip");
}
}
}

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

@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using Steeltoe.InitializrApi.Models;
using System;
using System.Threading.Tasks;
namespace Steeltoe.InitializrApi.Services
@ -19,4 +20,24 @@ namespace Steeltoe.InitializrApi.Services
/// <returns>The generated project archive as a byte array, or null if not able to generate project per spec.</returns>
Task<byte[]> GenerateProjectArchive(ProjectSpec spec);
}
/// <inheritdoc />
public class NoProjectForSpecException : Exception
{
/// <inheritdoc cref="Exception"/>
public NoProjectForSpecException(string message)
: base(message)
{
}
}
/// <inheritdoc />
public class InvalidSpecException : Exception
{
/// <inheritdoc cref="Exception"/>
public InvalidSpecException(string message)
: base(message)
{
}
}
}

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

@ -2,17 +2,17 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
// using System.IO;
using System.Text;
using System.Threading.Tasks;
// using FluentAssertions;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
// using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Steeltoe.InitializrApi.Controllers;
using Steeltoe.InitializrApi.Models;
using Steeltoe.InitializrApi.Services;
// using Xunit;
using Xunit;
namespace Steeltoe.InitializrApi.Test.Unit.Controllers
{
@ -22,9 +22,8 @@ namespace Steeltoe.InitializrApi.Test.Unit.Controllers
* positive tests *
* ----------------------------------------------------------------- */
/*
[Fact]
public void Configuration_Should_Specify_Defaults()
public async Task Configuration_Should_Specify_Defaults()
{
// Arrange
var config = new UiConfig
@ -43,27 +42,24 @@ namespace Steeltoe.InitializrApi.Test.Unit.Controllers
.Build();
// Act
var unknown = controller.GetProjectArchive(new ProjectSpec());
var unknown = await controller.GetProjectArchive(new ProjectSpec());
var result = Assert.IsType<FileContentResult>(unknown);
var projectPackage = Encoding.ASCII.GetString(result.FileContents);
// Assert
var result = Assert.IsType<FileContentResult>(unknown);
using var reader = new StreamReader(new MemoryStream(result.FileContents));
reader.ReadLine().Should().Be("project name=my project name");
reader.ReadLine().Should().Be("description=my description");
reader.ReadLine().Should().Be("namespace=my namespace");
reader.ReadLine().Should().Be("steeltoe version=my steeltoe version");
reader.ReadLine().Should().Be("dotnet framework=my dotnet framework");
reader.ReadLine().Should().Be("dotnet template=my dotnet template");
reader.ReadLine().Should().Be("language=my language");
reader.ReadLine().Should().Be("packaging=myarchive");
reader.ReadLine().Should().Be("dependencies=<na>");
reader.ReadLine().Should().BeNull();
projectPackage.Should().Contain("project name=my project name");
projectPackage.Should().Contain("description=my description");
projectPackage.Should().Contain("namespace=my namespace");
projectPackage.Should().Contain("steeltoe version=my steeltoe version");
projectPackage.Should().Contain("dotnet framework=my dotnet framework");
projectPackage.Should().Contain("dotnet template=my dotnet template");
projectPackage.Should().Contain("language=my language");
projectPackage.Should().Contain("packaging=myarchive");
projectPackage.Should().Contain("dependencies=<na>");
}
*/
/*
[Fact]
public void Dependencies_Should_Be_Case_Corrected()
public async Task Dependencies_Should_Be_Case_Corrected()
{
// Arrange
var config = new UiConfig
@ -95,75 +91,65 @@ namespace Steeltoe.InitializrApi.Test.Unit.Controllers
.Build();
// Act
var unknown = controller.GetProjectArchive(spec);
var unknown = await controller.GetProjectArchive(spec);
var result = Assert.IsType<FileContentResult>(unknown);
var projectPackage = Encoding.ASCII.GetString(result.FileContents);
// Assert
var result = Assert.IsType<FileContentResult>(unknown);
using var reader = new StreamReader(new MemoryStream(result.FileContents));
var body = reader.ReadToEnd();
body.Should().Contain("dependencies=CamelCaseDep");
projectPackage.Should().Contain("dependencies=CamelCaseDep");
}
*/
/* ----------------------------------------------------------------- *
* negative tests *
* ----------------------------------------------------------------- */
/*
[Fact]
public void No_Template_Found_Should_Return_404_Page_Not_Found()
public async Task No_Template_Found_Should_Return_404_Page_Not_Found()
{
// Arrange
var controller = new ProjectControllerBuilder().Build();
var spec = new ProjectSpec { Name = "nosuchtemplate" };
var spec = new ProjectSpec { Name = "nosuchproject" };
// Act
var unknown = controller.GetProjectArchive(spec);
var unknown = await controller.GetProjectArchive(spec);
var result = Assert.IsType<NotFoundObjectResult>(unknown);
// Assert
var result = Assert.IsType<NotFoundObjectResult>(unknown);
result.Value.ToString().Should()
.Be("No project template for spec: [name=nosuchtemplate,packaging=myarchive]");
result.Value.ToString().Should().Be("No project for spec.");
}
*/
/*
[Fact]
public void Unknown_Archive_Format_Should_Return_404_Page_Not_Found()
public async Task Unknown_Packaging_Should_Return_400_BadRequest()
{
// Arrange
var controller = new ProjectControllerBuilder().Build();
var spec = new ProjectSpec { Packaging = "nosuchformat" };
var spec = new ProjectSpec { Packaging = "nosuchpackaging" };
// Act
var unknown = controller.GetProjectArchive(spec);
var unknown = await controller.GetProjectArchive(spec);
var result = Assert.IsType<BadRequestObjectResult>(unknown);
// Assert
var result = Assert.IsType<NotFoundObjectResult>(unknown);
result.Value.ToString().Should().Be("Packaging 'nosuchformat' not found.");
result.Value.ToString().Should().Be("Packaging 'nosuchpackaging' not found.");
}
*/
/*
[Fact]
public void Unknown_Dependency_Should_Return_404_Page_Not_found()
public async Task Unknown_Dependency_Should_Return_404_Page_Not_found()
{
// Arrange
var controller = new ProjectControllerBuilder().Build();
var spec = new ProjectSpec { Dependencies = "nosuchdep" };
// Act
var unknown = controller.GetProjectArchive(spec);
var unknown = await controller.GetProjectArchive(spec);
var result = Assert.IsType<NotFoundObjectResult>(unknown);
// Assert
var result = Assert.IsType<NotFoundObjectResult>(unknown);
result.Value.ToString().Should().Be("Dependency 'nosuchdep' not found.");
}
*/
/*
[Fact]
public void Null_Archive_Format_Should_Return_500_Internal_Server_Error()
public async Task Null_Archive_Format_Should_Return_500_Internal_Server_Error()
{
// Arrange
var spec = new ProjectSpec();
@ -171,14 +157,13 @@ namespace Steeltoe.InitializrApi.Test.Unit.Controllers
var controller = new ProjectControllerBuilder().WithInitializrConfiguration(config).Build();
// Act
var unknown = controller.GetProjectArchive(spec);
var unknown = await controller.GetProjectArchive(spec);
var result = Assert.IsType<ObjectResult>(unknown);
// Assert
var result = Assert.IsType<ObjectResult>(unknown);
result.StatusCode.Should().Be(StatusCodes.Status500InternalServerError);
result.Value.Should().Be("Default packaging not configured.");
}
*/
/* ----------------------------------------------------------------- *
* test helpers *
@ -192,21 +177,15 @@ namespace Steeltoe.InitializrApi.Test.Unit.Controllers
internal ProjectController Build()
{
if (_uiConfig is null)
_uiConfig ??= new UiConfig
{
_uiConfig = new UiConfig
Packaging = new UiConfig.SingleSelectList
{
Packaging = new UiConfig.SingleSelectList
{
Default = "myarchive",
},
};
}
Default = "myarchive",
},
};
if (_generator is null)
{
_generator = new TestProjectGenerator();
}
_generator ??= new TestProjectGenerator();
var configurationService = new Mock<IUiConfigService>();
configurationService.Setup(svc => svc.GetUiConfig()).Returns(_uiConfig);
@ -232,29 +211,37 @@ namespace Steeltoe.InitializrApi.Test.Unit.Controllers
{
public Task<byte[]> GenerateProjectArchive(ProjectSpec spec)
{
return null;
/*
if (spec.Name != null && spec.Name.Equals("nosuchtemplate"))
if (spec.Name is "nosuchproject")
{
return null;
throw new NoProjectForSpecException($"No project for spec.");
}
var project = new Project();
project.FileEntries.Add(new FileEntry { Path = "project name", Text = spec.Name ?? "<na>" });
project.FileEntries.Add(new FileEntry { Path = "description", Text = spec.Description ?? "<na>" });
project.FileEntries.Add(new FileEntry { Path = "namespace", Text = spec.Namespace ?? "<na>" });
project.FileEntries.Add(new FileEntry
{ Path = "steeltoe version", Text = spec.SteeltoeVersion ?? "<na>" });
project.FileEntries.Add(new FileEntry
{ Path = "dotnet framework", Text = spec.DotNetFramework ?? "<na>" });
project.FileEntries.Add(
new FileEntry { Path = "dotnet template", Text = spec.DotNetTemplate ?? "<na>" });
project.FileEntries.Add(new FileEntry { Path = "language", Text = spec.Language ?? "<na>" });
project.FileEntries.Add(new FileEntry
{ Path = "packaging", Text = spec.Packaging ?? "<na>" });
project.FileEntries.Add(new FileEntry { Path = "dependencies", Text = spec.Dependencies ?? "<na>" });
return project;
*/
if (spec.Packaging is "nosuchpackaging")
{
throw new InvalidSpecException($"Packaging '{spec.Packaging}' not found.");
}
const char newline = '\n';
var buffer = new StringBuilder();
buffer.Append("project name=").Append(spec.Name ?? "<na>");
buffer.Append(newline);
buffer.Append("description=").Append(spec.Description ?? "<na>" );
buffer.Append(newline);
buffer.Append("namespace=").Append(spec.Namespace ?? "<na>" );
buffer.Append(newline);
buffer.Append("steeltoe version=").Append(spec.SteeltoeVersion ?? "<na>" );
buffer.Append(newline);
buffer.Append("dotnet framework=").Append(spec.DotNetFramework ?? "<na>" );
buffer.Append(newline);
buffer.Append("dotnet template=").Append(spec.DotNetTemplate ?? "<na>" );
buffer.Append(newline);
buffer.Append("language=").Append(spec.Language ?? "<na>" );
buffer.Append(newline);
buffer.Append("packaging=").Append(spec.Packaging ?? "<na>" );
buffer.Append(newline);
buffer.Append("dependencies=").Append(spec.Dependencies ?? "<na>" );
buffer.Append(newline);
return Task.FromResult(Encoding.ASCII.GetBytes(buffer.ToString()));
}
}
}