Skip to content

Commit

Permalink
Adding unit tests, refactoring file access to a service, adding Catal…
Browse files Browse the repository at this point in the history
…ogImageMissingException.
  • Loading branch information
ardalis committed Apr 28, 2017
1 parent ac89e73 commit 5dbbc4c
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 14 deletions.
13 changes: 13 additions & 0 deletions src/ApplicationCore/Exceptions/CatalogImageMissingException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace ApplicationCore.Exceptions
{
public class CatalogImageMissingException : Exception
{
public CatalogImageMissingException(string message,
Exception innerException = null)
: base(message, innerException: innerException)
{
}
}
}
1 change: 0 additions & 1 deletion src/ApplicationCore/Interfaces/IBasketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ public interface IIdentityParser<T>
{
T Parse(IPrincipal principal);
}

}
16 changes: 16 additions & 0 deletions src/ApplicationCore/Interfaces/IImageService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System.Security.Principal;
using System.Threading.Tasks;

namespace ApplicationCore.Interfaces
{

public interface IImageService
{
byte[] GetImageBytesById(int id);
}



}
22 changes: 22 additions & 0 deletions src/Infrastructure/FileSystem/LocalFileImageService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using ApplicationCore.Interfaces;
using Microsoft.AspNetCore.Hosting;
using System.IO;

namespace Infrastructure.FileSystem
{
public class LocalFileImageService : IImageService
{
private readonly IHostingEnvironment _env;

public LocalFileImageService(IHostingEnvironment env)
{
_env = env;
}
public byte[] GetImageBytesById(int id)
{
var contentRoot = _env.ContentRootPath + "//Pics";
var path = Path.Combine(contentRoot, id + ".png");
return File.ReadAllBytes(path);
}
}
}
3 changes: 3 additions & 0 deletions src/Infrastructure/Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.2" />
<PackageReference Include="StructureMap.Microsoft.DependencyInjection" Version="1.3.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ApplicationCore\ApplicationCore.csproj" />
</ItemGroup>

</Project>
35 changes: 23 additions & 12 deletions src/Web/Controllers/CatalogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,38 @@
using System;
using System.IO;
using System.Threading.Tasks;
using ApplicationCore.Interfaces;
using ApplicationCore.Exceptions;

namespace Microsoft.eShopWeb.Controllers
{
public class CatalogController : Controller
{
private readonly IHostingEnvironment _env;
private readonly ICatalogService _catalogSvc;
private readonly ICatalogService _catalogService;
private readonly IImageService _imageService;

public CatalogController(IHostingEnvironment env, ICatalogService catalogSvc)
public CatalogController(IHostingEnvironment env,
ICatalogService catalogService,
IImageService imageService)
{
_env = env;
_catalogSvc = catalogSvc;
_catalogService = catalogService;
_imageService = imageService;
}


// GET: /<controller>/
public async Task<IActionResult> Index(int? BrandFilterApplied, int? TypesFilterApplied, int? page)
{
var itemsPage = 10;
var catalog = await _catalogSvc.GetCatalogItems(page ?? 0, itemsPage, BrandFilterApplied, TypesFilterApplied);
var catalog = await _catalogService.GetCatalogItems(page ?? 0, itemsPage, BrandFilterApplied, TypesFilterApplied);

var vm = new CatalogIndex()
{
CatalogItems = catalog.Data,
Brands = await _catalogSvc.GetBrands(),
Types = await _catalogSvc.GetTypes(),
Brands = await _catalogService.GetBrands(),
Types = await _catalogService.GetTypes(),
BrandFilterApplied = BrandFilterApplied ?? 0,
TypesFilterApplied = TypesFilterApplied ?? 0,
PaginationInfo = new PaginationInfo()
Expand All @@ -53,13 +59,18 @@ public async Task<IActionResult> Index(int? BrandFilterApplied, int? TypesFilter
// GET: /<controller>/pic/{id}
public IActionResult GetImage(int id)
{
var contentRoot = _env.ContentRootPath + "//Pics";
var path = Path.Combine(contentRoot, id + ".png");
Byte[] b = System.IO.File.ReadAllBytes(path);
return File(b, "image/png");

byte[] imageBytes;
try
{
imageBytes = _imageService.GetImageBytesById(id);
}
catch (CatalogImageMissingException ex)
{
return NotFound();
}
return File(imageBytes, "image/png");
}

public IActionResult Error()
{
return View();
Expand Down
3 changes: 3 additions & 0 deletions src/Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using System.Text;
using Microsoft.AspNetCore.Http;
using ApplicationCore.Interfaces;
using Infrastructure.FileSystem;

namespace Microsoft.eShopWeb
{
Expand Down Expand Up @@ -65,6 +67,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped<ICatalogService, CachedCatalogService>();
services.AddScoped<CatalogService>();
services.Configure<CatalogSettings>(Configuration);
services.AddSingleton<IImageService, LocalFileImageService>();
services.AddMvc();

_services = services;
Expand Down
4 changes: 4 additions & 0 deletions tests/IntegrationTests/IntegrationTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
<ProjectReference Include="..\..\src\Web\Web.csproj" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>
14 changes: 13 additions & 1 deletion tests/UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.0" />

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Moq" Version="4.7.8" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Web\Web.csproj" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>
60 changes: 60 additions & 0 deletions tests/UnitTests/Web/Controllers/CatalogControllerGetImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using ApplicationCore.Exceptions;
using ApplicationCore.Interfaces;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopWeb.Controllers;
using Moq;
using Xunit;

namespace UnitTests
{
public class CatalogControllerGetImage
{
private Mock<IImageService> _mockImageService = new Mock<IImageService>();
private CatalogController _controller;
private int _testImageId = 123;
private byte[] _testBytes = { 0x01, 0x02, 0x03 };

public CatalogControllerGetImage()
{
_controller = new CatalogController(null, null, _mockImageService.Object);
}

[Fact]
public void CallsImageServiceWithId()
{
_mockImageService
.Setup(i => i.GetImageBytesById(_testImageId))
.Returns(_testBytes)
.Verifiable();

_controller.GetImage(_testImageId);
_mockImageService.Verify();
}

[Fact]
public void ReturnsFileResultWithBytesGivenSuccess()
{
_mockImageService
.Setup(i => i.GetImageBytesById(_testImageId))
.Returns(_testBytes);

var result = _controller.GetImage(_testImageId);

var fileResult = Assert.IsType<FileContentResult>(result);
var bytes = Assert.IsType<byte[]>(fileResult.FileContents);
}

[Fact]
public void ReturnsNotFoundResultGivenImageMissingException()
{
_mockImageService
.Setup(i => i.GetImageBytesById(_testImageId))
.Throws(new CatalogImageMissingException("missing image"));

var result = _controller.GetImage(_testImageId);

var actionResult = Assert.IsType<NotFoundResult>(result);
}
}
}

0 comments on commit 5dbbc4c

Please sign in to comment.